]> git.sesse.net Git - nms/blob - web/nettkart-text.pl
initial import
[nms] / web / nettkart-text.pl
1 #! /usr/bin/perl
2 use CGI;
3 use DBI;
4 my $cgi = CGI->new;
5
6 my $dbh = DBI->connect("dbi:Pg:dbname=snmpfetch;host=violet.tg05.gathering.org", "snmpfetch", "removed")
7         or die "Couldn't connect to database";
8 print $cgi->header(-type=>'text/html', -refresh=>'10; http://nms.tg05.gathering.org/nettkart-text.pl');
9
10 print <<"EOF";
11 <html>
12   <head>
13     <title>nettkart</title>
14   </head>
15   <body>
16     <map name="switches">
17 EOF
18
19 my $q = $dbh->prepare('select * from switches natural join placements natural left join
20 ( select switch,sum(bytes_in)/count(*) as
21 bytes_in,sum(bytes_out)/count(*) as bytes_out from get_datarate() group
22 by switch ) t1');
23 $q->execute();
24 while (my $ref = $q->fetchrow_hashref()) {
25         $ref->{'placement'} =~ /\((\d+),(\d+)\),\((\d+),(\d+)\)/;
26         
27         my $traffic = 4.0 * $ref->{'bytes_in'} + $ref->{'bytes_out'};  # average and convert to bits (should be about the same in practice)
28         my $ttext;
29         if ($traffic >= 1_000_000_000) {
30                 $ttext = sprintf "%.2f Gbit/port/sec", $traffic/1_000_000_000;
31         } elsif ($traffic => 1_000_000) {
32                 $ttext = sprintf "%.2f Mbit/port/sec", $traffic/1_000_000;
33         } else {
34                 $ttext = sprintf "%.2f kbit/port/sec", $traffic/1_000;
35         }
36
37         printf "      <area shape=\"rect\" coords=\"%u,%u,%u,%u\" href=\"showswitch.pl?id=%u\" alt=\"%s (%s)\" onmouseover=\"window.status='%s (%s)'; return true\" onmouseout=\"window.status=''\" />\n",
38                 $3, $4, $1, $2, $ref->{'switch'}, $ref->{'sysname'},
39                 $ttext, $ref->{'sysname'}, $ttext;
40 }
41 $dbh->disconnect;
42
43 print <<"EOF";
44     </map>
45
46     <p><img src="nettkart.pl" usemap="#switches" /></p>
47   </body>
48 </html>
49 EOF