aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKristian Lyngstol <kly@kly.no>2016-03-02 19:13:50 +0000
committerKristian Lyngstol <kly@kly.no>2016-03-02 19:13:50 +0000
commit559267d660f41e2e0f1119e59d4350be48071923 (patch)
tree492c96a5b966b5fc1a2421f04c8c2271e5a643cd
parentfb47ad1cd7c8b7aabe6098ca0d092df5e698b691 (diff)
nms: Tweak switches.pl and add switch-state.pl
-rwxr-xr-xinclude/nms/web.pm7
-rw-r--r--web/nms.gathering.org/API.rst90
-rwxr-xr-xweb/nms.gathering.org/switch-state.pl44
-rwxr-xr-xweb/nms.gathering.org/switches.pl18
4 files changed, 154 insertions, 5 deletions
diff --git a/include/nms/web.pm b/include/nms/web.pm
index af3f8b7..2b39911 100755
--- a/include/nms/web.pm
+++ b/include/nms/web.pm
@@ -61,6 +61,13 @@ sub setwhen {
return $when;
}
+sub ispublic() {
+ if (defined($get_params{'public'}) || $ENV{'REMOTE_USER'} eq "public") {
+ return 1;
+ } else {
+ return 0;
+ }
+}
# Sets the ifname. If we are logged in, it's simply set to "ifname", otherwise
# it's hashed for anonymization.
sub obfuscateifname {
diff --git a/web/nms.gathering.org/API.rst b/web/nms.gathering.org/API.rst
new file mode 100644
index 0000000..b56db64
--- /dev/null
+++ b/web/nms.gathering.org/API.rst
@@ -0,0 +1,90 @@
+API-dok
+=======
+
+Work in progress.
+
+General: All end-points that output time-based data accept the "now=<time>"
+argument, where, <time> is YYYY-MM-DDThh:mm:ss. E.g:
+
+GET /switch-state.pl?now=2015-04-02T15:00:00
+
+There is no guarantee that the data is exact time-wise, thus each endpoint
+should also output relevant time stamps.
+
+Currently error handling sucks.
+
+This document is in no way complete, but it's a start. It will be updated
+as time permits and API's stabilize.
+
+comment-add.pl
+--------------
+
+Methods: POST
+
+ -- Add a comment
+
+comment-change.pl
+-----------------
+
+Methods: POST
+
+Note that comments are never really deleted, but the state can be set to
+deleted, making sure they are never shown.
+
+comment.pl -- View comments
+---------------------------
+
+Methods: GET
+Update frequency: on user input
+
+ping.pl -- Being phased out
+---------------------------
+
+Methods: GET
+Update frequency: every second or so.
+
+Used to report switch latency and linknet latency.
+
+The switch latency is being integrated into switch-state.pl and linknet
+latency will similarly be moved.
+
+port-state.pl -- Get per-port statistics
+----------------------------------------
+
+Methods: GET
+Update frequency: Every few seconds, based on SNMP data.
+
+Private.
+
+Returns detailed per-port statistics. Being somewhat reorganized but will
+remain highly relevant.
+
+switches_add.pl -- Add a switch
+-------------------------------
+
+Methods: POST
+
+Add switches, supports same format as tools/add_switches.txt.pl
+
+Accepts an array of switches.
+
+switches.pl
+-----------
+
+Methods: GET
+Update frequency: Infrequent (on topology/config changes)
+
+List all switches and map positions. Output is filtered for public users.
+
+Used to draw switches on a map and provide static information.
+
+switch-state.pl -- List state for switches
+------------------------------------------
+
+Methods: GET
+Update frequency: Every second
+
+Provides state for each switch, including total port speed, uplink port
+speed, latency and temperature.
+
+100% public.
diff --git a/web/nms.gathering.org/switch-state.pl b/web/nms.gathering.org/switch-state.pl
new file mode 100755
index 0000000..f429bf9
--- /dev/null
+++ b/web/nms.gathering.org/switch-state.pl
@@ -0,0 +1,44 @@
+#! /usr/bin/perl
+# vim:ts=8:sw=8
+
+use lib '../../include';
+use nms::web;
+use strict;
+use warnings;
+
+my $query = 'select sysname,extract(epoch from date_trunc(\'second\',time)) as time, '.$nms::web::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 @fields = ('ifhcoutoctets','ifhcinoctets');
+ foreach my $val (@fields) {
+ if ($ref->{'ifname'} =~ /ge-0\/0\/4[4-7]/) {
+ $nms::web::json{'switches'}{$ref->{'sysname'}}{'uplinks'}{$val} += $ref->{$val};
+ }
+ $nms::web::json{'switches'}{$ref->{'sysname'}}{'total'}{$val} += $ref->{$val};
+ }
+ $nms::web::json{'switches'}{$ref->{'sysname'}}{'time'} += $ref->{'time'};
+}
+
+my $q3 = $nms::web::dbh->prepare('select distinct on (switch) switch,temp,time,sysname from switch_temp natural join switches where ' . $nms::web::when . ' order by switch,time desc');
+
+$q3->execute();
+while (my $ref = $q3->fetchrow_hashref()) {
+ my $sysname = $ref->{'sysname'};
+ $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'};
+}
+
+finalize_output();
diff --git a/web/nms.gathering.org/switches.pl b/web/nms.gathering.org/switches.pl
index 3e074f6..afd073f 100755
--- a/web/nms.gathering.org/switches.pl
+++ b/web/nms.gathering.org/switches.pl
@@ -20,17 +20,25 @@ while (my $ref = $q2->fetchrow_hashref()) {
my ($x1, $y1, $x2, $y2) = ($1, $2, $3, $4);
my $sysname = $ref->{'sysname'};
$nms::web::json{'switches'}{$ref->{'sysname'}}{'switchtype'} = $ref->{'switchtype'};
- $nms::web::json{'switches'}{$ref->{'sysname'}}{'management'}{'ip'} = $ref->{'ip'};
- $nms::web::json{'switches'}{$ref->{'sysname'}}{'management'}{'poll_frequency'} = $ref->{'poll_frequency'};
- $nms::web::json{'switches'}{$ref->{'sysname'}}{'management'}{'community'} = $ref->{'community'};
- $nms::web::json{'switches'}{$ref->{'sysname'}}{'management'}{'last_updated'} = $ref->{'last_updated'};
+ if (!(nms::web::ispublic())) {
+ $nms::web::json{'switches'}{$ref->{'sysname'}}{'management'}{'ip'} = $ref->{'ip'};
+ $nms::web::json{'switches'}{$ref->{'sysname'}}{'management'}{'poll_frequency'} = $ref->{'poll_frequency'};
+ $nms::web::json{'switches'}{$ref->{'sysname'}}{'management'}{'community'} = $ref->{'community'};
+ $nms::web::json{'switches'}{$ref->{'sysname'}}{'management'}{'last_updated'} = $ref->{'last_updated'};
+ }
$nms::web::json{'switches'}{$ref->{'sysname'}}{'placement'}{'x'} = $x2;
$nms::web::json{'switches'}{$ref->{'sysname'}}{'placement'}{'y'} = $y2;
$nms::web::json{'switches'}{$ref->{'sysname'}}{'placement'}{'width'} = $x1 - $x2;
$nms::web::json{'switches'}{$ref->{'sysname'}}{'placement'}{'height'} = $y1 - $y2;
}
-my $q4 = $nms::web::dbh->prepare('select linknet, (select sysname from switches where switch = switch1) as sysname1, addr1, (select sysname from switches where switch = switch2) as sysname2,addr2 from linknets');
+my $q4;
+if (!(nms::web::ispublic())) {
+ $q4 = $nms::web::dbh->prepare('select linknet, (select sysname from switches where switch = switch1) as sysname1, addr1, (select sysname from switches where switch = switch2) as sysname2,addr2 from linknets');
+} else {
+ $q4 = $nms::web::dbh->prepare('select linknet, (select sysname from switches where switch = switch1) as sysname1, (select sysname from switches where switch = switch2) as sysname2 from linknets');
+}
+
$q4->execute();
while (my $ref = $q4->fetchrow_hashref()) {
$nms::web::json{'linknets'}{$ref->{'linknet'}} = $ref;