Skip to content

Commit 055fb3c

Browse files
author
wafuwafu13
committed
check-dns: add expected-address option
1 parent 8752655 commit 055fb3c

File tree

3 files changed

+100
-12
lines changed

3 files changed

+100
-12
lines changed

check-dns/README.md

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,31 @@
55
Monitor DNS response.
66

77
## Synopsis
8+
9+
Check DNS server status
10+
If DNS server returns
11+
```
12+
NOERROR -> OK
13+
otherwise -> CRITICAL
14+
```
815
```
916
check-dns -H example.com -s 8.8.8.8
1017
```
1118

19+
Check IP-ADDRESS DNS server returns
20+
If DNS server returns 1.1.1.1 and 2.2.2.2
21+
```
22+
-a 1.1.1.1, 2.2.2.2 -> OK
23+
-a 1.1.1.1, 2.2.2.2, 3.3.3.3 -> WARNING
24+
-a 1.1.1.1 -> WARNING
25+
-a 1.1.1.1, 3.3.3.3 -> WARNING
26+
-a 3.3.3.3 -> CRITICAL
27+
-a 3.3.3.3, 4.4.4.4, 5.5.5.5 -> CRITICAL
28+
```
29+
```
30+
check-dns -H example.com -s 8.8.8.8 -a 93.184.216.34
31+
```
32+
1233
## Installation
1334

1435
First, build this program.
@@ -42,12 +63,13 @@ command = ["check-dns", "-H", "example.com", "-s", "8.8.8.8"]
4263
### Options
4364

4465
```
45-
-H, --host= The name or address you want to query
46-
-s, --server= DNS server you want to use for the lookup
47-
-p, --port= Port number you want to use (default: 53)
48-
-q, --querytype= DNS record query type where TYPE =(A, AAAA, SRV, TXT, MX, ANY) (default: A)
49-
-c, --queryclass= DNS record class type where TYPE =(IN, CS, CH, HS, NONE, ANY) (default: IN)
50-
--norec Set not recursive mode
66+
-H, --host= The name or address you want to query
67+
-s, --server= DNS server you want to use for the lookup
68+
-p, --port= Port number you want to use (default: 53)
69+
-q, --querytype= DNS record query type where TYPE =(A, AAAA, SRV, TXT, MX, ANY) (default: A)
70+
-c, --queryclass= DNS record class type where TYPE =(IN, CS, CH, HS, NONE, ANY) (default: IN)
71+
--norec Set not recursive mode
72+
-a, --expected-address= IP-ADDRESS you expect the DNS server to return. If multiple addresses are returned at once, you have to match the whole string of addresses separated with commas
5173
```
5274

5375
## For more information

check-dns/lib/check_dns.go

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ import (
1313
)
1414

1515
type dnsOpts struct {
16-
Host string `short:"H" long:"host" required:"true" description:"The name or address you want to query"`
17-
Server string `short:"s" long:"server" description:"DNS server you want to use for the lookup"`
18-
Port int `short:"p" long:"port" default:"53" description:"Port number you want to use"`
19-
QueryType string `short:"q" long:"querytype" default:"A" description:"DNS record query type where TYPE =(A, AAAA, SRV, TXT, MX, ANY)"`
20-
QueryClass string `short:"c" long:"queryclass" default:"IN" description:"DNS record class type where TYPE =(IN, CS, CH, HS, NONE, ANY)"`
21-
Norec bool `long:"norec" description:"Set not recursive mode"`
16+
Host string `short:"H" long:"host" required:"true" description:"The name or address you want to query"`
17+
Server string `short:"s" long:"server" description:"DNS server you want to use for the lookup"`
18+
Port int `short:"p" long:"port" default:"53" description:"Port number you want to use"`
19+
QueryType string `short:"q" long:"querytype" default:"A" description:"DNS record query type where TYPE =(A, AAAA, SRV, TXT, MX, ANY)"`
20+
QueryClass string `short:"c" long:"queryclass" default:"IN" description:"DNS record class type where TYPE =(IN, CS, CH, HS, NONE, ANY)"`
21+
Norec bool `long:"norec" description:"Set not recursive mode"`
22+
ExpectedAddress string `short:"a" long:"expected-address" description:"IP-ADDRESS you expect the DNS server to return. If multiple addresses are returned at once, you have to match the whole string of addresses separated with commas"`
2223
}
2324

