Skip to content

Commit ab2fc78

Browse files
author
wafuwafu13
committed
For expectation, the currently supported query types are A, AAAA
1 parent f0917e5 commit ab2fc78

File tree

4 files changed

+22
-217
lines changed

4 files changed

+22
-217
lines changed

check-dns/README.md

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,26 @@ Monitor DNS response.
1212
-H, --host= The name or address you want to query
1313
-s, --server= DNS server you want to use for the lookup
1414
-p, --port= Port number you want to use (default: 53)
15-
-q, --querytype= DNS record query type where TYPE =(A, AAAA, TXT, MX, CNAME) (default: A)
16-
-c, --queryclass= DNS record class type where TYPE =(IN, CS, CH, HS, NONE, ANY) (default: IN)
15+
-q, --querytype= DNS record query type (default: A)
16+
-c, --queryclass= DNS record class type (default: IN)
1717
--norec Set not recursive mode
18-
-e, --expected-string= The string you expect the DNS server to return. If multiple responses are returned at once, you have to specify whole string
18+
-e, --expected-string= IP-ADDRESS string you expect the DNS server to return. If multiple IP-ADDRESS are returned at once, you have to specify whole string
1919
```
2020

21-
- The currently supported query types are A, AAAA, TXT, MX, CNAME.
2221
- Punycode is not supported.
2322

2423
### Check DNS server status
2524

2625
If DNS server returns `NOERROR` in status of HEADER, then the checker result becomes `OK`, if not `NOERROR`, then `CRITICAL`
2726

2827
```
29-
check-dns -H example.com -s 8.8.8.8
28+
check-dns -H a.root-servers.net -s 8.8.8.8
3029
```
3130

3231
### Check string DNS server returns
3332

33+
- The currently supported query types are A, AAAA.
34+
3435
If DNS server returns 1.1.1.1 and 2.2.2.2
3536
```
3637
-a 1.1.1.1 -a 2.2.2.2 -> OK
@@ -41,7 +42,7 @@ If DNS server returns 1.1.1.1 and 2.2.2.2
4142
-a 3.3.3.3 -a 4.4.4.4 -a 5.5.5.5 -> CRITICAL
4243
```
4344
```
44-
check-dns -H example.com -s 8.8.8.8 -a 93.184.216.34
45+
check-dns -H a.root-servers.net -s 8.8.8.8 -e 198.41.0.4
4546
```
4647

4748
## Installation
@@ -72,43 +73,3 @@ If there are no problems in the execution result, add a setting in mackerel-agen
7273
[plugin.checks.dns-sample]
7374
command = ["check-dns", "-H", "example.com", "-s", "8.8.8.8"]
7475
```
75-
76-
## NOTICES AND INFORMATION
77-
78-
This plugin incorporates material from third parties.
79-
80-
### miekg/dns
81-
82-
**source**: https://github.com/miekg/dns
83-
84-
```
85-
BSD 3-Clause License
86-
87-
Copyright (c) 2009, The Go Authors. Extensions copyright (c) 2011, Miek Gieben.
88-
All rights reserved.
89-
90-
Redistribution and use in source and binary forms, with or without
91-
modification, are permitted provided that the following conditions are met:
92-
93-
1. Redistributions of source code must retain the above copyright notice, this
94-
list of conditions and the following disclaimer.
95-
96-
2. Redistributions in binary form must reproduce the above copyright notice,
97-
this list of conditions and the following disclaimer in the documentation
98-
and/or other materials provided with the distribution.
99-
100-
3. Neither the name of the copyright holder nor the names of its
101-
contributors may be used to endorse or promote products derived from
102-
this software without specific prior written permission.
103-
104-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
105-
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
106-
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
107-
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
108-
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
109-
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
110-
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
111-
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
112-
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
113-
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
114-
```

