#! /usr/bin/perl # vim:ts=8:sw=8 use lib '../../../../include'; 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);'); $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}}; my $smallport = $porti; $smallport =~ s/[0-9-].*$//; 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}{$smallport}{'ifHCInOctets'} += $port{'ifHCInOctets'}; $json{'switches'}{$sysname}{$smallport}{'ifHCOutOctets'} += $port{'ifHCOutOctets'}; if ($port{'ifOperStatus'} eq "up") { $json{'switches'}{$sysname}{$smallport}{'live'} += 1; } $json{'switches'}{$sysname}{$smallport}{'total'} += 1; } $json{'switches'}{$sysname}{'time'} = $ref->{'time'}; } $nms::web::cc{'max-age'} = "5"; $nms::web::cc{'stale-while-revalidate'} = "30"; finalize_output();