]> git.sesse.net Git - nms/blobdiff - web/ext/showswitch.pl
Lots of merging from TG06 NMS.
[nms] / web / ext / showswitch.pl
index 8cfa3900dae3418e3578a24565893f701fa4a30c..f2851c149f2998ab05a0ad2d3186e0b346f0f199 100755 (executable)
@@ -6,11 +6,14 @@ use POSIX ":sys_wait_h";
 use strict;
 use warnings;
 
+use lib '../../include';
+
+use nms;
 use mygraph;
 require 'glue.pl';
 
 my $cgi = CGI->new;
-my $switch = 9;        #$cgi->param('id');
+my $switch = $cgi->param('id');
 my $width = $cgi->param('width');
 my $height = $cgi->param('height');
 my @pids = ();
@@ -22,8 +25,7 @@ $height = 250 unless (defined($height));
 my $graph = mygraph::mygraph_new($width, $height);
 
 my $start = [Time::HiRes::gettimeofday];
-my $dbh = DBI->connect("dbi:Pg:dbname=tg", "tg", "tg06")
-    or die "Couldn't connect to database";
+my $dbh = nms::db_connect();                                                                           
 
 # Fetch the name
 my $ref = $dbh->selectrow_hashref('SELECT sysname FROM switches WHERE switch=?', undef, $switch);
@@ -38,7 +40,7 @@ print <<"EOF";
     <h1>Switch $switch ($ref->{'sysname'})</h1>
 EOF
 
-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;');
+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;');
 $q->execute($switch);
 
 my (@totx, @toty1, @toty2) = ();
@@ -46,8 +48,8 @@ my (@totx, @toty1, @toty2) = ();
 my (@x, @y1, @y2) = ();
 my $last_port = -1;
 my $portname = "";
-my $min_x = 1111637856.70337;  #time;
-my $max_x = 1111551456;                #time - 86400;
+my $min_x = time;
+my $max_x = time - 86400;
 my ($min_y, $max_y, $prev_time, $prev_in, $prev_out);
 my ($if,$of,$ifv,$ofv);
 my $idx;
@@ -60,7 +62,7 @@ while (my $ref = $q->fetchrow_hashref()) {
     my $in = $ref->{'bytes_in'};
     my $out = $ref->{'bytes_out'};
     next if ($time == $prev_time);
-
+    
     if ($ref->{'port'} != $last_port) {
        if ($last_port != -1) {
            my $filename = "$switch-$last_port-$width-$height.png";
@@ -218,7 +220,8 @@ mygraph::mygraph_make_graph($graph, $min_x, $max_x, $min_y, $max_y, 5);
 mygraph::mygraph_plot_series($graph, $x_c, $y1_c, $#totx, 255, 0, 0);
 mygraph::mygraph_plot_series($graph, $x_c, $y2_c, $#totx, 0, 0, 255);
 mygraph::mygraph_to_file($graph, "img/$filename");
-#mygraph::mygraph_cleanup ($graph);
+
+mygraph::mygraph_cleanup ($graph);
 # my $elapsedthis = Time::HiRes::tv_interval($startthis); 
 # printf "$elapsedthis seconds<br/>\n";