check-dns/lib/check_dns.go

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,12 @@ type dnsOpts struct {
1616
Host string `short:"H" long:"host" required:"true" description:"The name or address you want to query"`
1717
Server string `short:"s" long:"server" description:"DNS server you want to use for the lookup"`
1818
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, TXT, MX, CNAME)"`
20-
QueryClass string `short:"c" long:"queryclass" default:"IN" description:"DNS record class type where TYPE =(IN, CS, CH, HS, NONE, ANY)"`
19+
QueryType string `short:"q" long:"querytype" default:"A" description:"DNS record query type"`
20+
QueryClass string `short:"c" long:"queryclass" default:"IN" description:"DNS record class type"`
2121
Norec bool `long:"norec" description:"Set not recursive mode"`
22-
ExpectedString []string `short:"e" long:"expected-string" description:"The string you expect the DNS server to return. If multiple responses are returned at once, you have to specify whole string"`
22+
ExpectedString []string `short:"e" long:"expected-string" description:"IP-ADDRESS string you expect the DNS server to return. If multiple IP-ADDRESS are returned at once, you have to specify whole string"`
2323
}
2424

25-
var supportedQueryType = map[string]int{"A": 1, "AAAA": 1, "TXT": 1, "MX": 1, "CNAME": 1}
26-
2725
// Do the plugin
2826
func Do() {
2927
opts, err := parseArgs(os.Args[1:])
@@ -54,11 +52,10 @@ func (opts *dnsOpts) run() *checkers.Checker {
5452
}
5553
nameserver = net.JoinHostPort(nameserver, strconv.Itoa(opts.Port))
5654

57-
_, ok := supportedQueryType[strings.ToUpper(opts.QueryType)]
55+
queryType, ok := dns.StringToType[strings.ToUpper(opts.QueryType)]
5856
if !ok {
5957
return checkers.Critical(fmt.Sprintf("%s is not supported query type", opts.QueryType))
6058
}
61-
queryType := dns.StringToType[strings.ToUpper(opts.QueryType)]
6259
queryClass, ok := dns.StringToClass[strings.ToUpper(opts.QueryClass)]
6360
if !ok {
6461
return checkers.Critical(fmt.Sprintf("%s is invalid query class", opts.QueryClass))
@@ -90,6 +87,11 @@ func (opts *dnsOpts) run() *checkers.Checker {
9087
6: -e 3.3.3.3 -e 4.4.4.4 -e 5.5.5.5 -> CRITICAL
9188
**/
9289
if len(opts.ExpectedString) != 0 {
90+
supportedQueryType := map[string]int{"A": 1, "AAAA": 1}
91+
_, ok := supportedQueryType[strings.ToUpper(opts.QueryType)]
92+
if !ok {
93+
return checkers.Critical(fmt.Sprintf("%s is not supported query type. Only A, AAAA is supported for expectation.", opts.QueryType))
94+
}
9395
match := 0
9496
for _, expectedString := range opts.ExpectedString {
9597
for _, answer := range r.Answer {
@@ -100,16 +102,8 @@ func (opts *dnsOpts) run() *checkers.Checker {
100102
anserWithoutHeader = t.A.String()
101103
case *dns.AAAA:
102104
anserWithoutHeader = t.AAAA.String()
103-
case *dns.TXT:
104-
anserWithoutHeader = sprintTxt(t.Txt)
105-
// " is added by sprintTxt
106-
expectMatch = "\"" + expectedString + "\""
107-
case *dns.MX:
108-
anserWithoutHeader = strconv.Itoa(int(t.Preference)) + " " + sprintName(t.Mx)
109-
case *dns.CNAME:
110-
anserWithoutHeader = sprintName(t.Target)
111105
default:
112-
return checkers.Critical(fmt.Sprintf("%s is not supported query type", opts.QueryType))
106+
return checkers.Critical(fmt.Sprintf("%s is not supported query type. Only A, AAAA is supported for expectation.", opts.QueryType))
113107
}
114108
if anserWithoutHeader == expectMatch {
115109
match += 1

check-dns/lib/check_dns_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ func TestCheckDns(t *testing.T) {
8888
checkers.OK,
8989
[]string{"status: NOERROR", "2001:503:ba3e::2:30"},
9090
},
91+
{
92+
[]string{"-H", "a.root-servers.net", "-s", "8.8.8.8", "-q", "TXT", "-e", ""},
93+
checkers.CRITICAL,
94+
[]string{"is not supported query type. Only A, AAAA is supported for expectation."},
95+
},
9196
{
9297
[]string{"-H", "a.root-servers.net", "-s", "8.8.8.8", "-e", "198.41.0.3"},
9398
checkers.CRITICAL,

check-dns/lib/util.go

Lines changed: 0 additions & 155 deletions
This file was deleted.

0 commit comments

Comments
 (0)