7 my $dbh = DBI->connect("dbi:Pg:dbname=snmpfetch;host=violet.tg05.gathering.org", "snmpfetch", "removed")
8 or die "Couldn't connect to database";
10 GD::Image->trueColor(1);
11 $img = GD::Image->new('snmp-bg.png');
13 my $blk = $img->colorResolve(0, 0, 0);
16 my $i = 2.0 * ($y - 236.0) / (42.0 - 237.0);
17 my $clr = get_color($i);
19 $img->filledRectangle(32,$y,53,$y+1,$clr);
22 my $q = $dbh->prepare('select switch,port,bytes_in,bytes_out,placement,switchtype from switches natural join placements natural join get_current_datarate() where switchtype in (\'es3024\')');
24 while (my $ref = $q->fetchrow_hashref()) {
27 # 100kbit/port = all green
28 # 1gbit/port = all red
32 if (defined($ref->{'bytes_in'})) {
34 my $traffic = 4.0 * $ref->{'bytes_in'} + $ref->{'bytes_out'}; # average and convert to bits (should be about the same in practice)
36 my $max = 20_000_000.0; # 100mbit
37 my $min = 100_000.0; # 100kbit
38 if ($traffic >= $min) {
39 $intensity = 2.0 * (log($traffic / $min) / log(10)) / (log($max / $min) / log(10));
40 $intensity = 2.0 if ($intensity > 2.0);
42 $clr = get_color($intensity);
44 $clr = $img->colorResolve(0, 0, 255);
47 $ref->{'placement'} =~ /\((\d+),(\d+)\),\((\d+),(\d+)\)/;
48 my $npo = ($ref->{'switchtype'} eq 'es3024') ? 26 : 25;
49 my $f = ($ref->{'port'} - 1) % 2;
50 my $po = ($ref->{'port'} - 1 - $f)/2;
51 my $h = 2*($2-$4)/$npo;
54 $img->filledRectangle($3+$w*$f,$4+$po*$h,$3+$w+$w*$f,$4+$h*($po+1),$clr);
55 # $img->rectangle($3+$w*$f,$4+$po*$h,$3+$w+$w*$f,$4+$h*($po+1),$blk);
56 $img->rectangle($3,$4,$1,$2,$blk);
60 print $cgi->header(-type=>'image/png');
64 my $intensity = shift;
66 if ($intensity > 1.0) {
67 return $img->colorResolve(255.0, 255.0 * ((2.0 - $intensity) ** $gamma), 0);
69 return $img->colorResolve(255.0 * ($intensity ** $gamma), 255, 0);