diff options
Diffstat (limited to 'internal/monitor/dns.go')
| -rw-r--r-- | internal/monitor/dns.go | 64 |
1 files changed, 28 insertions, 36 deletions
diff --git a/internal/monitor/dns.go b/internal/monitor/dns.go index 4f4f099..a962b5d 100644 --- a/internal/monitor/dns.go +++ b/internal/monitor/dns.go @@ -12,7 +12,9 @@ import ( // DNSMonitor monitors DNS resolution type DNSMonitor struct { + id string name string + group string target string // Domain to resolve interval time.Duration timeout time.Duration @@ -34,7 +36,9 @@ func NewDNSMonitor(cfg config.MonitorConfig) (*DNSMonitor, error) { } return &DNSMonitor{ + id: cfg.ID(), name: cfg.Name, + group: cfg.Group, target: cfg.Target, interval: cfg.Interval.Duration, timeout: cfg.Timeout.Duration, @@ -48,11 +52,21 @@ func NewDNSMonitor(cfg config.MonitorConfig) (*DNSMonitor, error) { }, 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" @@ -91,7 +105,7 @@ func (m *DNSMonitor) RoundUptime() bool { // Check performs the DNS resolution check func (m *DNSMonitor) Check(ctx context.Context) *Result { result := &Result{ - MonitorName: m.name, + MonitorName: m.id, Timestamp: time.Now(), } @@ -127,16 +141,16 @@ func (m *DNSMonitor) Check(ctx context.Context) *Result { if len(ips) == 0 { result.Status = StatusDown - result.Error = fmt.Errorf("no IP addresses returned") + result.Error = fmt.Errorf("no %s records found", m.recordType) return result } - // Check if expected IPs match + // If expected IPs are specified, verify them if len(m.expectedIPs) > 0 { found := false - for _, expectedIP := range m.expectedIPs { - for _, ip := range ips { - if ip.String() == expectedIP { + for _, ip := range ips { + for _, expected := range m.expectedIPs { + if ip.String() == expected { found = true break } @@ -147,7 +161,7 @@ func (m *DNSMonitor) Check(ctx context.Context) *Result { } if !found { result.Status = StatusDegraded - result.Error = fmt.Errorf("resolved IPs don't match expected: got %v, expected %v", ips, m.expectedIPs) + result.Error = fmt.Errorf("expected IPs not found in response") return result } } @@ -164,7 +178,7 @@ func (m *DNSMonitor) Check(ctx context.Context) *Result { return result } - // Check if expected CNAME matches + // 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) @@ -174,7 +188,7 @@ func (m *DNSMonitor) Check(ctx context.Context) *Result { result.Status = StatusUp case "MX": - mxRecords, err := resolver.LookupMX(timeoutCtx, m.target) + mxs, err := resolver.LookupMX(timeoutCtx, m.target) result.ResponseTime = time.Since(start) if err != nil { @@ -183,7 +197,7 @@ func (m *DNSMonitor) Check(ctx context.Context) *Result { return result } - if len(mxRecords) == 0 { + if len(mxs) == 0 { result.Status = StatusDown result.Error = fmt.Errorf("no MX records found") return result @@ -192,7 +206,7 @@ func (m *DNSMonitor) Check(ctx context.Context) *Result { result.Status = StatusUp case "TXT": - txtRecords, err := resolver.LookupTXT(timeoutCtx, m.target) + txts, err := resolver.LookupTXT(timeoutCtx, m.target) result.ResponseTime = time.Since(start) if err != nil { @@ -201,7 +215,7 @@ func (m *DNSMonitor) Check(ctx context.Context) *Result { return result } - if len(txtRecords) == 0 { + if len(txts) == 0 { result.Status = StatusDown result.Error = fmt.Errorf("no TXT records found") return result @@ -210,31 +224,9 @@ func (m *DNSMonitor) Check(ctx context.Context) *Result { result.Status = StatusUp default: - // Fallback to generic IP lookup - 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 IP addresses returned") - return result - } - - result.Status = StatusUp - } - - // Check for slow DNS resolution (degraded if > 1 second) - if result.Status == StatusUp && result.ResponseTime > 1*time.Second { - result.Status = StatusDegraded - if result.Error == nil { - result.Error = fmt.Errorf("slow DNS resolution: %v", result.ResponseTime) - } + result.Status = StatusDown + result.Error = fmt.Errorf("unsupported record type: %s", m.recordType) } return result |