diff options
Diffstat (limited to 'web/nms.gathering.org/api')
-rw-r--r-- | web/nms.gathering.org/api/API.rst | 108 | ||||
-rwxr-xr-x | web/nms.gathering.org/api/private/comment-add | 20 | ||||
-rwxr-xr-x | web/nms.gathering.org/api/private/comment-change | 20 | ||||
-rwxr-xr-x | web/nms.gathering.org/api/private/comments | 15 | ||||
-rwxr-xr-x | web/nms.gathering.org/api/private/port-state | 31 | ||||
-rwxr-xr-x | web/nms.gathering.org/api/private/switch-add | 51 | ||||
-rwxr-xr-x | web/nms.gathering.org/api/private/switches-management | 30 | ||||
-rwxr-xr-x | web/nms.gathering.org/api/public/ping | 25 | ||||
-rwxr-xr-x | web/nms.gathering.org/api/public/switch-state | 44 | ||||
-rwxr-xr-x | web/nms.gathering.org/api/public/switches | 36 |
10 files changed, 380 insertions, 0 deletions
diff --git a/web/nms.gathering.org/api/API.rst b/web/nms.gathering.org/api/API.rst new file mode 100644 index 0000000..4751ee5 --- /dev/null +++ b/web/nms.gathering.org/api/API.rst @@ -0,0 +1,108 @@ +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/port-state +----------------------- + +Methods: GET + +Update frequency: Every few seconds, based on SNMP data. + +Returns detailed per-port statistics. Being somewhat reorganized but will +remain highly relevant. + +/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 switches, supports same format as tools/add_switches.txt.pl + +Accepts an array of switches. + +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.gathering.org/api/private/comment-add b/web/nms.gathering.org/api/private/comment-add new file mode 100755 index 0000000..beb7b21 --- /dev/null +++ b/web/nms.gathering.org/api/private/comment-add @@ -0,0 +1,20 @@ +#! /usr/bin/perl +# vim:ts=8:sw=8 +use lib '../../../../include'; +use utf8; +use nms::web; +use strict; +use warnings; + +my $data = db_safe_quote('comment'); +my $switch = db_safe_quote('switch'); +my $user = $dbh->quote($cgi->remote_user() || "undefined"); + +my $q = $nms::web::dbh->prepare("INSERT INTO switch_comments (time,username,switch,comment) values (now(),$user,(select switch from switches where sysname = $switch limit 1),$data)"); +$q->execute(); + +$nms::web::cc{'max-age'} = '0'; +$nms::web::cc{'stale-while-revalidate'} = '0'; +$nms::web::json{'state'} = 'ok'; + +finalize_output(); diff --git a/web/nms.gathering.org/api/private/comment-change b/web/nms.gathering.org/api/private/comment-change new file mode 100755 index 0000000..ccf336d --- /dev/null +++ b/web/nms.gathering.org/api/private/comment-change @@ -0,0 +1,20 @@ +#! /usr/bin/perl +# vim:ts=8:sw=8 +use lib '../../../../include'; +use utf8; +use nms; +use nms::web; +use strict; +use warnings; + +my $id = db_safe_quote('comment'); +my $state = db_safe_quote('state'); + +my $q = $nms::web::dbh->prepare("UPDATE switch_comments SET state = " . $state . " WHERE id = " . $id . ";"); +$q->execute(); + +$nms::web::cc{'max-age'} = '0'; +$nms::web::cc{'stale-while-revalidate'} = '0'; +$nms::web::json{'state'} = 'ok'; + +finalize_output(); diff --git a/web/nms.gathering.org/api/private/comments b/web/nms.gathering.org/api/private/comments new file mode 100755 index 0000000..4adfb2b --- /dev/null +++ b/web/nms.gathering.org/api/private/comments @@ -0,0 +1,15 @@ +#! /usr/bin/perl +# vim:ts=8:sw=8 + +use lib '../../../../include'; +use nms::web; +use strict; +use warnings; + +my $query = $nms::web::dbh->prepare('select sysname,extract(epoch from date_trunc(\'second\',time)) as time,state,username,id,comment from switch_comments natural join switches where state != \'delete\' order by time desc'); +$query->execute(); +while (my $ref = $query->fetchrow_hashref()) { + push @{$nms::web::json{'comments'}{$ref->{'sysname'}}{'comments'}},$ref; +} + +nms::web::finalize_output(); diff --git a/web/nms.gathering.org/api/private/port-state b/web/nms.gathering.org/api/private/port-state new file mode 100755 index 0000000..1f30181 --- /dev/null +++ b/web/nms.gathering.org/api/private/port-state @@ -0,0 +1,31 @@ +#! /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, 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 = ('ifhighspeed','ifhcoutoctets','ifhcinoctets'); + foreach my $val (@fields) { + $nms::web::json{'switches'}{$ref->{'sysname'}}{'ports'}{$ref->{'ifname'}}{$val} = $ref->{$val}; + } + $nms::web::json{'switches'}{$ref->{'sysname'}}{'ports'}{$ref->{'ifname'}}{'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'}; + $nms::web::json{'switches'}{$ref->{'sysname'}}{'temp_time'} = $ref->{'time'}; +} + +finalize_output(); diff --git a/web/nms.gathering.org/api/private/switch-add b/web/nms.gathering.org/api/private/switch-add new file mode 100755 index 0000000..2b1f306 --- /dev/null +++ b/web/nms.gathering.org/api/private/switch-add @@ -0,0 +1,51 @@ +#! /usr/bin/perl +# vim:ts=8:sw=8 + +#use CGI qw(fatalsToBrowser); +use DBI; +use lib '../../../../include'; +use nms; +use nms::web qw(%get_params %json finalize_output get_input); +use strict; +use warnings; +use JSON; +use Data::Dumper; + +$nms::web::cc{'max-age'} = "0"; + +my $in = get_input(); +my @tmp = @{JSON::XS::decode_json($in)}; + +my @added; +my @dups; + +my $sth = $nms::web::dbh->prepare("SELECT sysname FROM switches WHERE sysname=?"); +my $insert = $nms::web::dbh->prepare("INSERT INTO SWITCHES (ip, sysname, switchtype,placement) VALUES(?,?,'ex2200',?);"); + +foreach my $tmp2 (@tmp) { + my %switch = %{$tmp2}; + my $affected = 0; + + $sth->execute( $switch{'sysname'}); + while ( my @row = $sth->fetchrow_array ) { + $affected += 1; + } + + if ($affected == 0) { + my ($x1,$x2,$y1,$y2); + $x1 = $switch{'placement'}{'x1'}; + $y1 = $switch{'placement'}{'y1'}; + $x2 = $switch{'placement'}{'xx'}; + $y2 = $switch{'placement'}{'yy'}; + my $place = "(($x1,$y1),($x2,$y2))"; + $json{'foo'} = $place . "foo"; + $insert->execute($switch{'mgtmt4'}, $switch{'sysname'},$place); + push @added, $switch{'sysname'}; + } else { + push @dups, $switch{'sysname'}; + } +} +$json{'switches_addded'} = \@added; +$json{'switches_duplicate'} = \@dups; + +finalize_output(); diff --git a/web/nms.gathering.org/api/private/switches-management b/web/nms.gathering.org/api/private/switches-management new file mode 100755 index 0000000..474f674 --- /dev/null +++ b/web/nms.gathering.org/api/private/switches-management @@ -0,0 +1,30 @@ +#! /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 sysname,ip,switchtype,poll_frequency,community,last_updated from switches '); + +$q2->execute(); +while (my $ref = $q2->fetchrow_hashref()) { + my $sysname = $ref->{'sysname'}; + $nms::web::json{'switches'}{$ref->{'sysname'}} = $ref; +} + +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'); + +$q4->execute(); +while (my $ref = $q4->fetchrow_hashref()) { + $nms::web::json{'linknets'}{$ref->{'linknet'}} = $ref; +} + +finalize_output(); diff --git a/web/nms.gathering.org/api/public/ping b/web/nms.gathering.org/api/public/ping new file mode 100755 index 0000000..f713df1 --- /dev/null +++ b/web/nms.gathering.org/api/public/ping @@ -0,0 +1,25 @@ +#! /usr/bin/perl +use lib '../../../../include'; +use nms::web; + +my $q = $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)"); +$q->execute(); +while (my $ref = $q->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'}; +} + +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'} ]; +} + +$q->execute(); + +finalize_output(); diff --git a/web/nms.gathering.org/api/public/switch-state b/web/nms.gathering.org/api/public/switch-state new file mode 100755 index 0000000..e494e6d --- /dev/null +++ b/web/nms.gathering.org/api/public/switch-state @@ -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, 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/api/public/switches b/web/nms.gathering.org/api/public/switches new file mode 100755 index 0000000..d62169c --- /dev/null +++ b/web/nms.gathering.org/api/public/switches @@ -0,0 +1,36 @@ +#! /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,switchtype,poll_frequency,community,last_updated from switches'); + +$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'}}{'switchtype'} = $ref->{'switchtype'}; + $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(); |