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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
#! /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 = ( 'community', 'current_mac', 'distro_name', 'distro_phy_port', 'lldp_chassis_id', 'mgmt_v4_addr', 'mgmt_v4_gw', 'mgmt_v4_netsize', 'mgmt_v6_addr', 'mgmt_v6_gw', 'mgmt_v6_netsize', 'mgmt_vlan', 'placement', 'poll_frequency', 'subnet4', 'subnet6', 'switchtype', 'sysname', 'traffic_vlan');
sub convertplace
{
my %in = %{$_[0]};
my %out = ();
if (not defined $in{'x1'} and defined($in{'x'})) {
$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;
}
return \%out;
}
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) {
my %placement;
if (not defined ($switch{'placement'})) {
%placement = guess_placement($switch{'sysname'});
} else {
%placement = %{convertplace($switch{'placement'})};
}
if (not defined($switch{'ip'}) and defined($switch{'mgtmt4'})) {
$switch{'ip'} = $switch{'mgtmt4'};
}
if (not defined($switch{'secondary_ip'}) and defined($switch{'mgtmt6'})) {
$switch{'secondary_ip'} = $switch{'mgtmt6'};
}
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{$_});
}
} keys %switch;
$nms::web::dbh->do("INSERT INTO SWITCHES (ip, sysname, last_updated, locked, poll_frequency, community, lldp_chassis_id, secondary_ip, placement,subnet4,subnet6,distro) VALUES ($template{'ip'}, $template{'sysname'}, $template{'last_updated'}, $template{'locked'}, $template{'poll_frequency'}, $template{'community'}, $template{'lldp_chassis_id'}, $template{'secondary_ip'}, $template{'placement'},$template{'subnet4'},$template{'subnet6'},$template{'distro'});");
push @added, $switch{'sysname'};
} else {
if (defined($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'};
$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'};
}
if (not defined($switch{'secondary_ip'}) and defined($switch{'mgtmt6'})) {
$switch{'secondary_ip'} = $switch{'mgtmt6'};
}
my @set;
map {
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'};
}
}
$json{'switches_addded'} = \@added;
$json{'switches_updated'} = \@dups;
print "X-ban: /api/.*switches.*\n";
finalize_output();
|