5 use POSIX ":sys_wait_h";
13 my $switch = 9; #$cgi->param('id');
14 my $width = $cgi->param('width');
15 my $height = $cgi->param('height');
19 $width = 500 unless (defined($width));
20 $height = 250 unless (defined($height));
22 my $graph = mygraph::mygraph_new($width, $height);
24 my $start = [Time::HiRes::gettimeofday];
25 my $dbh = DBI->connect("dbi:Pg:dbname=tg", "tg", "tg06")
26 or die "Couldn't connect to database";
29 my $ref = $dbh->selectrow_hashref('SELECT sysname FROM switches WHERE switch=?', undef, $switch);
31 print $cgi->header(-type=>'text/html; charset=utf-8');
38 <h1>Switch $switch ($ref->{'sysname'})</h1>
41 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 \'2005-03-23 05:17:36+01\' and \'2005-03-24 05:17:36+01\' and switch=? order by switch,port,time;');
44 my (@totx, @toty1, @toty2) = ();
46 my (@x, @y1, @y2) = ();
49 my $min_x = 1111637856.70337; #time;
50 my $max_x = 1111551456; #time - 86400;
51 my ($min_y, $max_y, $prev_time, $prev_in, $prev_out);
52 my ($if,$of,$ifv,$ofv);
54 my ($min_ty,$max_ty) = (0, 10_000_000/8);
58 while (my $ref = $q->fetchrow_hashref()) {
59 my $time = $ref->{'time'};
60 my $in = $ref->{'bytes_in'};
61 my $out = $ref->{'bytes_out'};
62 next if ($time == $prev_time);
64 if ($ref->{'port'} != $last_port) {
65 if ($last_port != -1) {
66 my $filename = "$switch-$last_port-$width-$height.png";
76 # print "$width, $height, $min_x, $max_x, $min_y, $max_y, 5<br/>\n";
78 my $x_c = create_array(@x);
79 my $y1_c = create_array(@y1);
80 my $y2_c = create_array(@y2);
82 # my $startthis = [Time::HiRes::gettimeofday];
83 mygraph::mygraph_make_graph($graph, $min_x, $max_x, $min_y, $max_y, 5);
84 mygraph::mygraph_plot_series($graph, $x_c, $y1_c, $#x, 255, 0, 0);
85 mygraph::mygraph_plot_series($graph, $x_c, $y2_c, $#x, 0, 0, 255);
86 mygraph::mygraph_to_file($graph, "img/$filename");
87 # mygraph::mygraph_cleanup ($graph);
88 # my $elapsedthis = Time::HiRes::tv_interval($startthis);
89 # printf "$elapsedthis seconds<br/>\n";
96 $resthtml .= "<div style=\"float: left;\"><h2>$portname</h2>\n";
97 $resthtml .= "<p><img src=\"img/$filename\" width=\"$width\" height=\"$height\" /></p></div>\n";
100 # Reset all the variables
104 ($min_y,$max_y) = (0, 10_000_000/8);
105 $prev_time = $ref->{'time'};
106 $prev_in = $ref->{'bytes_in'};
107 $prev_out = $ref->{'bytes_out'};
108 $last_port = $ref->{'port'};
109 $portname = $ref->{'description'};
110 ($if,$of,$ifv,$ofv) = (0,0,0,0);
111 ($prev_time,$prev_in,$prev_out) = ($time,$in,$out);
117 # Assume overflow (unless the switch has been down for >10 minutes)
118 my ($calc_in, $calc_out) = ($in, $out);
119 if ($in < $prev_in || $out < $prev_out) {
121 if ($prev_time - $time > 600 || ($in + 4294967296 - $prev_in) > 2147483648 || ($out + 4294967296 - $prev_out) > 2147483648) {
122 ($prev_time,$prev_in,$prev_out) = ($time,$in,$out);
126 $calc_in += 4294967296 if ($in < $prev_in);
127 $calc_out += 4294967296 if ($out < $prev_out);
131 if ($in == $prev_in && $out == $prev_out) {
132 ($prev_time,$prev_in,$prev_out) = ($time,$in,$out);
136 # Find the current flow
137 my $if = ($calc_in - $prev_in) / ($time - $prev_time);
138 my $of = ($calc_out - $prev_out) / ($time - $prev_time);
140 # Summarize (we don't care about the summed variance for now)
141 $min_x = $time if (!defined($min_x) || $time < $min_x);
142 $max_x = $time if (!defined($max_x) || $time > $max_x);
143 $min_y = $if if (!defined($min_y) || $if < $min_y);
144 $min_y = $of if ($of < $min_y);
145 $max_y = $if if (!defined($max_y) || $if > $max_y);
146 $max_y = $of if ($of > $max_y);
148 my $pt = 0.5 * ($time + $prev_time);
154 while ($idx < $#totx && $pt > $totx[$idx]) {
157 if ($idx >= $#totx) {
163 $min_ty = $if if (!defined($min_ty) || $if < $min_ty);
164 $min_ty = $of if ($of < $min_ty);
165 $max_ty = $if if (!defined($max_ty) || $if > $max_ty);
166 $max_ty = $of if ($of > $max_ty);
168 if (!defined($last_totx) || $last_totx != $idx) {
174 $min_ty = $toty1[$idx] if (!defined($min_ty) || $toty1[$idx] < $min_ty);
175 $min_ty = $toty2[$idx] if ($toty2[$idx] < $min_ty);
176 $max_ty = $toty1[$idx] if (!defined($max_ty) || $toty1[$idx] > $max_ty);
177 $max_ty = $toty2[$idx] if ($toty2[$idx] > $max_ty);
180 ($prev_time,$prev_in,$prev_out) = ($time,$in,$out);
185 my $filename = "$switch-$last_port-$width-$height.png";
189 my $x_c = create_array(@x);
190 my $y1_c = create_array(@y1);
191 my $y2_c = create_array(@y2);
193 # my $startthis = [Time::HiRes::gettimeofday];
194 mygraph::mygraph_make_graph($graph, $min_x, $max_x, $min_y, $max_y, 5);
195 mygraph::mygraph_plot_series($graph, $x_c, $y1_c, $#x, 255, 0, 0);
196 mygraph::mygraph_plot_series($graph, $x_c, $y2_c, $#x, 0, 0, 255);
197 mygraph::mygraph_to_file($graph, "img/$filename");
198 # mygraph::mygraph_cleanup ($graph);
199 # my $elapsedthis = Time::HiRes::tv_interval($startthis);
200 # printf "$elapsedthis seconds<br/>\n";
207 $resthtml .= "<div style=\"float: left;\"><h2>$portname</h2>\n";
208 $resthtml .= "<p><img src=\"img/$filename\" width=\"$width\" height=\"$height\" /></p></div>\n";
211 $filename = "$switch-$width-$height.png";
212 my $x_c = create_array(@totx);
213 my $y1_c = create_array(@toty1);
214 my $y2_c = create_array(@toty2);
216 # my $startthis = [Time::HiRes::gettimeofday];
217 mygraph::mygraph_make_graph($graph, $min_x, $max_x, $min_y, $max_y, 5);
218 mygraph::mygraph_plot_series($graph, $x_c, $y1_c, $#totx, 255, 0, 0);
219 mygraph::mygraph_plot_series($graph, $x_c, $y2_c, $#totx, 0, 0, 255);
220 mygraph::mygraph_to_file($graph, "img/$filename");
221 #mygraph::mygraph_cleanup ($graph);
222 # my $elapsedthis = Time::HiRes::tv_interval($startthis);
223 # printf "$elapsedthis seconds<br/>\n";
225 # Wait for all the other graphs to be done
226 while (waitpid(-1, 0) != -1) {
232 print "<div style=\"float: left;\"><h2>Total</h2>\n";
233 print "<p><img src=\"img/$filename\" width=\"$width\" height=\"$height\" /></p></div>\n";
235 my $elapsed = Time::HiRes::tv_interval($start);
236 printf "<p style=\"clear: both;\">Page and all graphs generated in %.2f seconds.</p>\n", $elapsed;
237 print "</body>\n</html>\n";