Netsync/SNMP.pm

filter: Statementstmt 90.0% 18 / 20 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.5% 25 / 27 CC 1 SCAR 0.4
CC
1
Cov
90%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Netsync/Scribe.pm ↑ Summary Netsync.pm »
1 package App::Netsync::SNMP;
2  
3 =head1 NAME
4  
5 App::Netsync::SNMP - SNMP framework
6  
7 =head1 DESCRIPTION
8  
9 This package contains functions for handling SNMP communications.
10  
11 =head1 SYNOPSIS
12  
13 use App::Netsync::SNMP;
14  
15 App::Netsync::SNMP::configure({
16 'SecName' => 'your username here',
17 'SecLevel' => 'AuthPriv',
18 'AuthProto' => 'SHA',
19 'AuthPass' => 'your password here',
20 'PrivProto' => 'AES',
21 'PrivPass' => 'your key here',
22 },[
23 'IF-MIB','ENTITY-MIB', # standard
24 'CISCO-STACK-MIB', # Cisco
25 'FOUNDRY-SN-AGENT-MIB', # Brocade
26 'SEMI-MIB', # HP
27 ]);
28  
29 my $ip = '93.184.216.119';
30 my $session = App::Netsync::SNMP::Session $ip;
31  
32 my $info1 = App::Netsync::SNMP::Info $ip;
33 my $info2 = App::Netsync::SNMP::Info $session;
34  
35 my ($ifNames,$ifIIDs) = App::Netsync::SNMP::get1 ([
36 ['.1.3.6.1.2.1.31.1.1.1.1' => 'ifName'],
37 ['.1.3.6.1.2.1.2.2.1.2' => 'ifDescr'],
38 ],$session);
39  
40 App::Netsync::SNMP::set ('ifAlias',$_,'Vote for Pedro',$session) foreach @$ifIIDs;
41  
42 =cut
43  
44  
45 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 ▶use autodie; #XXX Is autodie adequate?
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
52 0 ▶use SNMP;
Subroutine BEGIN: called CC=1 SCAR=2.6
53 use SNMP::Info;
54 use version;
55  
56 our ($SCRIPT,$VERSION);
57 our %config;
58  
59 BEGIN {
60 ($SCRIPT) = fileparse ($0,"\.[^.]*");
61 ($VERSION) = version->declare('v4.0.0');
62 }
63  
64 INIT {
65 $config{'AuthPass'} = undef;
66 $config{'AuthProto'} = 'MD5';
67 $config{'Community'} = 'public';
68 $config{'Context'} = undef;
69 $config{'ContextEngineId'} = undef;
70 $config{'DestHost'} = undef;
71 $config{'PrivPass'} = undef;
72 $config{'PrivProto'} = 'DES';
73 $config{'RemotePort'} = 161;
74 $config{'Retries'} = 5;
75 $config{'RetryNoSuch'} = 0;
76 $config{'SecEngineId'} = undef;
77 $config{'SecLevel'} = 'noAuthNoPriv';
78 $config{'SecName'} = 'initial';
79 $config{'Timeout'} = 1000000;
80 $config{'Version'} = 3;
81  
82 $config{'MIBdir'} = '/usr/share/'.$SCRIPT.'/mib';
83 SNMP::addMibDirs($config{'MIBdir'});
84 }
85  
86  
87 =head1 METHODS
88  
89 =head2 configure
90  
91 configure the operating environment
92  
93 B<Arguments>
94  
95 I<( \%environment , \@MIBs )>
96  
97 =over 3
98  
99 =item environment
100  
101 key-value pairs of environment configurations
102  
103 B<Available Environment Settings>
104  
105 =over 4
106  
107 =item MIBdir
108  
109 the location of necessary MIBs
110  
111 default: F</usr/share/E<lt>script nameE<gt>/mib>
112  
113 =back
114  
115 See SNMP::Session documentation for more acceptable settings.
116  
117 =item MIBs
118  
119 a list of MIBs to load
120  
121 =back
122  
123 =cut
124  
125 sub configure {
126 warn 'too few arguments' if @_ < 2;
127 warn 'too many arguments' if @_ > 2;
128 my ($environment,$MIBs) = @_;
129  
130 $config{$_} = $environment->{$_} foreach keys %$environment;
131  
132 my $success = 1;
133 foreach my $MIB (@$MIBs) {
134 if (defined $MIB) {
135 $success = 0 unless defined SNMP::loadModules($MIB);
136 }
137 }
138 SNMP::initMib();
139  
140 $config{'ContextEngineId'} //= $config{'SecEngineId'};
141 unless (($config{'Version'} < 3) or
142 ($config{'SecLevel'} eq 'noAuthNoPriv') or
143 ($config{'SecLevel'} eq 'authNoPriv' and defined $config{'AuthPass'}) or
144 (defined $config{'AuthPass'} and defined $config{'PrivPass'})) {
145 warn 'SNMPv3 configuration is inadequate.';
146 $success = 0;
147 }
148 return $success;
149 }
150  
151  
152 =head2 Session
153  
154 returns an SNMP::Session object.
155  
156 I<Note: configure needs to be run first!>
157  
158 B<Arguments>
159  
160 I<( $ip )>
161  
162 =over 3
163  
164 =item ip
165  
166 an IP address to connect to
167  
168 =back
169  
170 =cut
171  
172 sub Session {
173 warn 'too few arguments' if @_ < 1;
174 warn 'too many arguments' if @_ > 1;
175 my ($ip) = @_;
176  
177 return SNMP::Session->new(
178 'AuthPass' => $config{'AuthPass'},
179 'AuthProto' => $config{'AuthProto'},
180 'Community' => $config{'Community'},
181 'Context' => $config{'Context'},
182 'ContextEngineId' => $config{'ContextEngineId'},
183 'DestHost' => $ip,
184 'PrivPass' => $config{'PrivPass'},
185 'PrivProto' => $config{'PrivProto'},
186 'RemotePort' => $config{'RemotePort'},
187 'Retries' => $config{'Retries'},
188 'RetryNoSuch' => $config{'RetryNoSuch'},
189 'SecEngineId' => $config{'SecEngineId'},
190 'SecLevel' => $config{'SecLevel'},
191 'SecName' => $config{'SecName'},
192 'Timeout' => $config{'Timeout'},
193 'Version' => $config{'Version'},
194 );
195 }
196  
197  
198 =head2 Info
199  
200 returns an SNMP::Info object
201  
202 I<Note: configure needs to be run first!>
203  
204 B<Arguments>
205  
206 I<( $ip )>
207  
208 =over 3
209  
210 =item ip
211  
212 an IP address to connect to OR an SNMP::Session
213  
214 =back
215  
216 I<Note: The following snippets are equivalent:>
217  
218 =over 3
219  
220 =item C<App::Netsync::SNMP::Info $ip;>
221  
222 =item C<App::Netsync::SNMP::Info App::Netsync::SNMP::Session $ip;>
223  
224 =back
225  
226 =cut
227  
228 sub Info {
229 warn 'too few arguments' if @_ < 1;
230 warn 'too many arguments' if @_ > 1;
231 my ($ip) = @_;
232  
233 my $session = Session $ip;
234 my $info = SNMP::Info->new(
235 'AutoSpecify' => 1,
236 'Session' => $session,
237 );
238 return ($session,$info);
239 }
240  
241  
242 =head2 get1
243  
244 attempt to retrieve an OID from a provided list, stopping on success
245  
246 B<Arguments>
247  
248 I<( \@OIDs , $ip )>
249  
250 =over 3
251  
252 =item OIDs
253  
254 a prioritized list of OIDs to try and retreive
255  
256 =item ip
257  
258 an IP address to connect to or an SNMP::Session
259  
260 =back
261  
262 =cut
263  
264 sub get1 {
265 warn 'too few arguments' if @_ < 2;
266 warn 'too many arguments' if @_ > 2;
267 my ($OIDs,$ip) = @_;
268  
269 my $session = $ip;
270 unless (blessed $session and $session->isa('SNMP::Session')) {
271 return undef if ref $session;
272 $session = SNMP $session;
273 return undef unless defined $session;
274 }
275  
276 my (@objects,@IIDs);
277 foreach my $OID (@$OIDs) {
278 my $query = SNMP::Varbind->new([$OID->[0]]);
279 while (my $object = $session->getnext($query)) {
280 last unless $query->tag eq $OID->[1] and not $session->{'ErrorNum'};
281 last if $object =~ /^ENDOFMIBVIEW$/;
282 $object =~ s/^\s*(.*?)\s*$/$1/;
283 push (@IIDs,$query->iid);
284 push (@objects,$object);
285 }
286 last if @objects != 0;
287 }
288 return undef if @objects == 0;
289 return (\@objects,\@IIDs);
290 }
291  
292  
293 =head2 set
294  
295 attempt to set a new value on a device using SNMP
296  
297 B<Arguments>
298  
299 I<( $OID , $IID , $value , $ip )>
300  
301 =over 3
302  
303 =item OID
304  
305 the OID to write the value argument to
306  
307 =item IID
308  
309 the IID to write the value argument to
310  
311 =item value
312  
313 the value to write to OID.IID
314  
315 =item ip
316  
317 an IP address to connect to OR an SNMP::Session
318  
319 =back
320  
321 =cut
322  
323 sub set {
324 warn 'too few arguments' if @_ < 4;
325 warn 'too many arguments' if @_ > 4;
326 my ($OID,$IID,$value,$ip) = @_;
327  
328 my $session = $ip;
329 unless (blessed $session and $session->isa('SNMP::Session')) {
330 return undef if ref $session;
331 $session = SNMP $session;
332 return undef unless defined $session;
333 }
334  
335 my $query = SNMP::Varbind->new([$OID,$IID,$value]);
336 $session->set($query);
337 return ($session->{'ErrorNum'}) ? $session->{'ErrorStr'} : 0;
338 }
339  
340  
341 =head1 AUTHOR
342  
343 David Tucker, C<< <dmtucker at ucsc.edu> >>
344  
345 =head1 BUGS
346  
347 Please report any bugs or feature requests to C<bug-netsync at rt.cpan.org>, or through
348 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=App-Netsync>. I will be notified, and then you'll
349 automatically be notified of progress on your bug as I make changes.
350  
351 =head1 SUPPORT
352  
353 You can find documentation for this module with the perldoc command.
354  
355 perldoc App::Netsync
356  
357 You can also look for information at:
358  
359 =over 4
360  
361 =item * RT: CPAN's request tracker (report bugs here)
362  
363 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=App-Netsync>
364  
365 =item * AnnoCPAN: Annotated CPAN documentation
366  
367 L<http://annocpan.org/dist/App-Netsync>
368  
369 =item * CPAN Ratings
370  
371 L<http://cpanratings.perl.org/d/App-Netsync>
372  
373 =item * Search CPAN
374  
375 L<http://search.cpan.org/dist/App-Netsync/>
376  
377 =back
378  
379 =head1 LICENSE
380  
381 Copyright 2013 David Tucker.
382  
383 This program is free software; you can redistribute it and/or modify it
384 under the terms of either: the GNU General Public License as published
385 by the Free Software Foundation; or the Artistic License.
386  
387 See L<http://dev.perl.org/licenses/> for more information.
388  
389 =cut
390  
391  
392 1;
« Netsync/Scribe.pm ↑ Summary Netsync.pm »