dsa-check-soas: support supplying an IP address for additional nameservers to check...
[mirror/dsa-nagios.git] / dsa-nagios-checks / checks / dsa-check-soas
1 #!/usr/bin/ruby
2
3 # Copyright 2006, 2012 Peter Palfrader
4 #
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:
12 #
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
15 #
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.
23
24 require 'ipaddr'
25 require 'resolv'
26 require 'optparse'
27 require 'yaml'
28
29 NAGIOS_STATUS = { :OK => 0, :WARNING => 1, :CRITICAL => 2, :UNKNOWN => -1 };
30 @verbose = 0;
31 @additional_nameservers = []
32
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
37   exit(code)
38 end
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 }
43         opts.parse!
44 end
45 show_help(ARGV.options, 1, STDERR) if ARGV.length == 0
46
47 warnings = []
48 oks = []
49
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
56         return addresses
57 end
58
59 dns = Resolv::DNS.new
60 ARGV.each{ |domain|
61         serial = []
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
69         end
70         @additional_nameservers.each do |ns|
71                 begin
72                         ipa = IPAddr.new(ns)  # check if it's an address
73                         addrs = [ns]
74                 rescue ArgumentError
75                         addrs = resolve_ns(dns, domain, ns)
76                 end
77                 warnings << "Duplicate nameserver #{ns} for #{domain}" if nameserver_addresses[ns]
78                 nameserver_addresses[ns] = addrs
79         end
80
81         nameserver_addresses.each_pair do |nameserver, addrs|
82                 puts "Testing nameserver #{nameserver} for #{domain}" if @verbose > 0
83                 addrs.each do |a|
84                         puts " Nameserver #{nameserver} is at #{a}" if @verbose > 0
85                         begin
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}"
90                         else
91                                 resolver.close
92                                 warnings << "Nameserver #{nameserver} for #{domain} returns #{soas.length} SOAs" if soas.length != 1
93                                 soas.each do |soa|
94                                         puts " Nameserver #{nameserver} returns serial #{soa.serial} for #{domain}" if @verbose > 0
95                                         serial << soa.serial unless serial.include? soa.serial
96                                 end
97                         end
98                 end
99         end
100         case serial.length
101                 when 0
102                         warnings << "Found no serials for #{domain}"
103                 when 1
104                         oks << "#{domain} is at #{serial.first}"
105                 else
106                         warnings << "Nameservers disagree on serials for #{domain}: found #{serial.join(', ')}" if serial.length != 1
107         end
108 }
109 dns.close
110
111 if warnings.length > 0
112         puts warnings.join('; ')
113         exit NAGIOS_STATUS[:WARNING]
114 else
115         puts oks.join('; ')
116         exit NAGIOS_STATUS[:OK]
117 end