diff options
| author | Kristian Lyngstol <kly@kly.no> | 2016-02-29 19:09:34 +0000 | 
|---|---|---|
| committer | Kristian Lyngstol <kly@kly.no> | 2016-02-29 19:09:34 +0000 | 
| commit | 4f6f935008acc0acbd43ccc134aa9845d34a2317 (patch) | |
| tree | 2c4b4436c39fb489890319bba48f7c73c9c2b8b0 | |
| parent | 63c2e8b35da1ee8de3bf9ff71ca4e25f7829dcec (diff) | |
nms: Adjust switches_add.pl
It now accepts an array of switches, and the output of add_switches.txt.pl
can be used directly as input, ignoring duplicates.
Still not done. Need placements etc.
| -rwxr-xr-x | web/nms.gathering.org/switches_add.pl | 37 | 
1 files changed, 26 insertions, 11 deletions
| diff --git a/web/nms.gathering.org/switches_add.pl b/web/nms.gathering.org/switches_add.pl index 06edf66..fe23049 100755 --- a/web/nms.gathering.org/switches_add.pl +++ b/web/nms.gathering.org/switches_add.pl @@ -14,16 +14,31 @@ use Data::Dumper;  $nms::web::cc{'max-age'} = "0";  my $in = get_input(); -my %tmp = %{JSON::XS::decode_json($in)}; - -my $query = "INSERT INTO SWITCHES (ip, sysname, switchtype) VALUES('" -	. $tmp{'mgtmt4'} . "','" -	. $tmp{'name'} . "','ex2200');"; - -$json{'sql'} = $query; - -my $q = $nms::web::dbh->prepare($query); -$q->execute() || die "foo"; - +my @tmp = @{JSON::XS::decode_json($in)}; + +my @added; +my @dups; + +my $sth = $nms::web::dbh->prepare("SELECT sysname FROM switches WHERE sysname=?"); +my $insert = $nms::web::dbh->prepare("INSERT INTO SWITCHES (ip, sysname, switchtype) VALUES(?,?,'ex2200');"); + +foreach my $tmp2 (@tmp) { +	my %switch = %{$tmp2}; +	my $affected = 0; + +	$sth->execute( $switch{'sysname'}); +	while ( my @row = $sth->fetchrow_array ) { +		$affected += 1; +	} + +	if ($affected == 0) { +		$insert->execute($switch{'mgtmt4'}, $switch{'sysname'}); +		push @added, $switch{'sysname'}; +	} else { +		push @dups, $switch{'sysname'}; +	} +} +$json{'switches_addded'} = \@added; +$json{'switches_duplicate'} = \@dups;  finalize_output(); | 
