diff options
Diffstat (limited to 'web/api')
-rwxr-xr-x | web/api/public/dhcp | 10 | ||||
-rwxr-xr-x | web/api/public/distro-tree | 8 | ||||
-rwxr-xr-x | web/api/public/ping | 4 | ||||
-rwxr-xr-x | web/api/public/switch-state | 28 | ||||
-rwxr-xr-x | web/api/public/switches | 7 | ||||
-rwxr-xr-x | web/api/read/distro-management | 31 | ||||
-rwxr-xr-x[-rw-r--r--] | web/api/read/networks | 15 | ||||
-rwxr-xr-x | web/api/read/oplog | 9 | ||||
-rwxr-xr-x | web/api/read/switches-management | 35 | ||||
-rwxr-xr-x | web/api/read/template-list | 21 | ||||
-rwxr-xr-x | web/api/write/linknet-add | 32 | ||||
-rw-r--r-- | web/api/write/network-add | 49 | ||||
-rw-r--r-- | web/api/write/network-update | 114 | ||||
-rwxr-xr-x | web/api/write/networks | 82 | ||||
-rwxr-xr-x | web/api/write/switch-add | 94 | ||||
-rwxr-xr-x | web/api/write/switches (renamed from web/api/write/switch-update) | 4 |
16 files changed, 166 insertions, 377 deletions
diff --git a/web/api/public/dhcp b/web/api/public/dhcp index 0b0bb49..f153a49 100755 --- a/web/api/public/dhcp +++ b/web/api/public/dhcp @@ -8,16 +8,16 @@ 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 . ' and switches.deleted = false order by sysname,time desc;'); +my $q = $nms::web::dbh->prepare('select distinct on (name) extract(epoch from date_trunc(\'second\',time)) as time,name from dhcp join networks on dhcp.network = networks.network where ' . $nms::web::when . ' order by name,time desc;'); $q->execute(); while ( my $ref = $q->fetchrow_hashref() ) { - my $sysname = $ref->{'sysname'}; - $json{'dhcp'}{$ref->{'sysname'}} = $ref->{'time'}; + my $sysname = $ref->{'sname'}; + $json{'dhcp'}{$ref->{'name'}} = $ref->{'time'}; } -my $q2 = $nms::web::dbh->prepare("select sysname,count(distinct mac) as clients,count(distinct ip) as addresses,count(mac) as acks from dhcp natural join switches where $nms::web::when and switches.deleted = false group by switches.sysname;"); +my $q2 = $nms::web::dbh->prepare("select name,count(distinct mac) as clients,count(distinct ip) as addresses,count(mac) as acks from dhcp natural join networks where $nms::web::when group by networks.name;"); $q2->execute(); while (my $ref = $q2->fetchrow_hashref()) { - $nms::web::json{'switches'}{$ref->{sysname}} = $ref; + $nms::web::json{'networks'}{$ref->{name}} = $ref; } $nms::web::cc{'max-age'} = "10"; 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/ping b/web/api/public/ping index 1928368..be37875 100755 --- a/web/api/public/ping +++ b/web/api/public/ping @@ -4,7 +4,7 @@ use strict; use warnings; use nms::web; -nms::web::setwhen('15s'); +nms::web::setwhen('25s'); 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)"); @@ -37,5 +37,5 @@ while ( my $ref = $lq->fetchrow_hashref() ) { } $nms::web::cc{'max-age'} = "1"; -$nms::web::cc{'stale-while-revalidate'} = "15"; +$nms::web::cc{'stale-while-revalidate'} = "5"; finalize_output(); diff --git a/web/api/public/switch-state b/web/api/public/switch-state index 965e881..528bf7d 100755 --- a/web/api/public/switch-state +++ b/web/api/public/switch-state @@ -7,17 +7,19 @@ 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 $target = $ENV{REQUEST_URI}; +#$target =~ s/$ENV{SCRIPT_NAME}//; +#$target =~ s/^\///; +#my ($switch, $port) = split(/\//,$target,2); +my $port; +my $switch; my $q; -if (!defined($switch)) { - $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 . ' and switches.deleted = false group by switch);'); -} else { - $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) and sysname = ' . $nms::web::dbh->quote($switch) . ' and switches.deleted = false;'); -} +#if (!defined($switch)) { + $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) and switches.deleted = \'f\' and not switches.tags \?& array[\'ignoreswitchstate\'];'); +#} else { +# $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) and sysname = ' . $nms::web::dbh->quote($switch) . ';'); +#} $q->execute(); while ( my $ref = $q->fetchrow_hashref() ) { @@ -42,7 +44,7 @@ while ( my $ref = $q->fetchrow_hashref() ) { $json{'switches'}{$sysname}{ifs}{$smallport}{'ifAlias'} = $port{'ifAlias'}; } if ($data{'ports'}{$porti}{'ifType'} ne "propVirtual" and - $data{'ports'}{$porti}{'ifAlias'} =~ m/LAG member/i) { + $data{'ports'}{$porti}{'ifAlias'} =~ m/Fysisk/i) { if ($port{'ifAdminStatus'} eq "up") { $json{'switches'}{$sysname}{'uplinks'}{'ifHCInOctets'} += $port{'ifHCInOctets'}; $json{'switches'}{$sysname}{'uplinks'}{'ifHCOutOctets'} += $port{'ifHCOutOctets'}; @@ -83,9 +85,9 @@ while ( my $ref = $q->fetchrow_hashref() ) { nms::web::setwhen('15m','10m'); my $q2; if (!defined($switch)) { - $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 = $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) and switches.deleted = \'f\' and not switches.tags \?& array[\'ignoreswitchstate\'];'); } else { - $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) and sysname = ' . $nms::web::dbh->quote($switch) . ';'); + $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) and switches.deleted = \'f\' and not switches.tags \?& array[\'ignoreswitchstate\'] and sysname = ' . $nms::web::dbh->quote($switch) . ';'); } $q2->execute(); @@ -109,7 +111,7 @@ while ( my $ref = $q2->fetchrow_hashref() ) { $json{'then'}{$sysname}{ifs}{$smallport}{'ifAlias'} = $port{'ifAlias'}; } if ($data{'ports'}{$porti}{'ifType'} ne "propVirtual" and - $data{'ports'}{$porti}{'ifAlias'} =~ m/LAG member/i) { + $data{'ports'}{$porti}{'ifAlias'} =~ m/Fysisk/i) { if ($port{'ifAdminStatus'} eq "up") { $json{'then'}{$sysname}{'uplinks'}{'ifHCInOctets'} += $port{'ifHCInOctets'}; $json{'then'}{$sysname}{'uplinks'}{'ifHCOutOctets'} += $port{'ifHCOutOctets'}; 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 7fbebef..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,subnet4,subnet6,traffic_vlan,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 100644..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/oplog b/web/api/read/oplog index fb187ea..f2ed3a7 100755 --- a/web/api/read/oplog +++ b/web/api/read/oplog @@ -6,7 +6,14 @@ use nms::web; use strict; use warnings; -my $query = $nms::web::dbh->prepare('select id,date_trunc(\'second\',time) as timestamp,extract(hour from time) as h, extract(minute from time) as m,systems,username,log from oplog order by id desc;'); +my $filter = ''; + +if (defined($get_params{'now'})) { + $filter = "where time < timestamp with time zone 'epoch' + " . db_safe_quote('now') . " * INTERVAL '1 second' "; +} +my $query = $nms::web::dbh->prepare('select id,date_trunc(\'second\',time) as timestamp,extract(hour from time) as h, extract(minute from time) as m,systems,username,log from oplog ' + . $filter . +'order by id desc;'); $query->execute(); while (my $ref = $query->fetchrow_hashref()) { my %meh; diff --git a/web/api/read/switches-management b/web/api/read/switches-management index 77badf4..118ea4d 100755 --- a/web/api/read/switches-management +++ b/web/api/read/switches-management @@ -13,29 +13,40 @@ use Data::Dumper; my $target = $ENV{REQUEST_URI}; $target =~ s/$ENV{SCRIPT_NAME}//; $target =~ s/^\///; -my ($switch, $port) = split(/\//,$target,2); +my $switch; +my $port; + +if ($target !~ m/now=/) { + ($switch, $port) = split(/\//,$target,2); +} + 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, 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,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 new file mode 100755 index 0000000..81b9986 --- /dev/null +++ b/web/api/read/template-list @@ -0,0 +1,21 @@ +#! /usr/bin/perl +# vim:ts=8:sw=8 + +use lib '/opt/gondul/include'; +use nms::web; +use strict; +use warnings; + +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; + $meh{'file'} = $file; + + 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 100644 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/network-update deleted file mode 100644 index f6684c5..0000000 --- a/web/api/write/network-update +++ /dev/null @@ -1,114 +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; - -$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/networks b/web/api/write/networks new file mode 100755 index 0000000..460a7ae --- /dev/null +++ b/web/api/write/networks @@ -0,0 +1,82 @@ +#! /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 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','subnet4','subnet6','gw4','gw6','router','vlan','tags'); + +foreach my $tmp2 (@tmp) { + my %network = %{$tmp2}; + my $affected = 0; + my %template = (); + map { $template{$_} = 'DEFAULT' } @fields; + if (not defined($network{'name'})) { + next; + } + + $sth->execute( $network{'name'}); + while ( my @row = $sth->fetchrow_array ) { + $affected += 1; + } + + if ($affected == 0) { + + map { + if (defined ($template{$_})) { + $template{$_} = $dbh->quote($network{$_}); + } + } keys %network; + + 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'})) { + $network{'tags'} =~ s/'/"/g; + } + my @set; + map { + if (defined($template{$_})) { + 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'}) . ";"); + push @dups, $network{'name'}; + } +} +$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 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 b71548b..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"; @@ -23,7 +24,7 @@ my @dups; my $sth = $nms::web::dbh->prepare("SELECT sysname FROM switches WHERE sysname=?"); -my @fields = ( 'community', 'tags', 'distro_name', 'distro_phy_port', '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', 'sysname', 'traffic_vlan', 'deleted'); +my @fields = ( 'community', 'tags', 'distro_name', 'distro_phy_port', 'mgmt_v4_addr', 'mgmt_v6_addr', 'mgmt_vlan', 'placement', 'poll_frequency', 'sysname', 'traffic_vlan', 'deleted'); sub convertplace { @@ -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; |