Skip to content

Commit a1f1bda

Browse files
author
wafuwafu13
committed
add query type and class option
1 parent ddb8b77 commit a1f1bda

File tree

2 files changed

+50
-16
lines changed

2 files changed

+50
-16
lines changed

check-dns/lib/check_dns.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,20 @@ import (
55
"net"
66
"os"
77
"strconv"
8+
"strings"
89

910
"github.com/jessevdk/go-flags"
1011
"github.com/mackerelio/checkers"
1112
"github.com/miekg/dns"
1213
)
1314

1415
type dnsOpts struct {
15-
Host string `short:"H" long:"host" required:"true" description:"The name or address you want to query"`
16-
Server string `short:"s" long:"server" description:"DNS server you want to use for the lookup"`
17-
Port int `short:"p" long:"port" default:"53" description:"Port number you want to use"`
18-
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"`
1922
}
2023

2124
// Do the plugin
@@ -48,13 +51,22 @@ func (opts *dnsOpts) run() *checkers.Checker {
4851
}
4952
nameserver = net.JoinHostPort(nameserver, strconv.Itoa(opts.Port))
5053

54+
queryType, ok := dns.StringToType[strings.ToUpper(opts.QueryType)]
55+
if !ok {
56+
return checkers.Critical(fmt.Sprintf("%s is invalid queryType", opts.QueryType))
57+
}
58+
queryClass, ok := dns.StringToClass[strings.ToUpper(opts.QueryClass)]
59+
if !ok {
60+
return checkers.Critical(fmt.Sprintf("%s is invalid queryClass", opts.QueryClass))
61+
}
62+
5163
c := new(dns.Client)
5264
m := &dns.Msg{
5365
MsgHdr: dns.MsgHdr{
5466
RecursionDesired: !opts.Norec,
5567
Opcode: dns.OpcodeQuery,
5668
},
57-
Question: []dns.Question{{Name: dns.Fqdn(opts.Host), Qtype: dns.TypeA, Qclass: uint16(dns.ClassINET)}},
69+
Question: []dns.Question{{Name: dns.Fqdn(opts.Host), Qtype: queryType, Qclass: uint16(queryClass)}},
5870
}
5971
m.Id = dns.Id()
6072

check-dns/lib/check_dns_test.go

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,47 +26,67 @@ func TestCheckDns(t *testing.T) {
2626
tests := []struct {
2727
args []string
2828
want_status checkers.Status
29-
want_msg string
29+
want_msg []string
3030
}{
3131
{
3232
[]string{"-H", "example.com"},
3333
checkers.OK,
34-
"status: NOERROR",
34+
[]string{"status: NOERROR"},
3535
},
3636
{
3737
[]string{"-H", "example.com", "--norec"},
3838
checkers.OK,
39-
"status: NOERROR",
39+
[]string{"status: NOERROR"},
4040
},
4141
{
4242
[]string{"-H", "exampleeeee.com"},
4343
checkers.CRITICAL,
44-
"status: NXDOMAIN",
44+
[]string{"status: NXDOMAIN"},
4545
},
4646
{
4747
[]string{"-H", "example.com", "-s", "8.8.8.8"},
4848
checkers.OK,
49-
"status: NOERROR",
49+
[]string{"status: NOERROR"},
5050
},
5151
{
5252
[]string{"-H", "exampleeeee.com", "-s", "8.8.8.8"},
5353
checkers.CRITICAL,
54-
"status: NXDOMAIN",
54+
[]string{"status: NXDOMAIN"},
5555
},
5656
{
5757
[]string{"-H", "exampleeeee.com", "-s", "8.8.8"},
5858
checkers.CRITICAL,
59-
"timeout",
59+
[]string{"timeout"},
6060
},
6161
{
6262
[]string{"-H", "jprs.co.jp", "-s", "202.11.16.49", "--norec"},
6363
checkers.OK,
64-
"status: NOERROR",
64+
[]string{"status: NOERROR"},
6565
},
6666
{
6767
[]string{"-H", "www.google.com", "-s", "202.11.16.49", "--norec"},
6868
checkers.CRITICAL,
69-
"status: REFUSED",
69+
[]string{"status: REFUSED"},
70+
},
71+
{
72+
[]string{"-H", "example.com", "-s", "8.8.8.8", "-q", "AAAA"},
73+
checkers.OK,
74+
[]string{"status: NOERROR", "AAAA"},
75+
},
76+
{
77+
[]string{"-H", "example.com", "-s", "8.8.8.8", "-q", "AAA"},
78+
checkers.CRITICAL,
79+
[]string{"AAA is invalid queryType"},
80+
},
81+
{
82+
[]string{"-H", "example.com", "-s", "8.8.8.8", "-c", "IN"},
83+
checkers.OK,
84+
[]string{"status: NOERROR"},
85+
},
86+
{
87+
[]string{"-H", "example.com", "-s", "8.8.8.8", "-c", "INN"},
88+
checkers.CRITICAL,
89+
[]string{"INN is invalid queryClass"},
7090
},
7191
}
7292

@@ -84,8 +104,10 @@ func TestCheckDns(t *testing.T) {
84104

85105
assert.Equal(t, tt.want_status, ckr.Status)
86106

87-
if !strings.Contains(ckr.Message, tt.want_msg) {
88-
t.Errorf("%s is not incleded in message", tt.want_msg)
107+
for _, want := range tt.want_msg {
108+
if !strings.Contains(ckr.Message, want) {
109+
t.Errorf("%s is not incleded in message", want)
110+
}
89111
}
90112
}
91113
}

0 commit comments

Comments
 (0)