-
Notifications
You must be signed in to change notification settings - Fork 0
/
aplol-ciscopi-aps.pl
executable file
·496 lines (412 loc) · 14.2 KB
/
aplol-ciscopi-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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#!/usr/bin/perl
# From command-line;
# https_proxy= HTTPS_PROXY= perl aplol-ciscopi-aps.pl
use warnings;
use strict;
use POSIX qw(strftime);
use POSIX qw(floor);
use Fcntl qw(:flock);
use Date::Parse;
use Scalar::Util qw/reftype/;
binmode(STDOUT, ":utf8");
# Fetch updated AP-information from Cisco Prime
# 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 (%locations, $root_aps, $db_aps, $wlcs, $time_start);
# Log
sub log_it{
$aplol->log_it("ciscopi-aps", "@_");
}
# Logs debug-stuff if debug has been turned on
sub debug_log{
$aplol->debug_log("ciscopi-aps", "@_");
}
# Logs error-stuff
sub error_log{
$aplol->error_log("ciscopi-aps", "@_");
}
# Shows runtime
sub show_runtime{
my $runtime = time() - $time_start;
log_it("Took $runtime seconds to complete.");
}
# fetch AP info
sub get_apinfo{
my $vd = shift;
my $url = "v1/data/AccessPointDetails.json?.full=true&type=\"UnifiedAp\"&_ctx.domain=$vd&.maxResults=1000";
return $aplol->get_json($url);
}
# fetch alarm info
sub get_alarminfo{
my $severity = shift;
my $url = "v1/data/Alarms.json?.full=true&severity=$severity&acknowledgementStatus=false&_ctx.domain=ROOT-DOMAIN&.maxResults=1000";
return $aplol->get_json($url);
}
# fetch all APs
sub get_aps{
my $vd = shift;
$vd = "ROOT-DOMAIN" unless($vd);
my $pi_aps = get_apinfo($vd);
my %aps;
if($pi_aps){
foreach my $apinfo (@$pi_aps){
# neighbor stuff
my $neighbor_count = keys %{$apinfo->{'accessPointDetailsDTO'}->{'cdpNeighbors'}->{'cdpNeighbor'}};
my ($neighbor_name, $neighbor_addr, $neighbor_port);
my $no_cdp = 0;
if($neighbor_count == 0){
$neighbor_name = '';
$neighbor_addr = "0.0.0.0";
$neighbor_port = '';
$no_cdp = 1;
} else {
$neighbor_name = $apinfo->{'accessPointDetailsDTO'}->{'cdpNeighbors'}->{'cdpNeighbor'}->{'neighborName'};
$neighbor_addr = $apinfo->{'accessPointDetailsDTO'}->{'cdpNeighbors'}->{'cdpNeighbor'}->{'neighborIpAddress'};
$neighbor_port = $apinfo->{'accessPointDetailsDTO'}->{'cdpNeighbors'}->{'cdpNeighbor'}->{'neighborPort'};
# remove whitespace from IP
$neighbor_addr =~ s/\s//g;
# remove domain from hostname
$neighbor_name =~ s/^(.+?)\..*/$1/;
# short portname
$neighbor_port =~ s/GigabitEthernet/Gi/;
$neighbor_port =~ s/FastEthernet/Fa/;
}
# online/offline?
my $associated = 0;
if( $apinfo->{'accessPointDetailsDTO'}->{'reachabilityStatus'} =~ m/^reachable$/i ){
$associated = 1;
} elsif( $apinfo->{'accessPointDetailsDTO'}->{'reachabilityStatus'} =~ m/^unknown$/i ){
# possibly associated, but "Admin status" set to "disabled"
if( ( $apinfo->{'accessPointDetailsDTO'}->{'unifiedApInfo'}->{'controllerName'} )
&& ( $apinfo->{'accessPointDetailsDTO'}->{'adminStatus'} =~ m/^disable$/i )){
# associated & admin disabled
$associated = 1;
}
}
# controller -- not present if AP is unassociated
my $controller = "unassociated";
if( $apinfo->{'accessPointDetailsDTO'}->{'unifiedApInfo'}->{'controllerName'} ){
$controller = $apinfo->{'accessPointDetailsDTO'}->{'unifiedApInfo'}->{'controllerName'};
}
# uptime
my $uptime = $apinfo->{'accessPointDetailsDTO'}->{'upTime'};
$uptime = 0 unless($uptime);
# weird scenario with a malformed AP entry
unless($apinfo->{'accessPointDetailsDTO'}->{'name'}){
use Data::Dumper;
print Dumper($apinfo);
show_runtime();
die(error_log("Malformed AP."));
}
# all variables should be ready
my %ap = (
name => $apinfo->{'accessPointDetailsDTO'}->{'name'},
ethmac => $apinfo->{'accessPointDetailsDTO'}->{'ethernetMac'},
wmac => $apinfo->{'accessPointDetailsDTO'}->{'macAddress'},
serial => $apinfo->{'accessPointDetailsDTO'}->{'serialNumber'},
ip => $apinfo->{'accessPointDetailsDTO'}->{'ipAddress'},
model => $apinfo->{'accessPointDetailsDTO'}->{'model'},
location => $apinfo->{'accessPointDetailsDTO'}->{'locationHeirarchy'},
controller => $controller,
associated => $associated,
uptime => $uptime,
neighbor_name => $neighbor_name,
neighbor_addr => $neighbor_addr,
neighbor_port => $neighbor_port,
no_cdp => $no_cdp,
clients_total => $apinfo->{'accessPointDetailsDTO'}->{'clientCount'},
clients_2ghz => $apinfo->{'accessPointDetailsDTO'}->{'clientCount_2_4GHz'},
clients_5ghz => $apinfo->{'accessPointDetailsDTO'}->{'clientCount_5GHz'},
);
# add info
$aps{$ap{ethmac}} = \%ap unless $aps{$ap{ethmac}};
# add VD for this location
unless ($locations{$ap{location}}){
$locations{$ap{location}} = [];
}
unless ($aplol->array_contains($locations{$ap{location}}, $vd)){
push(@{$locations{$ap{location}}}, $vd);
}
}
}
return \%aps;
}
# update all APs
sub update_from_prime{
my $db_vds = $aplol->get_vds();
# iterate through all VDs
# TODO: update DB with available VDs,
# (not possible until this info is available via PI API)
# (for now, we have to maintain the VDs in the DB manually)
foreach my $vd (keys %$db_vds){
my $aps = get_aps($vd);
if($vd =~ m/^ROOT-DOMAIN$/){
# all APs -- this is the authorative source
$root_aps = $aps;
}
}
# at this point we should have all locations
# and what VDs they belong to
# iterate through locations, add/remove, fix mapping
update_locations();
# now all locations should be up to date
# now we can add/deactivate/update APs
update_aps();
}
# update all locations in DB
sub update_locations{
# get all locations from DB
my $db_locations = $aplol->get_locations();
# delete all entries present in both lists
foreach my $location (keys %locations){
if($db_locations->{$location}){
# update VD's for this location in DB
update_location($location);
# delete from both
delete($locations{$location});
delete($db_locations->{$location});
}
}
# then delete everything from DB that
# only has an entry in the DB
foreach my $location (keys %$db_locations){
$aplol->delete_location($db_locations->{$location}{id});
}
# then add everything to DB that has
# an entry in the report list
foreach my $location (keys %locations){
# add location
$aplol->add_location($location);
# add VD <-> location mapping
foreach my $vd (@{$locations{$location}}){
$aplol->add_location_vd($vd, $location);
}
}
}
# update all VD's for a specific location
sub update_location{
my $location = "@_";
# get all current VD's for location
my $db_location_vds = $aplol->get_location_vds($location);
# check with current VD's
my %location_vds = map { $_ => 1 } @{$locations{$location}};
foreach my $vd (keys %location_vds){
if($db_location_vds->{$vd}){
# delete from both
delete($location_vds{$vd});
delete($db_location_vds->{$vd});
}
}
# delete from DB if only present in DB
foreach my $vd (keys %$db_location_vds){
$aplol->delete_location_vd($db_location_vds->{$vd}{vd_id}, $db_location_vds->{$vd}{location_id});
}
# add to DB if not present
foreach my $vd (keys %location_vds){
$aplol->add_location_vd($vd, $location);
}
}
# update all AP's
sub update_aps{
$wlcs = $aplol->get_wlcs_name();
$db_aps = $aplol->get_aps();
foreach my $ethmac (sort keys %$root_aps){
if($db_aps->{$ethmac}){
# update AP-info
update_ap($ethmac);
# delete from both
delete($root_aps->{$ethmac});
delete($db_aps->{$ethmac});
}
}
# deactivate if only present in DB
foreach my $ethmac (keys %$db_aps){
if($db_aps->{$ethmac}{active}){
# but only if not already deactivated
# we do this to avoid updating the 'updated' column
# hence the 'updated' column is "frozen" to
# whenever the AP was first deactivated
$aplol->deactivate_ap($ethmac);
}
}
# add to DB if not present
foreach my $ethmac (keys %$root_aps){
my $wlc_id = get_wlc_id($ethmac);
unless(defined($wlc_id)){
error_log("Controller '$root_aps->{$ethmac}{controller}' does not exist in DB. Please fix.");
next;
}
my $location_id = get_location_id($ethmac);
unless(defined($location_id)){
error_log("Location '$root_aps->{$ethmac}{location}' does not exist in DB. Please fix.");
next;
}
# at this point we should have valid $wlc_id and $location_id
$root_aps->{$ethmac}{wlc_id} = $wlc_id;
$root_aps->{$ethmac}{location_id} = $location_id;
$aplol->add_ap($root_aps->{$ethmac});
}
}
# find WLC ID, if associated
sub get_wlc_id{
my ($ethmac) = @_;
my $wlc_id;
if ($root_aps->{$ethmac}{controller} =~ m/^unassociated$/){
# not associated
$wlc_id = '0';
} else {
# associated
if ($wlcs->{$root_aps->{$ethmac}{controller}}){
# controller exists in DB
$wlc_id = $wlcs->{$root_aps->{$ethmac}{controller}}{id};
} else {
# does not exist, should only happen if we forgot to add new controllers
$wlc_id = undef;
}
}
return $wlc_id;
}
# find location ID
sub get_location_id{
my $ethmac = "@_";
my $location_id;
my $location_item = $aplol->get_location($root_aps->{$ethmac}{location});
if($location_item){
$location_id = $location_item->{id};
} else {
$location_id = undef;
}
return $location_id;
}
# update AP-info
# reports are always authorative
sub update_ap{
my ($ethmac) = @_;
my $wlc_id = get_wlc_id($ethmac);
unless(defined($wlc_id)){
error_log("Controller '$root_aps->{$ethmac}{controller}' does not exist in DB. Please fix.");
return;
}
my $location_id = get_location_id($ethmac);
unless(defined($location_id)){
error_log("Location '$root_aps->{$ethmac}{location}' does not exist in DB. Please fix.");
return;
}
# at this point we should have valid $wlc_id and $location_id
$root_aps->{$ethmac}{wlc_id} = $wlc_id;
$root_aps->{$ethmac}{location_id} = $location_id;
# make sure that we keep CDP neighbor information
# only do this if new value is blank/null/undef
# set new value to the same as we have in the DB
if($root_aps->{$ethmac}{no_cdp}){
$root_aps->{$ethmac}{neighbor_name} = $db_aps->{$ethmac}{neighbor_name};
$root_aps->{$ethmac}{neighbor_addr} = $db_aps->{$ethmac}{neighbor_addr};
$root_aps->{$ethmac}{neighbor_port} = $db_aps->{$ethmac}{neighbor_port};
}
$aplol->update_ap($root_aps->{$ethmac});
}
# get all alarms
sub update_alarms{
my $severity = 'CRITICAL';
my $alarms = get_alarminfo($severity);
if($alarms){
# reset all alarms (so we don't get old in DB)
$aplol->reset_alarms();
foreach my $alarm (@$alarms){
if($alarm->{'alarmsDTO'}->{'category'}->{'value'} =~ m/^AP$/){
# Alarm-type is AP
# Next if radio-interface-down alarms
next if($alarm->{'alarmsDTO'}->{'deviceName'} =~ m/Interface 802.11/);
# get last annotation
my ($alarm_annotation, $annotation_timestamp) = get_last_alarm_annotation($alarm->{'alarmsDTO'}->{'annotations'});
# get AP radio mac
my $wmac;
if($alarm->{'alarmsDTO'}->{'deviceName'} =~ m/,/){
# contains comma, old syntax
# $apname,$wmac
$wmac = (split(',', $alarm->{'alarmsDTO'}->{'deviceName'}))[1];
} elsif($alarm->{'alarmsDTO'}->{'deviceName'} =~ m/#/){
# contains hash, new syntax
# $wmac#$apname
$wmac = (split('#', $alarm->{'alarmsDTO'}->{'deviceName'}))[0];
} else {
# assume temporary syntax
# $wmac:$apname
$wmac = substr($alarm->{'alarmsDTO'}->{'deviceName'}, 0, 17);
}
# needs to be valid MAC address
unless($aplol->valid_mac($wmac)){
error_log("Invalid MAC address detected: '$wmac'");
next;
}
# timestamps
my $alarm_created = $alarm->{'alarmsDTO'}->{'timeStamp'};
my $alarm_updated = $alarm->{'alarmsDTO'}->{'lastUpdatedAt'};
my $alarm_timestamp = $alarm_updated;
my $alarm_string = 'undef';
if(floor(str2time($annotation_timestamp)) > floor(str2time($alarm_updated))){
# only if annotation was created after alarm was updated
# we always set the $alarm_updated regardless if we have an annotation
$alarm_string = $alarm_annotation;
}
# lets update DB
debug_log("update alarm: $wmac, $alarm_timestamp, $alarm_string");
$aplol->update_alarm($wmac, $alarm_timestamp, $alarm_string);
}
}
}
}
# fetch latest annotation
sub get_last_alarm_annotation{
my $annotations = shift;
if(defined($annotations)){
foreach my $annotation_info (values %$annotations){
# $annotation_info is hash if only 1 value
# if multiple values, it's array of hashes
if (reftype $annotation_info eq reftype {}) {
# hash, should be single value
# just return noteText
return ($annotation_info->{'noteText'}, $annotation_info->{'creationTimestamp'});
} else {
# assume array of hashes
# sort it first, and then pick most recent date
my @sorted = sort { str2time($b->{'creationTimestamp'}) <=>
str2time($a->{'creationTimestamp'})}
@$annotation_info;
return ($sorted[0]->{'noteText'}, $sorted[0]->{'creationTimestamp'});
}
}
} else {
# no annotations
return ("undef", "1999-01-01 12:00:00");
}
}
# 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
$time_start = time(); # set start time
$aplol->connect();
# update info from PI
# add/delete locations
# add/update/delete location<->VD mapping
# add/deactivate/update APs
update_from_prime();
# update/set alarm annotations
update_alarms();
# disconnect
$aplol->disconnect();
# how long did it take?
show_runtime();
__DATA__
Do not remove. Makes sure flock() code above works as it should.