3.0: added support for 'port' graph

This commit is contained in:
Jordi Sanfeliu 2012-12-17 15:11:26 +01:00
parent 5158695565
commit db3ddb51c4
1 changed files with 288 additions and 1 deletions

View File

@ -20,7 +20,7 @@
package port;
#use strict;
use strict;
use warnings;
use Monitorix;
use RRDs;
@ -207,4 +207,291 @@ sub port_update {
logger("ERROR: while updating $rrd: $err") if $err;
}
sub port_cgi {
my ($package, $config, $cgi) = @_;
my $port = $config->{port};
my $tf = $cgi->{tf};
my $colors = $cgi->{colors};
my $graph = $cgi->{graph};
my $silent = $cgi->{silent};
my $u = "";
my $width;
my $height;
my @riglim;
my @warning;
my @PNG;
my @PNGz;
my $name;
my @tmp;
my @tmpz;
my @CDEF;
my $T = "B";
my $vlabel = "bytes/s";
my $n;
my $n2;
my $str;
my $err;
my $rrd = $config->{base_lib} . $package . ".rrd";
my $title = $config->{graph_title}->{$package};
my $PNG_DIR = $config->{base_dir} . "/" . $config->{imgs_dir};
$title = !$silent ? $title : "";
if(lc($config->{netstats_in_bps}) eq "y") {
$T = "b";
$vlabel = "bits/s";
}
# text mode
#
if(lc($config->{iface_mode}) eq "text") {
if($title) {
main::graph_header($title, 2);
print(" <tr>\n");
print(" <td bgcolor='$colors->{title_bg_color}'>\n");
}
my (undef, undef, undef, $data) = RRDs::fetch("$rrd",
"--start=-$tf->{nwhen}$tf->{twhen}",
"AVERAGE",
"-r $tf->{res}");
$err = RRDs::error;
print("ERROR: while fetching $rrd: $err\n") if $err;
my $line1;
my $line2;
print(" <pre style='font-size: 12px; color: $colors->{fg_color}';>\n");
print(" ");
for($n = 0; $n < $port->{max} && $n < scalar(my @pl = split(',', $port->{list})); $n++) {
$pl[$n] = trim($pl[$n]);
my $pn = trim((split(',', $port->{desc}->{$pl[$n]}))[0]);
printf(" %-5s %-8s", $pl[$n], $pn);
$line1 .= " K$T/s_I K$T/s_O";
$line2 .= "-----------------";
}
print("\n");
print("Time$line1\n");
print("----$line2 \n");
my $line;
my @row;
my $time;
my $n2;
my $from;
my $to;
for($n = 0, $time = $tf->{tb}; $n < ($tf->{tb} * $tf->{ts}); $n++) {
$line = @$data[$n];
$time = $time - (1 / $tf->{ts});
printf(" %2d$tf->{tc} ", $time);
for($n2 = 0; $n2 < $port->{max} && $n2 < scalar(my @pl = split(',', $port->{list})); $n2++) {
$from = $n2 * 2;
$to = $from + 1;
my ($kin, $kout) = @$line[$from..$to];
$kin /= 1024;
$kout /= 1024;
if(lc($config->{netstats_in_bps}) eq "y") {
$kin *= 8;
$kout *= 8;
}
@row = ($kin, $kout);
printf(" %6d %6d ", @row);
}
print("\n");
}
print(" </pre>\n");
if($title) {
print(" </td>\n");
print(" </tr>\n");
main::graph_footer();
}
print(" <br>\n");
return;
}
# graph mode
#
if($silent eq "yes" || $silent eq "imagetag") {
$colors->{fg_color} = "#000000"; # visible color for text mode
$u = "_";
}
if($silent eq "imagetagbig") {
$colors->{fg_color} = "#000000"; # visible color for text mode
$u = "";
}
for($n = 0; $n < $port->{max}; $n++) {
$str = $u . $package . $n . "." . $tf->{when} . ".png";
push(@PNG, $str);
unlink("$PNG_DIR" . $str);
if(lc($config->{enable_zoom}) eq "y") {
$str = $u . $package . $n . "z." . $tf->{when} . ".png";
push(@PNGz, $str);
unlink("$PNG_DIR" . $str);
}
}
$n = 0;
while($n < $port->{max} && $n < scalar(my @pl = split(',', $port->{list}))) {
if($title) {
if($n == 0) {
main::graph_header($title, $port->{graphs_per_row});
}
print(" <tr>\n");
}
for($n2 = 0; $n2 < $port->{graphs_per_row}; $n2++) {
last unless ($n < $port->{max} && $n < scalar(@pl));
if($title) {
print(" <td bgcolor='" . $colors->{title_bg_color} . "'>\n");
}
my $pnum;
$pl[$n] = trim($pl[$n]);
my $pn = trim((split(',', $port->{desc}->{$pl[$n]}))[0]);
my $pp = trim((split(',', $port->{desc}->{$pl[$n]}))[1]);
my $prig = trim((split(',', $port->{desc}->{$pl[$n]}))[2]);
my $plim = trim((split(',', $port->{desc}->{$pl[$n]}))[3]);
undef(@riglim);
if(trim($prig) eq 1) {
push(@riglim, "--upper-limit=" . trim($plim));
} else {
if(trim($prig) eq 2) {
push(@riglim, "--upper-limit=" . trim($plim));
push(@riglim, "--rigid");
}
}
undef(@warning);
if($config->{os} eq "Linux") {
open(IN, "netstat -nl --$pp |");
while(<IN>) {
(undef, undef, undef, $pnum) = split(' ', $_);
chomp($pnum);
$pnum =~ s/.*://;
if($pnum eq $pl[$n]) {
last;
}
}
close(IN);
}
if(grep {$_ eq $config->{os}} ("FreeBSD", "OpenBSD")) {
open(IN, "netstat -anl -p $pp |");
while(<IN>) {
my $stat;
(undef, undef, undef, $pnum, undef, $stat) = split(' ', $_);
chomp($stat);
if($stat eq "LISTEN") {
chomp($pnum);
($pnum) = ($pnum =~ m/^.*?(\.\d+$)/);
$pnum =~ s/\.//;
if($pnum eq $pl[$n]) {
last;
}
}
}
close(IN);
}
if($pnum ne $pl[$n]) {
push(@warning, $colors->{warning_color});
}
$name = substr($pn, 0, 15);
undef(@tmp);
undef(@tmpz);
undef(@CDEF);
push(@tmp, "AREA:B_in#44EE44:Input");
push(@tmp, "AREA:B_out#4444EE:Output");
push(@tmp, "AREA:B_out#4444EE:");
push(@tmp, "AREA:B_in#44EE44:");
push(@tmp, "LINE1:B_out#0000EE");
push(@tmp, "LINE1:B_in#00EE00");
push(@tmpz, "AREA:B_in#44EE44:Input");
push(@tmpz, "AREA:B_out#4444EE:Output");
push(@tmpz, "AREA:B_out#4444EE:");
push(@tmpz, "AREA:B_in#44EE44:");
push(@tmpz, "LINE1:B_out#0000EE");
push(@tmpz, "LINE1:B_in#00EE00");
if(lc($config->{netstats_in_bps}) eq "y") {
push(@CDEF, "CDEF:B_in=in,8,*");
push(@CDEF, "CDEF:B_out=out,8,*");
} else {
push(@CDEF, "CDEF:B_in=in");
push(@CDEF, "CDEF:B_out=out");
}
($width, $height) = split('x', $config->{graph_size}->{mini});
if($silent =~ /imagetag/) {
($width, $height) = split('x', $config->{graph_size}->{remote}) if $silent eq "imagetag";
($width, $height) = split('x', $config->{graph_size}->{main}) if $silent eq "imagetagbig";
push(@tmp, "COMMENT: \\n");
push(@tmp, "COMMENT: \\n");
push(@tmp, "COMMENT: \\n");
}
RRDs::graph("$PNG_DIR" . "$PNG[$n]",
"--title=$name traffic ($tf->{nwhen}$tf->{twhen})",
"--start=-$tf->{nwhen}$tf->{twhen}",
"--imgformat=PNG",
"--vertical-label=$vlabel",
"--width=$width",
"--height=$height",
@riglim,
"--lower-limit=0",
@{$cgi->{version12}},
@{$cgi->{version12_small}},
@{$colors->{graph_colors}},
@warning,
"DEF:in=$rrd:port" . $n . "_in:AVERAGE",
"DEF:out=$rrd:port" . $n . "_out:AVERAGE",
@CDEF,
@tmp);
$err = RRDs::error;
print("ERROR: while graphing $PNG_DIR" . "$PNG[$n]: $err\n") if $err;
if(lc($config->{enable_zoom}) eq "y") {
($width, $height) = split('x', $config->{graph_size}->{zoom});
RRDs::graph("$PNG_DIR" . "$PNGz[$n]",
"--title=$name traffic ($tf->{nwhen}$tf->{twhen})",
"--start=-$tf->{nwhen}$tf->{twhen}",
"--imgformat=PNG",
"--vertical-label=$vlabel",
"--width=$width",
"--height=$height",
@riglim,
"--lower-limit=0",
@{$cgi->{version12}},
@{$cgi->{version12_small}},
@{$colors->{graph_colors}},
@warning,
"DEF:in=$rrd:port" . $n . "_in:AVERAGE",
"DEF:out=$rrd:port" . $n . "_out:AVERAGE",
@CDEF,
@tmpz);
$err = RRDs::error;
print("ERROR: while graphing $PNG_DIR" . "$PNGz[$n]: $err\n") if $err;
}
if($title || ($silent =~ /imagetag/ && $graph =~ /port$n/)) {
if(lc($config->{enable_zoom}) eq "y") {
if(lc($config->{disable_javascript_void}) eq "y") {
print(" <a href=\"" . $config->{url} . $config->{imgs_dir} . $PNGz[$n] . "\"><img src='" . $config->{url} . $config->{imgs_dir} . $PNG[$n] . "' border='0'></a>\n");
}
else {
print(" <a href=\"javascript:void(window.open('" . $config->{url} . $config->{imgs_dir} . $PNGz[$n] . "','','width=" . ($width + 115) . ",height=" . ($height + 100) . ",scrollbars=0,resizable=0'))\"><img src='" . $config->{url} . $config->{imgs_dir} . $PNG[$n] . "' border='0'></a>\n");
}
} else {
print(" <img src='" . $config->{url} . $config->{imgs_dir} . $PNG[$n] . "'>\n");
}
}
if($title) {
print(" </td>\n");
}
$n++;
}
if($title) {
print(" </tr>\n");
}
}
if($title) {
main::graph_footer();
}
print(" <br>\n");
return;
}
1;