diff options
Diffstat (limited to 'web/api')
-rwxr-xr-x | web/api/public/distro-tree | 8 | ||||
-rwxr-xr-x | web/api/public/switches | 7 | ||||
-rwxr-xr-x | web/api/read/distro-management | 31 | ||||
-rwxr-xr-x | web/api/read/networks | 15 | ||||
-rwxr-xr-x | web/api/read/switches-management | 27 | ||||
-rwxr-xr-x | web/api/read/template-list | 8 | ||||
-rwxr-xr-x | web/api/write/linknet-add | 32 | ||||
-rwxr-xr-x | web/api/write/network-add | 49 | ||||
-rwxr-xr-x | web/api/write/networks (renamed from web/api/write/network-update) | 21 | ||||
-rwxr-xr-x | web/api/write/switch-add | 94 | ||||
-rwxr-xr-x | web/api/write/switches (renamed from web/api/write/switch-update) | 2 |
11 files changed, 45 insertions, 249 deletions
diff --git a/web/api/public/distro-tree b/web/api/public/distro-tree index cff35ec..49ce607 100755 --- a/web/api/public/distro-tree +++ b/web/api/public/distro-tree @@ -10,16 +10,18 @@ use strict; use warnings; use Data::Dumper; -$nms::web::cc{'max-age'} = "20"; +$nms::web::cc{'max-age'} = "5"; +$nms::web::cc{'stale-while-revalidate'} = "30"; my $q2 = $nms::web::dbh->prepare('select sysname,distro_phy_port,distro_name from switches where placement is not null and distro_name is not null and distro_phy_port is not null and deleted = false'); $q2->execute(); while (my $ref = $q2->fetchrow_hashref()) { my $sysname = $ref->{'sysname'}; - my $phy = $ref->{'distro_phy_port'}; my $distro = $ref->{'distro_name'}; - $nms::web::json{'distro-tree'}{$distro}{$phy} = $sysname; + my $phy = $ref->{'distro_phy_port'}; + $nms::web::json{'distro-tree-phy'}{$distro}{$phy} = $sysname; + $nms::web::json{'distro-tree-sys'}{$distro}{$sysname} = $phy; } finalize_output(); diff --git a/web/api/public/switches b/web/api/public/switches index 8e7f333..9bb63d8 100755 --- a/web/api/public/switches +++ b/web/api/public/switches @@ -29,11 +29,4 @@ while (my $ref = $q2->fetchrow_hashref()) { $nms::web::json{'switches'}{$ref->{'sysname'}}{'tags'} = $data; } -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/distro-management b/web/api/read/distro-management deleted file mode 100755 index 55ca07e..0000000 --- a/web/api/read/distro-management +++ /dev/null @@ -1,31 +0,0 @@ -#! /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; - -my $target = $ENV{REQUEST_URI}; -$target =~ s/$ENV{SCRIPT_NAME}//; -$target =~ s/^\///; -my ($switch, $port) = split(/\//,$target,2); -my $q2; - -$nms::web::cc{'max-age'} = "5"; -$nms::web::cc{'stale-while-revalidate'} = "30"; - -$q2 = $nms::web::dbh->prepare('SELECT distro_name,sysname,distro_phy_port FROM switches WHERE placement is not null AND distro_name is not null AND distro_phy_port is not null and deleted = false'); - -$q2->execute(); -while (my $ref = $q2->fetchrow_hashref()) { - my $sysname = $ref->{'sysname'}; - my $distro = $ref->{'distro_name'}; - $nms::web::json{'distros'}{$distro}{$sysname} = $ref; -} - -finalize_output(); diff --git a/web/api/read/networks b/web/api/read/networks index 870ae8c..0fbdb15 100755 --- a/web/api/read/networks +++ b/web/api/read/networks @@ -13,23 +13,12 @@ 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'); +my $q2 = $nms::web::dbh->prepare('select name, vlan, networks.tags, switches.sysname as router, subnet4, subnet6, gw4, gw6 from networks left join switches on switches.switch = networks.router'); $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'}; + $nms::web::json{'networks'}{$ref->{'name'}} = $ref; my $data = JSON::XS::decode_json($ref->{'tags'}); $nms::web::json{'networks'}{$ref->{'name'}}{'tags'} = $data; } diff --git a/web/api/read/switches-management b/web/api/read/switches-management index b42c84b..118ea4d 100755 --- a/web/api/read/switches-management +++ b/web/api/read/switches-management @@ -25,23 +25,28 @@ my $q2; $nms::web::cc{'max-age'} = "5"; $nms::web::cc{'stale-while-revalidate'} = "30"; -if (!defined($switch)) { - $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, host(mgmt_net.gw4) as mgmt_v4_gw, host(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,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;'); +my $limit = ''; +if (defined($switch)) { + $limit = ' and sysname = ' . $nms::web::dbh->quote($switch); } +$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 + from switches where switches.placement is not null and switches.deleted = false $limit;"); + $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/read/template-list b/web/api/read/template-list index d1c8773..81b9986 100755 --- a/web/api/read/template-list +++ b/web/api/read/template-list @@ -6,10 +6,10 @@ use nms::web; use strict; use warnings; -my $template_dir = "/opt/gondul/web/templates"; - -opendir (DIR, $template_dir) or die $!; +my @dirs = ("/opt/gondul/web/templates","/opt/gondul/data/templates"); +foreach my $template_dir (@dirs) { +opendir (DIR, $template_dir) or next; while (my $file = readdir(DIR)) { next if ($file =~ m/^\./); my %meh; @@ -17,5 +17,5 @@ while (my $file = readdir(DIR)) { push @{$nms::web::json{'templates'}},\%meh; } - +} nms::web::finalize_output(); diff --git a/web/api/write/linknet-add b/web/api/write/linknet-add deleted file mode 100755 index 13ccd17..0000000 --- a/web/api/write/linknet-add +++ /dev/null @@ -1,32 +0,0 @@ -#! /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/network-add b/web/api/write/network-add deleted file mode 100755 index d1aab4e..0000000 --- a/web/api/write/network-add +++ /dev/null @@ -1,49 +0,0 @@ -#! /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/networks index 6344c37..460a7ae 100755 --- a/web/api/write/network-update +++ b/web/api/write/networks @@ -6,7 +6,6 @@ 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; @@ -23,7 +22,7 @@ my @dups; my $sth = $nms::web::dbh->prepare("SELECT name FROM networks WHERE name=?"); -my @fields = ('name','last_updated','subnet4','subnet6','gw4','gw6','routing_point','vlan','tags'); +my @fields = ('name','subnet4','subnet6','gw4','gw6','router','vlan','tags'); foreach my $tmp2 (@tmp) { my %network = %{$tmp2}; @@ -47,8 +46,16 @@ foreach my $tmp2 (@tmp) { } } keys %network; - - $nms::web::dbh->do("INSERT INTO NETWORKS (name, last_updated, subnet4, subnet6, routing_point, gw4, gw6, vlan, tags) VALUES ($template{'name'}, $template{'last_updated'}, $template{'subnet4'}, $template{'subnet6'}, $template{'routing_point'}, $template{'gw4'}, $template{'gw6'}, $template{'vlan'}, $template{'tags'});"); + if ($template{'router'} ne 'DEFAULT') { + $template{'router'} = "(select switch from switches where sysname = $template{'router'})"; + } + if ($template{'gw4'} eq 'DEFAULT' and $template{'subnet4'} ne 'DEFAULT') { + $template{'gw4'} = "host(inet $template{'subnet4'} + 1)"; + } + if ($template{'gw6'} eq 'DEFAULT' and $template{'subnet6'} ne 'DEFAULT') { + $template{'gw6'} = "host(inet $template{'subnet6'} + 1)"; + } + $nms::web::dbh->do("INSERT INTO NETWORKS (name, subnet4, subnet6, router, gw4, gw6, vlan, tags) VALUES ($template{'name'}, $template{'subnet4'}, $template{'subnet6'}, $template{'router'}, $template{'gw4'}, $template{'gw6'}, $template{'vlan'}, $template{'tags'});"); push @added, $network{'name'}; } else { if (defined($network{'tags'})) { @@ -57,7 +64,11 @@ foreach my $tmp2 (@tmp) { my @set; map { if (defined($template{$_})) { - push @set, "$_=" . $dbh->quote($network{$_}); + if ($_ eq "router") { + push @set, "router=(select switch from switches where sysname = " . $dbh->quote($network{$_}) . ")"; + } else { + push @set, "$_=" . $dbh->quote($network{$_}); + } } } keys %network; $nms::web::dbh->do("UPDATE networks SET " . join(", ", @set) . "WHERE name=" . $dbh->quote($network{'name'}) . ";"); diff --git a/web/api/write/switch-add b/web/api/write/switch-add deleted file mode 100755 index c92cf31..0000000 --- a/web/api/write/switch-add +++ /dev/null @@ -1,94 +0,0 @@ -#! /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 sysname FROM switches WHERE sysname=?"); - -my @fields = ( 'community', 'distro_name', 'distro_phy_port', 'mgmt_v4_addr', 'mgmt_v6_addr', 'mgmt_vlan', 'placement', 'poll_frequency', '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, 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'}); - } -} - -$json{'switches_addded'} = \@added; - -print "X-ban: /api/.*\n"; -finalize_output(); diff --git a/web/api/write/switch-update b/web/api/write/switches index bcf69e1..947404d 100755 --- a/web/api/write/switch-update +++ b/web/api/write/switches @@ -11,6 +11,7 @@ use strict; use warnings; use JSON; use Data::Dumper; +use nms::oplog qw(oplog); $nms::web::cc{'max-age'} = "0"; @@ -84,6 +85,7 @@ foreach my $tmp2 (@tmp) { $nms::web::dbh->do("INSERT INTO SWITCHES (ip, sysname, last_updated, locked, poll_frequency, tags, community, secondary_ip, placement,subnet4,subnet6,distro) VALUES ($template{'ip'}, $template{'sysname'}, $template{'last_updated'}, $template{'locked'}, $template{'poll_frequency'}, $template{'tags'}, $template{'community'}, $template{'secondary_ip'}, $template{'placement'},$template{'subnet4'},$template{'subnet6'},$template{'distro'});"); push @added, $switch{'sysname'}; + oplog("\"" . $switch{'sysname'} . "\"", "Switch added: " . $switch{'sysname'}); } else { if (defined($switch{'placement'})) { my %placement; |