#!/usr/bin/perl # # (c)2004 Andreas Wassatsch # released under GPLv2 # # based on traceroute.cgi of Ian Cass Knowledge Matters Ltd # (c)1999 Ian Cass Knowledge Matters Ltd # # This script should be put in your Nagios cgi-bin directory # (usually /usr/local/nagios/sbin) # # It will perform a traceroute from your Nagios box to # the machine that the check_ping plugin is pinging, # output includes links to host status and status map # for hosts known to Nagios # # This software is provided as-is, without any express or implied # warranty. In no event will the author be held liable for any mental # or physical damages arising from the use of this script. # # Legal note: # Nagios is a registered trademark of Ethan Galstad. # use strict; use File::Basename; use POSIX qw(strftime); # Global Settings #---------------- $| = 1; my($nagios) = "/usr/local/nagios"; my($urlbase) = "/nagios"; my($refresh) = 30; my($self) = basename($0); my($traceroute) = "/usr/sbin/traceroute -m 20 -q 1"; # Generate HTTP header #--------------------- my($mdate)=`date +"%a, %d %b %Y %H:%M:%S %Z"`; print "Cache-Control: no-store\n"; print "Pragma: no-cache\n"; print "Last-Modified: $mdate"; print "Expires: Thu, 01 Jan 1970 00:00:00 GMT\n"; print "Content-type: text/html\n\n"; # accept either traceroute/foo or traceroute?foo; default to REMOTE_ADDR # if nothing else is specified #----------------------------------------------------------------------- my($addr) = $ENV{PATH_INFO} || $ENV{QUERY_STRING} || $ENV{REMOTE_ADDR}; $addr =~ tr/+/ /; $addr =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $addr =~ s,^addr=,,; $addr =~ s/[^A-Za-z0-9\.-]//g; # for security # HTML Head with META Refresh info / stylesheet #---------------------------------------------- print "\n"; print "\n\ntraceroute to host $addr\n"; print "\n"; print "\n"; print "\n\n"; # Info Box #--------- print ""; print ""; print "
"; print "
traceroute
"; print "Generated by $self
"; print "Last Updated: $mdate
"; print "Updated every $refresh seconds
"; print "Nagios® - www.nagios.org
"; print "Logged in as $ENV{'REMOTE_USER'}
"; print "
"; print "

"; print "Traceroute to Host $addr

\n"; print "\n"; # read in nagios hosts #--------------------- my(@cfg); my($entry); my($bla); my($host); my(@hostlist); open(HOSTS, "$nagios/etc/hosts.cfg"); @cfg = grep {!/#/ && /host_name/} ; close(HOSTS); foreach $entry (@cfg) { $entry =~ s/^\s+//; ($bla, $host) = split(/\s+/,$entry); push @hostlist,$host; } # open traceroute pipe #--------------------- my($i)=0; open(TRACEROUTE, "$traceroute $addr |" ) || die "
couldn't open pipe to traceroute! $!
"; my(@arr); my($class); my($known_host); while () { chomp; s/\&/\&/g; s/\"; print "
"; print ""; print ""; print ""; print "\n"; } else { # class for odd/even lines #------------------------- if ($i/2 == int($i/2)) { $class="statusEven"; } else { $class="statusOdd"; } # parse traceroute lines #----------------------- s/^\s//g; (@arr) = split(/\s+/, $_, 4); if (grep(/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/,$arr[1])) { $arr[3] = $arr[2] ." ". $arr[3]; $arr[2] = $arr[1]; $arr[1] = "-"; } $arr[2] =~ s/\(//; $arr[2] =~ s/\)//; # check if host is known to nagios #--------------------------------- $known_host = 0; foreach $host (@hostlist) { if ($host eq $arr[1]) { $known_host++; } } # print table row #---------------- print ""; print "\n"; # hop print "\n"; # ip print "\n"; # rtt print "\n"; } $i++; } close(TRACEROUTE) || die "couldn't close pipe to traceroute! $!"; print "
HopHostIPRound Trip Time
$arr[0]"; print "\n"; print "\n"; } else { print "$arr[1]\n"; } print "
\n"; if ($known_host) { print ""; print "$arr[1]\n"; if ($known_host) { print ""; print "\n"; } print "
\n"; # host print "
$arr[2]$arr[3]
\n"; # footer #------- print " 

 

"; print "$self by Andreas Wassatsch"; print "\n"; # end #---- exit 0;