Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example cmd to header and clean quoting #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions bin/check-dns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
# Note: if testing reverse DNS with -t PTR option,
# results will end with trailing '.' (dot)
#
# EXAMPLES:
# check purple.com A record using the dns server 8.8.8.8
# $ check-dns.rb -s 8.8.8.8 -d purple.com -t A
#
# OUTPUT:
# plain-text
#
Expand All @@ -27,55 +31,55 @@
class DNS < Sensu::Plugin::Check::CLI

option :domain,
:description => "Domain to resolve (or ip if type PTR)",
:description => 'Domain to resolve (or ip if type PTR)',
:short => '-d DOMAIN',
:long => '--domain DOMAIN'

option :type,
:description => "Record type to resolve (A, AAAA, TXT, etc) use PTR for reverse lookup",
:description => 'Record type to resolve (A, AAAA, TXT, etc) use PTR for reverse lookup',
:short => '-t RECORD',
:long => '--type RECORD',
:default => 'A'

option :server,
:description => "Server to use for resolution",
:description => 'Server to use for resolution',
:short => '-s SERVER',
:long => '--server SERVER'

option :result,
:description => "A positive result entry",
:description => 'A positive result entry',
:short => '-r RESULT',
:long => '--result RESULT'

option :warn_only,
:description => "Warn instead of critical on failure",
:description => 'Warn instead of critical on failure',
:short => '-w',
:long => '--warn-only',
:boolean => true

option :debug,
:description => "Print debug information",
:description => 'Print debug information',
:long => '--debug',
:boolean => true

def resolve_domain
if config[:type] == 'PTR'
cmd = "dig #{config[:server] ? "@#{config[:server]}" : ""} -x #{config[:domain]} +short +time=1"
cmd = "dig #{config[:server] ? "@#{config[:server]}" : ''} -x #{config[:domain]} +short +time=1"
else
cmd = "dig #{config[:server] ? "@#{config[:server]}" : ""} #{config[:domain]} #{config[:type]} +short +time=1"
cmd = "dig #{config[:server] ? "@#{config[:server]}" : ''} #{config[:domain]} #{config[:type]} +short +time=1"
end
puts cmd if config[:debug]
output = `#{cmd}`
puts output if config[:debug]
# Trim, split, remove comments and empty lines
entries = output.strip.split("\n").reject{|l| l.match('^;') || l.match('^$')}
entries = output.strip.split("\n").reject { |l| l.match('^;') || l.match('^$') }
puts "Entries: #{entries}" if config[:debug]
entries
end

def run
if config[:domain].nil?
unknown "No domain specified"
unknown 'No domain specified'
else
entries = resolve_domain
if entries.length.zero?
Expand Down