Created
August 4, 2010 08:38
-
-
Save sherwind/507854 to your computer and use it in GitHub Desktop.
A Nagios script that checks if a server is listed in a RBL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -wT | |
# A Nagios script that checks if a server is listed in a RBL | |
# by Sherwin Daganato <[email protected]>, 20070607 | |
$|++; | |
use strict; | |
use Getopt::Long; | |
# --- BEGIN config | |
# see list at http://www.dnsstuff.com/tools/ip4r.ch?ip=127.0.0.1 and | |
# http://en.wikipedia.org/wiki/Comparison_of_DNS_blacklists | |
my @DNSBL = ( | |
'bl.spamcop.net', | |
'problems.dnsbl.sorbs.net', | |
'zen.spamhaus.org', # free only for individuals operating small mail servers with low email traffic | |
'ips.backscatterer.org', | |
); | |
my $SERVICE = 'DNSBL-CHECK'; | |
# --- END config | |
BEGIN { | |
$ENV{'PATH'} = '/bin:/usr/bin'; | |
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'}; | |
} | |
my %ERRORS = ('OK' => 0, 'WARNING' => 1, 'CRITICAL' => 2, 'UNKNOWN' => 3, 'DEPENDENT' => 4); | |
$SIG{__DIE__} = sub { | |
print "$_[0]\n"; | |
exit $ERRORS{UNKNOWN}; | |
}; | |
our $opt_I; | |
Getopt::Long::Configure('bundling'); | |
GetOptions('I=s' => \$opt_I, 'ipaddress=s' => \$opt_I); | |
die "Usage: $0 -I <IP-address>" unless $opt_I and (my $ipaddr_rev = get_ipaddr_rev($opt_I)); | |
my ($blocker, $txt_rdata); | |
foreach my $dnsbl (@DNSBL) { | |
my $domain = $ipaddr_rev . '.' . $dnsbl; | |
local $_ = dig($domain, 'A'); | |
if (/^;; ->>HEADER<<- opcode: QUERY, status: NOERROR,/m) { | |
local $_ = dig($domain, 'TXT'); | |
($txt_rdata) = /^$domain\.\s+\d+\s+IN\s+TXT\s+"(.*?)"/im; | |
$blocker = $dnsbl; | |
last; | |
} | |
} | |
if ($blocker) { | |
print "$SERVICE CRITICAL - Listed in $blocker" . ($txt_rdata ? " [$txt_rdata]" : ''); | |
exit $ERRORS{CRITICAL}; | |
} else { | |
print "$SERVICE OK - Not listed"; | |
exit $ERRORS{OK}; | |
} | |
sub get_ipaddr_rev { | |
join '.', reverse($_[0] =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/); | |
} | |
sub dig { | |
my ($domain, $type) = @_; | |
`/usr/bin/dig $domain $type +noall +comments +answer`; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment