]> git.sesse.net Git - nms/blob - web/portkart.pl
Merge.
[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('bg07.png');
13
14 my $blk = $img->colorResolve(0, 0, 0);
15
16 for my $y (42..236) {
17         my $i = 3.0 * ($y - 236.0) / (42.0 - 237.0);
18         my $clr = get_color($i);
19         
20         $img->filledRectangle(12,$y,33,$y+1,$clr);
21 }
22
23 $img->stringFT($blk, "/usr/share/fonts/truetype/msttcorefonts/Arial.ttf", 10, 0, 40, 47 + (236-42)*0.0/3.0, "1 Gbit/sec");
24 $img->stringFT($blk, "/usr/share/fonts/truetype/msttcorefonts/Arial.ttf", 10, 0, 40, 47 + (236-42)*1.0/3.0, "100 Mbit/sec");
25 $img->stringFT($blk, "/usr/share/fonts/truetype/msttcorefonts/Arial.ttf", 10, 0, 40, 47 + (236-42)*2.0/3.0, "10 Mbit/sec");
26 $img->stringFT($blk, "/usr/share/fonts/truetype/msttcorefonts/Arial.ttf", 10, 0, 40, 47 + (236-42)*3.0/3.0, "1 Mbit/sec");
27 $img->stringFT($blk, "/usr/share/fonts/truetype/msttcorefonts/Arial.ttf", 10, 0, 1000, 620, "NMS (C) 2005-2007 Tech:Server");
28
29 my $q = $dbh->prepare('select switch,port,bytes_in,bytes_out,placement,switchtype from switches natural join placements natural join get_datar
30 ate() where switchtype in (\'es3024\')');
31 $q->execute();
32 while (my $ref = $q->fetchrow_hashref()) {
33
34         # for now:
35         # 100kbit/port = all green
36         # 1gbit/port = all red
37         
38         my $clr;
39
40         if (defined($ref->{'bytes_in'})) {
41                 my $intensity = 0.0;
42                 my $traffic = 4.0 * $ref->{'bytes_in'} + $ref->{'bytes_out'};  # average and convert to bits (should be about the same in practice)
43
44                 my $max = 1_000_000_000.0;   # 1Gbit
45                 my $min =     1_000_000.0;   # 1Mbit
46                 if ($traffic >= $min) {
47                         $intensity = log($traffic / $min) / log(10);
48                         $intensity = 3.0 if ($intensity > 3.0);
49                 }
50                 $clr = get_color($intensity);
51         } else {
52                 $clr = $img->colorResolve(0, 0, 255);
53         }
54         
55         $ref->{'placement'} =~ /\((\d+),(\d+)\),\((\d+),(\d+)\)/;
56         my $npo = ($ref->{'switchtype'} eq 'es3024') ? 26 : 25;
57         my $f = ($ref->{'port'} - 1) % 2;
58         my $po = ($ref->{'port'} - 1 - $f)/2;
59         my $h = 2*($2-$4)/$npo;
60         my $w = ($1-$3)/2;
61         
62         $img->filledRectangle($3+$w*$f,$4+$po*$h,$3+$w+$w*$f,$4+$h*($po+1),$clr);
63 #       $img->rectangle($3+$w*$f,$4+$po*$h,$3+$w+$w*$f,$4+$h*($po+1),$blk);
64         $img->rectangle($3,$4,$1,$2,$blk);
65 }
66 $dbh->disconnect;
67
68 print $cgi->header(-type=>'image/png');
69 print $img->png;
70
71 sub get_color {
72         my $intensity = shift;
73         my $gamma = 1.0/1.90;
74         if ($intensity > 2.0) {
75                 return $img->colorResolve(255.0, 255.0 * (($intensity - 2.0) ** $gamma), 255.0 * (($intensity - 2.0) ** $gamma));
76         } elsif ($intensity > 1.0) {
77                 return $img->colorResolve(255.0, 255.0 * ((2.0 - $intensity) ** $gamma), 0);
78         } else {
79                 return $img->colorResolve(255.0 * ($intensity ** $gamma), 255, 0);
80         }
81 }