aboutsummaryrefslogtreecommitdiff
path: root/internal/monitor/icmp.go
blob: 9313cca9b1be06c80884e29a75631b582d19c696 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package monitor

import (
	"context"
	"fmt"
	"time"

	"github.com/Fuwn/kaze/internal/config"
	"github.com/go-ping/ping"
)

// ICMPMonitor monitors hosts using ICMP ping
type ICMPMonitor struct {
	id                string
	name              string
	group             string
	target            string
	interval          time.Duration
	timeout           time.Duration
	retries           int
	roundResponseTime bool
	roundUptime       bool
	count             int // Number of ping packets to send
}

// NewICMPMonitor creates a new ICMP monitor
func NewICMPMonitor(cfg config.MonitorConfig) (*ICMPMonitor, error) {
	// Default to 4 pings if not specified
	count := 4
	if cfg.PingCount > 0 {
		count = cfg.PingCount
	}

	return &ICMPMonitor{
		id:                cfg.ID(),
		name:              cfg.Name,
		group:             cfg.Group,
		target:            cfg.Target,
		interval:          cfg.Interval.Duration,
		timeout:           cfg.Timeout.Duration,
		retries:           cfg.Retries,
		roundResponseTime: cfg.RoundResponseTime,
		roundUptime:       cfg.RoundUptime,
		count:             count,
	}, nil
}

// ID returns the unique identifier for this monitor
func (m *ICMPMonitor) ID() string {
	return m.id
}

// Name returns the monitor's name
func (m *ICMPMonitor) Name() string {
	return m.name
}

// Group returns the group this monitor belongs to
func (m *ICMPMonitor) Group() string {
	return m.group
}

// Type returns the monitor type
func (m *ICMPMonitor) Type() string {
	return "icmp"
}

// Target returns the monitor target
func (m *ICMPMonitor) Target() string {
	return m.target
}

// Interval returns the check interval
func (m *ICMPMonitor) Interval() time.Duration {
	return m.interval
}

// Retries returns the number of retry attempts
func (m *ICMPMonitor) Retries() int {
	return m.retries
}

// HideSSLDays returns whether to hide SSL days from display
func (m *ICMPMonitor) HideSSLDays() bool {
	return false // ICMP doesn't use SSL
}

// RoundResponseTime returns whether to round response time
func (m *ICMPMonitor) RoundResponseTime() bool {
	return m.roundResponseTime
}

// RoundUptime returns whether to round uptime percentage
func (m *ICMPMonitor) RoundUptime() bool {
	return m.roundUptime
}

// Check performs the ICMP ping check
func (m *ICMPMonitor) Check(ctx context.Context) *Result {
	result := &Result{
		MonitorName: m.id,
		Timestamp:   time.Now(),
	}

	pinger, err := ping.NewPinger(m.target)
	if err != nil {
		result.Status = StatusDown
		result.Error = fmt.Errorf("failed to create pinger: %w", err)
		return result
	}

	// Configure pinger
	pinger.Count = m.count
	pinger.Timeout = m.timeout
	pinger.SetPrivileged(false) // Use unprivileged mode (UDP) by default

	// Run ping
	err = pinger.Run()
	if err != nil {
		result.Status = StatusDown
		result.Error = fmt.Errorf("ping failed: %w", err)
		return result
	}

	stats := pinger.Statistics()

	// Check if any packets were received
	if stats.PacketsRecv == 0 {
		result.Status = StatusDown
		result.Error = fmt.Errorf("no response: 0/%d packets received", stats.PacketsSent)
		return result
	}

	// Use average RTT as response time
	result.ResponseTime = stats.AvgRtt

	// Check packet loss
	if stats.PacketLoss > 0 {
		// Some packet loss - degraded
		result.Status = StatusDegraded
		result.Error = fmt.Errorf("%.1f%% packet loss (%d/%d)", stats.PacketLoss, stats.PacketsRecv, stats.PacketsSent)
	} else {
		// All packets received - up
		result.Status = StatusUp
	}

	return result
}