diff options
author | Kristian Lyngstol <kly@kly@.no> | 2016-03-21 20:11:55 +0100 |
---|---|---|
committer | Kristian Lyngstol <kly@kly@.no> | 2016-03-21 20:11:55 +0100 |
commit | 727e4ab31aa6d1a754711d4cd29dbcefae2e952a (patch) | |
tree | d1b4f17addedf5fe9695d6018efd880d13f0c5c1 /web/nms-public.gathering.org/old/portkart.pl | |
parent | 7a6cf59e67d569a412f7670d9f04b2378c546dbc (diff) |
NMS: NMS Public
Diffstat (limited to 'web/nms-public.gathering.org/old/portkart.pl')
-rwxr-xr-x | web/nms-public.gathering.org/old/portkart.pl | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/web/nms-public.gathering.org/old/portkart.pl b/web/nms-public.gathering.org/old/portkart.pl new file mode 100755 index 0000000..ce7dcdd --- /dev/null +++ b/web/nms-public.gathering.org/old/portkart.pl @@ -0,0 +1,93 @@ +#! /usr/bin/perl +use CGI; +use GD; +use DBI; +use lib '../../include'; +use nms; +use File::Basename; +my $cgi = CGI->new; +my $cwd = dirname($0); + +my $dbh = nms::db_connect(); + +GD::Image->trueColor(1); +$img = GD::Image->new($cwd.'/tg15-salkart.png'); + +my $blk = $img->colorResolve(0, 0, 0); + +for my $y (42..236) { + my $i = 3.0 * ($y - 236.0) / (42.0 - 237.0); + my $clr = get_color($i); + + $img->filledRectangle(12,$y,33,$y+1,$clr); +} + +$img->stringFT($blk, "/usr/share/fonts/truetype/msttcorefonts/Arial.ttf", 10, 0, 40, 47 + (236-42)*0.0/3.0, "1 Gbit/sec"); +$img->stringFT($blk, "/usr/share/fonts/truetype/msttcorefonts/Arial.ttf", 10, 0, 40, 47 + (236-42)*1.0/3.0, "100 Mbit/sec"); +$img->stringFT($blk, "/usr/share/fonts/truetype/msttcorefonts/Arial.ttf", 10, 0, 40, 47 + (236-42)*2.0/3.0, "10 Mbit/sec"); +$img->stringFT($blk, "/usr/share/fonts/truetype/msttcorefonts/Arial.ttf", 10, 0, 40, 47 + (236-42)*3.0/3.0, "1 Mbit/sec"); +$img->stringFT($blk, "/usr/share/fonts/truetype/msttcorefonts/Arial.ttf", 10, 0, 1600, 1000, "NMS (C) 2005-2007 Tech:Server"); + +sub portnum($) { + my ($port) = @_; + if ($port =~ /(\d+)$/) { + return $1; + } + warn "Unrecognized port name: $port"; + return undef; +} + +my $q = $dbh->prepare('select switch,ifname,ifhcinoctets,ifhcoutoctets,placement,switchtype from switches natural join placements natural join get_datarate() where switchtype like \'%2200%\' and sysname like \'e%-%\''); +$q->execute(); +while (my $ref = $q->fetchrow_hashref()) { + + # for now: + # 100kbit/port = all green + # 1gbit/port = all red + + my $clr; + + if (defined($ref->{'ifhcinoctets'})) { + my $intensity = 0.0; + my $traffic = 4.0 * ($ref->{'ifhcinoctets'} + $ref->{'ifhcoutoctets'}); # average and convert to bits (should be about the same in practice) + + my $max = 100_000_000_000.0; # 1Gbit + my $min = 1_000_000.0; # 1Mbit + if ($traffic >= $min) { + $intensity = log($traffic / $min) / log(10); + $intensity = 4.0 if ($intensity > 4.0); + } + $clr = get_color($intensity); + } else { + $clr = $img->colorResolve(0, 0, 255); + } + + $ref->{'placement'} =~ /\((\d+),(\d+)\),\((\d+),(\d+)\)/; + my $npo = 48; + my $f = portnum($ref->{'ifname'}) % 2; + my $po = (portnum($ref->{'ifname'}) - $f)/2; + my $h = 2*($2-$4)/$npo; + my $w = ($1-$3)/2; + + $img->filledRectangle($3+$w*$f,$4+$po*$h,$3+$w+$w*$f,$4+$h*($po+1),$clr); +# $img->rectangle($3+$w*$f,$4+$po*$h,$3+$w+$w*$f,$4+$h*($po+1),$blk); + $img->rectangle($3,$4,$1,$2,$blk); +} +$dbh->disconnect; + +print $cgi->header(-type=>'image/png'); +print $img->png; + +sub get_color { + my $intensity = shift; + my $gamma = 1.0/1.90; + if ($intensity > 3.0) { + return $img->colorResolve(255.0 * ((4.0 - $intensity) ** $gamma), 255.0 * ((4.0 - $intensity) ** $gamma), 255.0 * ((4.0 - $intensity) ** $gamma)); + } elsif ($intensity > 2.0) { + return $img->colorResolve(255.0, 255.0 * (($intensity - 2.0) ** $gamma), 255.0 * (($intensity - 2.0) ** $gamma)); + } elsif ($intensity > 1.0) { + return $img->colorResolve(255.0, 255.0 * ((2.0 - $intensity) ** $gamma), 0); + } else { + return $img->colorResolve(255.0 * ($intensity ** $gamma), 255, 0); + } +} |