aboutsummaryrefslogtreecommitdiffstats
path: root/web/nms-public.gathering.org/old/portkart.pl
diff options
context:
space:
mode:
authorMagnus Kirø <magnuskiro@gmail.com>2016-03-22 11:14:46 +0100
committerMagnus Kirø <magnuskiro@gmail.com>2016-03-22 11:14:46 +0100
commit7806081670c8c9807fc8b6628b3d7a6eff841ded (patch)
tree082e522b99cd5b864a8963523f2b3889f6cff808 /web/nms-public.gathering.org/old/portkart.pl
parente501dd7dd88bf52edd914a8f078141b91d53939b (diff)
parent2d19233ba32fd23ad7182d3cccb58cec9e377a75 (diff)
MERGE fix.
Diffstat (limited to 'web/nms-public.gathering.org/old/portkart.pl')
-rwxr-xr-xweb/nms-public.gathering.org/old/portkart.pl93
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);
+ }
+}