diff options
Diffstat (limited to 'web/nms.gathering.org/api')
-rw-r--r-- | web/nms.gathering.org/api/API.rst | 15 | ||||
-rwxr-xr-x | web/nms.gathering.org/api/private/comment-add | 1 | ||||
-rwxr-xr-x | web/nms.gathering.org/api/private/comment-change | 10 | ||||
-rwxr-xr-x | web/nms.gathering.org/api/private/port-state | 33 | ||||
-rwxr-xr-x | web/nms.gathering.org/api/private/snmp | 29 | ||||
-rwxr-xr-x | web/nms.gathering.org/api/private/switch-add | 72 | ||||
-rwxr-xr-x | web/nms.gathering.org/api/private/switches-management | 2 | ||||
-rwxr-xr-x | web/nms.gathering.org/api/public/ping | 12 | ||||
-rwxr-xr-x | web/nms.gathering.org/api/public/switch-state | 56 |
9 files changed, 136 insertions, 94 deletions
diff --git a/web/nms.gathering.org/api/API.rst b/web/nms.gathering.org/api/API.rst index 4751ee5..61fbb5a 100644 --- a/web/nms.gathering.org/api/API.rst +++ b/web/nms.gathering.org/api/API.rst @@ -46,15 +46,15 @@ Update frequency: on user input Lists comments. -/api/private/port-state ------------------------ +/api/private/snmp +----------------- Methods: GET Update frequency: Every few seconds, based on SNMP data. -Returns detailed per-port statistics. Being somewhat reorganized but will -remain highly relevant. +Returns full SNMP-data, split into two trees. 'misc' and 'ports'. + /api/private/switches-management -------------------------------- @@ -70,10 +70,15 @@ List management information for switches. Methods: POST -Add switches, supports same format as tools/add_switches.txt.pl +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 ...... diff --git a/web/nms.gathering.org/api/private/comment-add b/web/nms.gathering.org/api/private/comment-add index 2f8b0b7..26ff734 100755 --- a/web/nms.gathering.org/api/private/comment-add +++ b/web/nms.gathering.org/api/private/comment-add @@ -20,4 +20,5 @@ $nms::web::cc{'max-age'} = '0'; $nms::web::cc{'stale-while-revalidate'} = '0'; $nms::web::json{'state'} = 'ok'; +print "X-ban: /api/private/comments\n"; finalize_output(); diff --git a/web/nms.gathering.org/api/private/comment-change b/web/nms.gathering.org/api/private/comment-change index ccf336d..fb7da54 100755 --- a/web/nms.gathering.org/api/private/comment-change +++ b/web/nms.gathering.org/api/private/comment-change @@ -3,12 +3,16 @@ use lib '../../../../include'; use utf8; use nms; -use nms::web; +use nms::web qw($dbh db_safe_quote get_input finalize_output); + use strict; use warnings; -my $id = db_safe_quote('comment'); -my $state = db_safe_quote('state'); +my $in = get_input(); +my %tmp = %{JSON::XS::decode_json($in)}; + +my $id = $dbh->quote($tmp{'comment'}); +my $state = $dbh->quote($tmp{'state'}); my $q = $nms::web::dbh->prepare("UPDATE switch_comments SET state = " . $state . " WHERE id = " . $id . ";"); $q->execute(); diff --git a/web/nms.gathering.org/api/private/port-state b/web/nms.gathering.org/api/private/port-state deleted file mode 100755 index 8c6b64c..0000000 --- a/web/nms.gathering.org/api/private/port-state +++ /dev/null @@ -1,33 +0,0 @@ -#! /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'}; -} -$nms::web::cc{'max-age'} = '2'; -$nms::web::cc{'stale-while-revalidate'} = '120'; - -finalize_output(); diff --git a/web/nms.gathering.org/api/private/snmp b/web/nms.gathering.org/api/private/snmp new file mode 100755 index 0000000..4779659 --- /dev/null +++ b/web/nms.gathering.org/api/private/snmp @@ -0,0 +1,29 @@ +#! /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 JSON; +use Data::Dumper; + +$nms::web::cc{'max-age'} = "10"; + +my $q = $nms::web::dbh->prepare('select sysname,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'}; + + # This is, strictly speaking, redundant. But by doing this, we can + # re-use the standard methods of finalize_output() and whatnot. + my $data = JSON::XS::decode_json($ref->{'data'}); + + $nms::web::json{'snmp'}{$ref->{'sysname'}} = $data; +} + +finalize_output(); diff --git a/web/nms.gathering.org/api/private/switch-add b/web/nms.gathering.org/api/private/switch-add index 826b65b..979a1d8 100755 --- a/web/nms.gathering.org/api/private/switch-add +++ b/web/nms.gathering.org/api/private/switch-add @@ -22,7 +22,23 @@ my @dups; my $sth = $nms::web::dbh->prepare("SELECT sysname FROM switches WHERE sysname=?"); -my @fields = ('ip', 'sysname', 'switchtype', 'last_updated', 'locked', 'poll_frequency', 'community', 'lldp_chassis_id', 'secondary_ip', 'placement'); +my @fields = ('ip', 'sysname', 'switchtype', 'last_updated', 'locked', 'poll_frequency', 'community', 'lldp_chassis_id', 'secondary_ip', 'subnet4', 'subnet6', 'placement'); + +sub convertplace +{ + my %in = %{$_[0]}; + my %out = (); + + if (not defined $in{'x1'} and defined($in{'x'})) { + $out{'x1'} = int($in{'x'}); + $out{'y1'} = int($in{'y'}); + $out{'xx'} = int($in{'x'} + $in{'width'}); + $out{'yy'} = int($in{'y'} + $in{'height'}); + } else { + return \%in; + } + return \%out; +} foreach my $tmp2 (@tmp) { my %switch = %{$tmp2}; @@ -39,38 +55,55 @@ foreach my $tmp2 (@tmp) { } if ($affected == 0) { + my %placement; + if (not defined ($switch{'placement'})) { + %placement = guess_placement($switch{'sysname'}); + } else { + %placement = %{convertplace($switch{'placement'})}; + } + if (not defined($switch{'ip'}) and defined($switch{'mgtmt4'})) { + $switch{'ip'} = $switch{'mgtmt4'}; + } + my ($x1,$x2,$y1,$y2); + $x1 = $placement{'x1'}; + $y1 = $placement{'y1'}; + $x2 = $placement{'xx'}; + $y2 = $placement{'yy'}; + $switch{'placement'} = "(($x1,$y1),($x2,$y2))"; + map { if (defined ($template{$_})) { - $template{$_} = $dbh->quote($switch{$_}); + $template{$_} = $dbh->quote($switch{$_}); } } keys %switch; - if (not defined($switch{'placement'})) { - %{$switch{'placement'}} = guess_placement($switch{'sysname'}); - } - 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))"; - $template{'placement'} = $dbh->quote($place); $nms::web::dbh->do("INSERT INTO SWITCHES (ip, sysname, switchtype, last_updated, locked, poll_frequency, community, lldp_chassis_id, secondary_ip, placement) VALUES ($template{'ip'}, $template{'sysname'}, $template{'switchtype'}, $template{'last_updated'}, $template{'locked'}, $template{'poll_frequency'}, $template{'community'}, $template{'lldp_chassis_id'}, $template{'secondary_ip'}, $template{'placement'});"); push @added, $switch{'sysname'}; } else { if (defined($switch{'placement'})) { + my %placement; + if ($switch{'placement'} eq "reset") { + %placement = guess_placement($switch{'sysname'}); + } else { + %placement = %{convertplace($switch{'placement'})}; + } 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))"; - $switch{'placement'} = $place; + $x1 = $placement{'x1'}; + $y1 = $placement{'y1'}; + $x2 = $placement{'xx'}; + $y2 = $placement{'yy'}; + $switch{'placement'} = "(($x1,$y1),($x2,$y2))"; + push @dups, "not really, but: " . $switch{'placement'}; + } + if (not defined($switch{'ip'}) and defined($switch{'mgtmt4'})) { + $switch{'ip'} = $switch{'mgtmt4'}; } my @set; map { - push @set, "$_=" . $dbh->quote($switch{$_}); + if (defined($template{$_})) { + push @set, "$_=" . $dbh->quote($switch{$_}); + } } keys %switch; $nms::web::dbh->do("UPDATE SWITCHES SET " . join(", ", @set) . "WHERE sysname=" . $dbh->quote($switch{'sysname'}) . ";"); push @dups, $switch{'sysname'}; @@ -79,4 +112,5 @@ foreach my $tmp2 (@tmp) { $json{'switches_addded'} = \@added; $json{'switches_updated'} = \@dups; +print "X-ban: /api/.*switches.*\n"; finalize_output(); diff --git a/web/nms.gathering.org/api/private/switches-management b/web/nms.gathering.org/api/private/switches-management index 474f674..543d08d 100755 --- a/web/nms.gathering.org/api/private/switches-management +++ b/web/nms.gathering.org/api/private/switches-management @@ -12,7 +12,7 @@ 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 '); +my $q2 = $nms::web::dbh->prepare('select sysname,ip,poll_frequency,community,subnet4,subnet6,last_updated from switches '); $q2->execute(); while (my $ref = $q2->fetchrow_hashref()) { diff --git a/web/nms.gathering.org/api/public/ping b/web/nms.gathering.org/api/public/ping index f13a03b..bf92440 100755 --- a/web/nms.gathering.org/api/public/ping +++ b/web/nms.gathering.org/api/public/ping @@ -2,10 +2,18 @@ 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)"); +#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'}; + # This isn't pretty, feel free to fix, but I want age == seconds + # without decimals. + my ($h,$m,$ss) = split(':', $ref->{'age'}); + my ($s,undef) = split('\.', "$ss"); + + $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;"); @@ -20,8 +28,6 @@ while (my $ref = $lq->fetchrow_hashref()) { $nms::web::json{'linknets'}{$ref->{'linknet'}} = [ $ref->{'latency1_ms'}, $ref->{'latency2_ms'} ]; } -$q->execute(); - $nms::web::cc{'max-age'} = "1"; $nms::web::cc{'stale-while-revalidate'} = "5"; finalize_output(); diff --git a/web/nms.gathering.org/api/public/switch-state b/web/nms.gathering.org/api/public/switch-state index 999a1d8..38f4c57 100755 --- a/web/nms.gathering.org/api/public/switch-state +++ b/web/nms.gathering.org/api/public/switch-state @@ -2,45 +2,41 @@ # vim:ts=8:sw=8 use lib '../../../../include'; -use nms::web; +use nms::web qw (%json finalize_output); use strict; use warnings; +use Data::Dumper; -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(); +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 @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 %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]/ or $porti eq 'eth0') { + $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; + } -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'}; + $json{'switches'}{$sysname}{$smallport}{'ifHCInOctets'} += $port{'ifHCInOctets'}; + $json{'switches'}{$sysname}{$smallport}{'ifHCOutOctets'} += $port{'ifHCOutOctets'}; + if ($port{'ifOperStatus'} eq "up") { + $json{'switches'}{$sysname}{$smallport}{'live'} += 1; + } + $json{'switches'}{$sysname}{$smallport}{'total'} += 1; + } + $json{'switches'}{$sysname}{'time'} = $ref->{'time'}; } - $nms::web::cc{'max-age'} = "5"; $nms::web::cc{'stale-while-revalidate'} = "30"; finalize_output(); |