-
Notifications
You must be signed in to change notification settings - Fork 4
/
check_extreme_fdb.pl
executable file
·286 lines (254 loc) · 7.37 KB
/
check_extreme_fdb.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
#!/usr/bin/perl -w
#
# Copyright 2009-2017 Martin Scharm
#
# This file is part of bf-monitoring.
# <https://binfalse.de/software/nagios/>
# <https://github.com/binfalse/monitoring>
#
# bf-monitoring is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# bf-monitoring is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# bf-monitoring If not, see <http://www.gnu.org/licenses/>.
#
#################################################
#
# Monitor FDB table of an extreme networks device
# written by Martin Scharm
# see http://binfalse.de
#
#################################################
use strict;
use Net::SNMP;
use Getopt::Long;
use lib "/usr/lib/nagios/plugins/";
use utils qw($TIMEOUT %ERRORS);
my $FDB_TABLE = '1.3.6.1.4.1.1916.1.16.4.1';
my $FDB_TABLE_MAC = '1';
my $FDB_TABLE_VLAN = '2';
my $FDB_TABLE_PORT = '3';
my $FDB_TABLE_STATUS = '4';
my $VLAN_TABLE = '1.3.6.1.4.1.1916.1.2.1.2.1';
my $VLAN_INDEX = '1';
my $VLAN_DESCR = '2';
my $returnvalue = $ERRORS{"OK"};
my $returnstring = "";
my $returnsupp = "";
my $switch = undef;
my $community = undef;
my $help = undef;
my $print = 0;
my $warn = 0;
my $expected_file = undef;
# the hash containing expected entries
my %EXPECTED_ENTRIES = ();
# keys are the macs
# every object in this hash has following fields
my $EXPECTED_PORT = 0;
my $EXPECTED_VLAN = 1;
Getopt::Long::Configure ("bundling");
GetOptions(
'h' => \$help,
'help' => \$help,
's:s' => \$switch,
'switch:s' => \$switch,
'C:s' => \$community,
'community:s' => \$community,
'T:s' => \$TIMEOUT,
'timeout:s' => \$TIMEOUT,
'e:s' => \$expected_file,
'expected:s' => \$expected_file,
'W' => \$warn,
'warn' => \$warn,
'P' => \$print,
'print' => \$print
);
sub display_mac
{
my $mac = shift;
$mac =~ s/^0x//g;
$mac =~ s/[^:]{2}(?=[^\n ])/$&:/g;
return $mac;
}
sub compareable_mac
{
my $mac = shift;
# replace colons
$mac =~ s/://g;
# replace leading 0x
$mac =~ s/^0x//g;
# all to lower
return lc $mac;
}
sub print_usage
{
print "Usage: $0 -s <SWITCH> -C <COMMUNITY-STRING> -e <EXPECTED> [-W|--warn] [-T <TIMEOUT>] [-P|--print]\n\n";
print " <SWITCH> the switch's hostname or ip address\n";
print " <COMMUNITY-STRING> the community string as configured on the switch\n";
print " <TIMEOUT> max time to wait for an answer, defaults to ".$TIMEOUT."\n";
print " <EXPECTED> expected entries: if those mac addresses appear they are expected\n";
print " to be in that vlan and on that port, see --print\n";
print " -P | --print displays fdb, you can use this as a template for the <EXPECTED> file\n";
print " -W | --warn warn if an expected entry wasn't found\n";
}
# CHECKS
if ( defined($help) )
{
print_usage();
exit $ERRORS{"UNKNOWN"};
}
if ( !defined($switch) )
{
print "Need Switch-Address!\n";
print_usage();
exit $ERRORS{"UNKNOWN"};
}
if ( !defined($community) )
{
print "Need Community-String!\n";
print_usage();
exit $ERRORS{"UNKNOWN"};
}
if (!$print && !$expected_file)
{
print "Need File containing expected entries!\n";
print_usage();
exit $ERRORS{"UNKNOWN"};
}
# read the file with expected entries
if ($expected_file)
{
if (!open EXPECTED, $expected_file)
{
print "File $expected_file cannot be read!\n";
print_usage();
exit $ERRORS{"UNKNOWN"};
}
while (my $line = <EXPECTED>)
{
chomp $line;
next if $line =~ m/^#/;
my @fields = split "," , $line;
if (@fields != 3)
{
print "File $expected_file cannot be read! Unrecognized entry: $line\n";
print_usage();
exit $ERRORS{"UNKNOWN"};
}
$EXPECTED_ENTRIES{compareable_mac($fields[0])} = [$fields[1], $fields[2]];
}
close EXPECTED;
}
my ($session, $error) = Net::SNMP->session( -hostname => $switch, -version => 2, -community => $community, -timeout => $TIMEOUT);
# read vlan table
if (!defined($session)) {
printf("ERROR opening session: %s.\n", $error);
exit $ERRORS{"CRITICAL"};
}
my $vlan_table = $session->get_table(-baseoid => $VLAN_TABLE);
if (!defined($vlan_table))
{
printf("ERROR: Description table : %s.\n", $session->error);
$session->close;
exit $ERRORS{"CRITICAL"};
}
# building the hash with information on all the fans
my %vlan = ();
foreach my $k (keys %$vlan_table)
{
my ($type,$id) = ((split(/\./, $k)) [-2,-1]);
$vlan{$id}{$type} = $$vlan_table{$k};
}
# read fdb table
if (!defined($session)) {
printf("ERROR opening session: %s.\n", $error);
exit $ERRORS{"CRITICAL"};
}
my $fdb_table = $session->get_table(-baseoid => $FDB_TABLE);
if (!defined($fdb_table))
{
printf("ERROR: Description table : %s.\n", $session->error);
$session->close;
exit $ERRORS{"CRITICAL"};
}
# building the hash with information on all the fans
my %fdb = ();
foreach my $k (keys %$fdb_table)
{
my $ks = substr $k, length ($FDB_TABLE) + 1;
my $type = ((split(/\./, $ks)) [0]);
my $id = substr $ks, 2;
$fdb{$id}{$type} = $$fdb_table{$k} if ($type != $FDB_TABLE_MAC);
$fdb{$id}{$type} = compareable_mac ($$fdb_table{$k}) if ($type == $FDB_TABLE_MAC);
}
my $discrepancies = 0;
my $nohit = 0;
$returnsupp .= "expected " . (keys %EXPECTED_ENTRIES) . " entries in fdb; ";
# print the fdb
if ($print)
{
print "mac---------------port-vlan---\n";
foreach my $k (sort keys %fdb)
{
print display_mac ($fdb{$k}{$FDB_TABLE_MAC}) . ",". $fdb{$k}{$FDB_TABLE_PORT} . "," . $vlan{$fdb{$k}{$FDB_TABLE_VLAN}}{$VLAN_DESCR} . "\n" if $fdb{$k}{$FDB_TABLE_MAC} && $fdb{$k}{$FDB_TABLE_PORT} && $fdb{$k}{$FDB_TABLE_VLAN};
}
exit $ERRORS{"UNKNOWN"};
}
else
{
foreach my $k (sort keys %fdb)
{
if ($fdb{$k}{$FDB_TABLE_MAC} && $EXPECTED_ENTRIES{$fdb{$k}{$FDB_TABLE_MAC}})
{
my $mac = $fdb{$k}{$FDB_TABLE_MAC};
# port correct?
if ($EXPECTED_ENTRIES{$mac}[$EXPECTED_PORT] ne '*' && $EXPECTED_ENTRIES{$mac}[$EXPECTED_PORT] != $fdb{$k}{$FDB_TABLE_PORT})
{
$returnsupp .= "port of ".display_mac ($mac)." doesn't match: " . $EXPECTED_ENTRIES{$mac}[$EXPECTED_PORT] . " != " . $fdb{$k}{$FDB_TABLE_PORT} . "; ";
$discrepancies++;
}
# vlan correct?
if ($EXPECTED_ENTRIES{$mac}[$EXPECTED_VLAN] ne '*' && $EXPECTED_ENTRIES{$mac}[$EXPECTED_VLAN] ne $vlan{$fdb{$k}{$FDB_TABLE_VLAN}}{$VLAN_DESCR})
{
$returnsupp .= "port of ".display_mac ($mac)." doesn't match: " . $EXPECTED_ENTRIES{$mac}[$EXPECTED_VLAN] . " != " . $vlan{$fdb{$k}{$FDB_TABLE_VLAN}}{$VLAN_DESCR} . "; ";
$discrepancies++;
}
$EXPECTED_ENTRIES{$mac}[$EXPECTED_PORT] = "found";
}
}
foreach my $expected (sort keys %EXPECTED_ENTRIES)
{
if ($EXPECTED_ENTRIES{$expected}[$EXPECTED_PORT] ne "found")
{
$returnsupp .= display_mac ($expected)." not found; ";
$nohit++;
}
}
}
if ($discrepancies == 0)
{
if ($nohit == 0 || !$warn)
{
print "All is fine!|" . $returnsupp;
exit $ERRORS{"OK"};
}
else
{
print $nohit." macs not found in fdb|" . $returnsupp;
exit $ERRORS{"WARNING"};
}
}
else
{
print $discrepancies . " discrepancies in fdb";
print " and ".$nohit." macs not found" if ($nohit != 0);
print "|" . $returnsupp;
exit $ERRORS{"CRITICAL"};
}