]> git.sesse.net Git - nms/blob - web/ext/showswitch.pl
Added Chillout.
[nms] / web / ext / showswitch.pl
1 #!/usr/bin/perl
2 use CGI;
3 use DBI;
4 use Time::HiRes;
5 use POSIX ":sys_wait_h";
6 use strict;
7 use warnings;
8
9 use lib '../../include';
10
11 use nms;
12 use mygraph;
13 require 'glue.pl';
14
15 my $cgi = CGI->new;
16 my $switch = $cgi->param('id');
17 my $width = $cgi->param('width');
18 my $height = $cgi->param('height');
19 my @pids = ();
20 my $resthtml = "";
21
22 $width = 500 unless (defined($width));
23 $height = 250 unless (defined($height));
24
25 my $graph = mygraph::mygraph_new($width, $height);
26
27 my $start = [Time::HiRes::gettimeofday];
28 my $dbh = nms::db_connect();                                                                           
29
30 # Fetch the name
31 my $ref = $dbh->selectrow_hashref('SELECT sysname FROM switches WHERE switch=?', undef, $switch);
32
33 print $cgi->header(-type=>'text/html; charset=utf-8');
34 print <<"EOF";
35 <html>
36   <head>
37     <title>snmp</title>
38   </head>
39   <body>
40     <h1>Switch $switch ($ref->{'sysname'})</h1>
41 EOF
42
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;');
44 $q->execute($switch);
45
46 my (@totx, @toty1, @toty2) = ();
47
48 my (@x, @y1, @y2) = ();
49 my $last_port = -1;
50 my $portname = "";
51 my $min_x = time;
52 my $max_x = time - 86400;
53 my ($min_y, $max_y, $prev_time, $prev_in, $prev_out);
54 my ($if,$of,$ifv,$ofv);
55 my $idx;
56 my ($min_ty,$max_ty) = (0, 10_000_000/8);
57
58 $prev_time = -1;
59 my $last_totx;
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);
65     
66     if ($ref->{'port'} != $last_port) {
67         if ($last_port != -1) {
68             my $filename = "$switch-$last_port-$width-$height.png";
69
70             # reap children
71             waitpid(-1, WNOHANG);
72
73             my $numpids = 0;
74
75             my $pid = fork();
76             if ($pid == 0) {
77                 # write out the graph
78                 # print "$width, $height, $min_x, $max_x, $min_y, $max_y, 5<br/>\n";
79
80                 my $x_c = create_array(@x);
81                 my $y1_c = create_array(@y1);
82                 my $y2_c = create_array(@y2);
83
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";
92
93                 exit;
94             }
95
96             push @pids, $pid;
97
98             $resthtml .= "<div style=\"float: left;\"><h2>$portname</h2>\n";
99             $resthtml .= "<p><img src=\"img/$filename\" width=\"$width\" height=\"$height\" /></p></div>\n";
100         }
101
102         # Reset all the variables
103         @x = ();
104         @y1 = ();
105         @y2 = ();
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);
114         $idx = 0;
115         $last_totx = undef;
116         next;
117     }
118
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) {
122         # ick, heuristics
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);
125             next;
126         }
127
128         $calc_in += 4294967296 if ($in < $prev_in);
129         $calc_out += 4294967296 if ($out < $prev_out);
130     }
131
132     # Remove dupes
133     if ($in == $prev_in && $out == $prev_out) {
134         ($prev_time,$prev_in,$prev_out) = ($time,$in,$out);
135         next;
136     }
137
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);
141
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);
149
150     my $pt = 0.5 * ($time + $prev_time);
151
152     push @x, $pt;
153     push @y1, $if;
154     push @y2, $of;
155
156     while ($idx < $#totx && $pt > $totx[$idx]) {
157         ++$idx;
158     }
159     if ($idx >= $#totx) {
160         push @totx, $pt;
161         push @toty1, $if;
162         push @toty2, $of;
163         ++$idx;
164
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);
169     } else {
170         if (!defined($last_totx) || $last_totx != $idx) {
171             $toty1[$idx] += $if;
172             $toty2[$idx] += $of;
173         }
174         $last_totx = $idx;
175
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);
180     }
181
182     ($prev_time,$prev_in,$prev_out) = ($time,$in,$out);
183 }
184 $dbh->disconnect;
185
186 # last graph
187 my $filename = "$switch-$last_port-$width-$height.png";
188
189 my $pid = fork();
190 if ($pid == 0) {
191     my $x_c = create_array(@x);
192     my $y1_c = create_array(@y1);
193     my $y2_c = create_array(@y2);
194
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";
203
204     exit;
205 }
206
207 push @pids, $pid;
208
209 $resthtml .= "<div style=\"float: left;\"><h2>$portname</h2>\n";
210 $resthtml .= "<p><img src=\"img/$filename\" width=\"$width\" height=\"$height\" /></p></div>\n";
211
212 # total graph
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);
217
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");
223
224 mygraph::mygraph_cleanup ($graph);
225 # my $elapsedthis = Time::HiRes::tv_interval($startthis); 
226 # printf "$elapsedthis seconds<br/>\n";
227
228 # Wait for all the other graphs to be done
229 while (waitpid(-1, 0) != -1) {
230     1;
231 }
232
233 print $resthtml;
234
235 print "<div style=\"float: left;\"><h2>Total</h2>\n";
236 print "<p><img src=\"img/$filename\" width=\"$width\" height=\"$height\" /></p></div>\n";
237
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";