Debian Codegraph @

contrib/traceroute.cgi

raw
1 #!/usr/bin/perl
2 #
3 # (c)2004 Andreas Wassatsch
4 # released under GPLv2
5 #
6 # based on traceroute.cgi of Ian Cass Knowledge Matters Ltd
7 # (c)1999 Ian Cass Knowledge Matters Ltd
8 #
9 # This script should be put in your Nagios cgi-bin directory
10 # (usually /usr/local/nagios/sbin)
11 #
12 # It will perform a traceroute from your Nagios box to
13 # the machine that the check_ping plugin is pinging,
14 # output includes links to host status and status map
15 # for hosts known to Nagios
16 #
17 # This software is provided as-is, without any express or implied
18 # warranty. In no event will the author be held liable for any mental
19 # or physical damages arising from the use of this script.
20 #
21 # Legal note:
22 # Nagios is a registered trademark of Ethan Galstad.
23 #
24 use strict;
25 use File::Basename;
26 use POSIX qw(strftime);
27
28 # Global Settings
29 #----------------
30 $| = 1;
31 my($nagios) = "/usr/local/nagios";
32 my($urlbase) = "/nagios";
33 my($refresh) = 30;
34 my($self) = basename($0);
35 my($traceroute) = "/usr/sbin/traceroute -m 20 -q 1";
36
37 # Generate HTTP header
38 #---------------------
39 my($mdate)=`date +"%a, %d %b %Y %H:%M:%S %Z"`;
40 print "Cache-Control: no-store\n";
41 print "Pragma: no-cache\n";
42 print "Last-Modified: $mdate";
43 print "Expires: Thu, 01 Jan 1970 00:00:00 GMT\n";
44 print "Content-type: text/html\n\n";
45
46 # accept either traceroute/foo or traceroute?foo; default to REMOTE_ADDR
47 # if nothing else is specified
48 #-----------------------------------------------------------------------
49 my($addr) = $ENV{PATH_INFO} || $ENV{QUERY_STRING} || $ENV{REMOTE_ADDR};
50 $addr =~ tr/+/ /;
51 $addr =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
52 $addr =~ s,^addr=,,;
53 $addr =~ s/[^A-Za-z0-9\.-]//g; # for security
54
55
56 # HTML Head with META Refresh info / stylesheet
57 #----------------------------------------------
58 print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n";
59 print "<html>\n<head>\n<title>traceroute to host $addr</title>\n";
60 print "<meta http-equiv='refresh' content='$refresh;url=$self?$addr'>\n";
61 print "<link rel='stylesheet' type='text/css' href='$urlbase/stylesheets/status.css'>\n";
62 print "</head>\n<body>\n";
63
64 # Info Box
65 #---------
66 print "<table class='infoBox' border=1 cellspacing=0 cellpadding=0>";
67 print "<tr><td class='infoBox'>";
68 print "<div class='infoBoxTitle'>traceroute</div>";
69 print "Generated by $self<br>";
70 print "Last Updated: $mdate<br>";
71 print "Updated every $refresh seconds<br>";
72 print "Nagios&reg; - <a href='https://www.nagios.org' target='_new' class='homepageURL'>www.nagios.org</a><br>";
73 print "Logged in as <i>$ENV{'REMOTE_USER'}</i><br>";
74 print "</td></tr>";
75 print "</table>";
76
77 print "<p><div align=center class='statusTitle'>";
78 print "Traceroute to Host $addr</div><p>\n";
79
80 print "<table border=0 class='status'>\n";
81
82 # read in nagios hosts
83 #---------------------
84 my(@cfg);
85 my($entry);
86 my($bla);
87 my($host);
88 my(@hostlist);
89 open(HOSTS, "$nagios/etc/hosts.cfg");
90 @cfg = grep {!/#/ && /host_name/} <HOSTS>;
91 close(HOSTS);
92
93 foreach $entry (@cfg) {
94 $entry =~ s/^\s+//;
95 ($bla, $host) = split(/\s+/,$entry);
96 push @hostlist,$host;
97 }
98
99 # open traceroute pipe
100 #---------------------
101 my($i)=0;
102 open(TRACEROUTE, "$traceroute $addr |" ) ||
103 die "<pre>couldn't open pipe to traceroute! $!</pre>";
104
105 my(@arr);
106 my($class);
107 my($known_host);
108 while (<TRACEROUTE>) {
109 chomp;
110 s/\&/\&amp;/g;
111 s/\</\&lt;/g;
112
113 if ($i == 0) {
114
115 # print table header
116 #-------------------
117 print "<tr>";
118 print "<th class='status'>Hop</th>";
119 print "<th class='status'>Host</th>";
120 print "<th class='status'>IP</th>";
121 print "<th class='status'>Round Trip Time</th>";
122 print "</tr>\n";
123
124 } else {
125
126 # class for odd/even lines
127 #-------------------------
128 if ($i/2 == int($i/2)) {
129 $class="statusEven";
130 } else {
131 $class="statusOdd";
132 }
133
134 # parse traceroute lines
135 #-----------------------
136 s/^\s//g;
137 (@arr) = split(/\s+/, $_, 4);
138 if (grep(/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/,$arr[1])) {
139 $arr[3] = $arr[2] ." ". $arr[3];
140 $arr[2] = $arr[1];
141 $arr[1] = "-";
142 }
143 $arr[2] =~ s/\(//;
144 $arr[2] =~ s/\)//;
145
146 # check if host is known to nagios
147 #---------------------------------
148 $known_host = 0;
149 foreach $host (@hostlist) {
150 if ($host eq $arr[1]) {
151 $known_host++;
152 }
153 }
154
155 # print table row
156 #----------------
157 print "<tr>";
158 print "<td class='$class'>$arr[0]</td>\n"; # hop
159
160 print "<td class='$class'>";
161 print "<table width=100%><tr>\n";
162 print "<td align=left class='$class'>\n";
163
164 if ($known_host) {
165 print "<a href='$urlbase/cgi-bin/extinfo.cgi?type=1&host=$arr[1]'>";
166 print "$arr[1]</a></td>\n";
167 } else {
168 print "$arr[1]</td>\n";
169 }
170
171 print "<td width=25 align=right class='$class'>\n";
172 if ($known_host) {
173 print "<a href=statusmap.cgi?host=$arr[1]>";
174 print "<img align=top align=right border=0 width=20 height=20 ";
175 print "src='$urlbase/images/status3.gif'></a>\n";
176 }
177 print "</td></tr></table>\n"; # host
178
179 print "<td class='$class'>$arr[2]</td>\n"; # ip
180 print "<td class='$class'>$arr[3]</td>\n"; # rtt
181 print "</tr>\n";
182 }
183
184 $i++;
185 }
186 close(TRACEROUTE) || die "couldn't close pipe to traceroute! $!";
187
188 print "</table>\n";
189
190
191 # footer
192 #-------
193 print "&nbsp;<p>&nbsp;<p>";
194 print "<font size=-2><i>$self by Andreas Wassatsch</i></font>";
195 print "</body></html>\n";
196
197 # end
198 #----
199 exit 0;
200
201