diff options
Diffstat (limited to 'web/api')
-rwxr-xr-x | web/api/public/config | 3 | ||||
-rwxr-xr-x | web/api/public/ping | 20 | ||||
-rwxr-xr-x | web/api/read/switches-management | 2 | ||||
-rwxr-xr-x | web/api/write/linknet-add | 32 | ||||
-rwxr-xr-x | web/api/write/oplog | 2 | ||||
-rwxr-xr-x | web/api/write/switch-add | 4 |
6 files changed, 52 insertions, 11 deletions
diff --git a/web/api/public/config b/web/api/public/config index dd8ccc1..0f0d322 100755 --- a/web/api/public/config +++ b/web/api/public/config @@ -15,6 +15,9 @@ my $hostname = $ENV{'HTTP_HOST'} || ""; my $q2 = $nms::web::dbh->prepare('select id, publicvhost, shortname, data from config order by id desc limit 1;'); $q2->execute(); +$nms::web::json{'config'}{'data'} = 0; +$nms::web::json{'config'}{'shortname'} = "notset"; +$nms::web::json{'config'}{'publicvhost'} = "notset"; while (my $ref = $q2->fetchrow_hashref()) { $nms::web::json{'config'} = $ref; $nms::web::json{'config'}{'data'} = JSON::XS::decode_json($ref->{'data'}); diff --git a/web/api/public/ping b/web/api/public/ping index 36e3334..1928368 100755 --- a/web/api/public/ping +++ b/web/api/public/ping @@ -11,20 +11,24 @@ my $q = $nms::web::dbh->prepare("SELECT DISTINCT ON (sysname) (" . $nms::web::no $q->execute(); while (my $ref = $q->fetchrow_hashref()) { - $nms::web::json{'switches'}{$ref->{'sysname'}}{'latency'} = $ref->{'latency_ms'}; + $nms::web::json{'switches'}{$ref->{'sysname'}}{'latency4'} = $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'}; + $nms::web::json{'switches'}{$ref->{'sysname'}}{'age4'} = ($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 $q2 = $nms::web::dbh->prepare("SELECT DISTINCT ON (sysname) (" . $nms::web::now . " - time) as age,sysname, latency_ms FROM ping_secondary_ip 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'}}{'latency6'} = $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'}}{'age6'} = ($h*60*60) + ($m*60) + $s; # $$ref->{'age'}; +} 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(); diff --git a/web/api/read/switches-management b/web/api/read/switches-management index 2dd329c..8d899b7 100755 --- a/web/api/read/switches-management +++ b/web/api/read/switches-management @@ -12,7 +12,7 @@ use Data::Dumper; $nms::web::cc{'max-age'} = "60"; -my $q2 = $nms::web::dbh->prepare('select switch,sysname,mgmt_v4_addr,subnet4,subnet6,mgmt_v6_addr,mgmt_v4_gw,mgmt_v6_gw,mgmt_vlan,traffic_vlan,last_config_fetch,current_mac,poll_frequency,last_updated,distro_phy_port from switches where placement is not null'); +my $q2 = $nms::web::dbh->prepare('select sysname,mgmt_v4_addr,subnet4,subnet6,mgmt_v6_addr,mgmt_v4_gw,mgmt_v6_gw,mgmt_vlan,traffic_vlan,last_config_fetch,current_mac,poll_frequency,last_updated,distro_phy_port,community from switches where placement is not null'); $q2->execute(); while (my $ref = $q2->fetchrow_hashref()) { diff --git a/web/api/write/linknet-add b/web/api/write/linknet-add new file mode 100755 index 0000000..13ccd17 --- /dev/null +++ b/web/api/write/linknet-add @@ -0,0 +1,32 @@ +#! /usr/bin/perl +# vim:ts=8:sw=8 +use lib '/opt/gondul/include'; +use utf8; +use nms::web qw($dbh db_safe_quote get_input finalize_output); +use strict; +use warnings; + +my $in = get_input(); +my %tmp = %{JSON::XS::decode_json($in)}; + +my $q = $nms::web::dbh->prepare("INSERT INTO linknets (switch1, switch2) VALUES((SELECT switch FROM switches WHERE sysname = ? LIMIT 1), (SELECT switch FROM switches WHERE sysname = ? LIMIT 1));"); +my $sth = $nms::web::dbh->prepare("SELECT linknet FROM linknets WHERE switch1 = (SELECT switch FROM switches WHERE sysname = ? LIMIT 1) and switch2 = (SELECT switch FROM switches WHERE sysname = ? LIMIT 1);"); + +$sth->execute($tmp{'switch1'}, $tmp{'switch2'}); +my $affected = 0; +while ( my @row = $sth->fetchrow_array ) { + $affected += 1; +} + +print "X-affected: $affected\n"; +if ($affected eq 0) { + $q->execute($tmp{'switch1'}, $tmp{'switch2'}); +} + +$dbh->commit; +$nms::web::cc{'max-age'} = '0'; +$nms::web::cc{'stale-while-revalidate'} = '0'; +$nms::web::json{'state'} = 'ok'; + +print "X-ban: /api/public/.*\n"; +finalize_output(); diff --git a/web/api/write/oplog b/web/api/write/oplog index 736ba2b..73b807b 100755 --- a/web/api/write/oplog +++ b/web/api/write/oplog @@ -9,7 +9,7 @@ use warnings; my $in = get_input(); my %tmp = %{JSON::XS::decode_json($in)}; -my $user = $ENV{'REMOTE_USER'} || "undefined"; +my $user = $tmp{'user'} || $ENV{'REMOTE_USER'} || "undefined"; my $q = $nms::web::dbh->prepare("INSERT INTO oplog (username, systems, log) values (?,?,?);"); $q->execute($user, $tmp{'systems'}, $tmp{'log'}); diff --git a/web/api/write/switch-add b/web/api/write/switch-add index 56e5cb2..f77df0a 100755 --- a/web/api/write/switch-add +++ b/web/api/write/switch-add @@ -11,6 +11,7 @@ use strict; use warnings; use JSON; use Data::Dumper; +use nms::oplog qw(oplog); $nms::web::cc{'max-age'} = "0"; @@ -83,10 +84,11 @@ foreach my $tmp2 (@tmp) { $nms::web::dbh->do("INSERT INTO SWITCHES (mgmt_v4_addr, sysname, poll_frequency, community, lldp_chassis_id, mgmt_v6_addr, placement,subnet4,subnet6,distro_name) VALUES ($template{'mgmt_v4_addr'}, $template{'sysname'}, $template{'poll_frequency'}, $template{'community'}, $template{'lldp_chassis_id'}, $template{'mgmt_v6_addr'}, $template{'placement'},$template{'subnet4'},$template{'subnet6'},$template{'distro_name'});"); push @added, $switch{'sysname'}; + oplog("\"" . $switch{'sysname'} . "\"", "Switch added: " . $switch{'sysname'}); } } $json{'switches_addded'} = \@added; -print "X-ban: /api/.*switches.*\n"; +print "X-ban: /api/.*\n"; finalize_output(); |