aboutsummaryrefslogtreecommitdiffstats
path: root/web/api/public/switch-state
blob: 965e8813da6bbcb81d4a05ba9afb382e87623787 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#! /usr/bin/perl
# vim:ts=8:sw=8

use lib '/opt/gondul/include';
use nms::web qw (%json finalize_output);
use strict;
use warnings;
use Data::Dumper;

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 . ' and switches.deleted = false 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) . ' and switches.deleted = false;');
}

$q->execute();
while ( my $ref = $q->fetchrow_hashref() ) {
	my $sysname = $ref->{'sysname'};

	my %data = %{JSON::XS::decode_json($ref->{'data'})};

	$json{'switches'}{$sysname}{'clients'}{'live'} = 0;
	$json{'then'}{$sysname}{'clients'}{'live'} = 0;
	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$/) {
			next;
		}
		if ($data{'ports'}{$porti}{'ifAlias'} =~ m/client/i) {
			$smallport =~ s/[0-9-].*$//;
		} else {
			$json{'switches'}{$sysname}{ifs}{$smallport}{'ifAlias'} = $port{'ifAlias'};
		}
		if ($data{'ports'}{$porti}{'ifType'} ne "propVirtual" and
		    $data{'ports'}{$porti}{'ifAlias'} =~ m/LAG member/i) {
			if ($port{'ifAdminStatus'} eq "up") {
				$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;
			}
		}
		if ($data{'ports'}{$porti}{'ifType'} ne "propVirtual" and
		    $data{'ports'}{$porti}{'ifAlias'} =~ m/Clients/i) {
			if ($port{'ifAdminStatus'} eq "up") {
				$json{'switches'}{$sysname}{'clients'}{'ifHCInOctets'} += $port{'ifHCInOctets'};
				$json{'switches'}{$sysname}{'clients'}{'ifHCOutOctets'} += $port{'ifHCOutOctets'};
				if ($port{'ifOperStatus'} eq "up") {
					$json{'switches'}{$sysname}{'clients'}{'live'} += 1;
				}
				$json{'switches'}{$sysname}{'clients'}{'total'} += 1;
			}
		}
		$json{'switches'}{$sysname}{ifs}{$smallport}{'ifHCInOctets'} += $port{'ifHCInOctets'} || 0;
		$json{'switches'}{$sysname}{ifs}{$smallport}{'ifHCOutOctets'} += $port{'ifHCOutOctets'} || 0;
		$json{'switches'}{$sysname}{totals}{'ifHCInOctets'} += $port{'ifHCInOctets'} || 0;
		$json{'switches'}{$sysname}{totals}{'ifHCOutOctets'} += $port{'ifHCOutOctets'} || 0;
		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}{vcp}{jnxVirtualChassisPortInOctets} = $data{'vcp'}{'jnxVirtualChassisPortInOctets'};
	$json{'switches'}{$sysname}{vcp}{jnxVirtualChassisPortOutOctets} = $data{'vcp'}{'jnxVirtualChassisPortOutOctets'};
	$json{'switches'}{$sysname}{'temp'} = $data{'misc'}{'jnxOperatingTemp'}{'7.1.0.0'} || $data{'misc'}{'ciscoEnvMonTemperatureStatusValue'}{'1011'} || $data{'misc'}{'ciscoEnvMonTemperatureStatusValue'}{'1008'};
	$json{'switches'}{$sysname}{'time'} = $ref->{'time'};
}

nms::web::setwhen('15m','10m');
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() ) {
	my $sysname = $ref->{'sysname'};

	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$/) {
			next;
		}
		if ($data{'ports'}{$porti}{'ifAlias'} =~ m/client/i) {
			$smallport =~ s/[0-9-].*$//;
		} else {
			$json{'then'}{$sysname}{ifs}{$smallport}{'ifAlias'} = $port{'ifAlias'};
		}
		if ($data{'ports'}{$porti}{'ifType'} ne "propVirtual" and
		    $data{'ports'}{$porti}{'ifAlias'} =~ m/LAG member/i) {
			if ($port{'ifAdminStatus'} eq "up") {
				$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;
			}
		}
		if ($data{'ports'}{$porti}{'ifType'} ne "propVirtual" and
		    $data{'ports'}{$porti}{'ifAlias'} =~ m/Clients/i) {
			if ($port{'ifAdminStatus'} eq "up") {
				$json{'then'}{$sysname}{'clients'}{'ifHCInOctets'} += $port{'ifHCInOctets'};
				$json{'then'}{$sysname}{'clients'}{'ifHCOutOctets'} += $port{'ifHCOutOctets'};
				if ($port{'ifOperStatus'} eq "up") {
					$json{'then'}{$sysname}{'clients'}{'live'} += 1;
				}
				$json{'then'}{$sysname}{'clients'}{'total'} += 1;
			}
		}

		$json{'then'}{$sysname}{ifs}{$smallport}{'ifHCInOctets'} += $port{'ifHCInOctets'} || 0;
		$json{'then'}{$sysname}{ifs}{$smallport}{'ifHCOutOctets'} += $port{'ifHCOutOctets'} || 0;
		$json{'then'}{$sysname}{totals}{'ifHCInOctets'} += $port{'ifHCInOctets'} || 0;
		$json{'then'}{$sysname}{totals}{'ifHCOutOctets'} += $port{'ifHCOutOctets'} || 0;
		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}{vcp}{jnxVirtualChassisPortInOctets} = $data{'vcp'}{'jnxVirtualChassisPortInOctets'};
	$json{'then'}{$sysname}{vcp}{jnxVirtualChassisPortOutOctets} = $data{'vcp'}{'jnxVirtualChassisPortOutOctets'};
	$json{'then'}{$sysname}{'temp'} = $data{'misc'}{'jnxOperatingTemp'}{'7.1.0.0'} || $data{'misc'}{'ciscoEnvMonTemperatureStatusValue'}{'1011'} || $data{'misc'}{'ciscoEnvMonTemperatureStatusValue'}{'1008'};
	$json{'then'}{$sysname}{'time'} = $ref->{'time'};
}
$nms::web::cc{'max-age'} = "5";
$nms::web::cc{'stale-while-revalidate'} = "30";
finalize_output();