]> git.sesse.net Git - nms/blob - web/nettkart.pl
initial import
[nms] / web / nettkart.pl
1 #! /usr/bin/perl
2 use CGI;
3 use GD;
4 use DBI;
5 my $cgi = CGI->new;
6
7 my $dbh = DBI->connect("dbi:Pg:dbname=snmpfetch;host=violet.tg05.gathering.org", "snmpfetch", "removed")
8         or die "Couldn't connect to database";
9
10 GD::Image->trueColor(1);
11 $img = GD::Image->new('snmp-bg.png');
12
13 my $blk = $img->colorResolve(0, 0, 0);
14
15 for my $y (42..236) {
16         my $i = 2.0 * ($y - 236.0) / (42.0 - 237.0);
17         my $clr = get_color($i);
18         
19         $img->filledRectangle(32,$y,53,$y+1,$clr);
20 }
21
22 my $q = $dbh->prepare('select * from switches natural join placements natural left join
23 ( select switch,sum(bytes_in)/count(*) as
24 bytes_in,sum(bytes_out)/count(*) as bytes_out from get_datarate() group
25 by switch ) t1 where ip<>\'127.0.0.1\'');
26 $q->execute();
27 while (my $ref = $q->fetchrow_hashref()) {
28
29         # for now:
30         # 100kbit/port = all green
31         # 1gbit/port = all red
32         
33         my $clr;
34
35         if (defined($ref->{'bytes_in'})) {
36                 my $intensity = 0.0;
37                 my $traffic = 4.0 * $ref->{'bytes_in'} + $ref->{'bytes_out'};  # average and convert to bits (should be about the same in practice)
38
39                 my $max = 20_000_000.0;   # 10mbit
40                 my $min =    100_000.0;   # 100kbit
41                 if ($traffic >= $min) {
42                         $intensity = 2.0 * (log($traffic / $min) / log(10)) / (log($max / $min) / log(10));
43                         $intensity = 2.0 if ($intensity > 2.0);
44                 }       
45                 $clr = get_color($intensity);
46         } else {
47                 $clr = $img->colorResolve(0, 0, 255);
48         }
49         
50         $ref->{'placement'} =~ /\((\d+),(\d+)\),\((\d+),(\d+)\)/;
51         $img->filledRectangle($3,$4,$1,$2,$clr);
52         $img->rectangle($3,$4,$1,$2,$blk);
53         $img->stringUp(gdSmallFont,$3,$2-3,$ref->{'sysname'},$blk);
54 }
55 $dbh->disconnect;
56
57 print $cgi->header(-type=>'image/png');
58 print $img->png;
59
60 sub get_color {
61         my $intensity = shift;
62         my $gamma = 1.0/1.90;
63         if ($intensity > 1.0) {
64                 return $img->colorResolve(255.0, 255.0 * ((2.0 - $intensity) ** $gamma), 0);
65         } else {
66                 return $img->colorResolve(255.0 * ($intensity ** $gamma), 255, 0);
67         }
68 }