aboutsummaryrefslogtreecommitdiffstats
path: root/web/api
diff options
context:
space:
mode:
authorKristian Lyngstol <kristian@bohemians.org>2016-04-12 18:01:32 +0200
committerKristian Lyngstol <kristian@bohemians.org>2016-04-12 18:01:32 +0200
commit09ea945c3908fd42e90eb64c194d9af11d174206 (patch)
tree8bcf0f98113ea4a92effa1c39344730c7e6f50b0 /web/api
parentc3ebdbda43c23ade1e49dfd9f546fba1d15f9941 (diff)
Actual initial import
Fetched from tgmanage.
Diffstat (limited to 'web/api')
-rw-r--r--web/api/API.rst131
-rwxr-xr-xweb/api/public/dhcp20
-rwxr-xr-xweb/api/public/dhcp-summary16
-rwxr-xr-xweb/api/public/location44
-rwxr-xr-xweb/api/public/ping37
-rwxr-xr-xweb/api/public/switch-state97
-rwxr-xr-xweb/api/public/switches36
-rwxr-xr-xweb/api/read/comments15
-rwxr-xr-xweb/api/read/snmp30
-rwxr-xr-xweb/api/read/switches-management30
-rwxr-xr-xweb/api/write/comment-add24
-rwxr-xr-xweb/api/write/comment-change25
-rwxr-xr-xweb/api/write/switch-add92
-rwxr-xr-xweb/api/write/switch-update123
14 files changed, 720 insertions, 0 deletions
diff --git a/web/api/API.rst b/web/api/API.rst
new file mode 100644
index 0000000..7d5c88b
--- /dev/null
+++ b/web/api/API.rst
@@ -0,0 +1,131 @@
+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.
+
+Public
+......
+
+Use URL https://nms-public.tg16.gathering.org/api/public
+
+/api/public/dhcp
+----------------
+
+Methods: GET
+Update frequency: every second or so.
+
+Used to report time since last dhcp lease.
+
+/api/public/dhcp-summary
+------------------------
+
+Methods: GET
+Update frequency: every second or so.
+
+Used to report dhcp lease stats. Both recent stats, and total stats.
+
+/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.
+
+
+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.
diff --git a/web/api/public/dhcp b/web/api/public/dhcp
new file mode 100755
index 0000000..7b048d1
--- /dev/null
+++ b/web/api/public/dhcp
@@ -0,0 +1,20 @@
+#! /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;
+
+nms::web::setwhen('60m');
+my $q = $nms::web::dbh->prepare('select distinct on (sysname) extract(epoch from date_trunc(\'second\',time)) as time,sysname from dhcp join switches on dhcp.switch = switches.switch where ' . $nms::web::when . ' order by sysname,time desc;');
+$q->execute();
+while ( my $ref = $q->fetchrow_hashref() ) {
+ my $sysname = $ref->{'sysname'};
+ $json{'dhcp'}{$ref->{'sysname'}} = $ref->{'time'};
+}
+
+$nms::web::cc{'max-age'} = "10";
+$nms::web::cc{'stale-while-revalidate'} = "30";
+finalize_output();
diff --git a/web/api/public/dhcp-summary b/web/api/public/dhcp-summary
new file mode 100755
index 0000000..a0e5609
--- /dev/null
+++ b/web/api/public/dhcp-summary
@@ -0,0 +1,16 @@
+#! /usr/bin/perl
+use lib '../../../../include';
+use strict;
+use warnings;
+use nms::web;
+
+nms::web::setwhen('2h');
+
+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'} = $ref;
+}
+$nms::web::cc{'max-age'} = "10";
+$nms::web::cc{'stale-while-revalidate'} = "15";
+finalize_output();
diff --git a/web/api/public/location b/web/api/public/location
new file mode 100755
index 0000000..b940007
--- /dev/null
+++ b/web/api/public/location
@@ -0,0 +1,44 @@
+#! /usr/bin/perl
+# vim:ts=8:sw=8
+
+use lib '../../../../include';
+use nms::web;
+use strict;
+use warnings;
+use Data::Dumper;
+
+#my $query = $nms::web::dbh->prepare("select * from switches where '185.110.150.7' << subnet4");
+my $query = $nms::web::dbh->prepare("select * from switches where ? << subnet4 or ? << subnet6");
+
+print "Cache-Control: max-age=0";
+print "Content-Type: text/html";
+print "\n\n";
+
+# get user ip from somewhere.
+# HTTP_X_FORWARDED_FOR is set by varnish. When using varnish, the REMOTE_ADDR will always be localhost.
+
+my @xff = split(",",$ENV{HTTP_X_FORWARDED_FOR});
+my $addr = $xff[0] // $ENV{REMOTE_ADDR};
+
+$query->execute($addr,$addr);
+
+# add start html: header + body etc.
+print "
+<html>
+ <body style=\"text-align: center; font-size: 50pt;\">
+";
+
+# print address
+print "" . $addr ."<br \>";
+
+# print switch name and distroname.
+while ( my $ref = $query->fetchrow_hashref() ) {
+ print $ref->{sysname}. " @ " . $ref->{distro};
+ print "<br />";
+}
+
+# add end html
+print "
+ </body>
+</html>
+";
diff --git a/web/api/public/ping b/web/api/public/ping
new file mode 100755
index 0000000..811708d
--- /dev/null
+++ b/web/api/public/ping
@@ -0,0 +1,37 @@
+#! /usr/bin/perl
+use lib '../../../../include';
+use strict;
+use warnings;
+use nms::web;
+
+nms::web::setwhen('15s');
+
+my $q = $nms::web::dbh->prepare("SELECT DISTINCT ON (sysname) (" . $nms::web::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'} = "15";
+finalize_output();
diff --git a/web/api/public/switch-state b/web/api/public/switch-state
new file mode 100755
index 0000000..a6caea9
--- /dev/null
+++ b/web/api/public/switch-state
@@ -0,0 +1,97 @@
+#! /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;
+ if ($porti =~ m/\.0$/) {
+ next;
+ }
+ if (not $smallport =~ m/^ae/ and not $smallport =~ m/^et/) {
+ $smallport =~ s/[0-9-].*$//;
+ } else {
+ $json{'switches'}{$sysname}{ifs}{$smallport}{'ifAlias'} = $port{'ifAlias'};
+ }
+ 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}{'temp'} = $data{'misc'}{'jnxOperatingTemp'}{'7.1.0.0'};
+ $json{'switches'}{$sysname}{'time'} = $ref->{'time'};
+}
+
+nms::web::setwhen('15m','10m');
+my $q2 = $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);');
+
+$q2->execute();
+while ( my $ref = $q2->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;
+ if ($porti =~ m/\.0$/) {
+ next;
+ }
+ if (not $smallport =~ m/^ae/ and not $smallport =~ m/^et/) {
+ $smallport =~ s/[0-9-].*$//;
+ } else {
+ $json{'then'}{$sysname}{ifs}{$smallport}{'ifAlias'} = $port{'ifAlias'};
+ }
+ if ($porti =~ /ge-0\/0\/4[4-7]$/) {
+ $json{'then'}{$sysname}{'uplinks'}{'ifHCInOctets'} += $port{'ifHCInOctets'};
+ $json{'then'}{$sysname}{'uplinks'}{'ifHCOutOctets'} += $port{'ifHCOutOctets'};
+ if ($port{'ifOperStatus'} eq "up") {
+ $json{'then'}{$sysname}{'uplinks'}{'live'} += 1;
+ }
+ $json{'then'}{$sysname}{'uplinks'}{'total'} += 1;
+ }
+
+ $json{'then'}{$sysname}{ifs}{$smallport}{'ifHCInOctets'} += $port{'ifHCInOctets'};
+ $json{'then'}{$sysname}{ifs}{$smallport}{'ifHCOutOctets'} += $port{'ifHCOutOctets'};
+ $json{'then'}{$sysname}{totals}{'ifHCInOctets'} += $port{'ifHCInOctets'};
+ $json{'then'}{$sysname}{totals}{'ifHCOutOctets'} += $port{'ifHCOutOctets'};
+ if ($port{'ifOperStatus'} eq "up") {
+ $json{'then'}{$sysname}{ifs}{$smallport}{'live'} += 1;
+ $json{'then'}{$sysname}{totals}{'live'} += 1;
+ }
+ $json{'then'}{$sysname}{totals}{'total'} += 1;
+ }
+ $json{'then'}{$sysname}{'temp'} = $data{'misc'}{'enterprises.2636.3.1.13.1.7.7.1.0.0'}{''};
+ $json{'then'}{$sysname}{'time'} = $ref->{'time'};
+}
+$nms::web::cc{'max-age'} = "5";
+$nms::web::cc{'stale-while-revalidate'} = "30";
+finalize_output();
diff --git a/web/api/public/switches b/web/api/public/switches
new file mode 100755
index 0000000..890b7d7
--- /dev/null
+++ b/web/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,distro_name,placement,mgmt_v4_addr,mgmt_v6_addr,mgmt_v4_gw,mgmt_v6_gw,mgmt_vlan,traffic_vlan,last_config_fetch,current_mac,poll_frequency,community,last_updated,switchtype 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;
+ $nms::web::json{'switches'}{$ref->{'sysname'}}{'distro_name'} = $ref->{'distro_name'};
+}
+
+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();
diff --git a/web/api/read/comments b/web/api/read/comments
new file mode 100755
index 0000000..4adfb2b
--- /dev/null
+++ b/web/api/read/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/api/read/snmp b/web/api/read/snmp
new file mode 100755
index 0000000..f69ca62
--- /dev/null
+++ b/web/api/read/snmp
@@ -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 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/api/read/switches-management b/web/api/read/switches-management
new file mode 100755
index 0000000..4461a90
--- /dev/null
+++ b/web/api/read/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 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');
+
+$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/api/write/comment-add b/web/api/write/comment-add
new file mode 100755
index 0000000..bcea6dc
--- /dev/null
+++ b/web/api/write/comment-add
@@ -0,0 +1,24 @@
+#! /usr/bin/perl
+# vim:ts=8:sw=8
+use lib '../../../../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 $data = $dbh->quote($tmp{'comment'});
+my $switch = $dbh->quote($tmp{'switch'});
+my $user = $dbh->quote($ENV{'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';
+
+print "X-ban: /api/read/comments\n";
+finalize_output();
diff --git a/web/api/write/comment-change b/web/api/write/comment-change
new file mode 100755
index 0000000..0bdabc0
--- /dev/null
+++ b/web/api/write/comment-change
@@ -0,0 +1,25 @@
+#! /usr/bin/perl
+# vim:ts=8:sw=8
+use lib '../../../../include';
+use utf8;
+use nms;
+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 $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();
+
+$nms::web::cc{'max-age'} = '0';
+$nms::web::cc{'stale-while-revalidate'} = '0';
+$nms::web::json{'state'} = 'ok';
+
+print "X-ban: /api/read/comments\n";
+finalize_output();
diff --git a/web/api/write/switch-add b/web/api/write/switch-add
new file mode 100755
index 0000000..0051111
--- /dev/null
+++ b/web/api/write/switch-add
@@ -0,0 +1,92 @@
+#! /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 $dbh);
+use nms::util qw(guess_placement);
+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 @fields = ( 'community', 'current_mac', 'distro_name', 'distro_phy_port', 'lldp_chassis_id', 'mgmt_v4_addr', 'mgmt_v4_gw', 'mgmt_v6_addr', 'mgmt_v6_gw', 'mgmt_vlan', 'placement', 'poll_frequency', 'subnet4', 'subnet6', 'switchtype', 'sysname', 'traffic_vlan');
+
+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};
+ my $affected = 0;
+ my %template = ();
+ map { $template{$_} = 'DEFAULT' } @fields;
+ if (not defined($switch{'sysname'})) {
+ next;
+ }
+
+ $sth->execute( $switch{'sysname'});
+ while ( my @row = $sth->fetchrow_array ) {
+ $affected += 1;
+ }
+
+ 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'};
+ }
+ if (not defined($switch{'secondary_ip'}) and defined($switch{'mgtmt6'})) {
+ $switch{'secondary_ip'} = $switch{'mgtmt6'};
+ }
+ 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{$_});
+ }
+ } keys %switch;
+
+
+ $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'};
+ }
+}
+
+$json{'switches_addded'} = \@added;
+
+print "X-ban: /api/.*switches.*\n";
+finalize_output();
diff --git a/web/api/write/switch-update b/web/api/write/switch-update
new file mode 100755
index 0000000..14c8773
--- /dev/null
+++ b/web/api/write/switch-update
@@ -0,0 +1,123 @@
+#! /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 $dbh);
+use nms::util qw(guess_placement);
+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 @fields = ( 'community', 'current_mac', 'distro_name', 'distro_phy_port', 'lldp_chassis_id', 'mgmt_v4_addr', 'mgmt_v4_gw', 'mgmt_v4_netsize', 'mgmt_v6_addr', 'mgmt_v6_gw', 'mgmt_v6_netsize', 'mgmt_vlan', 'placement', 'poll_frequency', 'subnet4', 'subnet6', 'switchtype', 'sysname', 'traffic_vlan');
+
+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};
+ my $affected = 0;
+ my %template = ();
+ map { $template{$_} = 'DEFAULT' } @fields;
+ if (not defined($switch{'sysname'})) {
+ next;
+ }
+
+ $sth->execute( $switch{'sysname'});
+ while ( my @row = $sth->fetchrow_array ) {
+ $affected += 1;
+ }
+
+ 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'};
+ }
+ if (not defined($switch{'secondary_ip'}) and defined($switch{'mgtmt6'})) {
+ $switch{'secondary_ip'} = $switch{'mgtmt6'};
+ }
+ 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{$_});
+ }
+ } keys %switch;
+
+
+ $nms::web::dbh->do("INSERT INTO SWITCHES (ip, sysname, last_updated, locked, poll_frequency, community, lldp_chassis_id, secondary_ip, placement,subnet4,subnet6,distro) VALUES ($template{'ip'}, $template{'sysname'}, $template{'last_updated'}, $template{'locked'}, $template{'poll_frequency'}, $template{'community'}, $template{'lldp_chassis_id'}, $template{'secondary_ip'}, $template{'placement'},$template{'subnet4'},$template{'subnet6'},$template{'distro'});");
+ 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 = $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'};
+ }
+ if (not defined($switch{'secondary_ip'}) and defined($switch{'mgtmt6'})) {
+ $switch{'secondary_ip'} = $switch{'mgtmt6'};
+ }
+ my @set;
+ map {
+ 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'};
+ }
+}
+$json{'switches_addded'} = \@added;
+$json{'switches_updated'} = \@dups;
+
+print "X-ban: /api/.*switches.*\n";
+finalize_output();