-
Notifications
You must be signed in to change notification settings - Fork 0
/
aplol-wlc-aps.pl
executable file
·271 lines (217 loc) · 10.1 KB
/
aplol-wlc-aps.pl
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/usr/bin/perl
use warnings;
use strict;
use Net::SNMP;
use Net::SNMP::Util;
use Fcntl qw(:flock);
# Gathers information from the WLCs directly, and correalates this
# with the information within Cisco Prime
# Done as a sanity check that the information within Prime is OK
# Load aplol
my $aplol_dir;
BEGIN {
use FindBin;
$aplol_dir = "$FindBin::Bin"; # Assume working-folder is the path where this script resides
}
use lib $aplol_dir;
use aplol;
my $aplol = aplol->new();
my %config = $aplol->get_config();
my ($wlc_aps, $db_aps, $diff_aps);
# Log
sub log_it{
$aplol->log_it("wlc", "@_");
}
# Logs debug-stuff if debug has been turned on
sub debug_log{
$aplol->debug_log("wlc", "@_");
}
# Logs error-stuff
sub error_log{
$aplol->error_log("wlc", "@_");
}
# Convert SNMP-version of MAC-address, into IEEE-version
# Input looks like "0x0013d3c4a966" (without the quotes)
sub mac_snmp_to_hex{
my $snmp_mac = "@_";
return join(':', ($snmp_mac =~ m/(?:0x)?(\w{2})/g));
}
# Find broken MAC
sub find_broken_mac{
# sometimes Net::SNMP gets gibberish MAC-adresses, that should be valid
# if that happens, we can fetch it "manually" via snmpwalk
my ($switchip, $snmp_community, $macoid) = @_;
# snmpwalk-options: -Oqv
# -O OUTOPTS Toggle various defaults controlling output display:
# q: quick print for easier parsing
# v: print values only (not OID = value)
my $mac = (`/usr/bin/snmpwalk -Oqv -v$config{snmp}->{version} -c$snmp_community $switchip $macoid`)[0];
return "undef" unless($mac); # needs a value
# remove all non-valid characers
# make lowercase + insert ':'
# pad with zeroes
($mac = lc($mac) ) =~ s/[^0-9a-fA-F]//g;
$mac =~ s/..\K(?=.)/:/g;
$mac =~ s/(^|:)(?=[0-9a-fA-F](?::|$))/${1}0/g;
return $mac;
}
# fetch all AP's and their respective AP-groups
sub update_apgroups{
my $wlcs = $aplol->get_wlcs();
my %oids = (
# http://tools.cisco.com/Support/SNMP/do/BrowseOID.do?local=en&translate=Translate&objectInput=1.3.6.1.4.1.14179.2.2.1.1#oidContent
'bsnAPEntry' => '1.3.6.1.4.1.14179.2.2.1',
);
# iterate through all WLC's
foreach my $wlc_id (sort keys %$wlcs){
next unless ($wlcs->{$wlc_id}{active}); # only want active WLCs
log_it("Checking AP's on WLC '$wlcs->{$wlc_id}{name}' ($wlcs->{$wlc_id}{ipv4})...");
my ($session, $error) = Net::SNMP->session(
Hostname => $wlcs->{$wlc_id}{ipv4},
Community => $wlcs->{$wlc_id}{snmp_ro},
Version => $config{snmp}->{version},
Timeout => $config{snmp}->{timeout},
Retries => $config{snmp}->{retries},
);
if ($session){
my ($result, $error) = snmpwalk(snmp => $session,
oids => \%oids );
unless(keys %$result){
error_log("Could not poll '$wlcs->{$wlc_id}{name}': $error");
$session->close();
next;
}
foreach my $entry (keys %{$result->{bsnAPEntry}}){
if ($entry =~ m/^1\.3\./){
# bsnAPName
# http://tools.cisco.com/Support/SNMP/do/BrowseOID.do?local=en&translate=Translate&objectInput=1.3.6.1.4.1.14179.2.2.1.1.3#oidContent
(my $apoid = $entry) =~ s/^1\.3\.(.+)$/$1/;
# bsnAPGroupVlanName
# http://tools.cisco.com/Support/SNMP/do/BrowseOID.do?local=en&translate=Translate&objectInput=1.3.6.1.4.1.14179.2.2.1.1.30#oidContent
my $apgroupoid = "1.30.$apoid";
# bsnAPEthernetMacAddress
# http://tools.cisco.com/Support/SNMP/do/BrowseOID.do?local=en&translate=Translate&objectInput=1.3.6.1.4.1.14179.2.2.1.1.33#oidContent
my $apmacoid = "1.33.$apoid";
my $apname = $result->{bsnAPEntry}{$entry};
my $apgroup = $result->{bsnAPEntry}{$apgroupoid};
my $apmac = mac_snmp_to_hex($result->{bsnAPEntry}{$apmacoid});
# if we get zero value, we make sure to try manually first
$apmac = "undef" unless($apmac);
unless($apgroup){
error_log("No AP-group was found.");
error_log("$apname") if $apname;
next;
}
unless($aplol->valid_mac($apmac)){
# has to be valid, try fetch manually
my $apmacoid_complete = $oids{bsnAPEntry} . "." . $apmacoid;
my $fixed_mac = find_broken_mac($wlcs->{$wlc_id}{ipv4}, $wlcs->{$wlc_id}{snmp_ro}, $apmacoid_complete);
if($aplol->valid_mac($fixed_mac)){
# yay, we found valid
$apmac = $fixed_mac;
} else {
# nay, still invalid
error_log("Invalid MAC: $apmac / $fixed_mac ($apname)");
next;
}
}
my $apgroupoid_complete = $oids{bsnAPEntry} . "." . $apgroupoid;
# add to WLC-hash
$wlc_aps->{$apmac}{name} = $apname;
$wlc_aps->{$apmac}{wlc_apgroup} = $apgroup;
$wlc_aps->{$apmac}{wlc_name} = $wlcs->{$wlc_id}{name};
# update DB
$aplol->update_apgroup_info($apmac, $apgroup);
debug_log("$apmac, $apgroup, $apgroupoid_complete");
}
}
# close after checking all AP
$session->close();
} else {
error_log("Could not connect to '$wlcs->{$wlc_id}{name}': $error");
$session->close();
next;
}
}
}
sub compare_wlc_prime{
# fetch all AP's from DB
$db_aps = $aplol->get_active_aps();
foreach my $ethmac (sort keys %$db_aps){
if($wlc_aps->{$ethmac}){
# found on a WLC, check values
my $match = 1;
unless($db_aps->{$ethmac}{wlc_name} =~ m/^$wlc_aps->{$ethmac}{wlc_name}$/){
# not a match
$match = 0;
}
unless($db_aps->{$ethmac}{apgroup_name} =~ m/^$wlc_aps->{$ethmac}{wlc_apgroup}$/){
# not a match
$match = 0;
}
unless($match){
# non-identical entry, add to hash
# set values from WLC
$diff_aps->{$ethmac}{name} = $wlc_aps->{$ethmac}{name};
$diff_aps->{$ethmac}{wlc_apgroup} = $wlc_aps->{$ethmac}{wlc_apgroup};
$diff_aps->{$ethmac}{wlc_name} = $wlc_aps->{$ethmac}{wlc_name};
# set values from DB
$diff_aps->{$ethmac}{db_wlc_name} = $db_aps->{$ethmac}{wlc_name};
$diff_aps->{$ethmac}{db_wlc_name} = "undef" unless($diff_aps->{$ethmac}{db_wlc_name});
$diff_aps->{$ethmac}{db_apgroup} = $db_aps->{$ethmac}{apgroup_name};
$diff_aps->{$ethmac}{db_apgroup} = "undef" unless($diff_aps->{$ethmac}{db_apgroup});
}
} else {
# could happen if AP is disassociated with WLC
# entry is not identical, so we can just assume it's offline, and add relevant info
$diff_aps->{$ethmac}{name} = $db_aps->{$ethmac}{name};
$diff_aps->{$ethmac}{wlc_apgroup} = "undef";
$diff_aps->{$ethmac}{wlc_name} = "unassociated";
# set values from DB
$diff_aps->{$ethmac}{db_wlc_name} = $db_aps->{$ethmac}{wlc_name};
$diff_aps->{$ethmac}{db_apgroup} = $db_aps->{$ethmac}{apgroup_name};
}
}
# empty the aps_diff table
# we could just update whatever is there, but, bleh
$aplol->empty_aps_diff();
# at this point, $wlc_aps should contain all AP's that are
# 1) not equal to PI regarding associated WLC and/or wrong AP-group
# 2) not present at all on PI
# 3) present in PI, but not on WLC (offline, but online in Prime)
foreach my $ethmac (sort keys %$diff_aps){
if( ($diff_aps->{$ethmac}{wlc_name} =~ m/^unassociated$/) &&
($diff_aps->{$ethmac}{db_wlc_name} =~ m/^unassociated$/)){
# AP is unassociated on both systems
# we don't want to list these, as these are legit
next;
}
$aplol->add_aps_diff( $ethmac,
$diff_aps->{$ethmac}{name},
$diff_aps->{$ethmac}{wlc_apgroup},
$diff_aps->{$ethmac}{db_apgroup},
$diff_aps->{$ethmac}{wlc_name},
$diff_aps->{$ethmac}{db_wlc_name});
}
}
# We only want 1 instance of this script running
# Check if already running -- if so, abort.
unless (flock(DATA, LOCK_EX|LOCK_NB)) {
die("$0 is already running. Exiting.");
}
# connect
my $time_start = time(); # set start time
$aplol->connect();
# apgroups
update_apgroups();
# compare info from PI and WLC
# should reveal AP's within PI that has wrong AP-group or WLC-association
# it should also find AP's that are not yet discovered by PI
compare_wlc_prime();
# disconnect
$aplol->disconnect();
# How long did we run for?
my $runtime = time() - $time_start;
log_it("Took $runtime seconds to complete.");
__DATA__
Do not remove. Makes sure flock() code above works as it should.