5 use POSIX ":sys_wait_h";
11 my $switch = $cgi->param('id');
12 my $width = $cgi->param('width');
13 my $height = $cgi->param('height');
17 $width = 500 unless (defined($width));
18 $height = 250 unless (defined($height));
20 require './mygraph.pl';
22 my $start = [Time::HiRes::gettimeofday];
23 my $dbh = nms::db_connect();
26 my $ref = $dbh->selectrow_hashref('SELECT sysname FROM switches WHERE switch=?', undef, $switch);
28 print $cgi->header(-type=>'text/html; charset=utf-8');
35 <h1>Switch $switch ($ref->{'sysname'})</h1>
38 my $q = $dbh->prepare('select port,coalesce(description, \'Port \' || port) as description,extract(epoch from time) as time,bytes_in,bytes_out from polls natural join switches natural left join portnames where time between now() - \'1 day\'::interval and now() and switch=? order by switch,port,time;');
41 my (@totx, @toty1, @toty2) = ();
43 my (@x, @y1, @y2) = ();
47 my $max_x = time - 86400;
48 my ($min_y, $max_y, $prev_time, $prev_in, $prev_out);
49 my ($if,$of,$ifv,$ofv);
51 my ($min_ty,$max_ty) = (0, 10_000_000/8);
55 while (my $ref = $q->fetchrow_hashref()) {
56 my $time = $ref->{'time'};
57 my $in = $ref->{'bytes_in'};
58 my $out = $ref->{'bytes_out'};
59 next if ($time == $prev_time);
61 if ($ref->{'port'} != $last_port) {
62 if ($last_port != -1) {
63 my $filename = "$switch-$last_port-$width-$height.png";
71 my $graph = makegraph($width, $height, $min_x, $max_x, $min_y, $max_y, 5);
72 plotseries($graph, \@x, \@y1, 255, 0, 0, $min_x, $max_y);
73 plotseries($graph, \@x, \@y2, 0, 0, 255, $min_x, $max_y);
75 open GRAPH, ">img/$filename"
76 or die "img/$filename: $!";
77 print GRAPH $graph->png;
84 $resthtml .= "<div style=\"float: left;\"><h2>$portname</h2>\n";
85 $resthtml .= "<p><img src=\"img/$filename\" width=\"$width\" height=\"$height\" /></p></div>\n";
88 # Reset all the variables
92 ($min_y,$max_y) = (0, 10_000_000/8);
93 $prev_time = $ref->{'time'};
94 $prev_in = $ref->{'bytes_in'};
95 $prev_out = $ref->{'bytes_out'};
96 $last_port = $ref->{'port'};
97 $portname = $ref->{'description'};
98 ($if,$of,$ifv,$ofv) = (0,0,0,0);
99 ($prev_time,$prev_in,$prev_out) = ($time,$in,$out);
105 # Assume overflow (unless the switch has been down for >10 minutes)
106 my ($calc_in, $calc_out) = ($in, $out);
107 if ($in < $prev_in || $out < $prev_out) {
109 if ($prev_time - $time > 600 || ($in + 4294967296 - $prev_in) > 2147483648 || ($out + 4294967296 - $prev_out) > 2147483648) {
110 ($prev_time,$prev_in,$prev_out) = ($time,$in,$out);
114 $calc_in += 4294967296 if ($in < $prev_in);
115 $calc_out += 4294967296 if ($out < $prev_out);
119 if ($in == $prev_in && $out == $prev_out) {
120 ($prev_time,$prev_in,$prev_out) = ($time,$in,$out);
124 # Find the current flow
125 my $if = ($calc_in - $prev_in) / ($time - $prev_time);
126 my $of = ($calc_out - $prev_out) / ($time - $prev_time);
128 # Summarize (we don't care about the summed variance for now)
129 $min_x = $time if (!defined($min_x) || $time < $min_x);
130 $max_x = $time if (!defined($max_x) || $time > $max_x);
131 $min_y = $if if (!defined($min_y) || $if < $min_y);
132 $min_y = $of if ($of < $min_y);
133 $max_y = $if if (!defined($max_y) || $if > $max_y);
134 $max_y = $of if ($of > $max_y);
136 my $pt = 0.5 * ($time + $prev_time);
142 while ($idx < $#totx && $pt > $totx[$idx]) {
145 if ($idx >= $#totx) {
151 $min_ty = $if if (!defined($min_ty) || $if < $min_ty);
152 $min_ty = $of if ($of < $min_ty);
153 $max_ty = $if if (!defined($max_ty) || $if > $max_ty);
154 $max_ty = $of if ($of > $max_ty);
156 if (!defined($last_totx) || $last_totx != $idx) {
162 $min_ty = $toty1[$idx] if (!defined($min_ty) || $toty1[$idx] < $min_ty);
163 $min_ty = $toty2[$idx] if ($toty2[$idx] < $min_ty);
164 $max_ty = $toty1[$idx] if (!defined($max_ty) || $toty1[$idx] > $max_ty);
165 $max_ty = $toty2[$idx] if ($toty2[$idx] > $max_ty);
168 ($prev_time,$prev_in,$prev_out) = ($time,$in,$out);
173 my $filename = "$switch-$last_port-$width-$height.png";
177 my $graph = makegraph($width, $height, $min_x, $max_x, $min_y, $max_y, 5);
178 plotseries($graph, \@x, \@y1, 255, 0, 0, $min_x, $max_y);
179 plotseries($graph, \@x, \@y2, 0, 0, 255, $min_x, $max_y);
181 open GRAPH, ">img/$filename"
182 or die "img/$filename: $!";
183 print GRAPH $graph->png;
190 $resthtml .= "<div style=\"float: left;\"><h2>$portname</h2>\n";
191 $resthtml .= "<p><img src=\"img/$filename\" width=\"$width\" height=\"$height\" /></p></div>\n";
194 my $graph = makegraph($width, $height, $min_x, $max_x, $min_ty, $max_ty, 5);
195 plotseries($graph, \@totx, \@toty1, 255, 0, 0, $min_x, $max_ty);
196 plotseries($graph, \@totx, \@toty2, 0, 0, 255, $min_x, $max_ty);
198 my $filename = "$switch-$width-$height.png";
199 open GRAPH, ">img/$filename"
200 or die "img/$filename: $!";
201 print GRAPH $graph->png;
204 # Wait for all the other graphs to be done
205 while (waitpid(-1, 0) != -1) {
211 print "<div style=\"float: left;\"><h2>Total</h2>\n";
212 print "<p><img src=\"img/$filename\" width=\"$width\" height=\"$height\" /></p></div>\n";
214 my $elapsed = Time::HiRes::tv_interval($start);
215 printf "<p style=\"clear: both;\">Page and all graphs generated in %.2f seconds.</p>\n", $elapsed;
216 print "</body>\n</html>\n";