-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
342 lines (296 loc) · 9.7 KB
/
main.go
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package main
import (
"bytes"
"fmt"
"net"
"os"
"os/exec"
"sort"
"strconv"
"strings"
"github.com/cloudflare/cloudflare-go"
"github.com/olorin/nagiosplugin"
"github.com/pkg/profile"
"github.com/simonleung8/flags"
"github.com/ymkatz/nagios_check_dns_cloudflare/internal/cache"
"github.com/ymkatz/nagios_check_dns_cloudflare/internal/dns"
)
var (
version = "dev"
commit = "none"
date = "unknown"
builtBy = "unknown"
)
var f = flags.New()
func init() {
f.NewBoolFlag("help", "h", "Print detailed help")
f.NewBoolFlag("version", "V", "Print the version information")
f.NewStringFlag("hostname", "H", "The name to query")
f.NewStringFlag("dns-server", "s", "The DNS server to query. Cannot be used with --only-api")
f.NewBoolFlag("only-dns", "d", "Only do the DNS part of the check. Cannot be used with --only-api")
f.NewBoolFlag("only-api", "o", "Only do the API part of the check. Cannot be used with --only-dns")
f.NewStringFlag("zone", "z", "Cloudflare Zone to query. Defaults to domain name of host. Cannot be used with --only-dns")
f.NewStringSliceFlag("expected-address", "a", "If proxied, the expected API lookup result. If not proxied, the expected DNS lookup result.")
f.NewStringFlag("querytype", "q", "Optional DNS record query type for non-proxied lookups where TYPE =(A, AAAA, SRV, TXT, MX, ANY)\nThe default query type is 'A' (IPv4 host entry)\nProxied lookups always look up A and AAAA")
f.NewIntFlagWithDefault("timeout", "t", "Seconds before the check times out", 10)
f.NewStringFlag("cache-path", "", "Cache location for API results. Defaults to the first of `NAGIOS_PLUGIN_STATE_DIRECTORY` environment variable, `/usr/local/nagios/var`, or `$TEMP`, that can be found")
// TODO: Do we need these from the original check_dns?
// fc.NewBoolFlag("expect-authority", "A", "Optionally expect the DNS server to be authoritative for the lookup")
// fc.NewBoolFlag("accept-cname", "n", "Optionally accept cname responses as a valid result to a query\nThe default is to ignore cname responses as part of the result")
f.NewBoolFlag("memprofile", "", "")
f.NewBoolFlag("memprofileall", "", "")
}
func main() {
err := f.Parse(os.Args...)
var memP interface{ Stop() }
if f.IsSet("memprofile") {
cwd, _ := os.Getwd()
memP = profile.Start(profile.MemProfile, profile.ProfilePath(cwd))
}
if f.IsSet("memprofileall") {
cwd, _ := os.Getwd()
memP = profile.Start(profile.MemProfile, profile.ProfilePath(cwd), profile.MemProfileRate(1))
}
check := nagiosplugin.NewCheck()
defer check.Finish()
if err != nil {
check.Unknownf("Invalid command line arguments provided. %s", err)
}
if f.IsSet("help") {
fmt.Println(f.ShowUsage(4))
os.Exit(0)
}
if f.IsSet("version") {
fmt.Printf("check_dns_cloudflare %s, commit %s, built at %s by %s\n", version, commit, date, builtBy)
os.Exit(0)
}
onlyDNS := f.Bool("only-dns")
onlyAPI := f.Bool("only-api")
if onlyAPI {
if onlyDNS || f.IsSet("dns-server") {
check.Unknownf("Cannot use DNS options with --only-api flag")
}
}
var c *cache.CFCache
if f.IsSet("cache-path") {
c, err = cache.GetCache(f.String("cache-path"))
if err != nil {
check.Unknownf("Unwriteable cache path provided")
}
} else {
cachePath, found := os.LookupEnv("NAGIOS_PLUGIN_STATE_DIRECTORY")
if found {
// If we get an error just look for the next possible location
c, err = cache.GetCache(cachePath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error trying NAGIOS_PLUGIN_STATE_DIRECTORY: %s\n", err)
}
}
if c == nil {
c, err = cache.GetCache("/usr/local/nagios/var/")
if err != nil {
fmt.Fprintf(os.Stderr, "Error trying /usr/local/nagios/var/: %s\n", err)
}
}
if c == nil {
c, err = cache.GetCache(os.TempDir())
if err != nil {
check.Unknownf("Unable to determine a usable cache path. Please provide one. Error: %s", err)
}
}
}
if !f.IsSet("hostname") {
check.Criticalf("Hostname argument not provided")
}
hostname := f.String("hostname")
queryType := f.String("querytype")
expectedRaw := f.StringSlice("expected-address")
wantRecords := make(map[string]bool, len(expectedRaw))
for _, e := range expectedRaw {
if strings.Contains(e, ",") {
for _, e2 := range strings.Split(e, ",") {
wantRecords[e2] = false
}
} else {
wantRecords[e] = false
}
}
isProxied := false
if !onlyDNS {
var cfAPI *cloudflare.API
var cfZone string
cfToken, found := os.LookupEnv("CLOUDFLARE_API_TOKEN")
if !found {
check.Unknownf("You must set the CLOUDFLARE_API_TOKEN variable")
}
err := c.LoadCloudflareIPList()
if err != nil {
check.Unknownf("Unable to load list of Cloudflare public IPs")
}
cfAPI, err = cloudflare.NewWithAPIToken(cfToken)
if err != nil {
check.Criticalf("Unable to create Cloudflare API client")
}
if f.IsSet("zone") {
cfZone = f.String("zone")
} else {
cfZone = dns.GetDomainFromHostname(hostname)
}
records, err := c.GetCFZoneDNS(cfAPI, cfZone)
if err != nil {
check.Criticalf("Unable to query Cloudflare DNS records")
}
var foundRecord []string
for _, r := range records {
// fmt.Fprintf(os.Stderr, "RECORD: %v\n", r)
if r.Name == hostname {
// If we are filtering by query type, skip if this is the wrong type
// If we are not filtering by query type, the types we care about are A, AAAA, and CNAME
if (len(queryType) > 0 && r.Type != queryType) || (r.Type != "A" && r.Type != "AAAA" && r.Type != "CNAME") {
continue
}
// Set this so we know what DNS record to expect
// NOTE: The current behavior for multiple records with the same name
// is that if any of them is proxied they all are. This is probably
// undefined behavior but we will go with it for now.
isProxied = isProxied || r.Proxied
if len(wantRecords) > 0 {
if _, ok := wantRecords[r.Content]; ok {
wantRecords[r.Content] = true
foundRecord = append(foundRecord, r.Content)
} else {
check.AddResultf(nagiosplugin.CRITICAL, "Found unexpected DNS %s record: %s", r.Type, r.Content)
}
} else {
// If we don't expect a specific value, then any record is fine
foundRecord = append(foundRecord, r.Content)
}
}
}
if len(expectedRaw) > 0 {
// If we expected specific records, check that we found them all
foundAll := true
for wanted, wasFound := range wantRecords {
foundAll = foundAll && wasFound
if !wasFound {
check.Criticalf("CF API missing expected DNS content %s", wanted)
}
}
if foundAll {
check.AddResultf(nagiosplugin.OK, "%s", strings.Join(foundRecord, ","))
}
} else if len(foundRecord) > 0 {
check.AddResultf(nagiosplugin.OK, "%s", strings.Join(foundRecord, ","))
} else {
check.Criticalf("CF API does not have DNS record for %s", hostname)
}
}
// Now do the DNS checks
if !onlyAPI {
path, err := exec.LookPath("dig")
if err != nil {
check.Unknownf("Could not find 'dig' command on PATH")
}
args := []string{"+short"}
if f.IsSet("timeout") {
args = append(args, "+time="+strconv.Itoa(f.Int("timeout")))
}
if f.IsSet("dns-server") {
args = append(args, "@"+f.String("dns-server"))
}
if isProxied {
// Do A and AAAA lookups and make sure they are inside the CF IP ranges
// NOTE: Requires `DiG 9.x` for "Multiple Query" support
args = append(args, hostname, "-t", "A", hostname, "-t", "AAAA")
results, err := runCommand(path, args)
if err != nil {
check.Unknownf("Error running dig: %s", err)
}
if len(results) == 0 {
check.Criticalf("No DNS A+AAAA records found")
}
for _, ipStr := range results {
ip := net.ParseIP(ipStr)
if ip == nil {
check.AddResultf(nagiosplugin.CRITICAL, "Failed to parse IP `%s`", ipStr)
} else {
found := false
if ip.To4() == nil {
for _, cidr := range c.PublicCIDRsV6 {
if cidr.Contains(ip) {
found = true
break
}
}
} else {
for _, cidr := range c.PublicCIDRsV4 {
if cidr.Contains(ip) {
found = true
break
}
}
}
if !found {
check.AddResultf(nagiosplugin.CRITICAL, "IP `%s` does not belong to CloudFlare", ipStr)
}
}
}
// NOTE: We can add an "OK" here unconditionally because a CRITICAL will override it.
check.AddResultf(nagiosplugin.OK, "Results: %s", strings.Join(results, ","))
} else {
// Do a regular DNS check
args = append(args, hostname, "-t", queryType)
results, err := runCommand(path, args)
if err != nil {
check.Unknownf("Error running dig: %s", err)
}
if len(expectedRaw) > 0 {
if Equal(results, expectedRaw) {
check.AddResultf(nagiosplugin.OK, "Results: %s", strings.Join(results, ","))
} else {
check.AddResultf(nagiosplugin.CRITICAL, "DNS lookup result different from expected\nExpected:\t%s\nGot:\t%s", expectedRaw, strings.Join(results, ","))
}
} else {
check.AddResultf(nagiosplugin.OK, "Results: %s", strings.Join(results, ","))
}
}
}
if memP != nil {
memP.Stop()
}
}
func runCommand(path string, args []string) ([]string, error) {
fmt.Fprintf(os.Stderr, "Executing `%s %s`\n", path, strings.Join(args, " "))
cmd := exec.Command(path, args...)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return nil, err
}
results := strings.Split(strings.ReplaceAll(out.String(), "\r\n", "\n"), "\n")
// If there is a blank last element, remove it
if len(results) > 1 && len(results[len(results)-1]) == 0 {
results = results[:len(results)-1]
}
for i := range results {
s := results[i]
sz := len(s)
if sz > 0 && s[sz-1] == '.' {
results[i] = s[:sz-1]
}
}
sort.Strings(results)
return results, nil
}
func Equal(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}