From 820287be1769b442f101a712a0f008b739f50699 Mon Sep 17 00:00:00 2001 From: Kristian Lyngstol Date: Thu, 10 Mar 2016 19:15:17 +0000 Subject: NMS: Tweak caching and updates --- web/nms.gathering.org/api/private/switch-add | 33 +++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 6 deletions(-) (limited to 'web/nms.gathering.org/api/private/switch-add') diff --git a/web/nms.gathering.org/api/private/switch-add b/web/nms.gathering.org/api/private/switch-add index af8db38..826b65b 100755 --- a/web/nms.gathering.org/api/private/switch-add +++ b/web/nms.gathering.org/api/private/switch-add @@ -6,6 +6,7 @@ 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; @@ -21,13 +22,16 @@ my @dups; my $sth = $nms::web::dbh->prepare("SELECT sysname FROM switches WHERE sysname=?"); -my @fields = ('ip', 'sysname', 'switchtype', 'last_updated', 'locked', 'poll_frequency', 'community', 'lldp_chassis_id', 'secondary_ip'); +my @fields = ('ip', 'sysname', 'switchtype', 'last_updated', 'locked', 'poll_frequency', 'community', 'lldp_chassis_id', 'secondary_ip', 'placement'); 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 ) { @@ -40,6 +44,21 @@ foreach my $tmp2 (@tmp) { $template{$_} = $dbh->quote($switch{$_}); } } keys %switch; + if (not defined($switch{'placement'})) { + %{$switch{'placement'}} = guess_placement($switch{'sysname'}); + } + + my ($x1,$x2,$y1,$y2); + $x1 = $switch{'placement'}{'x1'}; + $y1 = $switch{'placement'}{'y1'}; + $x2 = $switch{'placement'}{'xx'}; + $y2 = $switch{'placement'}{'yy'}; + my $place = "(($x1,$y1),($x2,$y2))"; + $template{'placement'} = $dbh->quote($place); + + $nms::web::dbh->do("INSERT INTO SWITCHES (ip, sysname, switchtype, last_updated, locked, poll_frequency, community, lldp_chassis_id, secondary_ip, placement) VALUES ($template{'ip'}, $template{'sysname'}, $template{'switchtype'}, $template{'last_updated'}, $template{'locked'}, $template{'poll_frequency'}, $template{'community'}, $template{'lldp_chassis_id'}, $template{'secondary_ip'}, $template{'placement'});"); + push @added, $switch{'sysname'}; + } else { if (defined($switch{'placement'})) { my ($x1,$x2,$y1,$y2); $x1 = $switch{'placement'}{'x1'}; @@ -47,15 +66,17 @@ foreach my $tmp2 (@tmp) { $x2 = $switch{'placement'}{'xx'}; $y2 = $switch{'placement'}{'yy'}; my $place = "(($x1,$y1),($x2,$y2))"; - $template{'placement'} = $place; + $switch{'placement'} = $place; } - $nms::web::dbh->do("INSERT INTO SWITCHES (ip, sysname, switchtype, last_updated, locked, poll_frequency, community, lldp_chassis_id, secondary_ip) VALUES ($template{'ip'}, $template{'sysname'}, $template{'switchtype'}, $template{'last_updated'}, $template{'locked'}, $template{'poll_frequency'}, $template{'community'}, $template{'lldp_chassis_id'}, $template{'secondary_ip'});"); - push @added, $switch{'sysname'}; - } else { + my @set; + map { + 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_duplicate'} = \@dups; +$json{'switches_updated'} = \@dups; finalize_output(); -- cgit v1.2.3 From 3877f4d3a4389050250aa34474cabee6826f208f Mon Sep 17 00:00:00 2001 From: Kristian Lyngstol Date: Sat, 12 Mar 2016 13:52:52 +0000 Subject: NMS: Fix switch updates (better) --- web/nms.gathering.org/api/private/switch-add | 32 ++++++++++++++++------------ 1 file changed, 18 insertions(+), 14 deletions(-) (limited to 'web/nms.gathering.org/api/private/switch-add') diff --git a/web/nms.gathering.org/api/private/switch-add b/web/nms.gathering.org/api/private/switch-add index 826b65b..5abe0f4 100755 --- a/web/nms.gathering.org/api/private/switch-add +++ b/web/nms.gathering.org/api/private/switch-add @@ -39,22 +39,25 @@ foreach my $tmp2 (@tmp) { } if ($affected == 0) { + my %placement; + if (not defined ($switch{'placement'})) { + %placement = guess_placement($switch{'sysname'}); + } else { + %placement = %{$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))"; + map { if (defined ($template{$_})) { - $template{$_} = $dbh->quote($switch{$_}); + $template{$_} = $dbh->quote($switch{$_}); } } keys %switch; - if (not defined($switch{'placement'})) { - %{$switch{'placement'}} = guess_placement($switch{'sysname'}); - } - my ($x1,$x2,$y1,$y2); - $x1 = $switch{'placement'}{'x1'}; - $y1 = $switch{'placement'}{'y1'}; - $x2 = $switch{'placement'}{'xx'}; - $y2 = $switch{'placement'}{'yy'}; - my $place = "(($x1,$y1),($x2,$y2))"; - $template{'placement'} = $dbh->quote($place); $nms::web::dbh->do("INSERT INTO SWITCHES (ip, sysname, switchtype, last_updated, locked, poll_frequency, community, lldp_chassis_id, secondary_ip, placement) VALUES ($template{'ip'}, $template{'sysname'}, $template{'switchtype'}, $template{'last_updated'}, $template{'locked'}, $template{'poll_frequency'}, $template{'community'}, $template{'lldp_chassis_id'}, $template{'secondary_ip'}, $template{'placement'});"); push @added, $switch{'sysname'}; @@ -65,12 +68,13 @@ foreach my $tmp2 (@tmp) { $y1 = $switch{'placement'}{'y1'}; $x2 = $switch{'placement'}{'xx'}; $y2 = $switch{'placement'}{'yy'}; - my $place = "(($x1,$y1),($x2,$y2))"; - $switch{'placement'} = $place; + $switch{'placement'} = "(($x1,$y1),($x2,$y2))"; } my @set; map { - push @set, "$_=" . $dbh->quote($switch{$_}); + 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'}; -- cgit v1.2.3 From 9f992ae24299d81643188530bc6f79e76f141c03 Mon Sep 17 00:00:00 2001 From: Kristian Lyngstol Date: Sun, 13 Mar 2016 12:55:06 +0000 Subject: NMS: Pick up IP's properly Nobody look because I just defaulted to ipv4. --- web/nms.gathering.org/api/private/switch-add | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'web/nms.gathering.org/api/private/switch-add') diff --git a/web/nms.gathering.org/api/private/switch-add b/web/nms.gathering.org/api/private/switch-add index 5abe0f4..93416ff 100755 --- a/web/nms.gathering.org/api/private/switch-add +++ b/web/nms.gathering.org/api/private/switch-add @@ -45,6 +45,9 @@ foreach my $tmp2 (@tmp) { } else { %placement = %{$switch{'placement'}}; } + if (not defined($switch{'ip'}) and defined($switch{'mgtmt4'})) { + $switch{'ip'} = $switch{'mgtmt4'}; + } my ($x1,$x2,$y1,$y2); $x1 = $placement{'x1'}; $y1 = $placement{'y1'}; @@ -70,6 +73,9 @@ foreach my $tmp2 (@tmp) { $y2 = $switch{'placement'}{'yy'}; $switch{'placement'} = "(($x1,$y1),($x2,$y2))"; } + if (not defined($switch{'ip'}) and defined($switch{'mgtmt4'})) { + $switch{'ip'} = $switch{'mgtmt4'}; + } my @set; map { if (defined($template{$_})) { -- cgit v1.2.3 From 544368f68e6a55ed619b91f631b0e200ad7ed5ad Mon Sep 17 00:00:00 2001 From: Kristian Lyngstol Date: Sun, 13 Mar 2016 14:17:30 +0000 Subject: NMS: Allow editing switches in GUI It's not pretty, but it works and should cover most of the use-cases. Placement-editing is not meant to be the primary way to edit anything. --- web/nms.gathering.org/api/private/switch-add | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) (limited to 'web/nms.gathering.org/api/private/switch-add') diff --git a/web/nms.gathering.org/api/private/switch-add b/web/nms.gathering.org/api/private/switch-add index 93416ff..64e5e58 100755 --- a/web/nms.gathering.org/api/private/switch-add +++ b/web/nms.gathering.org/api/private/switch-add @@ -24,6 +24,22 @@ my $sth = $nms::web::dbh->prepare("SELECT sysname FROM switches WHERE sysname=?" my @fields = ('ip', 'sysname', 'switchtype', 'last_updated', 'locked', 'poll_frequency', 'community', 'lldp_chassis_id', 'secondary_ip', 'placement'); +sub convertplace +{ + my %in = %{$_[0]}; + my %out = (); + + if (not defined $in{'x1'} and defined($in{'x'})) { + $out{'x1'} = $in{'x'}; + $out{'y1'} = $in{'y'}; + $out{'xx'} = $in{'x'} + $in{'width'}; + $out{'yy'} = $in{'y'} + $in{'height'}; + } else { + return \%in; + } + return \%out; +} + foreach my $tmp2 (@tmp) { my %switch = %{$tmp2}; my $affected = 0; @@ -43,7 +59,7 @@ foreach my $tmp2 (@tmp) { if (not defined ($switch{'placement'})) { %placement = guess_placement($switch{'sysname'}); } else { - %placement = %{$switch{'placement'}}; + %placement = %{convertplace($switch{'placement'})}; } if (not defined($switch{'ip'}) and defined($switch{'mgtmt4'})) { $switch{'ip'} = $switch{'mgtmt4'}; @@ -66,11 +82,12 @@ foreach my $tmp2 (@tmp) { push @added, $switch{'sysname'}; } else { if (defined($switch{'placement'})) { + my %placement = %{convertplace($switch{'placement'})}; my ($x1,$x2,$y1,$y2); - $x1 = $switch{'placement'}{'x1'}; - $y1 = $switch{'placement'}{'y1'}; - $x2 = $switch{'placement'}{'xx'}; - $y2 = $switch{'placement'}{'yy'}; + $x1 = $placement{'x1'}; + $y1 = $placement{'y1'}; + $x2 = $placement{'xx'}; + $y2 = $placement{'yy'}; $switch{'placement'} = "(($x1,$y1),($x2,$y2))"; } if (not defined($switch{'ip'}) and defined($switch{'mgtmt4'})) { -- cgit v1.2.3 From e7eb548d99a324048d6fd08cb5a0243e47753451 Mon Sep 17 00:00:00 2001 From: Kristian Lyngstol Date: Sun, 13 Mar 2016 15:54:14 +0000 Subject: NMSjs: Enable interactive movement of switches! Fixes #24 God that felt good. It's not very pretty, but it does seem very functional. --- web/nms.gathering.org/api/private/switch-add | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'web/nms.gathering.org/api/private/switch-add') diff --git a/web/nms.gathering.org/api/private/switch-add b/web/nms.gathering.org/api/private/switch-add index 64e5e58..e177b5e 100755 --- a/web/nms.gathering.org/api/private/switch-add +++ b/web/nms.gathering.org/api/private/switch-add @@ -30,10 +30,10 @@ sub convertplace my %out = (); if (not defined $in{'x1'} and defined($in{'x'})) { - $out{'x1'} = $in{'x'}; - $out{'y1'} = $in{'y'}; - $out{'xx'} = $in{'x'} + $in{'width'}; - $out{'yy'} = $in{'y'} + $in{'height'}; + $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; } @@ -89,6 +89,7 @@ foreach my $tmp2 (@tmp) { $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'}; -- cgit v1.2.3 From a9656e1fa4bcec5bcbbfe5ca29671d6701014ce8 Mon Sep 17 00:00:00 2001 From: Kristian Lyngstol Date: Sun, 13 Mar 2016 17:07:28 +0000 Subject: NMS: Support resetting switch position to buest-guess Simply type "reset" in the gui (including quotes) and it'll guess based on name. --- web/nms.gathering.org/api/private/switch-add | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'web/nms.gathering.org/api/private/switch-add') diff --git a/web/nms.gathering.org/api/private/switch-add b/web/nms.gathering.org/api/private/switch-add index e177b5e..92479f0 100755 --- a/web/nms.gathering.org/api/private/switch-add +++ b/web/nms.gathering.org/api/private/switch-add @@ -82,7 +82,12 @@ foreach my $tmp2 (@tmp) { push @added, $switch{'sysname'}; } else { if (defined($switch{'placement'})) { - my %placement = %{convertplace($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'}; -- cgit v1.2.3