diff options
Diffstat (limited to 'web/api')
-rwxr-xr-x | web/api/public/switches | 2 | ||||
-rw-r--r-- | web/api/read/networks | 37 | ||||
-rwxr-xr-x | web/api/read/switches-management | 4 | ||||
-rw-r--r-- | web/api/write/network-add | 49 | ||||
-rw-r--r-- | web/api/write/network-update | 114 | ||||
-rwxr-xr-x | web/api/write/switch-add | 8 |
6 files changed, 207 insertions, 7 deletions
diff --git a/web/api/public/switches b/web/api/public/switches index 44fde3c..8e7f333 100755 --- a/web/api/public/switches +++ b/web/api/public/switches @@ -13,7 +13,7 @@ use Data::Dumper; $nms::web::cc{'max-age'} = "5"; $nms::web::cc{'stale-while-revalidate'} = "30"; -my $q2 = $nms::web::dbh->prepare('select switch,sysname,tags,distro_name,placement,mgmt_v4_addr,mgmt_v6_addr,mgmt_v4_gw,mgmt_v6_gw,mgmt_vlan,traffic_vlan,poll_frequency,community,last_updated from switches where placement is not null and deleted = false'); +my $q2 = $nms::web::dbh->prepare('select switch, sysname, tags, distro_name, placement, last_updated from switches where placement is not null and deleted = false'); $q2->execute(); while (my $ref = $q2->fetchrow_hashref()) { diff --git a/web/api/read/networks b/web/api/read/networks new file mode 100644 index 0000000..870ae8c --- /dev/null +++ b/web/api/read/networks @@ -0,0 +1,37 @@ +#! /usr/bin/perl +# vim:ts=8:sw=8 + +use CGI qw(fatalsToBrowser); +use DBI; +use lib '/opt/gondul/include'; +use nms; +use nms::web; +use strict; +use warnings; +use Data::Dumper; + +$nms::web::cc{'max-age'} = "5"; +$nms::web::cc{'stale-while-revalidate'} = "30"; + +my $q2 = $nms::web::dbh->prepare('select network, name, vlan, tags, routing_point, placement, last_updated, subnet4, subnet6, gw4, gw6 from networks'); + +$q2->execute(); +while (my $ref = $q2->fetchrow_hashref()) { + $ref->{'placement'} =~ /\((-?\d+),(-?\d+)\),\((-?\d+),(-?\d+)\)/; + my ($x1, $y1, $x2, $y2) = ($1, $2, $3, $4); + my $name = $ref->{'name'}; + $nms::web::json{'networks'}{$ref->{'name'}}{'vlan'} = $ref->{'vlan'}; + $nms::web::json{'networks'}{$ref->{'name'}}{'subnet4'} = $ref->{'subnet4'}; + $nms::web::json{'networks'}{$ref->{'name'}}{'subnet6'} = $ref->{'subnet6'}; + $nms::web::json{'networks'}{$ref->{'name'}}{'gw4'} = $ref->{'gw4'}; + $nms::web::json{'networks'}{$ref->{'name'}}{'gw6'} = $ref->{'gw6'}; + $nms::web::json{'networks'}{$ref->{'name'}}{'placement'}{'x'} = $x2; + $nms::web::json{'networks'}{$ref->{'name'}}{'placement'}{'y'} = $y2; + $nms::web::json{'networks'}{$ref->{'name'}}{'placement'}{'width'} = $x1 - $x2; + $nms::web::json{'networks'}{$ref->{'name'}}{'placement'}{'height'} = $y1 - $y2; + $nms::web::json{'networks'}{$ref->{'name'}}{'routing_point'} = $ref->{'routing_point'}; + my $data = JSON::XS::decode_json($ref->{'tags'}); + $nms::web::json{'networks'}{$ref->{'name'}}{'tags'} = $data; +} + +finalize_output(); diff --git a/web/api/read/switches-management b/web/api/read/switches-management index 9182b60..77badf4 100755 --- a/web/api/read/switches-management +++ b/web/api/read/switches-management @@ -20,9 +20,9 @@ $nms::web::cc{'max-age'} = "5"; $nms::web::cc{'stale-while-revalidate'} = "30"; if (!defined($switch)) { - $q2 = $nms::web::dbh->prepare('select sysname,host(mgmt_v4_addr) as mgmt_v4_addr,subnet4,subnet6,host(mgmt_v6_addr) as mgmt_v6_addr,host(mgmt_v4_gw) as mgmt_v4_gw,host(mgmt_v6_gw) as mgmt_v6_gw,mgmt_vlan,traffic_vlan,poll_frequency,last_updated,distro_name,distro_phy_port,community from switches where placement is not null and deleted = false'); + $q2 = $nms::web::dbh->prepare('select switches.sysname, host(switches.mgmt_v4_addr) as mgmt_v4_addr, host(switches.mgmt_v6_addr) as mgmt_v6_addr, switches.mgmt_vlan, switches.traffic_vlan, switches.poll_frequency, switches.last_updated, switches.distro_name, switches.distro_phy_port, switches.community, traffic_net.subnet4, traffic_net.subnet6, mgmt_net.gw4 as mgmt_v4_gw, mgmt_net.gw6 as mgmt_v6_gw from switches left join networks as traffic_net on (switches.traffic_vlan = traffic_net.name) left join networks as mgmt_net on (switches.mgmt_vlan = mgmt_net.name) where switches.placement is not null and switches.deleted = false;'); } else { - $q2 = $nms::web::dbh->prepare('select sysname,host(mgmt_v4_addr) as mgmt_v4_addr,subnet4,subnet6,host(mgmt_v6_addr) as mgmt_v6_addr,host(mgmt_v4_gw) as mgmt_v4_gw,host(mgmt_v6_gw) as mgmt_v6_gw,mgmt_vlan,traffic_vlan,poll_frequency,last_updated,distro_name,distro_phy_port,community from switches where placement is not null and deleted = false and sysname = ' . $nms::web::dbh->quote($switch) . ';'); + $q2 = $nms::web::dbh->prepare('select sysname,host(mgmt_v4_addr) as mgmt_v4_addr,host(mgmt_v6_addr) as mgmt_v6_addr,mgmt_vlan,traffic_vlan,poll_frequency,last_updated,distro_name,distro_phy_port,community from switches where placement is not null and sysname = ' . $nms::web::dbh->quote($switch) . ' and switches.deleted = false;'); } $q2->execute(); diff --git a/web/api/write/network-add b/web/api/write/network-add new file mode 100644 index 0000000..d1aab4e --- /dev/null +++ b/web/api/write/network-add @@ -0,0 +1,49 @@ +#! /usr/bin/perl +# vim:ts=8:sw=8 + +#use CGI qw(fatalsToBrowser); +use DBI; +use lib '/opt/gondul/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; +use nms::oplog qw(oplog); + +$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 name FROM networks WHERE name=?"); + +foreach my $tmp2 (@tmp) { + my %network = %{$tmp2}; + my $affected = 0; + my %template = (); + if (not defined($network{'name'})) { + next; + } + + $sth->execute( $network{'name'}); + while ( my @row = $sth->fetchrow_array ) { + $affected += 1; + } + + if ($affected == 0) { + $nms::web::dbh->do("INSERT INTO NETWORKS (name) VALUES ('$network{'name'}');"); + push @added, $network{'name'}; + oplog("\"" . $network{'name'} . "\"", "Network added: " . $network{'name'}); + } +} + +$json{'networks_addded'} = \@added; + +print "X-ban: /api/.*\n"; +finalize_output(); diff --git a/web/api/write/network-update b/web/api/write/network-update new file mode 100644 index 0000000..f6684c5 --- /dev/null +++ b/web/api/write/network-update @@ -0,0 +1,114 @@ +#! /usr/bin/perl +# vim:ts=8:sw=8 + +#use CGI qw(fatalsToBrowser); +use DBI; +use lib '/opt/gondul/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 name FROM networks WHERE name=?"); + + +my @fields = ('name','last_updated','placement','subnet4','subnet6','gw4','gw6','routing_point','vlan','tags'); + +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 %network = %{$tmp2}; + my $affected = 0; + my %template = (); + map { $template{$_} = 'DEFAULT' } @fields; + if (not defined($network{'sysname'})) { + next; + } + + $sth->execute( $network{'sysname'}); + while ( my @row = $sth->fetchrow_array ) { + $affected += 1; + } + + if ($affected == 0) { + my %placement; + if (not defined ($network{'placement'})) { + %placement = guess_placement($network{'sysname'}); + } else { + %placement = %{convertplace($network{'sysname'})}; + } + my ($x1,$x2,$y1,$y2); + $x1 = $placement{'x1'}; + $y1 = $placement{'y1'}; + $x2 = $placement{'xx'}; + $y2 = $placement{'yy'}; + $network{'placement'} = "(($x1,$y1),($x2,$y2))"; + + map { + if (defined ($template{$_})) { + $template{$_} = $dbh->quote($network{$_}); + } + } keys %network; + + + $nms::web::dbh->do("INSERT INTO NETWORKS (name, last_updated, placement, subnet4, subnet6, routing_point, gw4, gw6, vlan, tags) VALUES ($template{'sysname'}, $template{'last_updated'}, $template{'placement'}, $template{'subnet4'}, $template{'subnet6'}, $template{'routing_point'}, $template{'gw4'}, $template{'gw6'}, $template{'vlan'}, $template{'tags'});"); + push @added, $network{'sysname'}; + } else { + if (defined($network{'placement'})) { + my %placement; + if ($network{'placement'} eq "reset") { + %placement = guess_placement($network{'sysname'}); + } else { + %placement = %{convertplace($network{'placement'})}; + } + my ($x1,$x2,$y1,$y2); + $x1 = $placement{'x1'}; + $y1 = $placement{'y1'}; + $x2 = $placement{'xx'}; + $y2 = $placement{'yy'}; + $network{'placement'} = "(($x1,$y1),($x2,$y2))"; + push @dups, "not really, but: " . $network{'placement'}; + } + if (defined($network{'tags'})) { + $network{'tags'} =~ s/'/"/g; + } + my @set; + map { + if (defined($template{$_})) { + push @set, "$_=" . $dbh->quote($network{$_}); + } + } keys %network; + $nms::web::dbh->do("UPDATE NETWORKS SET " . join(", ", @set) . "WHERE name=" . $dbh->quote($network{'sysname'}) . ";"); + push @dups, $network{'sysname'}; + } +} +$json{'networks_addded'} = \@added; +$json{'networks_updated'} = \@dups; + +print "X-ban: /api/.*networks.*\n"; +finalize_output(); diff --git a/web/api/write/switch-add b/web/api/write/switch-add index dbaa1e5..c92cf31 100755 --- a/web/api/write/switch-add +++ b/web/api/write/switch-add @@ -23,7 +23,7 @@ my @dups; my $sth = $nms::web::dbh->prepare("SELECT sysname FROM switches WHERE sysname=?"); -my @fields = ( 'community', 'distro_name', 'distro_phy_port', 'mgmt_v4_addr', 'mgmt_v4_gw', 'mgmt_v6_addr', 'mgmt_v6_gw', 'mgmt_vlan', 'placement', 'poll_frequency', 'subnet4', 'subnet6', 'sysname', 'traffic_vlan'); +my @fields = ( 'community', 'distro_name', 'distro_phy_port', 'mgmt_v4_addr', 'mgmt_v6_addr', 'mgmt_vlan', 'placement', 'poll_frequency', 'sysname', 'traffic_vlan'); sub convertplace { @@ -74,15 +74,15 @@ foreach my $tmp2 (@tmp) { $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, mgmt_v6_addr, placement,subnet4,subnet6,distro_name) VALUES ($template{'mgmt_v4_addr'}, $template{'sysname'}, $template{'poll_frequency'}, $template{'community'}, $template{'mgmt_v6_addr'}, $template{'placement'},$template{'subnet4'},$template{'subnet6'},$template{'distro_name'});"); + + $nms::web::dbh->do("INSERT INTO SWITCHES (mgmt_v4_addr, sysname, poll_frequency, community, mgmt_v6_addr, placement, distro_name) VALUES ($template{'mgmt_v4_addr'}, $template{'sysname'}, $template{'poll_frequency'}, $template{'community'}, $template{'mgmt_v6_addr'}, $template{'placement'}, $template{'distro_name'});"); push @added, $switch{'sysname'}; oplog("\"" . $switch{'sysname'} . "\"", "Switch added: " . $switch{'sysname'}); } |