1 | 1 | simandl | # |
2 | | | # $Id: HotSaNICsnmp.pm,v 1.24 2004/02/08 17:04:51 bernisys Exp $ |
3 | | | # |
4 | | | |
5 | | | package HotSaNICsnmp; |
6 | | | |
7 | | | use Fcntl; |
8 | | | use HotSaNICparser; |
9 | | | |
10 | | | ($VERSION = '$Revision: 1.24 $') =~ s/.*(\d+\.\d+).*/$1/; |
11 | | | |
12 | | | # Get modulename |
13 | | | my $MODNAME = HotSaNICparser::get_module_name(); |
14 | | | |
15 | | | # Get HotSaNIC global configuration |
16 | | | my %conf = HotSaNICparser::get_config("../..",$MODNAME); |
17 | | | |
18 | | | # Hash to hold the probed/read versions of diffrerent agents. |
19 | | | my %agent_v; |
20 | | | |
21 | | | # |
22 | | | # get_session($host, $community, $ver) |
23 | | | # |
24 | | | # returns a new Net::SNMP session and sets version |
25 | | | # |
26 | | | |
27 | | | sub get_session { |
28 | | | my ($host, $comm, $ver) = @_; |
29 | | | |
30 | | | undef my $result; |
31 | | | |
32 | | | $result = Net::SNMP->session( |
33 | | | Hostname => $host, |
34 | | | Community => $comm, |
35 | | | Port => 161 |
36 | | | ); |
37 | | | |
38 | | | if(defined($result)) { |
39 | | | $result->version($ver); |
40 | | | $result->timeout(2); |
41 | | | $result->retries(1); |
42 | | | } |
43 | | | |
44 | | | return $result; |
45 | | | } |
46 | | | |
47 | | | # |
48 | | | # probe_version($host, $community) |
49 | | | # |
50 | | | # Checks the given hosts ability to answer bulk requests and returns |
51 | | | # either '1' if no bulk request was possible and '2c' if it was. |
52 | | | # |
53 | | | |
54 | | | sub probe_version { |
55 | | | my $sys = 0; # use Net::SNMP by default |
56 | | | |
57 | | | eval { require Net::SNMP; }; |
58 | | | $sys = 1 if($@); # use system commands |
59 | | | |
60 | | | my ($host, $comm) = @_; |
61 | | | my $result = 1; # default return value is '1' |
62 | | | |
63 | | | # Now send a probe request to check which version to use |
64 | | | if($sys == 1) { |
65 | | | print time." $MODNAME: probe_version() fallback\n" if $conf{DEBUGLEVEL} >= 2; |
66 | | | return $result if !defined($conf{SNMPBULKWALK}); |
67 | | | open RESULT,"$conf{SNMPBULKWALK} -c $comm -v 2c -r 1 -t 2 $host .1.3.6.1.2.1.1.1|"; |
68 | | | $result = '2c' if $? == 0; |
69 | | | close RESULT; |
70 | | | } else { |
71 | | | print time." $MODNAME: probe_version() module\n" if $conf{DEBUGLEVEL} >= 2; |
72 | | | my $session = get_session($host, $comm, '2c'); |
73 | | | |
74 | | | return $result if !defined($session); |
75 | | | |
76 | | | my @test = '.1.3.6.1.2.1.1.1'; |
77 | | | |
78 | | | $result = '2c' if defined($session->get_bulk_request(-varbindlist => \@test)); |
79 | | | $session->close(); |
80 | | | } |
81 | | | |
82 | | | return $result; |
83 | | | } |
84 | | | |
85 | | | # |
86 | | | # set_version($host, $comm) |
87 | | | # |
88 | | | # trys to determit the version to use with specified host. If not in the |
89 | | | # info file the probe function will be called to set a version. |
90 | | | # |
91 | | | |
92 | | | sub set_version { |
93 | | | my ($host, $comm) = @_; |
94 | | | |
95 | | | $lockfile=$conf{VARDIR}."modules/$host.lock"; |
96 | | | $infofile=$conf{VARDIR}."modules/$host.info"; |
97 | | | |
98 | | | sleep 1 while !sysopen(LOCK, $lockfile, O_WRONLY|O_EXCL|O_CREAT, 0644); # grab lock or wait |
99 | | | |
100 | | | # Read info either from filesystem or probe it. |
101 | | | if( -e $infofile ) { |
102 | | | open (INFO, "<$infofile"); |
103 | | | $agent_v{$host} = <INFO>; |
104 | | | close INFO; |
105 | | | } else { |
106 | | | open (INFO, ">$infofile"); |
107 | | | $agent_v{$host} = probe_version($host, $comm); |
108 | | | print INFO $agent_v{$host}; |
109 | | | close INFO; |
110 | | | } |
111 | | | |
112 | | | close LOCK; # close lockfile |
113 | | | unlink $lockfile; # release lock |
114 | | | |
115 | | | print time." $MODNAME: set_version() [$agent_v{$host}]\n" if $conf{DEBUGLEVEL} >= 1; |
116 | | | } |
117 | | | |
118 | | | # |
119 | | | # snmp_walk($host, $comm, @oid) |
120 | | | # |
121 | | | # This function is used by the modules to retrieve one or more subtables from |
122 | | | # the SNMP tree on $host. The root OIDs are given in the third argument. |
123 | | | # The returned value is a reference to a hash which holds the result, |
124 | | | # where the OIDs are used as keys and guess the values are values from |
125 | | | # the snmptree. |
126 | | | # |
127 | | | # Note: The module has not to care about anything like using CPAN module |
128 | | | # or system commands, since this is handled transparent in this lib |
129 | | | # |
130 | | | |
131 | | | sub snmp_walk { |
132 | | | my ($host, $comm, @oid) = @_; |
133 | | | undef my %result; |
134 | | | |
135 | | | print time." $MODNAME: enter snmp_walk()\n" if $conf{DEBUGLEVEL} >= 3; |
136 | | | |
137 | | | set_version($host, $comm) if !defined($agent_v{$host}); |
138 | | | |
139 | | | for (@oid) { |
140 | | | my $res = snmp_mod_walk($host, $comm, $_); |
141 | | | next if !defined ($res); |
142 | | | foreach ( keys(%$res) ) { |
143 | | | $result{$_} = $$res{$_}; |
144 | | | } |
145 | | | } |
146 | | | |
147 | | | print time." $MODNAME: leave snmp_walk()\n" if $conf{DEBUGLEVEL} >= 3; |
148 | | | |
149 | | | return \%result; |
150 | | | } |
151 | | | |
152 | | | # |
153 | | | # snmp_get($host, $comm, @oid) |
154 | | | # |
155 | | | # Retrieves one value from the given $host. Works similar to snmp_walk |
156 | | | # and return also a hash reference. This way you can query more values |
157 | | | # with get @ once. |
158 | | | # |
159 | | | |
160 | | | sub snmp_get { |
161 | | | my ($host, $comm, @oid) = @_; |
162 | | | undef my %result; |
163 | | | |
164 | | | print time." $MODNAME: enter snmp_get()\n" if $conf{DEBUGLEVEL} >= 3; |
165 | | | |
166 | | | set_version($host, $comm) if !defined($agent_v{$host}); |
167 | | | |
168 | | | $result{$_} = snmp_mod_get($host, $comm, $_) for (@oid); |
169 | | | |
170 | | | print time." $MODNAME: leave snmp_get()\n" if $conf{DEBUGLEVEL} >= 3; |
171 | | | |
172 | | | return \%result; |
173 | | | } |
174 | | | |
175 | | | # |
176 | | | # snmp_mod_get($host, $community, $oid) |
177 | | | # |
178 | | | # This is the Net::SNMP based implementation of snmp_get, which will |
179 | | | # hand out control to the system command fallback, if module is not |
180 | | | # availible. |
181 | | | # |
182 | | | |
183 | | | sub snmp_mod_get { |
184 | | | eval { require Net::SNMP; }; |
185 | | | return snmp_sys_get(@_) if($@); |
186 | | | |
187 | | | my ($hostname, $community, $oid) = @_; |
188 | | | undef my $response; |
189 | | | |
190 | | | my $session = get_session($hostname, $community, $agent_v{$hostname}); |
191 | | | |
192 | | | return undef if !defined($session); |
193 | | | |
194 | | | if(!defined($response = $session->get_request($oid))) { |
195 | | | $session->close; |
196 | | | return undef; |
197 | | | } |
198 | | | |
199 | | | $session->close; |
200 | | | print time." $MODNAME: snmp_mod_get() [$$response{$oid}]\n" if $conf{DEBUGLEVEL} >= 3; |
201 | | | return $$response{$oid}; |
202 | | | } |
203 | | | |
204 | | | # |
205 | | | # snmp_mod_walk($host, $community, $oid) |
206 | | | # |
207 | | | # The Net::SNMP based implementation of snmp_walk, which will hand |
208 | | | # out control to system command based fallback, if module is |
209 | | | # not availible. |
210 | | | # |
211 | | | |
212 | | | sub snmp_mod_walk { |
213 | | | eval { require Net::SNMP; }; |
214 | | | return snmp_sys_walk(@_) if($@); |
215 | | | |
216 | | | my ($hostname, $community, $oid) = @_; |
217 | | | undef my $response; |
218 | | | |
219 | | | my $session = get_session($hostname, $community, $agent_v{$hostname}); |
220 | | | |
221 | | | return undef if(!defined($session)); |
222 | | | |
223 | | | if(!defined($response = $session->get_table($oid))) { |
224 | | | $session->close; |
225 | | | return undef; |
226 | | | } |
227 | | | |
228 | | | $session->close; |
229 | | | print time." $MODNAME: snmp_mod_walk() [$response]\n" if $conf{DEBUGLEVEL} >= 3; |
230 | | | return $response; |
231 | | | } |
232 | | | |
233 | | | # |
234 | | | # snmp_sys_walk($host, $community, $oid) |
235 | | | # |
236 | | | # THis is the system command based fallback to snmp_walk, which |
237 | | | # will be used if no CPAN module is availible on the system using |
238 | | | # snmp_walk. |
239 | | | # |
240 | | | |
241 | | | sub snmp_sys_walk { |
242 | | | my ($host,$com,$oid)=@_; |
243 | | | undef my %result; |
244 | | | |
245 | | | defined($conf{SNMPWALK}) || return undef; |
246 | | | |
247 | | | my $command; |
248 | | | |
249 | | | if($agent_v{$host} eq '2c') { |
250 | | | $command = "$conf{SNMPBULKWALK} -c $com -v 2c -r 1 -t 2 -On -Oq $host $oid"; |
251 | | | } else { |
252 | | | $command = "$conf{SNMPWALK} -c $com -v 1 -r 1 -t 2 -On -Oq $host $oid"; |
253 | | | } |
254 | | | |
255 | | | open OUTPUT,"$command|" || return undef; |
256 | | | while (<OUTPUT>) { |
257 | | | chomp; |
258 | | | my ($key, $value) = split; |
259 | | | $result{$key} = $value; |
260 | | | } |
261 | | | close OUTPUT; |
262 | | | |
263 | | | print time." $MODNAME: snmp_sys_walk() [".\%result."]\n" if $conf{DEBUGLEVEL} >= 3; |
264 | | | return \%result; |
265 | | | } |
266 | | | |
267 | | | # |
268 | | | # snmp_sys_get($host, $community, $oid) |
269 | | | # |
270 | | | # Analog to all the others here is the fallback to system commands |
271 | | | # for the get function. Used if, how could it be, no CPAN module |
272 | | | # availible. |
273 | | | # |
274 | | | |
275 | | | sub snmp_sys_get { |
276 | | | my ($host,$com,$oid)=@_; |
277 | | | defined($conf{SNMPGET}) || return undef; |
278 | | | |
279 | | | open RESULT,"$conf{SNMPGET} -c $com -v $agent_v{$host} -r 1 -t 2 -Oq $host $oid|" || return undef; |
280 | | | my $result=<RESULT>; |
281 | | | close RESULT; |
282 | | | |
283 | | | chomp $result; |
284 | | | (undef, $result) = split / /,$result; |
285 | | | |
286 | | | print time." $MODNAME: snmp_sys_get() [$result]\n" if $conf{DEBUGLEVEL} >= 3; |
287 | | | return $result; |
288 | | | } |
289 | | | |
290 | | | 1; |
291 | | | |