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
|
#!/usr/bin/ruby1.8
require 'resolv-replace'
#
# This is a protocol tester for DNS.
#
class DNSTest
#
# Data passed from the JSON hash.
#
attr_reader :test_data
#
# The error text we return on failure.
#
attr_reader :error
#
# Save the data away.
#
def initialize( data )
@test_data = data
@error = nil
#
# Ensure we have a host to monitor
#
if ( @test_data["target_host"].nil? )
@error = "Missing host to test against."
raise ArgumentError, @error
end
#
# Ensure we have test-specific keys.
#
if ( @test_data["resolve_name"].nil? )
@error = "Missing host to resolve."
raise ArgumentError, @error
end
if ( @test_data["resolve_type"].nil? )
@error = "Missing type to resolve, for example A/MX/NS."
raise ArgumentError, @error
end
if ( @test_data["resolve_expected"].nil? )
@error = "Missing expected results to compare against.."
raise ArgumentError, @error
end
end
#
# Run the test.
#
# Return "true" on success
#
# Return "false" on failure.
#
# If the test fails the details should be retrieved from "error()".
#
def run_test
#
# Reset state from previous test.
#
@error = nil
#
# Get the nameserver to resolve with
#
nameserver = @test_data["target_host"]
#
# Get the record type to lookup
#
record = @test_data["resolve_type"]
#
# Get the hostname to lookup
#
lookup = @test_data["resolve_name"]
#
# Get the expected results
#
expected = @test_data["resolve_expected"]
#
# Do the lookup
#
results = resolve_via( nameserver, record, lookup )
#
# Show the results if we found something.
#
if ( @test_data['verbose'] )
results.each do |res|
puts "Result: #{res}"
end
end
#
# OK we have an array of results. If every one of the expected
# results is contained in those results returned then we're good.
#
expected.split( /;/ ).each do |required|
if ( ! results.include?( required ) )
@error = "The expected result #{required} was not found in the results: #{results.join(",")}"
return false
end
end
return true
end
#
# Resolve an IP
#
def resolve_via( server, ltype, name )
puts "Resolving #{name} [#{ltype}] via server #{server}"
results = Array.new()
Resolv::DNS.open(:nameserver=>[server]) do |dns|
case ltype
when /^A$/ then
dns.getresources(name, Resolv::DNS::Resource::IN::A).map{ |r| results.push r.address.to_s() }
when /^AAAA$/ then
dns.getresources(name, Resolv::DNS::Resource::IN::AAAA).map{ |r| results.push r.address.to_s() }
when /^NS$/ then
dns.getresources(name, Resolv::DNS::Resource::IN::MX).map{ |r| results.pushResolv.getaddresses(r.name.to_s()) }
when /^MX$/ then
dns.getresources(name, Resolv::DNS::Resource::IN::MX).map{ |r| results.push Resolv.getaddresses(r.exchange.to_s()) }
end
end
results.flatten!
return results
end
#
# Return the error text for why this test failed.
#
def error
return @error
end
end
#
# Sample test, for testing.
#
if __FILE__ == $0 then
#
# Sample data.
#
test = {
"target_host" => "a.ns.bytemark.co.uk",
"test_type" => "dns",
"verbose" => 1,
"timeout" => 4,
"test_alert" => "DNS failure",
"resolve_name" => "support.bytemark.co.uk",
"resolve_type" => "MX",
"resolve_expected" => "89.16.184.148;89.16.184.149;89.16.184.150"
}
#
# Run the test.
#
obj = DNSTest.new( test )
if ( obj.run_test )
puts "TEST OK"
else
puts "TEST FAILED"
puts obj.error()
end
end
|