diff options
Diffstat (limited to 'web/nms.gathering.org/api/public/switch-state')
-rwxr-xr-x | web/nms.gathering.org/api/public/switch-state | 47 |
1 files changed, 32 insertions, 15 deletions
diff --git a/web/nms.gathering.org/api/public/switch-state b/web/nms.gathering.org/api/public/switch-state index 999a1d8..cd9c0d2 100755 --- a/web/nms.gathering.org/api/public/switch-state +++ b/web/nms.gathering.org/api/public/switch-state @@ -2,12 +2,41 @@ # vim:ts=8:sw=8 use lib '../../../../include'; -use nms::web; +use nms::web qw (%json finalize_output); 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);'); -my $query = 'select sysname,extract(epoch from date_trunc(\'second\',time)) as time, ifname,ifhighspeed,ifhcinoctets,ifhcoutoctets from polls natural join switches where time in (select max(time) from polls where ' . $nms::web::when . ' group by switch,ifname);'; -my $q = $nms::web::dbh->prepare($query); +$q->execute(); +while (my $ref = $q->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}}; + if ($porti =~ /ge-0\/0\/4[4-7]/ or $porti eq 'eth0') { + $json{'switches'}{$sysname}{'uplinks'}{'ifHCInOctets'} += $port{'ifHCInOctets'}; + $json{'switches'}{$sysname}{'uplinks'}{'ifHCOutOctets'} += $port{'ifHCOutOctets'}; + if ($port{'ifOperStatus'} eq "up") { + $json{'switches'}{$sysname}{'uplinks'}{'live'} += 1; + } + $json{'switches'}{$sysname}{'uplinks'}{'total'} += 1; + } + $json{'switches'}{$sysname}{'ifHCInOctets'} += $port{'ifHCInOctets'}; + $json{'switches'}{$sysname}{'ifHCOutOctets'} += $port{'ifHCOutOctets'}; + if ($port{'ifOperStatus'} eq "up") { + $json{'switches'}{$sysname}{'live'} += 1; + } + $json{'switches'}{$sysname}{'total'} += 1; + } + $json{'switches'}{$sysname}{'time'} = $ref->{'time'}; +} + +finalize_output(); +exit; $q->execute(); while (my $ref = $q->fetchrow_hashref()) { @@ -29,18 +58,6 @@ while (my $ref = $q3->fetchrow_hashref()) { $nms::web::json{'switches'}{$ref->{'sysname'}}{'temp'} = $ref->{'temp'}; } -my $q2 = $nms::web::dbh->prepare("SELECT DISTINCT ON (sysname) time,sysname, latency_ms FROM ping NATURAL JOIN switches WHERE time in (select max(time) from ping where " . $nms::web::when . " group by switch)"); -$q2->execute(); -while (my $ref = $q2->fetchrow_hashref()) { - $nms::web::json{'switches'}{$ref->{'sysname'}}{'latency'} = $ref->{'latency_ms'}; -} - -my $qs = $nms::web::dbh->prepare("SELECT DISTINCT ON (switch) switch, latency_ms FROM ping_secondary_ip WHERE " . $nms::web::when . " ORDER BY switch, time DESC;"); -$qs->execute(); -while (my $ref = $qs->fetchrow_hashref()) { - $nms::web::json{'switches'}{$ref->{'switch'}}{'latency_secondary'} = $ref->{'latency_ms'}; -} - $nms::web::cc{'max-age'} = "5"; $nms::web::cc{'stale-while-revalidate'} = "30"; finalize_output(); |