3 # Copyright 2006, 2012 Peter Palfrader
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 NAGIOS_STATUS = { :OK => 0, :WARNING => 1, :CRITICAL => 2, :UNKNOWN => -1 };
31 @additional_nameservers = []
33 def show_help(parser, code=0, io=STDOUT)
34 program_name = File.basename($0, '.*')
35 io.puts "Usage: #{program_name} [options] <domainname> [<domainname> ...]"
36 io.puts parser.summarize
39 ARGV.options do |opts|
40 opts.on_tail("-h", "--help" , "Display this help screen") { show_help(opts) }
41 opts.on("-v", "--verbose" , String, "Be verbose") { @verbose += 1 }
42 opts.on("-a", "--add=HOST" , String, "Also check SOA on <nameserver>") { |val| @additional_nameservers << val }
45 show_help(ARGV.options, 1, STDERR) if ARGV.length == 0
50 def resolve_ns(dns, domain, nameserver)
51 puts "Getting A record for nameserver #{nameserver} for #{domain}" if @verbose > 0
52 arecords = dns.getresources(nameserver, Resolv::DNS::Resource::IN::A)
53 warnings << "Nameserver #{nameserver} for #{domain} has #{arecords.length} A records" if arecords.length != 1
54 addresses = arecords.map { |a| a.address.to_s }
55 puts "Addresses for nameserver #{nameserver} for #{domain}: #{addresses.join(', ')}" if @verbose > 0
62 nameservers = dns.getresources(domain, Resolv::DNS::Resource::IN::NS)
63 nameservernames = nameservers.collect{ |ns| ns.name.to_s }
64 nameserver_addresses = {}
65 nameservernames.each do |nameserver|
66 addrs = resolve_ns(dns, domain, nameserver)
67 warnings << "Duplicate nameserver #{nameserver} for #{domain}" if nameserver_addresses[nameserver]
68 nameserver_addresses[nameserver] = addrs
70 @additional_nameservers.each do |ns|
72 ipa = IPAddr.new(ns) # check if it's an address
75 addrs = resolve_ns(dns, domain, ns)
77 warnings << "Duplicate nameserver #{ns} for #{domain}" if nameserver_addresses[ns]
78 nameserver_addresses[ns] = addrs
81 nameserver_addresses.each_pair do |nameserver, addrs|
82 puts "Testing nameserver #{nameserver} for #{domain}" if @verbose > 0
84 puts " Nameserver #{nameserver} is at #{a}" if @verbose > 0
86 resolver = Resolv::DNS.new({:nameserver => a})
87 soas = resolver.getresources(domain, Resolv::DNS::Resource::IN::SOA)
88 rescue SystemCallError => e
89 warnings << "Could not resolve #{domain} on #{nameserver}: #{e.message}"
92 warnings << "Nameserver #{nameserver} for #{domain} returns #{soas.length} SOAs" if soas.length != 1
94 puts " Nameserver #{nameserver} returns serial #{soa.serial} for #{domain}" if @verbose > 0
95 serial << soa.serial unless serial.include? soa.serial
102 warnings << "Found no serials for #{domain}"
104 oks << "#{domain} is at #{serial.first}"
106 warnings << "Nameservers disagree on serials for #{domain}: found #{serial.join(', ')}" if serial.length != 1
111 if warnings.length > 0
112 puts warnings.join('; ')
113 exit NAGIOS_STATUS[:WARNING]
116 exit NAGIOS_STATUS[:OK]