diff options
author | root <root@frank.tg14.gathering.org> | 2014-04-18 04:39:16 +0200 |
---|---|---|
committer | root <root@frank.tg14.gathering.org> | 2014-04-18 04:39:16 +0200 |
commit | 78af7a10e7d5edfa638e5f165130ae170ca32f5a (patch) | |
tree | 05f5e26e43049c43ee683cd1cf619e42cb3a1f41 | |
parent | 22d3a315a78035b1e04db42d30ec0a01aaa5ca9d (diff) |
Add missing file nettkart-json.pl.
-rwxr-xr-x | web/nms.gathering.org/nettkart-json.pl | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/web/nms.gathering.org/nettkart-json.pl b/web/nms.gathering.org/nettkart-json.pl new file mode 100755 index 0000000..54ef1d2 --- /dev/null +++ b/web/nms.gathering.org/nettkart-json.pl @@ -0,0 +1,72 @@ +#! /usr/bin/perl +use CGI; +use DBI; +use POSIX; +use lib '../../include'; +use strict; +use warnings; +use nms; +use JSON::XS; +use Digest::MD5; + +my $cgi = CGI->new; +my $secret = $cgi->param('secret'); +my $noise = $cgi->param('noise') // 0; +my $dbh = nms::db_connect(); + +my %json = (); +my $q = $dbh->prepare("select * from ( SELECT switch,sysname,sum(bytes_in) AS bytes_in,sum(bytes_out) AS bytes_out from switches natural left join get_current_datarate() where ip <> inet '127.0.0.1' group by switch,sysname) t1 natural join placements order by zorder;"); +$q->execute(); +while (my $ref = $q->fetchrow_hashref()) { + + # for now: + # 10Mbit/switch = green + # 100Mbit/switch = yellow + # 1Gbit/switch = red + # 10Gbit/switch = white + + my $clr; + + if (defined($ref->{'bytes_in'})) { + my $intensity = 0.0; + my $traffic = 4.0 * ($ref->{'bytes_in'} + $ref->{'bytes_out'}); # average and convert to bits (should be about the same in practice) + + my $max = 100_000_000_000.0; # 100Gbit + my $min = 10_000_000.0; # 10Mbit + if ($traffic >= $min) { + $intensity = log($traffic / $min) / log(10); + + my $fudge = oct('0x'.substr(Digest::MD5::md5_hex($cgi->{'sysname'} . $cgi->param('secret')), 0, 8)); + $intensity += $fudge * $noise; + + $intensity = 4.0 if ($intensity > 4.0); + } + $clr = get_color($intensity); + } else { + $clr = [ 0, 0, 255 ]; + } + + $clr = sprintf("#%02x%02x%02x", + POSIX::floor($clr->[0] + 0.5), + POSIX::floor($clr->[1] + 0.5), + POSIX::floor($clr->[2] + 0.5)); + + $json{'switches'}{$ref->{'switch'}}{'color'} = $clr; +} +$dbh->disconnect; +print CGI::header(-type=>'text/json; charset=utf-8'); +print JSON::XS::encode_json(\%json); + +sub get_color { + my $intensity = shift; + my $gamma = 1.0/1.90; + if ($intensity > 3.0) { + return [ 255.0 * ((4.0 - $intensity) ** $gamma), 255.0 * ((4.0 - $intensity) ** $gamma), 255.0 * ((4.0 - $intensity) ** $gamma) ]; + } elsif ($intensity > 2.0) { + return [ 255.0, 255.0 * (($intensity - 2.0) ** $gamma), 255.0 * (($intensity - 2.0) ** $gamma) ]; + } elsif ($intensity > 1.0) { + return [ 255.0, 255.0 * ((2.0 - $intensity) ** $gamma), 0 ]; + } else { + return [ 255.0 * ($intensity ** $gamma), 255, 0 ]; + } +} |