diff options
Diffstat (limited to 'web/nms.gathering.org/api')
-rw-r--r-- | web/nms.gathering.org/api/API.rst | 90 | ||||
-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 | 44 | ||||
-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 | 47 |
9 files changed, 336 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..b56db64 --- /dev/null +++ b/web/nms.gathering.org/api/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/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..6410805 --- /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, '.$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 = ('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..1dc331f --- /dev/null +++ b/web/nms.gathering.org/api/private/switch-add @@ -0,0 +1,44 @@ +#! /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) 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) { + $insert->execute($switch{'mgtmt4'}, $switch{'sysname'}); + 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/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..81e8d91 --- /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, '.$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/api/public/switches b/web/nms.gathering.org/api/public/switches new file mode 100755 index 0000000..4df81f2 --- /dev/null +++ b/web/nms.gathering.org/api/public/switches @@ -0,0 +1,47 @@ +#! /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 natural join placements'); + +$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'}; + 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; +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; +} + +finalize_output(); |