]> git.sesse.net Git - nms/blob - web/showswitch.pl
Merge.
[nms] / web / 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 use lib '../include';
9 use nms;
10 my $cgi = CGI->new;
11 my $switch = $cgi->param('id');
12 my $width = $cgi->param('width');
13 my $height = $cgi->param('height');
14 my @pids = ();
15 my $resthtml = "";
16
17 $width = 500 unless (defined($width));
18 $height = 250 unless (defined($height));
19
20 require './mygraph.pl';
21
22 my $start = [Time::HiRes::gettimeofday];
23 my $dbh = nms::db_connect();
24
25 # Fetch the name
26 my $ref = $dbh->selectrow_hashref('SELECT sysname FROM switches WHERE switch=?', undef, $switch);
27
28 print $cgi->header(-type=>'text/html; charset=utf-8');
29 print <<"EOF";
30 <html>
31   <head>
32     <title>snmp</title>
33   </head>
34   <body>
35     <h1>Switch $switch ($ref->{'sysname'})</h1>
36 EOF
37
38 my $q = $dbh->prepare('select port,coalesce(description, \'Port \' || port) as description,extract(epoch from time) as time,bytes_in,bytes_out from switches natural left join portnames natural join polls where time between now() - \'1 day\'::interval and now() and switch=? order by switch,port,time;');
39 $q->execute($switch);
40
41 my (@totx, @toty1, @toty2) = ();
42
43 my (@x, @y1, @y2) = ();
44 my $last_port = -1;
45 my $portname = "";
46 my $min_x = time;
47 my $max_x = time - 86400;
48 my ($min_y, $max_y, $prev_time, $prev_in, $prev_out);
49 my ($if,$of,$ifv,$ofv);
50 my $idx;
51 my ($min_ty,$max_ty) = (0, 10_000_000/8);
52
53 $prev_time = -1;
54 my $last_totx;
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);
60
61         if ($ref->{'port'} != $last_port) {
62                 if ($last_port != -1) {
63                         my $filename = "$switch-$last_port-$width-$height.png";
64
65                         # reap children
66                         waitpid(-1, WNOHANG);
67
68                         my $pid = fork();
69                         if ($pid == 0) {
70 # write out the graph
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);
74
75                                 open GRAPH, ">img/$filename"
76                                         or die "img/$filename: $!";
77                                 print GRAPH $graph->png;
78                                 close GRAPH;
79                                 exit;
80                         }
81
82                         push @pids, $pid;
83
84                         $resthtml .= "<div style=\"float: left;\"><h2>$portname</h2>\n";
85                         $resthtml .= "<p><img src=\"img/$filename\" width=\"$width\" height=\"$height\" /></p></div>\n";
86                 }
87         
88                 # Reset all the variables
89                 @x = ();
90                 @y1 = ();
91                 @y2 = ();
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);
100                 $idx = 0;
101                 $last_totx = undef;
102                 next;
103         }
104
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) {
108                 # ick, heuristics
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);
111                         next;
112                 }
113
114                 $calc_in += 4294967296 if ($in < $prev_in);
115                 $calc_out += 4294967296 if ($out < $prev_out);
116         }
117
118         # Remove dupes
119         if ($in == $prev_in && $out == $prev_out) {
120                 ($prev_time,$prev_in,$prev_out) = ($time,$in,$out);
121                 next;
122         }
123
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);
127
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);
135
136         my $pt = 0.5 * ($time + $prev_time);
137
138         push @x, $pt;
139         push @y1, $if;
140         push @y2, $of;
141
142         while ($idx < $#totx && $pt > $totx[$idx]) {
143                 ++$idx;
144         }
145         if ($idx >= $#totx) {
146                 push @totx, $pt;
147                 push @toty1, $if;
148                 push @toty2, $of;
149                 ++$idx;
150
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);
155         } else {
156                 if (!defined($last_totx) || $last_totx != $idx) {
157                         $toty1[$idx] += $if;
158                         $toty2[$idx] += $of;
159                 }
160                 $last_totx = $idx;
161
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);
166         }
167         
168         ($prev_time,$prev_in,$prev_out) = ($time,$in,$out);
169 }
170 $dbh->disconnect;
171
172 # last graph
173 my $filename = "$switch-$last_port-$width-$height.png";
174
175 my $pid = fork();
176 if ($pid == 0) {
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);
180
181         open GRAPH, ">img/$filename"
182         or die "img/$filename: $!";
183         print GRAPH $graph->png;
184         close GRAPH;
185         exit;
186 }
187
188 push @pids, $pid;
189
190 $resthtml .= "<div style=\"float: left;\"><h2>$portname</h2>\n";
191 $resthtml .= "<p><img src=\"img/$filename\" width=\"$width\" height=\"$height\" /></p></div>\n";
192                 
193 # total graph
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);
197
198 my $filename = "$switch-$width-$height.png";
199 open GRAPH, ">img/$filename"
200 or die "img/$filename: $!";
201 print GRAPH $graph->png;
202 close GRAPH;
203
204 # Wait for all the other graphs to be done
205 while (waitpid(-1, 0) != -1) {
206         1;
207 }
208
209 print $resthtml;
210
211 print "<div style=\"float: left;\"><h2>Total</h2>\n";
212 print "<p><img src=\"img/$filename\" width=\"$width\" height=\"$height\" /></p></div>\n";
213
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";