diff options
author | Kristian Lyngstol <kly@kly@.no> | 2016-03-22 23:27:32 +0100 |
---|---|---|
committer | Kristian Lyngstol <kly@kly@.no> | 2016-03-22 23:27:32 +0100 |
commit | 348d197899f37e535feefd5055ae8ef553a71bdf (patch) | |
tree | 235807273cdfe6582e1080f62ee5e2a11dac4110 | |
parent | 068baf7c5de1c4bf3a9b12a5eb8c88057178b19d (diff) |
NMS: "Dirty" hack to get switches.then in switch-state
-rwxr-xr-x | include/nms/web.pm | 7 | ||||
-rwxr-xr-x | web/nms.gathering.org/api/public/switch-state | 39 |
2 files changed, 45 insertions, 1 deletions
diff --git a/include/nms/web.pm b/include/nms/web.pm index b36d692..c9f7236 100755 --- a/include/nms/web.pm +++ b/include/nms/web.pm @@ -58,13 +58,18 @@ sub db_safe_quote { sub setwhen { $now = "now()"; my $window = '15m'; - if (@_ == 1) { + my $offset = '0s'; + if (@_ > 0) { $window = $_[0]; } + if (@_ > 1) { + $offset = $_[1]; + } if (defined($get_params{'now'})) { $now = db_safe_quote('now') . "::timestamp "; $cc{'max-age'} = "3600"; } + $now = "(" . $now . " - '" . $offset . "'::interval)"; $when = " time > " . $now . " - '".$window."'::interval and time < " . $now . " "; } diff --git a/web/nms.gathering.org/api/public/switch-state b/web/nms.gathering.org/api/public/switch-state index 7fa6ff5..32cf9a6 100755 --- a/web/nms.gathering.org/api/public/switch-state +++ b/web/nms.gathering.org/api/public/switch-state @@ -46,6 +46,45 @@ while ( my $ref = $q->fetchrow_hashref() ) { $json{'switches'}{$sysname}{'time'} = $ref->{'time'}; } +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);'); + +$q2->execute(); +while ( my $ref = $q2->fetchrow_hashref() ) { + my $sysname = $ref->{'sysname'}; + + my %data = %{JSON::XS::decode_json($ref->{'data'})}; + + for my $porti (keys %{$data{'ports'}}) { + my %port = %{$data{'ports'}{$porti}}; + my $smallport = $porti; + if (not $smallport =~ m/^ae/) { + $smallport =~ s/[0-9-].*$//; + } else { + $json{'then'}{$sysname}{ifs}{$smallport}{'ifAlias'} = $port{'ifAlias'}; + } + if ($porti =~ /ge-0\/0\/4[4-7]$/) { + $json{'then'}{$sysname}{'uplinks'}{'ifHCInOctets'} += $port{'ifHCInOctets'}; + $json{'then'}{$sysname}{'uplinks'}{'ifHCOutOctets'} += $port{'ifHCOutOctets'}; + if ($port{'ifOperStatus'} eq "up") { + $json{'then'}{$sysname}{'uplinks'}{'live'} += 1; + } + $json{'then'}{$sysname}{'uplinks'}{'total'} += 1; + } + + $json{'then'}{$sysname}{ifs}{$smallport}{'ifHCInOctets'} += $port{'ifHCInOctets'}; + $json{'then'}{$sysname}{ifs}{$smallport}{'ifHCOutOctets'} += $port{'ifHCOutOctets'}; + $json{'then'}{$sysname}{totals}{'ifHCInOctets'} += $port{'ifHCInOctets'}; + $json{'then'}{$sysname}{totals}{'ifHCOutOctets'} += $port{'ifHCOutOctets'}; + if ($port{'ifOperStatus'} eq "up") { + $json{'then'}{$sysname}{ifs}{$smallport}{'live'} += 1; + $json{'then'}{$sysname}{totals}{'live'} += 1; + } + $json{'then'}{$sysname}{totals}{'total'} += 1; + } + $json{'then'}{$sysname}{'time'} = $ref->{'time'}; +} $nms::web::cc{'max-age'} = "5"; $nms::web::cc{'stale-while-revalidate'} = "30"; finalize_output(); |