2425
// Do the plugin
@@ -76,9 +77,44 @@ func (opts *dnsOpts) run() *checkers.Checker {
7677
}
7778

7879
checkSt := checkers.OK
80+
/**
81+
if DNS server return 1.1.1.1, 2.2.2.2
82+
1: --expected-address 1.1.1.1, 2.2.2.2 -> OK
83+
2: --expected-address 1.1.1.1, 2.2.2.2, 3.3.3.3 -> WARNING
84+
3: --expected-address 1.1.1.1 -> WARNING
85+
4: --expected-address 1.1.1.1, 3.3.3.3 -> WARNING
86+
5: --expected-address 3.3.3.3 -> CRITICAL
87+
6: --expected-address 3.3.3.3, 4.4.4.4, 5.5.5.5 -> CRITICAL
88+
**/
89+
if opts.ExpectedAddress != "" {
90+
expectedAddress := strings.Split(opts.ExpectedAddress, ",")
91+
match := 0
92+
for _, v := range expectedAddress {
93+
for _, answer := range r.Answer {
94+
if strings.Contains(answer.String(), strings.TrimSpace(v)) {
95+
match += 1
96+
}
97+
}
98+
}
99+
if match == len(r.Answer) {
100+
if len(expectedAddress) == len(r.Answer) { // case 1
101+
checkSt = checkers.OK
102+
} else { // case 2
103+
checkSt = checkers.WARNING
104+
}
105+
} else {
106+
if match > 0 { // case 3,4
107+
checkSt = checkers.WARNING
108+
} else { // case 5,6
109+
checkSt = checkers.CRITICAL
110+
}
111+
}
112+
}
113+
79114
if r.MsgHdr.Rcode != dns.RcodeSuccess {
80115
checkSt = checkers.CRITICAL
81116
}
117+
82118
msg := fmt.Sprintf("HEADER-> %s\n", r.MsgHdr.String())
83119
for _, answer := range r.Answer {
84120
msg += fmt.Sprintf("ANSWER-> %s\n", answer)

check-dns/lib/check_dns_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,36 @@ func TestCheckDns(t *testing.T) {
7878
checkers.CRITICAL,
7979
[]string{"INN is invalid queryClass"},
8080
},
81+
{
82+
[]string{"-H", "example.com", "-s", "8.8.8.8", "-a", "93.184.216.34"},
83+
checkers.OK,
84+
[]string{"status: NOERROR", "93.184.216.34"},
85+
},
86+
{
87+
[]string{"-H", "example.com", "-s", "8.8.8.8", "-q", "AAAA", "--expected-address", "2606:2800:220:1"},
88+
checkers.OK,
89+
[]string{"status: NOERROR", "2606:2800:220:1"},
90+
},
91+
{
92+
[]string{"-H", "example.com", "-s", "8.8.8.8", "-a", "93.184.216.33"},
93+
checkers.CRITICAL,
94+
[]string{"status: NOERROR", "93.184.216.34"},
95+
},
96+
{
97+
[]string{"-H", "exampleeeee.com", "-s", "8.8.8.8", "-a", "93.184.216.34"},
98+
checkers.CRITICAL,
99+
[]string{"status: NXDOMAIN"},
100+
},
101+
{
102+
[]string{"-H", "example.com", "-s", "8.8.8.8", "-a", "93.184.216.33,93.184.216.34"},
103+
checkers.WARNING,
104+
[]string{"status: NOERROR", "93.184.216.34"},
105+
},
106+
{
107+
[]string{"-H", "example.com", "-s", "8.8.8.8", "-a", "93.184.216.33, 93.184.216.34"},
108+
checkers.WARNING,
109+
[]string{"status: NOERROR", "93.184.216.34"},
110+
},
81111
}
82112

83113
for i, tt := range tests {

0 commit comments

Comments
 (0)