aboutsummaryrefslogtreecommitdiffstats
path: root/web/nms-public.gathering.org/api/public/switch-state
blob: 7fa6ff5824ab4602b7c854fcb399774c18be02ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#! /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;
		if (not $smallport =~ m/^ae/) {
			$smallport =~ s/[0-9-].*$//;
		} else {
			$json{'switches'}{$sysname}{ifs}{$smallport}{'ifAlias'} = $port{'ifAlias'};
		}
		if ($porti =~  /ge-0\/0\/4[4-7]$/) {
			$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}{ifs}{$smallport}{'ifHCInOctets'} += $port{'ifHCInOctets'};
		$json{'switches'}{$sysname}{ifs}{$smallport}{'ifHCOutOctets'} += $port{'ifHCOutOctets'};
		$json{'switches'}{$sysname}{totals}{'ifHCInOctets'} += $port{'ifHCInOctets'};
		$json{'switches'}{$sysname}{totals}{'ifHCOutOctets'} += $port{'ifHCOutOctets'};
		if ($port{'ifOperStatus'} eq "up") {
			$json{'switches'}{$sysname}{ifs}{$smallport}{'live'} += 1;
			$json{'switches'}{$sysname}{totals}{'live'} += 1;
		}
		$json{'switches'}{$sysname}{totals}{'total'} += 1;
	}
	$json{'switches'}{$sysname}{'time'} = $ref->{'time'};
}

$nms::web::cc{'max-age'} = "5";
$nms::web::cc{'stale-while-revalidate'} = "30";
finalize_output();