-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.cgi
executable file
·482 lines (381 loc) · 14.8 KB
/
index.cgi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#!/usr/bin/perl -w -T
#======================================================================
#
# psTracerouteViewer index.cgi
#
# A cgi for viewing traceroute data stored in PerfSonar.
#
# Version 2, with support for revised psTracerouteUtils lib w/
# esmond support, and mtu reporting.
#
# Written by: Dale W. Carder, [email protected]
# Network Services Group
# Division of Information Technology
# University of Wisconsin at Madison
#
# Inspired in large part by traceroute_improved.cgi by Yuan Cao <[email protected]>
#
# Copyright 2014 The University of Wisconsin Board of Regents
# Licensed and distributed under the terms of the Artistic License 2.0
#
# See the file LICENSE for details or reference the URL:
# http://www.perlfoundation.org/artistic_license_2_0
#
#======================================================================
#TODO time each operation and present it at the bottom.
#======================================================================
# C O N F I G U R A T I O N S E C T I O N
#
my $Script = 'index.cgi';
my $Default_mahost = 'http://localhost:8086/perfSONAR_PS/services/tracerouteMA';
#
#======================================================================
#======================================================================
# U S E A N D R E Q U I R E
use lib "/opt/perfsonar_ps/toolkit/lib";
use strict;
use psTracerouteUtils 2.0;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
use Date::Manip;
use Socket;
use Socket6;
use Data::Validate::IP qw(is_ipv4 is_ipv6);
use Data::Dumper;
use DateTime::TimeZone;
use POSIX qw(tzset);
#
#======================================================================
#======================================================================
# F U N C T I O N P R O T O T Y P E S
sub parseInput();
sub lookup($;$);
sub displayTrData();
sub displayTop();
sub displaySelectBox();
sub utcOffset($);
#
#======================================================================
#======================================================================
# G L O B A L S
my $mahost; # measurement archive host url
my $stime; # start time passed in
my $etime; # end time passed in
my $epoch_stime; # start time in unix epoch
my $epoch_etime; # end time in unix epoch
my %endpoint; # measurement endpoints
my $epselect; # endpoint selection
my $donotdedup; # deduplication checkbox
my %dnscache; # duh
#
#======================================================================
#======================================================================
# M A I N
# print http header
print("Content-Type: text/html;\n\n");
parseInput();
print "<input type='hidden' name='s' value='$epoch_stime'>\n";
print "<input type='hidden' name='e' value='$epoch_etime'>\n";
displayTop();
my $msg = GetTracerouteMetadata($mahost,$epoch_stime,$epoch_etime,\%endpoint);
if ( scalar(keys((%endpoint))) < 1 ) {
unless(defined($msg)) { $msg = ' '; }
print "<b><font color=\"red\">Error: No Measurement Archives available.<br>$msg</font></b>\n<br>\n";
} else {
displaySelectBox();
if ($epselect ne 'unselected') {
displayTrData();
}
}
print "<br><br>";
exit();
#============= B E G I N S U B R O U T I N E S =============================
# Sanity Check all cgi input, and set defaults if none are given
sub parseInput() {
# timezone support
my $tz;
# It turns out that DateTime::TimeZone can die() if it can't
# figure out the timezone. Instead, catch that and set a default.
# http://code.google.com/p/perfsonar-ps/issues/detail?id=819
eval { $tz = DateTime::TimeZone->new(name=>'local'); };
if ( $@ ) {
$ENV{TZ} = "America/Chicago";
} else {
$ENV{TZ} = $tz->name;
}
if (defined(param('tzselect'))) {
if (param('tzselect') =~ m/^[0-9a-zA-Z:\/_\-]+$/ ) {
$ENV{TZ} = param('tzselect');
}
}
tzset;
# measurement archive url
if (defined(param("mahost"))) {
if (param("mahost") =~ m/^[0-9a-zA-Z\/:_\.\-]+$/) {
$mahost = param("mahost");
} else {
die("Illegal characters in measurement archive host url.");
}
} else {
$mahost = $Default_mahost;
}
# start time
if (defined(param("stime"))) {
if (param("stime") =~ m/^[0-9a-zA-Z: \/]+$/) {
$stime = param("stime");
$epoch_stime = UnixDate(ParseDate($stime),"%s");
} else {
die('Illegal start time: ' . param('stime'));
}
} else {
# default to last 24 hours
#$epoch_stime = UnixDate(ParseDate("now"),"%s") - 86400;
#$stime = ParseDateString("epoch $epoch_stime");
$stime = "yesterday";
$epoch_stime = UnixDate(ParseDate($stime),"%s");
}
# end time
if (defined(param("etime"))) {
if (param("etime") =~ m/^[0-9a-zA-Z: \/]+$/) {
$etime = param("etime");
$epoch_etime = UnixDate(ParseDate($etime),"%s");
} else {
die("Illegal end time.");
}
} else {
$etime = "now";
$epoch_etime = UnixDate(ParseDate($etime),"%s");
}
if ($epoch_stime >= $epoch_etime) {
die("Start time $epoch_stime is after end time $epoch_etime.");
}
if (defined(param('epselect'))) {
if (param('epselect') =~ m/^[0-9a-zA-Z\-\/]+$/ ) {
$epselect = param('epselect');
} else {
die("Illegal endpoint selection: " . param('epselect'));
}
} else {
$epselect = "unselected";
}
# deduplication checkbox
if (defined(param('donotdedup'))) {
if (param('donotdedup') eq 1) {
$donotdedup = 1;
}
} else {
$donotdedup = 0;
}
}
# given something, return the opposite
sub lookup($;$) {
my $thing = shift;
my $af = shift;
my $r;
if (defined($af)) {
if (defined($dnscache{$af}{$thing})) {
return $dnscache{$af}{$thing};
}
} elsif (defined($dnscache{$thing})) {
return $dnscache{$thing};
}
if (is_ipv4($thing)) {
$r = gethostbyaddr(inet_pton(AF_INET,$thing),AF_INET);
} elsif (is_ipv6($thing)) {
$r = gethostbyaddr(inet_pton(AF_INET6,$thing),AF_INET6);
# assume we're given a name, and a preference
} elsif (defined($af)) {
my $n = scalar(gethostbyname2($thing,$af));
if (defined($n)) {
$r = inet_ntop($af,$n);
}
}
if (defined($r)) {
if (defined($af)) {
$dnscache{$af}{$thing} = $r;
} else {
$dnscache{$thing} = $r;
}
return $r;
} else {
return " ";
}
}
sub displayTrData() {
# display traceroute data
my %topology;
my $msg = GetTracerouteData($mahost,$epselect,$epoch_stime,$epoch_etime,\%topology);
if (defined($msg) && $msg ne '') {
print "<font color='red'>Error: $msg</font><p>";
}
unless($donotdedup) {
my %new_topology;
DeduplicateTracerouteData(\%topology,\%new_topology);
undef(%topology);
%topology = %new_topology;
}
#print "<pre>\n";
#print Dumper(%topology);
#print "</pre>\n";
foreach my $time (sort keys %topology) {
my $humantime = scalar(localtime($time));
print "\n\n<h3>Topology beginning at $humantime (" . utcOffset($ENV{'TZ'}) .")</h3><blockquote>\n";
print "<input type='hidden' name='t' value='$time'>\n";
print "<table border=1 cellspacing=0 cellpadding=3>\n";
print "<tr><th>Hop</th><th>Router</th><th>IP</th><th>MTU</th></tr>\n";
foreach my $hopnum (sort { $a <=> $b } keys %{$topology{$time}} ) {
my $sayecmp=" ";
foreach my $router (keys %{$topology{$time}{$hopnum}}) {
# detect if this hop has more than router
my $name = $router;
if (scalar(keys %{$topology{$time}{$hopnum}}) > 1) { $sayecmp = "(<b>ECMP</b>)"; }
if (lookup($router) ne ' ') {
$name = lookup($router);
}
# handle MTU
my $mtu = ' ';
if (defined($topology{$time}{$hopnum}{$router}) && $topology{$time}{$hopnum}{$router} != 1){
$mtu = $topology{$time}{$hopnum}{$router};
}
print "<tr><td>$hopnum $sayecmp</td><td>$name</td><td>$router</td><td>$mtu</td></tr>\n";
}
}
print "</table></blockquote>";
}
} # end displayTrData()
sub displaySelectBox() {
my $html3=<<EOM;
Select endpoints available on $mahost<br>
<select name="epselect">
<option value="unselected">Select one ...
EOM
print $html3;
my %options;
foreach my $id (keys %endpoint) {
my $srchost;
my $dsthost;
# some logic to do dns lookups as needed to make the select field human friendly
if ($endpoint{$id}{'srctype'} =~ m/ipv[46]/ ) {
$srchost = lookup($endpoint{$id}{'srcval'}) . ' ('. $endpoint{$id}{'srcval'} . ')' ;
} else { # we have a hostname, but want the ip
if ($endpoint{$id}{'dsttype'} eq 'ipv6') {
$srchost = $endpoint{$id}{'srcval'} . ' ('. lookup($endpoint{$id}{'srcval'},AF_INET6) .') ';
# so, now we have to guess if this is a v4 or v6 host. prefer v6 like a typical host.
} elsif (lookup($endpoint{$id}{'srcval'},AF_INET6) ne ' ' && lookup($endpoint{$id}{'dstval'},AF_INET6) ne ' ') {
$srchost = $endpoint{$id}{'srcval'} . ' ('. lookup($endpoint{$id}{'srcval'},AF_INET6) .') ';
} else {
$srchost = $endpoint{$id}{'srcval'} . ' ('. lookup($endpoint{$id}{'srcval'},AF_INET) .') ';
}
}
if ($endpoint{$id}{'dsttype'} =~ m/ipv[46]/ ) {
$dsthost = lookup($endpoint{$id}{'dstval'}) . ' ('. $endpoint{$id}{'dstval'} . ')' ;
} else { # we have a hostname, but want the ip
if ($endpoint{$id}{'srctype'} eq 'ipv6') {
$dsthost = $endpoint{$id}{'dstval'} . ' ('. lookup($endpoint{$id}{'dstval'},AF_INET6) .') ';
# so, now we have to guess if this is a v4 or v6 host. prefer v6 like a typical host.
} elsif (lookup($endpoint{$id}{'dstval'},AF_INET6) ne ' ' && lookup($endpoint{$id}{'srcval'},AF_INET6) ne ' ') {
$dsthost = $endpoint{$id}{'dstval'} . ' ('. lookup($endpoint{$id}{'dstval'},AF_INET6) .') ';
} else{
$dsthost = $endpoint{$id}{'dstval'} . ' ('. lookup($endpoint{$id}{'dstval'},AF_INET) .') ';
}
}
# determine if something was already selected or not.
my $selected=" ";
if ($id eq $epselect) { $selected="selected=\"selected\""; }
$options{"$srchost ----> $dsthost"} = "<option value=\"$id\" $selected >";
}
foreach my $srcdst (sort keys(%options)) {
print $options{$srcdst} . $srcdst . "\n" ;
}
# see if the checkbox should be selected
my $dedupsel = " ";
if ($donotdedup) {
$dedupsel = "checked=\"yes\"";
}
my $html2=<<EOM;
</select><br>
<input type="checkbox" name="donotdedup" value="1" $dedupsel>Do not de-duplicate results
<input type="submit" value="Submit query">
</form>
EOM
print $html2;
} #end displaySelectBox()
sub displayTop() {
my $ma_size = length($mahost) + 10;
# print some html
my $html1 =<<EOM;
<html>
<head>
<META NAME="robots" CONTENT="noindex,nofollow">
<title>psTracerouteViewer v2</title>
<style type="text/css">\@import url(jscalendar/calendar-win2k-1.css);</style>
<script type="text/javascript" src="jscalendar/calendar.js"></script>
<script type="text/javascript" src="jscalendar/lang/calendar-en.js"></script>
<script type="text/javascript" src="jscalendar/calendar-setup.js"></script>
</head>
<h2>psTracerouteViewer v2</h2>
<table border=0 width="100%">
<tr>
<td valign="top">
<form name="query1" method="get" action="$Script">
Measurement Archive: <input type="text" name="mahost" value="$mahost" size="$ma_size"> <br>
Start Time: <input type="text" id="stime" name="stime" value="$stime" size="18">
<img src="calendaricon.jpg" id="s_trigger" border=0>
<script type="text/javascript">
Calendar.setup({
inputField : "stime",
ifFormat : "%m/%d/%Y %I:%M %p",
showsTime : true,
button : "s_trigger",
step : 1
});
</script><br>
End Time: <input type="text" id="etime" name="etime" value="$etime" size="18">
<img src="calendaricon.jpg" id="e_trigger" border=0>
<script type="text/javascript">
Calendar.setup({
inputField : "etime",
ifFormat : "%m/%d/%Y %I:%M %p",
showsTime : true,
button : "e_trigger",
step : 1
});
</script>
<br>
EOM
print $html1;
# timezone support
#print "tz=" . $ENV{TZ} . "<br>\n";
print "Timezone: <select name=\"tzselect\">\n";
foreach my $tz (DateTime::TimeZone->all_names) {
my $tz_selected = " ";
if ($tz eq $ENV{'TZ'}) { $tz_selected = "selected=\"selected\""; }
print "<option $tz_selected value=\"$tz\">$tz (" . utcOffset($tz) . ")</option>\n";
}
print "</select>\n";
my $html11=<<EOM;
<input type="submit" value="Query MA">
</td><td valign="top">
<center>
<a href="http://www.wisc.edu" border=0><img src="uwm.gif"></a><br>
<br>
Maintained by <a href="http://net.doit.wisc.edu/~dwcarder">Dale W. Carder</a><br>
Get the <a href=\"https://github.com/dwcarder/psTracerouteViewer\">source!
</center>
</td></tr></table>
<br><hr>
EOM
print $html11;
} #end displayTop();
sub utcOffset($) {
my $tz = shift;
# figure out the offset from gmt
my $t = DateTime::TimeZone->new(name => $tz);
my $now = DateTime->now;
my $offset = $t->offset_for_datetime($now);
$offset = $offset / 60 / 60;
if ($offset > 0) { $offset = "UTC +" . $offset; }
elsif ($offset == 0) { $offset = "UTC"; }
else { $offset = "UTC $offset"; }
return($offset);
} # end utcOffset