aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKristian Lyngstol <kristian@bohemians.org>2016-02-29 20:40:46 +0100
committerKristian Lyngstol <kristian@bohemians.org>2016-02-29 20:40:46 +0100
commit711e1f0f3fc392afe87105c83140e3e5d729317e (patch)
tree14dd0b764da20602fa9c06ce4b405c5a7b92e554
parent42b0129ec342b8044d0d84c94151da160c288602 (diff)
parent4f6f935008acc0acbd43ccc134aa9845d34a2317 (diff)
Merge branch 'master' of github.com:tech-server/tgmanage
-rwxr-xr-xweb/nms.gathering.org/switches_add.pl37
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();