blob: 26ada441ba961f197dba847459817f8f6d60ee0a (
plain)
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
|
#! /usr/bin/perl
use strict;
use warnings;
use SNMP;
use nms;
package nms::snmp;
use base 'Exporter';
our @EXPORT = qw();
BEGIN {
# $SNMP::debugging = 1;
# sudo mkdir /usr/share/mibs/site
# cd /usr/share/mibs/site
# wget -O- ftp://ftp.cisco.com/pub/mibs/v2/v2.tar.gz | sudo tar --strip-components=3 -zxvvf -
SNMP::initMib();
SNMP::addMibDirs("/srv/tgmanage/mibs/StandardMibs");
SNMP::addMibDirs("/srv/tgmanage/mibs/JuniperMibs");
SNMP::loadModules('SNMPv2-MIB');
SNMP::loadModules('ENTITY-MIB');
SNMP::loadModules('IF-MIB');
SNMP::loadModules('LLDP-MIB');
SNMP::loadModules('IP-MIB');
SNMP::loadModules('IP-FORWARD-MIB');
}
sub snmp_open_session {
my ($ip, $community, $async) = @_;
$async //= 0;
my %options = (UseEnums => 1);
if ($ip =~ /:/) {
$options{'DestHost'} = "udp6:$ip";
} else {
$options{'DestHost'} = "udp:$ip";
}
if ($community =~ /^snmpv3:(.*)$/) {
my ($username, $authprotocol, $authpassword, $privprotocol, $privpassword) = split /\//, $1;
$options{'SecName'} = $username;
$options{'SecLevel'} = 'authNoPriv';
$options{'AuthProto'} = $authprotocol;
$options{'AuthPass'} = $authpassword;
if (defined($privprotocol) && defined($privpassword)) {
$options{'SecLevel'} = 'authPriv';
$options{'PrivProto'} = $privprotocol;
$options{'PrivPass'} = $privpassword;
}
$options{'Version'} = 3;
} else {
$options{'Community'} = $community;
$options{'Version'} = 2;
}
my $session = SNMP::Session->new(%options);
if (defined($session) && ($async || defined($session->getnext('sysDescr')))) {
return $session;
} else {
die 'Could not open SNMP session to ' . $ip;
}
}
# Not currently in use; kept around for reference.
sub fetch_multi_snmp {
my ($session, @oids) = @_;
my %results = ();
# Do bulk reads of 40 and 40; seems to be about the right size for 1500-byte packets.
for (my $i = 0; $i < scalar @oids; $i += 40) {
my $end = $i + 39;
$end = $#oids if ($end > $#oids);
my @oid_slice = @oids[$i..$end];
my $localresults = $session->get_request(-varbindlist => \@oid_slice);
return undef if (!defined($localresults));
while (my ($key, $value) = each %$localresults) {
$results{$key} = $value;
}
}
return \%results;
}
|