diff options
Diffstat (limited to 'web/nms-public.gathering.org/api')
-rw-r--r-- | web/nms-public.gathering.org/api/API.rst | 113 | ||||
-rwxr-xr-x | web/nms-public.gathering.org/api/public/dhcp-summary | 21 | ||||
-rwxr-xr-x | web/nms-public.gathering.org/api/public/ping | 37 | ||||
-rwxr-xr-x | web/nms-public.gathering.org/api/public/switch-state | 47 | ||||
-rwxr-xr-x | web/nms-public.gathering.org/api/public/switches | 35 |
5 files changed, 253 insertions, 0 deletions
diff --git a/web/nms-public.gathering.org/api/API.rst b/web/nms-public.gathering.org/api/API.rst new file mode 100644 index 0000000..61fbb5a --- /dev/null +++ b/web/nms-public.gathering.org/api/API.rst @@ -0,0 +1,113 @@ +API-dok +======= + +Work in progress. + +There are two relevant paths: /api/public and /api/private. One requires +user-login in, the other does not. + +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 /api/public/switch-state?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. + +Private +....... + +/api/private/comment-add +------------------------ + +Methods: POST + +Add a comment + +/api/private/comment-change +--------------------------- + +Methods: POST + +Note that comments are never really deleted, but the state can be set to +deleted, making sure they are never shown. + +/api/private/comments +--------------------- + +Methods: GET + +Update frequency: on user input + +Lists comments. + +/api/private/snmp +----------------- + +Methods: GET + +Update frequency: Every few seconds, based on SNMP data. + +Returns full SNMP-data, split into two trees. 'misc' and 'ports'. + + +/api/private/switches-management +-------------------------------- + +Methods: GET + +Update frequency: Infrequent (on topology/config changes) + +List management information for switches. + +/api/private/switch-add +----------------------- + +Methods: POST + +Add or update switches, supports same format as tools/add_switches.txt.pl + +Accepts an array of switches. + +Magic: If you set placement to be "reset", it will re-calculate placement +based on sysname. For new switches, this is redundant as an empty +placement-field will trigger the same behavior. + + +Public +...... + +/api/public/ping +---------------- + +Methods: GET +Update frequency: every second or so. + +Used to report linknet latency. + +The switch latency is being integrated into switch-state.pl and linknet +latency will similarly be moved. + +/api/public/switches +-------------------- + +Methods: GET +Update frequency: Infrequent (on topology/config changes) + +List all switches and map positions. + +Used to draw switches on a map and provide static information. + +/api/public/switch-state +------------------------ + +Methods: GET +Update frequency: Every second + +Provides state for each switch, including total port speed, uplink port +speed, latency and temperature. diff --git a/web/nms-public.gathering.org/api/public/dhcp-summary b/web/nms-public.gathering.org/api/public/dhcp-summary new file mode 100755 index 0000000..dd643b2 --- /dev/null +++ b/web/nms-public.gathering.org/api/public/dhcp-summary @@ -0,0 +1,21 @@ +#! /usr/bin/perl +use lib '../../../../include'; +use strict; +use warnings; +use nms::web; + +nms::web::setwhen('2h'); +my $q = $nms::web::dbh->prepare("select count(distinct mac) as clients,count(distinct ip) as addresses,count(mac) as acks from dhcp;"); +$q->execute(); +while (my $ref = $q->fetchrow_hashref()) { + $nms::web::json{'dhcp'}{'total'} = $ref; +} + +my $q2 = $nms::web::dbh->prepare("select count(distinct mac) as clients,count(distinct ip) as addresses,count(mac) as acks from dhcp where $nms::web::when;"); +$q2->execute(); +while (my $ref = $q2->fetchrow_hashref()) { + $nms::web::json{'dhcp'}{'recent'} = $ref; +} +$nms::web::cc{'max-age'} = "10"; +$nms::web::cc{'stale-while-revalidate'} = "15"; +finalize_output(); diff --git a/web/nms-public.gathering.org/api/public/ping b/web/nms-public.gathering.org/api/public/ping new file mode 100755 index 0000000..ba572af --- /dev/null +++ b/web/nms-public.gathering.org/api/public/ping @@ -0,0 +1,37 @@ +#! /usr/bin/perl +use lib '../../../../include'; +use strict; +use warnings; +use nms::web; + +#nms::web::setwhen('1s'); + +my $q = $nms::web::dbh->prepare("SELECT DISTINCT ON (sysname) (now() - time) as age,sysname, latency_ms FROM ping NATURAL JOIN switches WHERE time in (select max(time) from ping where " + . $nms::web::when . " group by switch)"); + +$q->execute(); +while (my $ref = $q->fetchrow_hashref()) { + $nms::web::json{'switches'}{$ref->{'sysname'}}{'latency'} = $ref->{'latency_ms'}; + # Get seconds, without decimlas, from timestamp. + # '00:01:01.435601' => 61 seconds. + my ( $h, $m, $s ) = split( ':|\.', $ref->{'age'} ); + $nms::web::json{'switches'}{$ref->{'sysname'}}{'age'} = ($h*60*60) + ($m*60) + $s; # $$ref->{'age'}; +} + +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'}; +} + +my $lq = $nms::web::dbh->prepare("SELECT DISTINCT ON (linknet) linknet, latency1_ms, latency2_ms FROM linknet_ping WHERE " + . $nms::web::when . " ORDER BY linknet, time DESC;"); +$lq->execute(); +while ( my $ref = $lq->fetchrow_hashref() ) { + $nms::web::json{'linknets'}{$ref->{'linknet'}} = [ $ref->{'latency1_ms'}, $ref->{'latency2_ms'} ]; +} + +$nms::web::cc{'max-age'} = "1"; +$nms::web::cc{'stale-while-revalidate'} = "5"; +finalize_output(); diff --git a/web/nms-public.gathering.org/api/public/switch-state b/web/nms-public.gathering.org/api/public/switch-state new file mode 100755 index 0000000..6f7573c --- /dev/null +++ b/web/nms-public.gathering.org/api/public/switch-state @@ -0,0 +1,47 @@ +#! /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]/) { + $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(); diff --git a/web/nms-public.gathering.org/api/public/switches b/web/nms-public.gathering.org/api/public/switches new file mode 100755 index 0000000..7e7f10b --- /dev/null +++ b/web/nms-public.gathering.org/api/public/switches @@ -0,0 +1,35 @@ +#! /usr/bin/perl +# vim:ts=8:sw=8 + +use CGI qw(fatalsToBrowser); +use DBI; +use lib '../../../../include'; +use nms; +use nms::web; +use strict; +use warnings; +use Data::Dumper; + +$nms::web::cc{'max-age'} = "60"; + +my $q2 = $nms::web::dbh->prepare('select switch,sysname,placement,ip,poll_frequency,community,last_updated from switches where placement is not null'); + +$q2->execute(); +while (my $ref = $q2->fetchrow_hashref()) { + $ref->{'placement'} =~ /\((-?\d+),(-?\d+)\),\((-?\d+),(-?\d+)\)/; + my ($x1, $y1, $x2, $y2) = ($1, $2, $3, $4); + my $sysname = $ref->{'sysname'}; + $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, (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; +} + +finalize_output(); |