#!/usr/bin/perl -w # # DNS monitor # $| = 1; my $help = grep(/-h/, @ARGV); my $verbose = grep(/-v/, @ARGV); my $noemail = grep(/--noemail/, @ARGV); die "Usage: $0 [-h] [-v] [--noemail]\n" if $help; my $email_to = $ENV{'USER'}; die "Env.variable USER is not set" if ! $email_to; my $confLog = "/home/olchansk/public_html/pingmon/dnsmon.log"; open(LOG,">>$confLog") || die "Cannot write \"$confLog\": $!\n"; my $emailsubject; my $emailtext; #my $h = "smtp.triumf.ca"; my $h = "142.90.100.19"; my $now = time(); my $dns_start = time(); my $gethost = `host $h 2>&1`; my $dns_done = time(); my $dns_time = $dns_done - $dns_start; #print "host $h dns time $dns_time\n"; # ladd00.triumf.ca has address 142.90.111.60 # # host linstore # linstore.triumf.ca is an alias for linstore.dmz.triumf.ca. # linstore.dmz.triumf.ca has address 142.90.152.208 # linstore.dmz.triumf.ca has address 142.90.152.209 foreach my $h (sort split(/\n/, $gethost)) { if ($h =~ /has address (\d+.*)/) { $xip .= "," if ($xip); $xip .= $1; #print "[$h] IP address [$ip]\n"; } if ($h =~ /alias for (\w+.*)\./) { $xip .= "," if ($xip); $xip .= "$1"; #print "[$h] IP address [$ip]\n"; } #print "[$h][$xip]\n"; } if ($gethost =~ /has address (\d+.*)/) { $ip = $1; #print "[$h] IP address [$ip]\n"; if ($dns_time > 1) { print LOG "".localtime($now).": $h DNS lookup took $dns_time seconds and returned [$ip]\n"; $emailsubject = "$h DNS lookup took $dns_time seconds and returned [$ip]\n"; } } elsif ($gethost =~ /name pointer (\w+.*)/) { $ip = $1; #print "[$h] IP address [$ip]\n"; if ($dns_time > 1) { print LOG "".localtime($now).": $h DNS lookup took $dns_time seconds and returned [$ip]\n"; $emailsubject = "$h DNS lookup took $dns_time seconds and returned [$ip]\n"; $emailtext = "$h DNS lookup took $dns_time seconds and returned: $gethost\n"; } } elsif ($gethost =~ /NXDOMAIN/) { # no IP address print LOG "".localtime($now).": $h DNS lookup took $dns_time seconds and returned NXDOMAIN\n"; $emailsubject = "$h DNS lookup took $dns_time seconds and returned NXDOMAIN\n"; $emailtext = "$h DNS lookup took $dns_time seconds and returned NXDOMAIN: $gethost\n"; } else { print LOG "".localtime($now).": $h DNS lookup took $dns_time seconds and returned something strange: $gethost\n"; $emailsubject = "$h DNS lookup took $dns_time seconds and returned something strange\n"; $emailtext = "$h DNS lookup took $dns_time seconds and returned something strange: $gethost\n"; } if ((! $noemail) && $emailsubject) { my $cmd = "Mail -s \'dnsmon: $emailsubject\' $email_to"; #print "Email: $cmd\n"; open(OUT, "|$cmd"); print OUT $emailtext; print OUT "\n--\ndnsmon\n"; close OUT; } close LOG; exit 0; # end file