]> git.sesse.net Git - nms/blob - web/portkart.pl
259376e39522f73a024449260de09e853620ff1e
[nms] / web / portkart.pl
1 #! /usr/bin/perl
2 use CGI;
3 use GD;
4 use DBI;
5 use lib '../include';
6 use nms;
7 my $cgi = CGI->new;
8
9 my $dbh = nms::db_connect();
10
11 GD::Image->trueColor(1);
12 $img = GD::Image->new('bg2.png');
13
14 my $blk = $img->colorResolve(0, 0, 0);
15
16 for my $y (42..236) {
17         my $i = 2.0 * ($y - 236.0) / (42.0 - 237.0);
18         my $clr = get_color($i);
19         
20         $img->filledRectangle(32,$y,53,$y+1,$clr);
21 }
22
23 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\')');
24 $q->execute();
25 while (my $ref = $q->fetchrow_hashref()) {
26
27         # for now:
28         # 100kbit/port = all green
29         # 1gbit/port = all red
30         
31         my $clr;
32
33         if (defined($ref->{'bytes_in'})) {
34                 my $intensity = 0.0;
35                 my $traffic = 4.0 * $ref->{'bytes_in'} + $ref->{'bytes_out'};  # average and convert to bits (should be about the same in practice)
36
37                 my $max = 20_000_000.0;   # 100mbit
38                 my $min =    100_000.0;   # 100kbit
39                 if ($traffic >= $min) {
40                         $intensity = 2.0 * (log($traffic / $min) / log(10)) / (log($max / $min) / log(10));
41                         $intensity = 2.0 if ($intensity > 2.0);
42                 }
43                 $clr = get_color($intensity);
44         } else {
45                 $clr = $img->colorResolve(0, 0, 255);
46         }
47         
48         $ref->{'placement'} =~ /\((\d+),(\d+)\),\((\d+),(\d+)\)/;
49         my $npo = ($ref->{'switchtype'} eq 'es3024') ? 26 : 25;
50         my $f = ($ref->{'port'} - 1) % 2;
51         my $po = ($ref->{'port'} - 1 - $f)/2;
52         my $h = 2*($2-$4)/$npo;
53         my $w = ($1-$3)/2;
54         
55         $img->filledRectangle($3+$w*$f,$4+$po*$h,$3+$w+$w*$f,$4+$h*($po+1),$clr);
56 #       $img->rectangle($3+$w*$f,$4+$po*$h,$3+$w+$w*$f,$4+$h*($po+1),$blk);
57         $img->rectangle($3,$4,$1,$2,$blk);
58 }
59 $dbh->disconnect;
60
61 print $cgi->header(-type=>'image/png');
62 print $img->png;
63
64 sub get_color {
65         my $intensity = shift;
66         my $gamma = 1.0/1.90;
67         if ($intensity > 1.0) {
68                 return $img->colorResolve(255.0, 255.0 * ((2.0 - $intensity) ** $gamma), 0);
69         } else {
70                 return $img->colorResolve(255.0 * ($intensity ** $gamma), 255, 0);
71         }
72 }