Use PERL_LWP_SSL_CA_PATH not HTTPS_CA_DIR as the latter has issues.
[mirror/dsa-nagios.git] / dsa-nagios-checks / checks / dsa-check-soas
1 #!/usr/bin/ruby
2
3 # Copyright 2006, 2012, 2014 Peter Palfrader
4 #           2012  Uli Martens
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining
7 # a copy of this software and associated documentation files (the
8 # "Software"), to deal in the Software without restriction, including
9 # without limitation the rights to use, copy, modify, merge, publish,
10 # distribute, sublicense, and/or sell copies of the Software, and to
11 # permit persons to whom the Software is furnished to do so, subject to
12 # the following conditions:
13 #
14 # The above copyright notice and this permission notice shall be
15 # included in all copies or substantial portions of the Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25
26 # the each_resource function is lifted from ruby 1.9.1's resolv.rb, with the
27 # minor modification that we do not unconditionally set the message's RD flag
28 # to 1.  Its license is:
29 #
30 # Copyright (C) 1993-2010 Yukihiro Matsumoto. All rights reserved.
31 #
32 # Redistribution and use in source and binary forms, with or without
33 # modification, are permitted provided that the following conditions
34 # are met:
35 # 1. Redistributions of source code must retain the above copyright
36 # notice, this list of conditions and the following disclaimer.
37 # 2. Redistributions in binary form must reproduce the above copyright
38 # notice, this list of conditions and the following disclaimer in the
39 # documentation and/or other materials provided with the distribution.
40 #
41 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
42 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 # SUCH DAMAGE.
52
53
54 require 'ipaddr'
55 require 'resolv'
56 require 'optparse'
57 require 'yaml'
58
59 NAGIOS_STATUS = { :OK => 0, :WARNING => 1, :CRITICAL => 2, :UNKNOWN => -1 };
60 @verbose = 0;
61 @additional_nameservers = []
62 @check_soa_nameservers = true;
63
64 def show_help(parser, code=0, io=STDOUT)
65   program_name = File.basename($0, '.*')
66   io.puts "Usage: #{program_name} [options] <domainname> [<domainname> ...]"
67   io.puts parser.summarize
68   exit(code)
69 end
70 ARGV.options do |opts|
71         opts.on_tail("-h", "--help" , "Display this help screen")                               { show_help(opts) }
72         opts.on("-v", "--verbose"   , String, "Be verbose")                                     { @verbose += 1 }
73         opts.on("-a", "--add=HOST"  , String, "Also check SOA on <nameserver>")                 { |val| @additional_nameservers << val }
74         opts.on("-n", "--no-soa-ns" , String, "Don't query SOA record for list of nameservers") { @check_soa_nameservers = false }
75         opts.parse!
76 end
77 show_help(ARGV.options, 1, STDERR) if ARGV.length == 0
78
79 if @additional_nameservers.count <= 1 and not @check_soa_nameservers
80         program_name = File.basename($0, '.*')
81         STDERR.puts "#{program_name}: Only know about #{@additional_nameservers.count} nameserver(s) and --no-soa-ns specified.  I want at least two."
82         exit(1)
83 end
84
85 class DSADNS < Resolv::DNS
86         attr_reader :rd
87         attr_writer :rd
88
89         def initialize(*args)
90                 super
91                 @rd = 1
92         end
93
94         def each_resource(name, typeclass, &proc)
95                 lazy_initialize
96                 requester = make_udp_requester
97                 senders = {}
98                 begin
99                         @config.resolv(name) {|candidate, tout, nameserver, port|
100                                 msg = Message.new
101 puts @rd
102                                 msg.rd = @rd
103                                 msg.add_question(candidate, typeclass)
104                                 unless sender = senders[[candidate, nameserver, port]]
105                                         sender = senders[[candidate, nameserver, port]] =
106                                                 requester.sender(msg, candidate, nameserver, port)
107                                 end
108                                 reply, reply_name = requester.request(sender, tout)
109                                 case reply.rcode
110                                 when RCode::NoError
111                                         if reply.tc == 1 and not Requester::TCP === requester
112                                                 requester.close
113                                                 # Retry via TCP:
114                                                 requester = make_tcp_requester(nameserver, port)
115                                                 senders = {}
116                                                 # This will use TCP for all remaining candidates (assuming the
117                                                 # current candidate does not already respond successfully via
118                                                 # TCP). This makes sense because we already know the full
119                                                 # response will not fit in an untruncated UDP packet.
120                                                 redo
121                                         else
122                                                 extract_resources(reply, reply_name, typeclass, &proc)
123                                         end
124                                         return
125                                 when RCode::NXDomain
126                                         raise Config::NXDomain.new(reply_name.to_s)
127                                 else
128                                         raise Config::OtherResolvError.new(reply_name.to_s)
129                                 end
130                         }
131                 ensure
132                         requester.close
133                 end
134         end
135 end
136
137 warnings = []
138 oks = []
139
140 def resolve_ns(dns, domain, nameserver)
141         puts "Getting A record for nameserver #{nameserver} for #{domain}" if @verbose > 0
142         arecords = dns.getresources(nameserver, Resolv::DNS::Resource::IN::A)
143         warnings << "Nameserver #{nameserver} for #{domain} has #{arecords.length} A records" if arecords.length != 1
144         addresses = arecords.map { |a| a.address.to_s }
145         puts "Addresses for nameserver #{nameserver} for #{domain}: #{addresses.join(', ')}" if @verbose > 0
146         return addresses
147 end
148
149 dns = Resolv::DNS.new
150 ARGV.each{ |domain|
151         serial = []
152         nameserver_addresses = {}
153         if @check_soa_nameservers
154                 nameservers = dns.getresources(domain, Resolv::DNS::Resource::IN::NS)
155                 nameservernames = nameservers.collect{ |ns| ns.name.to_s }
156                 nameservernames.each do |nameserver|
157                         addrs = resolve_ns(dns, domain, nameserver)
158                         warnings << "Duplicate nameserver #{nameserver} for #{domain}" if nameserver_addresses[nameserver]
159                         nameserver_addresses[nameserver] = addrs
160                 end
161         end
162         @additional_nameservers.each do |ns|
163                 begin
164                         ipa = IPAddr.new(ns)  # check if it's an address
165                         addrs = [ns]
166                 rescue ArgumentError
167                         addrs = resolve_ns(dns, domain, ns)
168                 end
169                 warnings << "Duplicate nameserver #{ns} for #{domain}" if nameserver_addresses[ns]
170                 nameserver_addresses[ns] = addrs
171         end
172
173         nameserver_addresses.each_pair do |nameserver, addrs|
174                 puts "Testing nameserver #{nameserver} for #{domain}" if @verbose > 0
175                 addrs.each do |a|
176                         puts " Nameserver #{nameserver} is at #{a}" if @verbose > 0
177                         begin
178                                 resolver = DSADNS.new({:nameserver => a})
179                                 resolver.rd = 0
180                                 soas = resolver.getresources(domain, Resolv::DNS::Resource::IN::SOA)
181                         rescue SystemCallError => e
182                                 warnings << "Could not resolve #{domain} on #{nameserver}: #{e.message}"
183                         else
184                                 resolver.close
185                                 warnings << "Nameserver #{nameserver} for #{domain} returns #{soas.length} SOAs" if soas.length != 1
186                                 soas.each do |soa|
187                                         puts " Nameserver #{nameserver} returns serial #{soa.serial} for #{domain}" if @verbose > 0
188                                         serial << soa.serial unless serial.include? soa.serial
189                                 end
190                         end
191                 end
192         end
193         case serial.length
194                 when 0
195                         warnings << "Found no serials for #{domain}"
196                 when 1
197                         oks << "#{domain} is at #{serial.first}"
198                 else
199                         warnings << "Nameservers disagree on serials for #{domain}: found #{serial.join(', ')}" if serial.length != 1
200         end
201 }
202 dns.close
203
204 if warnings.length > 0
205         puts warnings.join('; ')
206         exit NAGIOS_STATUS[:WARNING]
207 else
208         puts oks.join('; ')
209         exit NAGIOS_STATUS[:OK]
210 end