]> git.sesse.net Git - nms/blob - web/portkart.pl
initial import
[nms] / web / portkart.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 switch,port,bytes_in,bytes_out,placement,switchtype from switches natural join placements natural join get_current_datarate() where switchtype in (\'es3024\')');
23 $q->execute();
24 while (my $ref = $q->fetchrow_hashref()) {
25
26         # for now:
27         # 100kbit/port = all green
28         # 1gbit/port = all red
29         
30         my $clr;
31
32         if (defined($ref->{'bytes_in'})) {
33                 my $intensity = 0.0;
34                 my $traffic = 4.0 * $ref->{'bytes_in'} + $ref->{'bytes_out'};  # average and convert to bits (should be about the same in practice)
35
36                 my $max = 20_000_000.0;   # 100mbit
37                 my $min =    100_000.0;   # 100kbit
38                 if ($traffic >= $min) {
39                         $intensity = 2.0 * (log($traffic / $min) / log(10)) / (log($max / $min) / log(10));
40                         $intensity = 2.0 if ($intensity > 2.0);
41                 }
42                 $clr = get_color($intensity);
43         } else {
44                 $clr = $img->colorResolve(0, 0, 255);
45         }
46         
47         $ref->{'placement'} =~ /\((\d+),(\d+)\),\((\d+),(\d+)\)/;
48         my $npo = ($ref->{'switchtype'} eq 'es3024') ? 26 : 25;
49         my $f = ($ref->{'port'} - 1) % 2;
50         my $po = ($ref->{'port'} - 1 - $f)/2;
51         my $h = 2*($2-$4)/$npo;
52         my $w = ($1-$3)/2;
53         
54         $img->filledRectangle($3+$w*$f,$4+$po*$h,$3+$w+$w*$f,$4+$h*($po+1),$clr);
55 #       $img->rectangle($3+$w*$f,$4+$po*$h,$3+$w+$w*$f,$4+$h*($po+1),$blk);
56         $img->rectangle($3,$4,$1,$2,$blk);
57 }
58 $dbh->disconnect;
59
60 print $cgi->header(-type=>'image/png');
61 print $img->png;
62
63 sub get_color {
64         my $intensity = shift;
65         my $gamma = 1.0/1.90;
66         if ($intensity > 1.0) {
67                 return $img->colorResolve(255.0, 255.0 * ((2.0 - $intensity) ** $gamma), 0);
68         } else {
69                 return $img->colorResolve(255.0 * ($intensity ** $gamma), 255, 0);
70         }
71 }