]> git.sesse.net Git - nms/blob - web/nettkart.pl
Optimize the get_datarate() function, using some SQL and some C instead.
[nms] / web / nettkart.pl
1 #! /usr/bin/perl
2 use CGI;
3 use GD;
4 use Image::Magick;
5 use DBI;
6 use lib '../include';
7 use nms;
8 my $cgi = CGI->new;
9
10 # Sekrit night-mode
11 my $night = defined($cgi->param('night'));
12
13 my $dbh = nms::db_connect();
14
15 GD::Image->trueColor(1);
16 my ($img, $text_img);
17
18 $img = GD::Image->new('bg07.png');
19 if ($night) {
20         my ($width, $height) = ($img->width, $img->height); 
21
22         $img = GD::Image->new($width, $height);
23         $img->alphaBlending(0);
24         $img->saveAlpha(1);
25         my $blank = $img->colorAllocateAlpha(0, 0, 0, 127);
26         $img->filledRectangle(0, 0, $img->width - 1, $img->height - 1, $blank);
27
28         $text_img = GD::Image->new($width, $height);
29         $text_img->alphaBlending(0);
30         $text_img->saveAlpha(1);
31         my $blank = $text_img->colorAllocateAlpha(0, 0, 0, 127);
32         $text_img->filledRectangle(0, 0, $text_img->width - 1, $text_img->height - 1, $blank);
33 } else {
34         $img = GD::Image->new('bg07.png');
35         $text_img = $img;
36 }
37
38 my $blk = $img->colorResolve(0, 0, 0);
39
40 for my $y (42..236) {
41         my $i = 4.0 * ($y - 236.0) / (42.0 - 237.0);
42         my $clr = get_color($i);
43         
44         $img->filledRectangle(12, $y, 33, $y+1, $clr);
45         $text_img->filledRectangle(12, $y, 33, $y+1, $clr);
46 }
47
48 $text_img->rectangle(12,42,33,236,$blk);
49
50 my $tclr = $night ? $text_img->colorResolve(255, 255, 255) : $blk;
51 $text_img->stringFT($tclr, "/usr/share/fonts/truetype/msttcorefonts/Arial.ttf", 10, 0, 40, 47 + (236-42)*0.0/4.0, "100 Gbit/sec");
52 $text_img->stringFT($tclr, "/usr/share/fonts/truetype/msttcorefonts/Arial.ttf", 10, 0, 40, 47 + (236-42)*1.0/4.0, "10 Gbit/sec");
53 $text_img->stringFT($tclr, "/usr/share/fonts/truetype/msttcorefonts/Arial.ttf", 10, 0, 40, 47 + (236-42)*2.0/4.0, "1 Gbit/sec");
54 $text_img->stringFT($tclr, "/usr/share/fonts/truetype/msttcorefonts/Arial.ttf", 10, 0, 40, 47 + (236-42)*3.0/4.0, "100 Mbit/sec");
55 $text_img->stringFT($tclr, "/usr/share/fonts/truetype/msttcorefonts/Arial.ttf", 10, 0, 40, 47 + (236-42)*4.0/4.0, "10 Mbit/sec");
56 $text_img->stringFT($tclr, "/usr/share/fonts/truetype/msttcorefonts/Arial.ttf", 10, 0, 1000, 620, "NMS (C) 2005-2007 Tech:Server");
57
58 my $q = $dbh->prepare('select * from switches natural join placements natural left join current_datarate order by zorder');
59 $q->execute();
60 while (my $ref = $q->fetchrow_hashref()) {
61
62         # for now:
63         # 10Mbit/switch = green
64         # 100Mbit/switch = yellow
65         # 1Gbit/switch = red
66         # 10Gbit/switch = white
67         
68         my $clr;
69
70         if (defined($ref->{'bytes_in'})) {
71                 my $intensity = 0.0;
72                 my $traffic = 4.0 * $ref->{'bytes_in'} + $ref->{'bytes_out'};  # average and convert to bits (should be about the same in practice)
73
74                 my $max = 10_000_000_000.0;   # 10Gbit
75                 my $min =     10_000_000.0;   # 10Mbit
76                 if ($traffic >= $min) {
77                         $intensity = log($traffic / $min) / log(10);
78                         $intensity = 3.0 if ($intensity > 3.0);
79                 }
80                 $clr = get_color($intensity);
81         } else {
82                 $clr = $img->colorResolve(0, 0, 255);
83         }
84         
85         $ref->{'placement'} =~ /\((\d+),(\d+)\),\((\d+),(\d+)\)/;
86         $img->filledRectangle($3,$4,$1,$2,$clr);
87         $text_img->filledRectangle($3,$4,$1,$2,$clr);
88
89         $img->rectangle($3,$4,$1,$2,$blk);
90         $text_img->rectangle($3,$4,$1,$2,$blk);
91         $text_img->stringUp(gdSmallFont,$3,$2-3,$ref->{'sysname'},$blk);
92 }
93 $dbh->disconnect;
94
95 print $cgi->header(-type=>'image/png');
96 if ($night) {
97         my $magick = Image::Magick->new;
98         $magick->BlobToImage($img->png);
99         $magick->Blur(sigma=>10.0, channels=>'All');
100         $magick->Gamma(gamma=>1.90);
101
102         my $m2 = Image::Magick->new;
103         $m2->Read('snmp-bg.png');
104         $m2->Negate();
105         $m2->Composite(image=>$magick, compose=>'Atop');
106
107         my $m3 = Image::Magick->new;
108         $m3->BlobToImage($text_img->png);
109         $m2->Composite(image=>$m3, compose=>'Atop');
110         
111         $img = $m2->ImageToBlob();
112         print $img;
113 } else {        
114         print $img->png;
115 }
116
117 sub get_color {
118         my $intensity = shift;
119         my $gamma = 1.0/1.90;
120         if ($intensity > 3.0) {
121                 return $img->colorResolve(255.0 * ((4.0 - $intensity) ** $gamma), 255.0 * ((4.0 - $intensity) ** $gamma), 255.0 * ((4.0 - $intensity) ** $gamma));
122         } elsif ($intensity > 2.0) {
123                 return $img->colorResolve(255.0, 255.0 * (($intensity - 2.0) ** $gamma), 255.0 * (($intensity - 2.0) ** $gamma));
124         } elsif ($intensity > 1.0) {
125                 return $img->colorResolve(255.0, 255.0 * ((2.0 - $intensity) ** $gamma), 0);
126         } else {
127                 return $img->colorResolve(255.0 * ($intensity ** $gamma), 255, 0);
128         }
129 }