diff options
-rwxr-xr-x | include/nms/web.pm | 7 | ||||
-rwxr-xr-x | web/nms.gathering.org/api/public/switch-state | 39 | ||||
-rwxr-xr-x | web/nms.gathering.org/api/public/switches | 2 | ||||
-rwxr-xr-x | web/nms.gathering.org/api/read/switches-management | 2 | ||||
-rw-r--r-- | web/nms.gathering.org/js/nms-map-handlers.js | 60 |
5 files changed, 76 insertions, 34 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(); diff --git a/web/nms.gathering.org/api/public/switches b/web/nms.gathering.org/api/public/switches index 72c77be..890b7d7 100755 --- a/web/nms.gathering.org/api/public/switches +++ b/web/nms.gathering.org/api/public/switches @@ -12,7 +12,7 @@ use Data::Dumper; $nms::web::cc{'max-age'} = "60"; -my $q2 = $nms::web::dbh->prepare('select switch,sysname,distro_name,placement,mgmt_v4_addr,mgmt_v6_addr,mgmt_v4_gw,mgmt_v4_netsize,mgmt_v6_gw,mgmt_v6_netsize,mgmt_vlan,traffic_vlan,last_config_fetch,current_mac,poll_frequency,community,last_updated,switchtype from switches where placement is not null'); +my $q2 = $nms::web::dbh->prepare('select switch,sysname,distro_name,placement,mgmt_v4_addr,mgmt_v6_addr,mgmt_v4_gw,mgmt_v6_gw,mgmt_vlan,traffic_vlan,last_config_fetch,current_mac,poll_frequency,community,last_updated,switchtype from switches where placement is not null'); $q2->execute(); while (my $ref = $q2->fetchrow_hashref()) { diff --git a/web/nms.gathering.org/api/read/switches-management b/web/nms.gathering.org/api/read/switches-management index 1bb4c4c..7697671 100755 --- a/web/nms.gathering.org/api/read/switches-management +++ b/web/nms.gathering.org/api/read/switches-management @@ -13,7 +13,7 @@ use Data::Dumper; $nms::web::cc{'max-age'} = "60"; #my $q2 = $nms::web::dbh->prepare('select sysname,ip,poll_frequency,subnet4,subnet6,distro,last_updated from switches '); -my $q2 = $nms::web::dbh->prepare('select switch,sysname,mgmt_v4_addr,mgmt_v6_addr,mgmt_v4_gw,mgmt_v4_netsize,mgmt_v6_gw,mgmt_v6_netsize,mgmt_vlan,traffic_vlan,last_config_fetch,current_mac,poll_frequency,last_updated,switchtype from switches where placement is not null'); +my $q2 = $nms::web::dbh->prepare('select switch,sysname,mgmt_v4_addr,mgmt_v6_addr,mgmt_v4_gw,mgmt_v6_gw,mgmt_vlan,traffic_vlan,last_config_fetch,current_mac,poll_frequency,last_updated,switchtype from switches where placement is not null'); $q2->execute(); while (my $ref = $q2->fetchrow_hashref()) { diff --git a/web/nms.gathering.org/js/nms-map-handlers.js b/web/nms.gathering.org/js/nms-map-handlers.js index dac630a..82d0c4a 100644 --- a/web/nms.gathering.org/js/nms-map-handlers.js +++ b/web/nms.gathering.org/js/nms-map-handlers.js @@ -133,6 +133,8 @@ function uplinkInit() */ function trafficInit() { + nmsData.addHandler("switches","mapHandler",trafficUpdater); + nmsData.addHandler("switchstate","mapHandler",trafficUpdater); var m = 1024 * 1024 / 8; drawGradient([lightgreen,green,orange,red]); setLegend(1,colorFromSpeed(0),"0 (N/A)"); @@ -144,33 +146,28 @@ function trafficInit() function trafficUpdater() { - if (!nms.switches_now["switches"]) + if (!nmsData.switchstate.switches || !nmsData.switchstate.then) return; - for (var sw in nms.switches_now["switches"]) { + for (var sw in nmsData.switchstate.switches) { var speed = 0; - for (var port in nms.switches_now["switches"][sw]["ports"]) { - if (/ge-0\/0\/44$/.exec(port) || - /ge-0\/0\/45$/.exec(port) || - /ge-0\/0\/46$/.exec(port) || - /ge-0\/0\/47$/.exec(port)) - { - if (!nms.switches_then["switches"][sw] || - !nms.switches_then["switches"][sw]["ports"] || - !nms.switches_then["switches"][sw]["ports"][port]) - continue; - var t = nms.switches_then["switches"][sw]["ports"][port]; - var n = nms.switches_now["switches"][sw]["ports"][port]; - speed += (parseInt(t["ifhcoutoctets"]) -parseInt(n["ifhcoutoctets"])) / (parseInt(t["time"] - n["time"])); - speed += (parseInt(t["ifhcinoctets"]) -parseInt(n["ifhcinoctets"])) / (parseInt(t["time"] - n["time"])); - } - } + try { + var t = parseInt(nmsData.switchstate.then[sw].uplinks.ifHCOutOctets); + var n = parseInt(nmsData.switchstate.switches[sw].uplinks.ifHCOutOctets); + var tt = parseInt(nmsData.switchstate.then[sw].time); + var nt = parseInt(nmsData.switchstate.switches[sw].time); + } catch (e) { continue;}; + var tdiff = nt - tt; + var diff = n - t; + speed = diff / tdiff; if(!isNaN(speed)) - setSwitchColor(sw,colorFromSpeed(speed)); + nmsMap.setSwitchColor(sw,colorFromSpeed(speed)); } } function trafficTotInit() { + nmsData.addHandler("switches","mapHandler",trafficTotUpdater); + nmsData.addHandler("switchstate","mapHandler",trafficTotUpdater); var m = 1024 * 1024 / 8; drawGradient([lightgreen,green,orange,red]); setLegend(1,colorFromSpeed(0),"0 (N/A)"); @@ -182,20 +179,21 @@ function trafficTotInit() function trafficTotUpdater() { - if (!nms.switches_now["switches"]) + if (!nmsData.switchstate.switches || !nmsData.switchstate.then) return; - for (var sw in nms.switches_now["switches"]) { + for (var sw in nmsData.switchstate.switches) { var speed = 0; - for (var port in nms.switches_now["switches"][sw]["ports"]) { - if (!nms.switches_then["switches"][sw] || - !nms.switches_then["switches"][sw]["ports"] || - !nms.switches_then["switches"][sw]["ports"][port]) - continue; - var t = nms.switches_then["switches"][sw]["ports"][port]; - var n = nms.switches_now["switches"][sw]["ports"][port]; - speed += (parseInt(t["ifhcoutoctets"]) -parseInt(n["ifhcoutoctets"])) / (parseInt(t["time"] - n["time"])); - } - setSwitchColor(sw,colorFromSpeed(speed,5)); + try { + var t = parseInt(nmsData.switchstate.then[sw].totals.ifHCOutOctets); + var n = parseInt(nmsData.switchstate.switches[sw].totals.ifHCOutOctets); + var tt = parseInt(nmsData.switchstate.then[sw].time); + var nt = parseInt(nmsData.switchstate.switches[sw].time); + } catch (e) { continue;}; + var tdiff = nt - tt; + var diff = n - t; + speed = diff / tdiff; + if(!isNaN(speed)) + nmsMap.setSwitchColor(sw,colorFromSpeed(speed)); } } |