File: SnmpUtils.pm

package info (click to toggle)
haci 0.98c-2.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,240 kB
  • sloc: perl: 23,790; javascript: 1,817; sh: 225; makefile: 8
file content (136 lines) | stat: -rw-r--r-- 3,220 bytes parent folder | download | duplicates (5)
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
package HaCi::SnmpUtils;

use warnings;
use strict;
use Net::SNMP qw(:snmp);

use HaCi::Log qw/warnl debug/;

require Exporter;
our @ISA				= qw(Exporter);
our @EXPORT_OK	= qw(
	getSNMPSession getSNMPTree getSNMPValue
);

our $conf; *conf  = \$HaCi::Conf::conf;

sub getSNMPSession {
	my $host			= shift;
	my $comm			= shift;
	my $opts			= shift || {};
	my $port			= $opts->{port} || 161;
	my $nBlocking	= $opts->{nBlocking} || 1;
	my $version		= $opts->{version} || 'snmpv2';
	my $timeout		= $opts->{timeout} || 10;
  $version			= 'snmpv' . $version unless $version =~ /^snmpv/;

	my ($session, $error) = Net::SNMP->session(
		-hostname			=> $host,
		-port					=> $port,
		-community		=> $comm,
		-version			=> $version,
		-nonblocking	=> $nBlocking,
		-timeout			=> $timeout
	);
	if (!defined($session)) {
		&warnl("Cannot initiate Session for " . $host . ": $error! Abort...", 1);
		return undef;
	}

	return $session;
}

sub getSNMPTree {
  my $session = shift;
	my $oid			= shift;
	my $results	= {};
  my $result  = undef;
	my $version	= $session->version;

  if ($version == 0) {
    $result = $session->get_next_request(
      -callback     => [\&snmpCB, $results, $oid],
      -delay        => 0,
      -varbindlist  => [$oid]
    );
  }
  elsif ($version == 1 || $version == 3) {
    $result = $session->get_bulk_request(
      -callback       => [\&snmpCB, $results, $oid],
      -maxrepetitions => 10,
      -delay          => 0,
      -varbindlist    => [$oid]
    );
  }
  else {
    &warnl("Bad SNMP Version '$version'!", 1);
    return undef;
  }

  if (!defined($result)) {
    &warnl("Cannot retrieve any Results for System: " . $session->hostname . ": " . $session->error, 1);
    return undef;
  } 
    
  snmp_dispatcher();
  
  return $results;
}

sub snmpCB {
	my ($session, $results, $query)	= @_;
	my $version											= $session->version;

	if (!defined($session->var_bind_list)) {
		&warnl("SNMP-Error: " . $session->error, 1);
	} else {
		my $next  = undef;
		foreach my $oid (oid_lex_sort(keys(%{$session->var_bind_list}))) {
			if (!oid_base_match($query, $oid)) {
				$next = undef;
				last;
			}
			$next							= $oid; 
			my $value					= $session->var_bind_list->{$oid};
			$results->{$oid}	= $value;
		}
		if (defined($next)) {
			my $result  = undef;
			if ($version == 0) {
				$result = $session->get_next_request(
					-callback     => [\&snmpCB, $results, $query],
					-delay        => 0,
					-varbindlist  => [$next]
				);
		  }
			elsif ($version == 1 || $version == 3) {
				$result = $session->get_bulk_request(
					-callback       => [\&snmpCB, $results, $query],
					-maxrepetitions => 10,
					-delay          => 0,
					-varbindlist    => [$next]
				);
			} else {
        &warnl("Bad SNMP Version '$version'!", 1);
      }
      if (!defined($result)) {
        &warnl("SNMP-Errro: " . $session->error, 1);
      }
    }
  }
}

sub getSNMPValue {
	my $session = shift;
	my $snmpOID = shift;
	my $result	= $session->get_request(
		-varbindlist  => [$snmpOID] 
	);  
	if (!defined($result)) {
		&warnl("SNMP-Error: " . $session->error, 1) unless $session->error =~ /noSuchName/;
		return undef;
	}
	return (ref $result eq 'HASH') ? $result->{$snmpOID} : undef;
}

1;