Skip to content

Instantly share code, notes, and snippets.

@hirose31
Created March 26, 2009 05:05
Show Gist options
  • Save hirose31/85899 to your computer and use it in GitHub Desktop.
Save hirose31/85899 to your computer and use it in GitHub Desktop.
check-dns-ans.pl
#!/usr/bin/env perl
# Time-stamp: <2008-01-13 23:16:29 JST, hirose>
use strict;
use warnings;
use Data::Dumper;
use IO::File;
STDOUT->autoflush(1);
STDERR->autoflush(1);
use Net::DNS;
use POSIX qw(strftime);
use Time::HiRes qw(gettimeofday tv_interval);
sub d(@) {
my $d = Dumper(\@_);
$d =~ s/\\x{([0-9a-z]+)}/chr(hex($1))/ge;
print $d;
}
our @NS_XName =
qw(
195.234.42.1
87.98.164.164
88.191.64.64
);
our @NS_EditDNS =
qw(
74.52.212.235
72.249.105.234
64.251.10.77
87.239.16.157
91.186.15.45
);
our @NS = (@NS_XName, @NS_EditDNS);
our %NS_TAG;
map { $NS_TAG{$_} = 'XN' } @NS_XName;
map { $NS_TAG{$_} = 'ED' } @NS_EditDNS;
$SIG{__DIE__} = $SIG{INT} = $SIG{TERM} = sub {exit};
my %Fail = map {$_=>0} @NS;
MAIN: {
my $hostname = shift @ARGV or die "missing argument: hostname";
printf "query target: %s\n", $hostname;
my %resolver = map { $_ => Net::DNS::Resolver->new(
nameservers => [$_],
tcp_timeout => 5,
udp_timeout => 5,
) } @NS;
for (;;) {
for my $ns (@NS) {
my($ans, $elapse) = ("", 0);
my $t0 = [gettimeofday];
if (my $query = $resolver{$ns}->query($hostname, 'A')) {
$elapse = tv_interval $t0;
$ans = join ",", map {$_->address} grep {$_->type eq 'A'} $query->answer;
} else {
$Fail{$ns}++;
}
printf("%s %2s %-15s [%9.6f] (%s)\n",
strftime("%Y-%m-%d %H:%M:%S", localtime),
$NS_TAG{$ns} || "--",
$ns,
$elapse,
$ans,
);
exit if $ENV{NOLOOP};
}
sleep($ENV{INTERVAL} || 300);
}
}
END {
print "\nfail count\n";
for my $ns (@NS) {
printf("%2s %-15s %4d\n",
$NS_TAG{$ns} || "--",
$ns,
$Fail{$ns},
);
}
}
__END__
# for Emacsen
# Local Variables:
# mode: cperl
# cperl-indent-level: 4
# indent-tabs-mode: nil
# coding: utf-8
# End:
# vi: set ts=4 sw=4 sts=0 :
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment