From 490560f31c20dd8191ee1a0f8f6e90b2820d7c77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20Kir=C3=B8?= Date: Tue, 22 Mar 2016 13:03:34 +0100 Subject: API structure change. --- web/nms.gathering.org/api/private/comment-add | 24 ---- web/nms.gathering.org/api/private/comment-change | 24 ---- web/nms.gathering.org/api/private/comments | 15 --- web/nms.gathering.org/api/private/snmp | 30 ----- web/nms.gathering.org/api/private/switch-add | 92 ---------------- web/nms.gathering.org/api/private/switch-update | 122 --------------------- .../api/private/switches-management | 30 ----- .../api/private/which-switch-am-i-on | 40 ------- web/nms.gathering.org/api/read/comments | 15 +++ web/nms.gathering.org/api/read/snmp | 30 +++++ web/nms.gathering.org/api/read/switches-management | 30 +++++ .../api/read/which-switch-am-i-on | 40 +++++++ web/nms.gathering.org/api/write/comment-add | 24 ++++ web/nms.gathering.org/api/write/comment-change | 24 ++++ web/nms.gathering.org/api/write/switch-add | 92 ++++++++++++++++ web/nms.gathering.org/api/write/switch-update | 122 +++++++++++++++++++++ 16 files changed, 377 insertions(+), 377 deletions(-) delete mode 100755 web/nms.gathering.org/api/private/comment-add delete mode 100755 web/nms.gathering.org/api/private/comment-change delete mode 100755 web/nms.gathering.org/api/private/comments delete mode 100755 web/nms.gathering.org/api/private/snmp delete mode 100755 web/nms.gathering.org/api/private/switch-add delete mode 100755 web/nms.gathering.org/api/private/switch-update delete mode 100755 web/nms.gathering.org/api/private/switches-management delete mode 100755 web/nms.gathering.org/api/private/which-switch-am-i-on create mode 100755 web/nms.gathering.org/api/read/comments create mode 100755 web/nms.gathering.org/api/read/snmp create mode 100755 web/nms.gathering.org/api/read/switches-management create mode 100755 web/nms.gathering.org/api/read/which-switch-am-i-on create mode 100755 web/nms.gathering.org/api/write/comment-add create mode 100755 web/nms.gathering.org/api/write/comment-change create mode 100755 web/nms.gathering.org/api/write/switch-add create mode 100755 web/nms.gathering.org/api/write/switch-update (limited to 'web/nms.gathering.org/api') diff --git a/web/nms.gathering.org/api/private/comment-add b/web/nms.gathering.org/api/private/comment-add deleted file mode 100755 index 26ff734..0000000 --- a/web/nms.gathering.org/api/private/comment-add +++ /dev/null @@ -1,24 +0,0 @@ -#! /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/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 deleted file mode 100755 index fb7da54..0000000 --- a/web/nms.gathering.org/api/private/comment-change +++ /dev/null @@ -1,24 +0,0 @@ -#! /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'; - -finalize_output(); diff --git a/web/nms.gathering.org/api/private/comments b/web/nms.gathering.org/api/private/comments deleted file mode 100755 index 4adfb2b..0000000 --- a/web/nms.gathering.org/api/private/comments +++ /dev/null @@ -1,15 +0,0 @@ -#! /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/snmp b/web/nms.gathering.org/api/private/snmp deleted file mode 100755 index f69ca62..0000000 --- a/web/nms.gathering.org/api/private/snmp +++ /dev/null @@ -1,30 +0,0 @@ -#! /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 deleted file mode 100755 index 48f62fc..0000000 --- a/web/nms.gathering.org/api/private/switch-add +++ /dev/null @@ -1,92 +0,0 @@ -#! /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 = ('ip', 'sysname', 'last_updated', 'locked', 'poll_frequency', 'community', 'lldp_chassis_id', 'secondary_ip', 'subnet4', 'subnet6', 'placement', 'distro', 'secondary_ip'); - -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'}; - } -} - -$json{'switches_addded'} = \@added; - -print "X-ban: /api/.*switches.*\n"; -finalize_output(); diff --git a/web/nms.gathering.org/api/private/switch-update b/web/nms.gathering.org/api/private/switch-update deleted file mode 100755 index 6507d2b..0000000 --- a/web/nms.gathering.org/api/private/switch-update +++ /dev/null @@ -1,122 +0,0 @@ -#! /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 = ('ip', 'sysname', 'last_updated', 'locked', 'poll_frequency', 'community', 'lldp_chassis_id', 'secondary_ip', 'subnet4', 'subnet6', 'placement', 'distro', 'secondary_ip'); - -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(); diff --git a/web/nms.gathering.org/api/private/switches-management b/web/nms.gathering.org/api/private/switches-management deleted file mode 100755 index 6b5fdcd..0000000 --- a/web/nms.gathering.org/api/private/switches-management +++ /dev/null @@ -1,30 +0,0 @@ -#! /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,poll_frequency,community,subnet4,subnet6,distro,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/private/which-switch-am-i-on b/web/nms.gathering.org/api/private/which-switch-am-i-on deleted file mode 100755 index 0d3f2b4..0000000 --- a/web/nms.gathering.org/api/private/which-switch-am-i-on +++ /dev/null @@ -1,40 +0,0 @@ -#! /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 '88.92.5.67' << subnet4"); -$query->execute(); - -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 $addr = $ENV{HTTP_X_FORWARDED_FOR} // $ENV{REMOTE_ADDR}; - -# add start html: header + body etc. -print " - - -"; - -# print address -print "" . $addr ."
"; - -# print switch name and distroname. -while ( my $ref = $query->fetchrow_hashref() ) { - print $ref->{sysname}. " @ " . $ref->{distro}; -} - -# add end html -print " - - -"; diff --git a/web/nms.gathering.org/api/read/comments b/web/nms.gathering.org/api/read/comments new file mode 100755 index 0000000..4adfb2b --- /dev/null +++ b/web/nms.gathering.org/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/nms.gathering.org/api/read/snmp b/web/nms.gathering.org/api/read/snmp new file mode 100755 index 0000000..f69ca62 --- /dev/null +++ b/web/nms.gathering.org/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/nms.gathering.org/api/read/switches-management b/web/nms.gathering.org/api/read/switches-management new file mode 100755 index 0000000..6b5fdcd --- /dev/null +++ b/web/nms.gathering.org/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 sysname,ip,poll_frequency,community,subnet4,subnet6,distro,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/read/which-switch-am-i-on b/web/nms.gathering.org/api/read/which-switch-am-i-on new file mode 100755 index 0000000..0d3f2b4 --- /dev/null +++ b/web/nms.gathering.org/api/read/which-switch-am-i-on @@ -0,0 +1,40 @@ +#! /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 '88.92.5.67' << subnet4"); +$query->execute(); + +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 $addr = $ENV{HTTP_X_FORWARDED_FOR} // $ENV{REMOTE_ADDR}; + +# add start html: header + body etc. +print " + + +"; + +# print address +print "" . $addr ."
"; + +# print switch name and distroname. +while ( my $ref = $query->fetchrow_hashref() ) { + print $ref->{sysname}. " @ " . $ref->{distro}; +} + +# add end html +print " + + +"; diff --git a/web/nms.gathering.org/api/write/comment-add b/web/nms.gathering.org/api/write/comment-add new file mode 100755 index 0000000..26ff734 --- /dev/null +++ b/web/nms.gathering.org/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/private/comments\n"; +finalize_output(); diff --git a/web/nms.gathering.org/api/write/comment-change b/web/nms.gathering.org/api/write/comment-change new file mode 100755 index 0000000..fb7da54 --- /dev/null +++ b/web/nms.gathering.org/api/write/comment-change @@ -0,0 +1,24 @@ +#! /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'; + +finalize_output(); diff --git a/web/nms.gathering.org/api/write/switch-add b/web/nms.gathering.org/api/write/switch-add new file mode 100755 index 0000000..48f62fc --- /dev/null +++ b/web/nms.gathering.org/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 = ('ip', 'sysname', 'last_updated', 'locked', 'poll_frequency', 'community', 'lldp_chassis_id', 'secondary_ip', 'subnet4', 'subnet6', 'placement', 'distro', 'secondary_ip'); + +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'}; + } +} + +$json{'switches_addded'} = \@added; + +print "X-ban: /api/.*switches.*\n"; +finalize_output(); diff --git a/web/nms.gathering.org/api/write/switch-update b/web/nms.gathering.org/api/write/switch-update new file mode 100755 index 0000000..6507d2b --- /dev/null +++ b/web/nms.gathering.org/api/write/switch-update @@ -0,0 +1,122 @@ +#! /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 = ('ip', 'sysname', 'last_updated', 'locked', 'poll_frequency', 'community', 'lldp_chassis_id', 'secondary_ip', 'subnet4', 'subnet6', 'placement', 'distro', 'secondary_ip'); + +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(); -- cgit v1.2.3