aboutsummaryrefslogtreecommitdiffstats
path: root/web/nms.gathering.org/api/private/switch-add
blob: 2b1f306b16c2a019836eef194c950b4764f0d756 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#! /usr/bin/perl
# vim:ts=8:sw=8

#use CGI qw(fatalsToBrowser);
use DBI;
use lib '../../../../include';
use nms;
use nms::web qw(%get_params %json finalize_output get_input);
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 sysname FROM switches WHERE sysname=?");
my $insert = $nms::web::dbh->prepare("INSERT INTO SWITCHES (ip, sysname, switchtype,placement) 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) {
		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))";
		$json{'foo'} = $place . "foo";
		$insert->execute($switch{'mgtmt4'}, $switch{'sysname'},$place);
		push @added, $switch{'sysname'};
	} else {
		push @dups, $switch{'sysname'};
	}
}
$json{'switches_addded'} = \@added;
$json{'switches_duplicate'} = \@dups;

finalize_output();