aboutsummaryrefslogtreecommitdiff
path: root/internal/monitor/dns.go
blob: a962b5d7ecab8f11f32c2e78a416432e08cbc5a2 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package monitor

import (
	"context"
	"fmt"
	"net"
	"strings"
	"time"

	"github.com/Fuwn/kaze/internal/config"
)

// DNSMonitor monitors DNS resolution
type DNSMonitor struct {
	id                string
	name              string
	group             string
	target            string // Domain to resolve
	interval          time.Duration
	timeout           time.Duration
	retries           int
	roundResponseTime bool
	roundUptime       bool
	dnsServer         string   // Optional DNS server
	expectedIPs       []string // Expected IP addresses
	expectedCNAME     string   // Expected CNAME
	recordType        string   // DNS record type (A, AAAA, CNAME, MX, TXT, etc.)
}

// NewDNSMonitor creates a new DNS monitor
func NewDNSMonitor(cfg config.MonitorConfig) (*DNSMonitor, error) {
	// Default to A record if not specified
	recordType := "A"
	if cfg.RecordType != "" {
		recordType = strings.ToUpper(cfg.RecordType)
	}

	return &DNSMonitor{
		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,
		dnsServer:         cfg.DNSServer,
		expectedIPs:       cfg.ExpectedIPs,
		expectedCNAME:     cfg.ExpectedCNAME,
		recordType:        recordType,
	}, nil
}

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

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

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

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

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

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

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

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

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

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

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

	// Create resolver
	resolver := &net.Resolver{}
	if m.dnsServer != "" {
		// Use custom DNS server
		resolver = &net.Resolver{
			PreferGo: true,
			Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
				d := net.Dialer{Timeout: m.timeout}
				return d.DialContext(ctx, "udp", m.dnsServer)
			},
		}
	}

	// Create timeout context
	timeoutCtx, cancel := context.WithTimeout(ctx, m.timeout)
	defer cancel()

	start := time.Now()

	switch m.recordType {
	case "A", "AAAA":
		ips, err := resolver.LookupIP(timeoutCtx, "ip", m.target)
		result.ResponseTime = time.Since(start)

		if err != nil {
			result.Status = StatusDown
			result.Error = fmt.Errorf("DNS lookup failed: %w", err)
			return result
		}

		if len(ips) == 0 {
			result.Status = StatusDown
			result.Error = fmt.Errorf("no %s records found", m.recordType)
			return result
		}

		// If expected IPs are specified, verify them
		if len(m.expectedIPs) > 0 {
			found := false
			for _, ip := range ips {
				for _, expected := range m.expectedIPs {
					if ip.String() == expected {
						found = true
						break
					}
				}
				if found {
					break
				}
			}
			if !found {
				result.Status = StatusDegraded
				result.Error = fmt.Errorf("expected IPs not found in response")
				return result
			}
		}

		result.Status = StatusUp

	case "CNAME":
		cname, err := resolver.LookupCNAME(timeoutCtx, m.target)
		result.ResponseTime = time.Since(start)

		if err != nil {
			result.Status = StatusDown
			result.Error = fmt.Errorf("CNAME lookup failed: %w", err)
			return result
		}

		// If expected CNAME is specified, verify it
		if m.expectedCNAME != "" && cname != m.expectedCNAME {
			result.Status = StatusDegraded
			result.Error = fmt.Errorf("CNAME mismatch: got %s, expected %s", cname, m.expectedCNAME)
			return result
		}

		result.Status = StatusUp

	case "MX":
		mxs, err := resolver.LookupMX(timeoutCtx, m.target)
		result.ResponseTime = time.Since(start)

		if err != nil {
			result.Status = StatusDown
			result.Error = fmt.Errorf("MX lookup failed: %w", err)
			return result
		}

		if len(mxs) == 0 {
			result.Status = StatusDown
			result.Error = fmt.Errorf("no MX records found")
			return result
		}

		result.Status = StatusUp

	case "TXT":
		txts, err := resolver.LookupTXT(timeoutCtx, m.target)
		result.ResponseTime = time.Since(start)

		if err != nil {
			result.Status = StatusDown
			result.Error = fmt.Errorf("TXT lookup failed: %w", err)
			return result
		}

		if len(txts) == 0 {
			result.Status = StatusDown
			result.Error = fmt.Errorf("no TXT records found")
			return result
		}

		result.Status = StatusUp

	default:
		result.ResponseTime = time.Since(start)
		result.Status = StatusDown
		result.Error = fmt.Errorf("unsupported record type: %s", m.recordType)
	}

	return result
}