diff options
| author | Kristian Lyngstol <kly@kly.no> | 2016-07-01 16:49:44 +0200 | 
|---|---|---|
| committer | Kristian Lyngstol <kly@kly.no> | 2016-07-01 16:49:44 +0200 | 
| commit | 8b32a6fc1f4e3dcc8b7feb29281602a625e6f5f1 (patch) | |
| tree | 3b3e63562a60bc16bf7da90a3fd28d08c2f0183a /web/api | |
| parent | 18be7f053b6a7a8cd19a7340466b68f579eb33cd (diff) | |
Implement support for per-switch/port queries for switch-state
Diffstat (limited to 'web/api')
| -rwxr-xr-x | web/api/public/switch-state | 29 | 
1 files changed, 25 insertions, 4 deletions
diff --git a/web/api/public/switch-state b/web/api/public/switch-state index 47de6d5..d1495d5 100755 --- a/web/api/public/switch-state +++ b/web/api/public/switch-state @@ -7,9 +7,20 @@ use strict;  use warnings;  use Data::Dumper; -my $q = $nms::web::dbh->prepare('select sysname,extract(epoch from date_trunc(\'second\',time)) as time,data from snmp natural join switches where id in (select max(id) from snmp where ' -        . $nms::web::when . 'group by switch);'); +print "Content-type: text/plain\n\n"; +my $target = $ENV{REQUEST_URI}; +$target =~ s/$ENV{SCRIPT_NAME}//; +$target =~ s/^\///; +my ($switch, $port) = split(/\//,$target,2); +my $q; +if (!defined($switch)) { +	$q = $nms::web::dbh->prepare('select sysname,extract(epoch from date_trunc(\'second\',time)) as time,data from snmp natural join switches where id in (select max(id) from snmp where ' . $nms::web::when . 'group by switch);'); +} else { +	$q = $nms::web::dbh->prepare('select sysname,extract(epoch from date_trunc(\'second\',time)) as time,data from snmp natural join switches where id in (select max(id) from snmp where ' . $nms::web::when . 'group by switch) and sysname = ' . $nms::web::dbh->quote($switch) . ';'); +} + +print "s: $switch, p: $port\n";  $q->execute();  while ( my $ref = $q->fetchrow_hashref() ) {  	my $sysname = $ref->{'sysname'}; @@ -17,6 +28,9 @@ while ( my $ref = $q->fetchrow_hashref() ) {  	my %data = %{JSON::XS::decode_json($ref->{'data'})};  	for my $porti (keys %{$data{'ports'}}) { +		if (defined($port) and $port ne "" and $port ne $porti) { +			next; +		}  		my %port = %{$data{'ports'}{$porti}};  		my $smallport = $porti;  		if ($porti =~ m/\.0$/) { @@ -51,8 +65,12 @@ while ( my $ref = $q->fetchrow_hashref() ) {  }  nms::web::setwhen('15m','10m'); -my $q2 = $nms::web::dbh->prepare('select sysname,extract(epoch from date_trunc(\'second\',time)) as time,data from snmp natural join switches where id in (select max(id) from snmp where ' -        . $nms::web::when . 'group by switch);'); +my $q2; +if (!defined($switch)) { +	$q2 = $nms::web::dbh->prepare('select sysname,extract(epoch from date_trunc(\'second\',time)) as time,data from snmp natural join switches where id in (select max(id) from snmp where ' . $nms::web::when . 'group by switch);'); +} else { +	$q2 = $nms::web::dbh->prepare('select sysname,extract(epoch from date_trunc(\'second\',time)) as time,data from snmp natural join switches where id in (select max(id) from snmp where ' . $nms::web::when . 'group by switch) and sysname = ' . $nms::web::dbh->quote($switch) . ';'); +}  $q2->execute();  while ( my $ref = $q2->fetchrow_hashref() ) { @@ -61,6 +79,9 @@ while ( my $ref = $q2->fetchrow_hashref() ) {  	my %data = %{JSON::XS::decode_json($ref->{'data'})};  	for my $porti (keys %{$data{'ports'}}) { +		if (defined($port) and $port ne "" and $port ne $porti) { +			next; +		}  		my %port = %{$data{'ports'}{$porti}};  		my $smallport = $porti;  		if ($porti =~ m/\.0$/) {  | 
