File: test_class.pl

package info (click to toggle)
libsnmp-info-perl 3.20-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,896 kB
  • ctags: 1,290
  • sloc: perl: 21,211; makefile: 2; sh: 1
file content (346 lines) | stat: -rwxr-xr-x 8,065 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/perl
#
# test_class.pl
#
# Copyright (c) 2013 Eric Miller
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#     * Redistributions of source code must retain the above copyright notice,
#       this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the University of California, Santa Cruz nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

use strict;
use warnings;
use Carp;
use Getopt::Long;
use Pod::Usage;
use SNMP::Info;

my $EMPTY = q{};

# Default Values
my $class  = $EMPTY;
my @dump   = ();
my $debug  = 0;
my $cache  = 0;
my $device = '';
my $comm   = '';
my $ver    = 2;
my $ignore = 0;
my $help   = 0;
my $nobulk = 0;
my $mibdirs;
my %dumped;

GetOptions(
    'c|class=s'  => \$class,
    'd|dev=s'    => \$device,
    's|comm=s'   => \$comm,
    'v|ver=i'    => \$ver,
    'i|ignore'   => \$ignore,
    'p|print=s'  => \@dump,
    'm|mibdir=s' => \$mibdirs,
    'n|nobulk'   => \$nobulk,
    'x|debug+'   => \$debug,
    'k|cache'    => \$cache,
    'h|?|help'   => sub { pod2usage(1); },
);

unless ( $device and $comm ) {
    pod2usage(2);
}

if ( $ignore && !defined $mibdirs ) {
    print "mibdirs must be provided if ignoring snmp.conf \n\n";
    pod2usage(1);
}

local $ENV{'SNMPCONFPATH'} = $EMPTY     if $ignore;
local $ENV{'MIBDIRS'}      = "$mibdirs" if $ignore;

if ( defined $mibdirs ) {
    SNMP::addMibDirs($mibdirs);
}

$class = $class ? "SNMP::Info::$class" : 'SNMP::Info';

( my $mod = "$class.pm" )
    =~ s{::}{/}xg;    # SNMP::Info::Layer3 => SNMP/Info/Layer3.pm
if ( !eval { require $mod; 1; } ) {
    croak "Could not load $class. Error Message: $@\n";
}

my $class_ver = $class->VERSION();

print
    "Class $class ($class_ver) loaded from SNMP::Info $SNMP::Info::VERSION.\n";

if ( scalar @dump ) { print 'Dumping : ', join( q{,}, @dump ), "\n" }

my %args = ();
if ($nobulk) {
    $args{BulkWalk} = 0;
}

my $dev = $class->new(
    'AutoSpecify' => 0,
    'AutoVerBack' => 0,
    'Debug'       => $debug,
    'Version'     => $ver,
    'DestHost'    => $device,
    'Community'   => $comm,
    %args
) or die "\n";

print "Connected to $device.\n";
print 'Detected Class: ', $dev->device_type(), "\n";
print "Using    Class: $class (-c to change)\n";

my $layers = $dev->layers();
my $descr  = $dev->description();

if ( !defined $layers || !defined $descr ) {
    die
	"Are you sure you got the right community string and version?\nCan't fetch layers or description.\n";
}

print "\nFetching base info...\n\n";

my @base_fns = qw/vendor model os os_ver description contact location
    layers mac serial/;

foreach my $fn (@base_fns) {
    test_global( $dev, $fn );
}

print "\nFetching interface info...\n\n";

my @fns = qw/interfaces i_type i_ignore i_description i_mtu i_speed i_mac i_up
    i_up_admin i_name i_duplex i_duplex_admin i_stp_state
    i_vlan i_pvid i_lastchange/;

foreach my $fn (@fns) {
    test_fn( $dev, $fn );
}

print "\nFetching VLAN info...\n\n";

my @vlan = qw/v_index v_name/;

foreach my $fn (@vlan) {
    test_fn( $dev, $fn );
}

print "\nFetching topology info...\n\n";

my @topo = qw/c_if c_ip c_port c_id c_platform/;

foreach my $fn (@topo) {
    test_fn( $dev, $fn );
}

