I need this script to be able to check both IPs that are given to it and exit with an OK... if one of those expected IPs is returned.
The script is run like this:
/bin/dns_checker.pl -s 69.34.55.66 -q htt.jababa.com -e 69.44.56.33,45.47.43.55
Right now, the script is failing, but when I only specify one IP it passes for both of the IPs.
basically, i want this dns query script to pass if it finds just one of the IPs in the list of IPs given to it. the list of IPs given is what i bolded.
thank you guys
#use strict;
use Getopt::Long;
use utils qw(%ERRORS);
use vars qw($opt_V $opt_h $opt_H $opt_w $opt_c $warning $critical);
my $PROGNAME = "check_dns";
my $VERSION = "Beta 1";
my $query = "/usr/bin/host";
sub messages();
Getopt::Long::Configure('bundling');
GetOptions
("V" => \$opt_V, "version" => \$opt_V,
"h" => \$opt_h, "help" => \$opt_h,
"q=s" => \$opt_q, "query=s" => \$opt_q,
"s=s" => \$opt_s, "server=s" => \$opt_p,
"e=s" => \$opt_e, "expected=s" => \$opt_e);
messages();
if ($opt_V) { print $Messages{Version} and exit $ERRORS{'UNKNOWN'}; }
if ($opt_h) { print $Messages{Usage} and exit $ERRORS{'UNKNOWN'}; }
print $Messages{Files} and exit unless (-x $query);
unless ($opt_q && $opt_s) { print $Messages{Usage} and exit $ERRORS{'UNKNOWN'}; }
my $response = `$query $opt_q $opt_s`;
my @response = split('\n', $response);
my @expected = split(',', $opt_e);
my $success = '';
my $fail = '';
my $critical = '';
my $timeout = '';
messages();
@expected = sort(@expected);
IP: foreach my $ip (@expected) {
my $found = 0;
foreach (@response) {
print $Messages{'Timeout'} and exit $ERRORS{'CRITICAL'} if /connection timed out/;
print $Messages{'Failure'} and exit $ERRORS{'CRITICAL'} if /not found/;
#$found = 1 and last if /$opt_q has address $ip/; # This doesnt work because the 'host' command resolves the query string to it's alias
$found = 1 and last if /has address $ip/;
}
$success .= "$ip, " if $found;
$fail .= "$ip, " unless $found;
}
chop $success && chop $success;
chop $fail && chop $fail;
messages();
print $Messages{'Warning'} and exit $ERRORS{'WARNING'} if $fail;
print $Messages{'Ok'} and exit $ERRORS{'OK'} if $success;
print "Unknown Output\n" and exit;
sub messages () {
%Messages = ( 'Files' => "make sure $query exists and is executable.\n",
'Timeout' => "Connection Timed Out, DNS Unavailable!.\n",
'Failure' => "Critical: $opt_s returned $opt_q not found.\n",
'Version' => "$PROGNAME Version: $VERSION\n",
'Ok' => "Ok: $opt_s returned $success on query for $opt_q.\n",
'Warning' => "Warning: $opt_s did not return $fail on query for $opt_q.\n",
'Usage' => "Usage: $PROGNAME -s <server> -q <query> -e <expected>\n"
."\t<server> = DNS Server to query\n"
."\t<query> = DNS record to query\n"
."\t<expected> = comma seperated lists of expected IP's\n" );
}