1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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);
}
}
|