print "\nFetching module info...\n\n";

my @modules = qw/e_descr e_type e_parent e_name e_class e_pos e_hwver
    e_fwver e_swver e_model e_serial e_fru/;

foreach my $fn (@modules) {
    test_fn( $dev, $fn );
}

foreach my $fn (@dump) {
    if ( !$dumped{$fn} ) { test_fn( $dev, $fn ) }
}

if ($cache) {
    eval {
        require Data::Printer;
    } && eval {
        print "\nDumping cache...\n\n";
        Data::Printer::p $dev;
    };
}

#--------------------------------

sub test_global {
    my $info   = shift;
    my $method = shift;

    my $value = $info->$method();

    if ( !defined $value ) {
	printf "%-20s Does not exist.\n", $method;
	return 0;
    }
    $value =~ s/[[:cntrl:]]+/ /gx;
    if ( length $value > 60 ) {
	$value = substr $value, 0, 60;
	$value .= '...';
    }
    printf "%-20s %s \n", $method, $value;
    return 1;
}

sub test_fn {
    my $info   = shift;
    my $method = shift;

    my $results = $info->$method();

    # If accidentally called on a global, pass it along nicely.
    if ( defined $results && !ref $results ) {
	return test_global( $dev, $method );
    }
    if ( !defined $results && !scalar keys %{$results} ) {
	printf "%-20s Empty Results.\n", $method;
	return 0;
    }

    printf "%-20s %d rows.\n", $method, scalar keys %{$results};
    if ( grep {/^$method$/x} @dump ) {
	$dumped{$method} = 1;
	foreach my $iid ( keys %{$results} ) {
	    print "  $iid : ";
	    if ( ref( $results->{$iid} ) eq 'ARRAY' ) {
		print '[ ', join( ', ', @{ $results->{$iid} } ), ' ]';
	    }
	    else {
		print $results->{$iid};
	    }
	    print "\n";
	}
    }
    return 1;
}

__END__

=head1 NAME

test_class.pl - Test a device against an SNMP::Info class.

=head1 AUTHOR

Eric Miller

=head1 SYNOPSIS

test_class.pl [options]

Options:

    -c|class    SNMP::Info class to use, Layer2::Catalyst    
    -d|dev      Device
    -s|comm     SNMP community
    -v|ver      SNMP version
    -p|print    Print values 
    -i|ignore   Ignore Net-SNMP configuration file
    -m|mibdir   Directory containing MIB Files
    -n|nobulk   Disable bulkwalk
    -x|debug    Debugging flag
    -k|cache    Dump cache (requires Data::Printer to be installed)
    -h|?|help   Brief help message

=head1 OPTIONS

=over 8

=item B<-class>

Specific SNMP::Info class to use.  Defaults to SNMP::Info if no specific
class provided.

-class Layer2::Catalyst

=item B<-dev>

Device to test against.  No default and a mandatory option.

-dev 1.2.3.4

=item B<-comm>

SNMP community string.  No default and a mandatory option.

-comm public

=item B<-ver>

SNMP version. Default 2.

-ver 1

=item B<-print>

Print values of a class method rather than summarizing.  May be repeated
multiple times. 

-print i_description -print i_type

=item B<-ignore >

Ignore Net-SNMP configuration file snmp.conf.  If this used mibdirs must be
provided.

-ignore

=item B<-mibdir>

Directory containing MIB Files.  Multiple directories should be separated by a
colon ':'. 

-mibdir /usr/local/share/snmp/mibs/rfc:/usr/local/share/snmp/mibs/net-snmp

=item B<-nobulk >

Disable SNMP bulkwalk. Default bulkwalk is on and utilized with version 2.

-nobulk

=item B<-debug>

Turns on SNMP::Info debug.

-debug

=item B<-cache>

Dumps the table and leaf cache at the end of running. Requires that the
L<Data::Printer> module be installed, otherwise does nothing.

-cache

=item B<-help>

Print help message and exits.

=back

=head1 DESCRIPTION

B<test_class.pl> will test a device against a specfied SNMP::Info class.
This allows debugging and testing of live devices to include validating
device support with existing classes.

=cut