bin/check-dns.rb in sensu-plugins-dns-0.0.5 vs bin/check-dns.rb in sensu-plugins-dns-0.0.6
- old
+ new
@@ -1,11 +1,11 @@
#! /usr/bin/env ruby
#
# check-dns
#
# DESCRIPTION:
-# This plugin checks DNS resolution using ruby `resolv`.
+# This plugin checks DNS resolution using dnsruby .
# Note: if testing reverse DNS with -t PTR option,
# results will not end with trailing '.' (dot)
#
# OUTPUT:
# plain text
@@ -13,10 +13,11 @@
# PLATFORMS:
# Linux, BSD
#
# DEPENDENCIES:
# gem: sensu-plugin
+# gem: dnsruby
#
# USAGE:
# example commands
#
# NOTES:
@@ -27,12 +28,11 @@
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.
#
require 'sensu-plugin/check/cli'
-require 'resolv'
-
+require 'dnsruby'
#
# DNS
#
class DNS < Sensu::Plugin::Check::CLI
option :domain,
@@ -66,34 +66,48 @@
description: 'Print debug information',
long: '--debug',
boolean: true
def resolve_domain
- resolv = config[:server].nil? ? Resolv::DNS.new : Resolv::DNS.new(nameserver: [config[:server]])
- if config[:type] == 'PTR'
- entries = resolv.getnames(config[:domain]).map(&:to_s)
- else
- entries = resolv.getaddresses(config[:domain]).map(&:to_s)
- end
+ resolv = config[:server].nil? ? Dnsruby::Resolver.new : Dnsruby::Resolver.new(nameserver: [config[:server]])
+
+ entries = resolv.query(config[:domain], config[:type])
+
puts "Entries: #{entries}" if config[:debug]
entries
end
def run
unknown 'No domain specified' if config[:domain].nil?
- entries = resolve_domain
- if entries.length.zero?
- output = "Could not resolve #{config[:domain]}"
+ begin
+ entries = resolve_domain
+ rescue Dnsruby::NXDomain
+ output = "Could not resolve #{config[:domain]} #{config[:type]} record"
+ critical(output)
+ return
+ rescue => e
+ output = "Couldn not resolve #{config[:domain]}: #{e}"
+ config[:warn_only] ? warning(output) : critical(output)
+ return
+ end
+ puts entries.answer if config[:debug]
+ if entries.answer.length.zero?
+ output = "Could not resolve #{config[:domain]} #{config[:type]} record"
config[:warn_only] ? warning(output) : critical(output)
elsif config[:result]
- if entries.include?(config[:result])
- ok "Resolved #{config[:domain]} including #{config[:result]}"
+ if entries.answer.count > 1
+ b = entries.answer.rrsets("#{config[:type]}").to_s
else
- critical "Resolved #{config[:domain]} did not include #{config[:result]}"
+ b = entries.answer.first.to_s
end
+ if b.include?(config[:result])
+ ok "Resolved #{config[:domain]} #{config[:type]} included #{config[:result]}"
+ else
+ critical "Resolved #{config[:domain]} #{config[:type]} did not include #{config[:result]}"
+ end
else
- ok "Resolved #{config[:domain]} #{config[:type]} records"
+ ok "Resolved #{config[:domain]} #{config[:type]}"
end
end
end