git.lirion.de

Of git, get, and gud

summaryrefslogtreecommitdiffstats
path: root/nagios-plugins-contrib-24.20190301~bpo9+1/dsa/checks/dsa-check-soas
blob: eb28d8c97517ceb883cf6c148514599a096ff63e (plain)
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
#!/usr/bin/ruby

# Copyright 2006, 2012, 2014 Peter Palfrader
#           2012  Uli Martens
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


# the each_resource function is lifted from ruby 1.9.1's resolv.rb, with the
# minor modification that we do not unconditionally set the message's RD flag
# to 1.  Its license is:
#
# Copyright (C) 1993-2010 Yukihiro Matsumoto. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.


require 'ipaddr'
require 'resolv'
require 'optparse'
require 'yaml'

NAGIOS_STATUS = { :OK => 0, :WARNING => 1, :CRITICAL => 2, :UNKNOWN => -1 };
 _æ_ verbose = 0;
 _æ_ additional_nameservers = []
 _æ_ check_soa_nameservers = true;

def show_help(parser, code=0, io=STDOUT)
  program_name = File.basename($0, '.*')
  io.puts "Usage: #{program_name} [options]  [ ...]"
  io.puts parser.summarize
  exit(code)
end
ARGV.options do |opts|
        opts.on_tail("-h", "--help" , "Display this help screen")                               { show_help(opts) }
        opts.on("-v", "--verbose"   , String, "Be verbose")                                     {  _æ_ verbose += 1 }
        opts.on("-a", "--add=HOST"  , String, "Also check SOA on ")                 { |val|  _æ_ additional_nameservers << val }
        opts.on("-n", "--no-soa-ns" , String, "Don't query SOA record for list of nameservers") {  _æ_ check_soa_nameservers = false }
        opts.parse!
end
show_help(ARGV.options, 1, STDERR) if ARGV.length == 0

if  _æ_ additional_nameservers.count <= 1 and not  _æ_ check_soa_nameservers
	program_name = File.basename($0, '.*')
	STDERR.puts "#{program_name}: Only know about #{ _æ_ additional_nameservers.count} nameserver(s) and --no-soa-ns specified.  I want at least two."
	exit(1)
end

class DSADNS < Resolv::DNS
	attr_reader :rd
	attr_writer :rd

	def initialize(*args)
		super
		 _æ_ rd = 1
	end

	def each_resource(name, typeclass, &proc)
		lazy_initialize
		requester = make_udp_requester
		senders = {}
		begin
			 _æ_ config.resolv(name) {|candidate, tout, nameserver, port|
				msg = Message.new
				msg.rd =  _æ_ rd
				msg.add_question(candidate, typeclass)
				unless sender = senders[[candidate, nameserver, port]]
					sender = senders[[candidate, nameserver, port]] =
						requester.sender(msg, candidate, nameserver, port)
				end
				reply, reply_name = requester.request(sender, tout)
				case reply.rcode
				when RCode::NoError
					if reply.tc == 1 and not Requester::TCP === requester
						requester.close
						# Retry via TCP:
						requester = make_tcp_requester(nameserver, port)
						senders = {}
						# This will use TCP for all remaining candidates (assuming the
						# current candidate does not already respond successfully via
						# TCP).	This makes sense because we already know the full
						# response will not fit in an untruncated UDP packet.
						redo
					else
						extract_resources(reply, reply_name, typeclass, &proc)
					end
					return
				when RCode::NXDomain
					raise Config::NXDomain.new(reply_name.to_s)
				else
					raise Config::OtherResolvError.new(reply_name.to_s)
				end
			}
		ensure
			requester.close
		end
	end
end

warnings = []
oks = []

def resolve_ns(dns, domain, nameserver)
	puts "Getting A record for nameserver #{nameserver} for #{domain}" if  _æ_ verbose > 0
	arecords = dns.getresources(nameserver, Resolv::DNS::Resource::IN::A)
	warnings << "Nameserver #{nameserver} for #{domain} has #{arecords.length} A records" if arecords.length != 1
	addresses = arecords.map { |a| a.address.to_s }
	puts "Addresses for nameserver #{nameserver} for #{domain}: #{addresses.join(', ')}" if  _æ_ verbose > 0
	return addresses
end

dns = Resolv::DNS.new
ARGV.each{ |domain|
	serial = {}
	nameserver_addresses = {}
	if  _æ_ check_soa_nameservers
		nameservers = dns.getresources(domain, Resolv::DNS::Resource::IN::NS)
		nameservernames = nameservers.collect{ |ns| ns.name.to_s }
		nameservernames.each do |nameserver|
			addrs = resolve_ns(dns, domain, nameserver)
			warnings << "Duplicate nameserver #{nameserver} for #{domain}" if nameserver_addresses[nameserver]
			nameserver_addresses[nameserver] = addrs
		end
	end
	 _æ_ additional_nameservers.each do |ns|
		begin
			ipa = IPAddr.new(ns)  # check if it's an address
			addrs = [ns]
		rescue ArgumentError
			addrs = resolve_ns(dns, domain, ns)
		end
		warnings << "Duplicate nameserver #{ns} for #{domain}" if nameserver_addresses[ns]
		nameserver_addresses[ns] = addrs
	end

	nameserver_addresses.each_pair do |nameserver, addrs|
		puts "Testing nameserver #{nameserver} for #{domain}" if  _æ_ verbose > 0
		addrs.each do |a|
			puts " Nameserver #{nameserver} is at #{a}" if  _æ_ verbose > 0
			begin
				resolver = DSADNS.new({:nameserver => a})
				resolver.rd = 0
				soas = resolver.getresources(domain, Resolv::DNS::Resource::IN::SOA)
			rescue SystemCallError => e
				warnings << "Could not resolve #{domain} on #{nameserver}: #{e.message}"
			else
				resolver.close
				warnings << "Nameserver #{nameserver} for #{domain} returns #{soas.length} SOAs" if soas.length != 1
				soas.each do |soa|
					puts " Nameserver #{nameserver} returns serial #{soa.serial} for #{domain}" if  _æ_ verbose > 0
					sn = soa.serial.to_i
					if serial.has_key? sn then
						serial[sn] << nameserver
					else
						serial[sn] = [nameserver]
					end
				end
			end
		end
	end
	case serial.keys.length
		when 0
			warnings << "Found no serials for #{domain}"
		when 1
			oks << "#{domain} is at #{serial.keys.first}"
		else
			text = []
			serial.keys.sort.each do |sn|
				text << "#{sn} (#{serial[sn].join(', ')})"
			end
			warnings << "Nameservers disagree on serials for #{domain}: found #{text.join(', ')}"
	end
}
dns.close

if warnings.length > 0
	puts warnings.join('; ')
	exit NAGIOS_STATUS[:WARNING]
else
	puts oks.join('; ')
	exit NAGIOS_STATUS[:OK]
end