-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_cisco_wlan.pl
executable file
·557 lines (467 loc) · 13.3 KB
/
check_cisco_wlan.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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
#!/usr/bin/perl
#
# Copyright (C) 2017 Jon Nistor
#
# --
# Author: Jon Nistor ([email protected])
# Purporse: Monitoring Plugin to check for Cisco WLC controllers.
# Count the number of clients on the controller / per SSID.
# MIB: Based on CISCO-LWAPP-WLAN-MIB
# Vendors: Cisco
#
# Version: 0.02
#
# History:
#
# 2017-05-21 0.01 Initial
# 2017-06-22 0.02 Added IPv6 support.
#
#
# --
# NOTE: Plugin Exit Codes (ref: https://docs.icinga.com/latest/en/pluginapi.html#returncode)
# RetCode Svc-State Host State
# 0 OK UP
# 1 WARNING UP or DOWN/UNREACHABLE
# 2 CRITICAL DOWN/UNREACHABLE
# 3 UNKNOWN DOWN/UNREACHABLE
#
# --
use Monitoring::Plugin;
use Monitoring::Plugin::Getopt;
use Monitoring::Plugin::Threshold;
use Net::SNMP qw(oid_lex_sort);
use Socket qw( :DEFAULT );
use Socket6;
#
use strict;
use constant VERSION => '0.01';
$SIG{'ALRM'} = sub {
plugin_exit( UNKNOWN, "Plugin took too long to complete (alarm)");
};
# -----------------------------------------------------------------------------
# PROG: Build initial object
my $np = Monitoring::Plugin->new(
shortname => "",
usage => "Usage: %s [-H|--host <controller>] [-P|--snmpver <2|3>]" .
" [-s|--snmpcomm <comm>] [-d|--debug] [-v|--verbose]" .
" [-n|--ssid <SSIDname>] [-a|--all]" .
" [-w|--warning <clientCnt>] [-c|--critical <clientCnt>]" .
" [-4] [-6]" .
"",
version => VERSION,
url => 'https://github.com/nistorj/monitoring-plugins',
blurb => 'Check wlan client counts (total/per ssid)',
);
# -----------------------------------------------------------------------------
# PROG: Building arguments list, usage/help documentation
#
# -a | --all Flag to enable counting of all clients
# -d | --debug Enable debug output (not to be used in production)
# -H | --host Hostname of Cisco Wireless Controller
# -n | --ssid Specific SSID to match on
# -r | --result Integer value of answer (clients)
# -s | --snmpcomm SNMP community of the WLC.
# -P | --snmpver SNMP version to use in polling
# -v | --verbose Provide a little more output
$np->add_arg(
spec => 'all|a',
help => '-a, --all Enable total client count',
default => undef,
required => 0
);
$np->add_arg(
spec => 'debug|d',
help => '-d, --debug output',
default => undef,
required => 0
);
$np->add_arg(
spec => 'ssid|n=s',
help => '-n, --ssid=SSIDname WLAN name to match',
default => undef,
required => 0
);
$np->add_arg(
spec => 'host|H=s',
help => '-H, --host=HOSTNAME of Cisco wireless controller to poll',
default => undef,
required => 1
);
$np->add_arg(
spec => 'result|r=s',
help => '-r, --result=INTEGER Integer value of BGP State',
default => undef,
required => 0
);
$np->add_arg(
spec => 'snmpcomm|s=s',
help => '-s, --snmpcomm=STRING Communtiy read string',
default => undef,
required => 1,
);
$np->add_arg(
spec => 'snmpver|P=s',
help => '-P, --snmpver=(2|3) SNMP polling version',
default => 2,
required => 0
);
$np->add_arg(
spec => 'verbose|v',
help => '-v, --verbose display more information',
default => undef,
required => 0
);
$np->add_arg(
spec => 'clienthigh|A',
help => '-A, --clienthigh alert if session count is above value',
default => undef,
required => 0
);
$np->add_arg(
spec => 'clientlow|B',
help => '-B, --clientlow alert if session count drops below value',
default => undef,
required => 0
);
$np->add_arg(
spec => 'v4|4',
help => '-4, --v4 Force polling over IPv4',
default => undef,
required => 0
);
$np->add_arg(
spec => 'v6|6',
help => '-6, --v6 Force polling over IPv6',
default => undef,
required => 0
);
$np->add_arg(
spec => 'warning|w=s',
help => 'warning threshold',
required => 0
);
$np->add_arg(
spec => 'critical|c=s',
help => 'critical threshold',
required => 0
);
my $threshold_state = Monitoring::Plugin::Threshold->set_thresholds(
warning => $np->opts->{'warning'},
critical => $np->opts->{'critical'}
);
# Parse @ARGV and process standard arguments (e.g. usage, help, version)
$np->getopts;
if( defined( $np->opts->{'debug'} ) &&
$np->opts->{'debug'} == 1 ) # Enable up verbose as well?
{
$np->opts->{'verbose'} = 1;
# DBG: Print out args passed
print " OPT: Passing arguments.. \n";
print " OPT: host: " . $np->opts->{'host'} . "\n";
print " OPT: snmpver: " . $np->opts->{'snmpver'} . "\n";
print " OPT: clientlow: " . $np->opts->{'clientlow'} . "\n";
print " OPT: clienthigh: " . $np->opts->{'clienthigh'} . "\n";
print " OPT: result: " . $np->opts->{'result'} . "\n";
print " OPT: all: " . $np->opts->{'all'} . "\n";
print " OPT: ssid: " . $np->opts->{'ssid'} . "\n";
print " OPT: v4: " . $np->opts->{'v4'} . "\n";
print " OPT: v6: " . $np->opts->{'v6'} . "\n";
print " OPT: warn: " . $np->opts->{'warning'} . "\n";
print " OPT: crit: " . $np->opts->{'critical'} . "\n";
}
if( defined($np->opts->{'clientlow'}) && defined($np->opts->{'clienthigh'}) )
{
$np->plugin_exit( UNKNOWN, "Cannot define both clientlow and " .
" clienthigh values.");
}
if( defined($np->opts->{'all'}) && defined($np->opts->{'ssid'}) )
{
$np->plugin_exit( UNKNOWN, "Cannot define both ssid name and all.");
}
# -----------------------------------------------------------------------------
# SNMP: mib:oid information
my %wlanOIDtable = (
#----------------------------------------------------------------------
# NOTE: Using CISCO-LWAPP-WLAN-MIB information
'sysObjectID' => '1.3.6.1.2.1.1.2.0',
'cLWlanRowStatus' => '1.3.6.1.4.1.9.9.512.1.1.1.1.2',
'cLWlanProfileName' => '1.3.6.1.4.1.9.9.512.1.1.1.1.3',
'cLWlanSsid' => '1.3.6.1.4.1.9.9.512.1.1.1.1.4',
# MIB: AIRESPACE-WIRELESS-MIB
'bsnDot11EssNumberOfMobileStations' => '1.3.6.1.4.1.14179.2.1.1.1.38',
);
my %wlan;
# -----------------------------------------------------------------------------
# PROG: Construct SNMP session information.
my $addrHost = $np->opts->{'host'};
my @addrInfo = getaddrinfo($addrHost, 'snmp', AF_UNSPEC, SOCK_STREAM, 'udp');
$np->plugin_exit( UNKNOWN, "Cannot resolve $addrHost" ) unless( scalar(@addrInfo) >= 5 );
if( ( ($addrInfo[0] == AF_INET6) &&
(not defined($np->opts->{'v4'})) ) || defined($np->opts->{'v6'}) )
{
$np->opts->{'snmptransport'} = "udp6";
}
elsif( ( ($addrInfo[0] == AF_INET) &&
(not defined($np->opts->{'v6'})) ) || defined($np->opts->{'v4'}) )
{
$np->opts->{'snmptransport'} = "udp";
} else {
$np->plugin_exit( UNKNOWN, "Address family cann't resolve $addrHost" );
}
# DBG: What transport method?
if( $np->opts->{'debug'} )
{
print "SNMP: Transport will be " . $np->opts->{'snmptransport'} . "\n";
}
my @snmpopts;
if( $np->opts->{'snmpver'} == 2 || $np->opts->{'snmpver'} eq "2c" )
{
push(@snmpopts, 'community', $np->opts->{'snmpcomm'} );
}
elsif( $np->opts->{'snmpver'} == 3 )
{
my ($v3_user,$v3_pass,$v3_prot,$v3_priv_pass,$v3_priv_prot)
= split(":", $np->opts->{'snmpcomm'});
if( defined($v3_user) )
{
push(@snmpopts, 'username', $v3_user);
}
if( defined($v3_pass) )
{
push(@snmpopts, ($v3_pass =~ /^0x/) ?
'authkey' : 'authpassword', $v3_pass);
}
if( defined($v3_prot) )
{
push(@snmpopts, 'authprotocol', $v3_prot);
}
if( defined($v3_priv_pass) )
{
push(@snmpopts, ($v3_priv_pass =~ /^0x/) ?
'privkey' : 'privpassword', $v3_priv_pass);
}
if( defined($v3_priv_prot) )
{
push(@snmpopts, 'privprotocol', $v3_priv_prot);
}
if( $np->opts->{'debug'} )
{
print "SNMP: v3 user $v3_user\n";
print "SNMP: v3 authpass $v3_pass, authprot $v3_prot\n";
print "SNMP: v3 privpass $v3_priv_pass, privprot $v3_priv_prot\n";
}
}
elsif ( $np->opts->{'snmpcomm'} )
{
# SNMP: Force v2 if nothing else.
$np->opts->{'snmpver'} = 2;
push( @snmpopts, 'community', $np->opts->{'snmpcomm'} );
} else {
$np->plugin_exit( UNKNOWN, "No proper SNMP ver/comm combo" );
}
my ($session, $error) = Net::SNMP->session(
-nonblocking => 0,
-timeout => 15,
-retries => 1,
-hostname => $np->opts->{'host'},
-version => $np->opts->{'snmpver'},
-domain => $np->opts->{'snmptransport'},
-debug => 0x00,
@snmpopts
);
if( not defined($session) )
{
$np->plugin_exit( CRITICAL, "SNMP session check failed: " . $error );
exit(1);
}
# -----------------------------------------------------------------------------
# PROG: Start process of polling WLAN/SSID information
if( $np->opts->{'ssid'} )
{
_wlan_ssid();
}
elsif ( $np->opts->{'all'} )
{
_wlan_clientCount();
} else {
$np->plugin_exit( UNKNOWN, "Need to specify either 'all' or SSID wlan");
}
# VARS: Set a few vars.
my $result;
my @snmpoids;
# ------------------------------------------------------------------------------
#
sub _raw_debug
{
my $data = shift;
use Data::Dumper;
$Data::Dumper::Indent = 3;
print Dumper($data);
}
# ------------------------------------------------------------------------------
#
sub _wlan_ssid
{
if( $np->opts->{'debug'} )
{
my $this_subs_name = (caller(0))[3];
print " SUB: Function $this_subs_name processing ...\n";
}
#
if( not defined($np->opts->{'ssid'}) )
{
$np->plugin_exit( UNKNOWN, "No SSID wlan has been defined" );
}
my $e_ssid = shift || $np->opts->{'ssid'};
if( $e_ssid !~ /[a-z0-9]/i )
{
print "WLAN: $e_ssid\n";
$np->plugin_exit( CRITICAL,
"Invalid character detected in wlane" );
}
# PROG: First we need to pull the cLWlanSsid table
#
my $result = $session->get_table( -baseoid =>
$wlanOIDtable{'cLWlanSsid'} );
if( not defined($result) )
{
$session->close;
$np->plugin_exit( UNKNOWN, "Wireless Controller doesn't " .
"support CISCO-LWAPP-WLAN-MIB::cLWlanSsid" );
}
# -- Find specific peer information
my $ssidMatch = 0;
SSIDOID: foreach my $l_snmpOID ( oid_lex_sort( keys( %{$result}) ) )
{
next if( $result->{$l_snmpOID} ne $e_ssid );
$ssidMatch = 1;
my $baseLength = length( $wlanOIDtable{'cLWlanSsid'} ) + 1;
my $l_vars = substr( $l_snmpOID, $baseLength );
my $l_index = substr( $l_vars, 0 );
my $s_ssid = $result->{$l_snmpOID};
$wlan{'ssid'}{'name'} = $e_ssid;
$wlan{'ssid'}{'count'}++;
push( @{$wlan{'ssid'}{'index'}}, $l_index );
if( $np->opts->{'debug'} )
{
print "WLAN: added $e_ssid, index id $l_index\n";
}
}
if( $ssidMatch == 0 )
{
$np->plugin_exit( UNKNOWN, "WLAN error: Does ssid exist ".
"on this controller?" );
}
if( $np->opts->{'verbose'} )
{
print "WLAN: All instances found idx: " .
"@{$wlan{'ssid'}{'index'}}" . "\n";
}
foreach my $x_index ( sort @{$wlan{'ssid'}{'index'}} )
{
# PROG: check against each index number to build hash info.
_wlan_clientCount($x_index);
} # end:foreach
}
sub _wlan_clientCount
{
if( $np->opts->{'debug'} )
{
my $this_subs_name = (caller(0))[3];
print " SUB: Function $this_subs_name processing ...\n";
}
my $o_index = shift || 0;
# --
my $n_result = $session->get_table( -baseoid =>
$wlanOIDtable{'bsnDot11EssNumberOfMobileStations'} );
if( not defined($n_result) )
{
$session->close;
$np->plugin_exit( UNKNOWN, "Wireless controler doesn't support" .
" AIRESPACE-WIRELESS-MIB::bsnDot11EssNumberOfMobileStations" );
}
# -- Find specific client count per SSID
CNTOID: foreach my $l_snmpOID ( oid_lex_sort( keys( %{$n_result}) ) )
{
my $baseLength = length( $wlanOIDtable{'bsnDot11EssNumberOfMobileStations'} ) + 1;
my $l_vars = substr( $l_snmpOID, $baseLength );
my $l_index = substr( $l_vars, 0 );
next if( $o_index && ( $o_index != $l_index ) );
# PROG: Insert results of client counts
$wlan{'clients'}{'total'} += $n_result->{$l_snmpOID};
if( $np->opts->{'debug'} )
{
print " CNT: instance $l_index with " .
$n_result->{$l_snmpOID} . " clients.\n";
}
} # END:foreach
}
# ------------------------------------------------------------------------------
# PROG: Output results.
#
if( $np->opts->{'verbose'} )
{
print " DBG: Processing output\n";
}
my $userCount = $wlan{'clients'}{'total'};
my $o_result = $np->opts->{'result'};
# O/RESULT: If _RESULT_ option is matched, force OK.
if( defined($np->opts->{'result'}) &&
($np->opts->{'result'} == $userCount) )
{
$np->plugin_exit( OK, "Client count $userCount" );
} elsif (defined($np->opts->{'result'}) &&
($np->opts->{'result'} != $userCount) )
{
$np->plugin_exit( CRITICAL, "Client count $userCount, " .
"expecting $o_result" );
}
# O/NBRLOW
if( defined($np->opts->{'clientlow'}) )
{
my $x_crit = $np->opts->{'critical'};
my $x_warn = $np->opts->{'warning'};
if( defined($np->opts->{'critical'}) &&
($np->opts->{'critical'} >= $userCount)
) #end:if
{
$np->plugin_exit( CRITICAL, "Client count $userCount, low " .
"critical threshold $x_crit");
}
elsif( defined($np->opts->{'warning'}) &&
($np->opts->{'warning'} >= $userCount)
) #end:if
{
$np->plugin_exit( WARNING, "Client count $userCount, low " .
"warning threshold $x_warn");
}
}
# O/NBRHIGH
if( defined($np->opts->{'clienthigh'}) )
{
my $x_crit = $np->opts->{'critical'};
my $x_warn = $np->opts->{'warning'};
if( defined($np->opts->{'critical'}) &&
($np->opts->{'critical'} <= $userCount)
) #end:if
{
$np->plugin_exit( CRITICAL, "Client count $userCount, high " .
"critical threshold $x_crit");
}
elsif( defined($np->opts->{'warning'}) &&
($np->opts->{'warning'} <= $userCount)
) #end:if
{
$np->plugin_exit( WARNING, "Client count $userCount, high " .
"warning threshold $x_warn");
}
}
if( $np->opts->{'ssid'} )
{
$np->plugin_exit( OK, "SSID " . $np->opts->{'ssid'} .
" client count $userCount" );
} else {
$np->plugin_exit( OK, "Client count $userCount" );
}
# EOF
#