#! /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 $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 sysname FROM switches WHERE sysname=?"); 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 ) { $affected += 1; } if ($affected == 0) { map { if (defined ($template{$_})) { $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'}; $y1 = $switch{'placement'}{'y1'}; $x2 = $switch{'placement'}{'xx'}; $y2 = $switch{'placement'}{'yy'}; my $place = "(($x1,$y1),($x2,$y2))"; $switch{'placement'} = $place; } 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_updated'} = \@dups; finalize_output();