5 use POSIX ":sys_wait_h";
9 use lib '../../include';
16 my $switch = $cgi->param('id');
17 my $width = $cgi->param('width');
18 my $height = $cgi->param('height');
22 $width = 500 unless (defined($width));
23 $height = 250 unless (defined($height));
25 my $graph = mygraph::mygraph_new($width, $height);
27 my $start = [Time::HiRes::gettimeofday];
28 my $dbh = nms::db_connect();
31 my $ref = $dbh->selectrow_hashref('SELECT sysname FROM switches WHERE switch=?', undef, $switch);
33 print $cgi->header(-type=>'text/html; charset=utf-8');
40 <h1>Switch $switch ($ref->{'sysname'})</h1>
43 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;');
46 my (@totx, @toty1, @toty2) = ();
48 my (@x, @y1, @y2) = ();
52 my $max_x = time - 86400;
53 my ($min_y, $max_y, $prev_time, $prev_in, $prev_out);
54 my ($if,$of,$ifv,$ofv);
56 my ($min_ty,$max_ty) = (0, 10_000_000/8);
60 while (my $ref = $q->fetchrow_hashref()) {
61 my $time = $ref->{'time'};
62 my $in = $ref->{'bytes_in'};
63 my $out = $ref->{'bytes_out'};
64 next if ($time == $prev_time);
66 if ($ref->{'port'} != $last_port) {
67 if ($last_port != -1) {
68 my $filename = "$switch-$last_port-$width-$height.png";
78 # print "$width, $height, $min_x, $max_x, $min_y, $max_y, 5<br/>\n";
80 my $x_c = create_array(@x);
81 my $y1_c = create_array(@y1);
82 my $y2_c = create_array(@y2);
84 # my $startthis = [Time::HiRes::gettimeofday];
85 mygraph::mygraph_make_graph($graph, $min_x, $max_x, $min_y, $max_y, 5);
86 mygraph::mygraph_plot_series($graph, $x_c, $y1_c, $#x, 255, 0, 0);
87 mygraph::mygraph_plot_series($graph, $x_c, $y2_c, $#x, 0, 0, 255);
88 mygraph::mygraph_to_file($graph, "img/$filename");
89 # mygraph::mygraph_cleanup ($graph);
90 # my $elapsedthis = Time::HiRes::tv_interval($startthis);
91 # printf "$elapsedthis seconds<br/>\n";
98 $resthtml .= "<div style=\"float: left;\"><h2>$portname</h2>\n";
99 $resthtml .= "<p><img src=\"img/$filename\" width=\"$width\" height=\"$height\" /></p></div>\n";
102 # Reset all the variables
106 ($min_y,$max_y) = (0, 10_000_000/8);
107 $prev_time = $ref->{'time'};
108 $prev_in = $ref->{'bytes_in'};
109 $prev_out = $ref->{'bytes_out'};
110 $last_port = $ref->{'port'};
111 $portname = $ref->{'description'};
112 ($if,$of,$ifv,$ofv) = (0,0,0,0);
113 ($prev_time,$prev_in,$prev_out) = ($time,$in,$out);
119 # Assume overflow (unless the switch has been down for >10 minutes)
120 my ($calc_in, $calc_out) = ($in, $out);
121 if ($in < $prev_in || $out < $prev_out) {
123 if ($prev_time - $time > 600 || ($in + 4294967296 - $prev_in) > 2147483648 || ($out + 4294967296 - $prev_out) > 2147483648) {
124 ($prev_time,$prev_in,$prev_out) = ($time,$in,$out);
128 $calc_in += 4294967296 if ($in < $prev_in);
129 $calc_out += 4294967296 if ($out < $prev_out);
133 if ($in == $prev_in && $out == $prev_out) {
134 ($prev_time,$prev_in,$prev_out) = ($time,$in,$out);
138 # Find the current flow
139 my $if = ($calc_in - $prev_in) / ($time - $prev_time);
140 my $of = ($calc_out - $prev_out) / ($time - $prev_time);
142 # Summarize (we don't care about the summed variance for now)
143 $min_x = $time if (!defined($min_x) || $time < $min_x);
144 $max_x = $time if (!defined($max_x) || $time > $max_x);
145 $min_y = $if if (!defined($min_y) || $if < $min_y);
146 $min_y = $of if ($of < $min_y);
147 $max_y = $if if (!defined($max_y) || $if > $max_y);
148 $max_y = $of if ($of > $max_y);
150 my $pt = 0.5 * ($time + $prev_time);
156 while ($idx < $#totx && $pt > $totx[$idx]) {
159 if ($idx >= $#totx) {
165 $min_ty = $if if (!defined($min_ty) || $if < $min_ty);
166 $min_ty = $of if ($of < $min_ty);
167 $max_ty = $if if (!defined($max_ty) || $if > $max_ty);
168 $max_ty = $of if ($of > $max_ty);
170 if (!defined($last_totx) || $last_totx != $idx) {
176 $min_ty = $toty1[$idx] if (!defined($min_ty) || $toty1[$idx] < $min_ty);
177 $min_ty = $toty2[$idx] if ($toty2[$idx] < $min_ty);
178 $max_ty = $toty1[$idx] if (!defined($max_ty) || $toty1[$idx] > $max_ty);
179 $max_ty = $toty2[$idx] if ($toty2[$idx] > $max_ty);
182 ($prev_time,$prev_in,$prev_out) = ($time,$in,$out);
187 my $filename = "$switch-$last_port-$width-$height.png";
191 my $x_c = create_array(@x);
192 my $y1_c = create_array(@y1);
193 my $y2_c = create_array(@y2);
195 # my $startthis = [Time::HiRes::gettimeofday];
196 mygraph::mygraph_make_graph($graph, $min_x, $max_x, $min_y, $max_y, 5);
197 mygraph::mygraph_plot_series($graph, $x_c, $y1_c, $#x, 255, 0, 0);
198 mygraph::mygraph_plot_series($graph, $x_c, $y2_c, $#x, 0, 0, 255);
199 mygraph::mygraph_to_file($graph, "img/$filename");
200 # mygraph::mygraph_cleanup ($graph);
201 # my $elapsedthis = Time::HiRes::tv_interval($startthis);
202 # printf "$elapsedthis seconds<br/>\n";
209 $resthtml .= "<div style=\"float: left;\"><h2>$portname</h2>\n";
210 $resthtml .= "<p><img src=\"img/$filename\" width=\"$width\" height=\"$height\" /></p></div>\n";
213 $filename = "$switch-$width-$height.png";
214 my $x_c = create_array(@totx);
215 my $y1_c = create_array(@toty1);
216 my $y2_c = create_array(@toty2);
218 # my $startthis = [Time::HiRes::gettimeofday];
219 mygraph::mygraph_make_graph($graph, $min_x, $max_x, $min_y, $max_y, 5);
220 mygraph::mygraph_plot_series($graph, $x_c, $y1_c, $#totx, 255, 0, 0);
221 mygraph::mygraph_plot_series($graph, $x_c, $y2_c, $#totx, 0, 0, 255);
222 mygraph::mygraph_to_file($graph, "img/$filename");
224 mygraph::mygraph_cleanup ($graph);
225 # my $elapsedthis = Time::HiRes::tv_interval($startthis);
226 # printf "$elapsedthis seconds<br/>\n";
228 # Wait for all the other graphs to be done
229 while (waitpid(-1, 0) != -1) {
235 print "<div style=\"float: left;\"><h2>Total</h2>\n";
236 print "<p><img src=\"img/$filename\" width=\"$width\" height=\"$height\" /></p></div>\n";
238 my $elapsed = Time::HiRes::tv_interval($start);
239 printf "<p style=\"clear: both;\">Page and all graphs generated in %.2f seconds.</p>\n", $elapsed;
240 print "</body>\n</html>\n";