/usr/share/perl5/Munin/Plugin/SNMP.pm is in munin-node 1.4.6-3ubuntu3.4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 | # -*- cperl -*-
#
# Copyright (C) 2004-2009 Dagfinn Ilmari Mannsaaker, Nicolai Langfeldt,
# Linpro AS
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2 dated June,
# 1991.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA.
# This Module is user documented inline, interspersed with code with
# perlpod. The documentation for the session() function is the
# pattern. Please maintain it in the same way.
# $Id: SNMP.pm 3868 2010-08-19 09:19:37Z mha $
=head1 NAME
Munin::Plugin::SNMP - Net::SNMP subclass for Munin plugins
=head1 SYNOPSIS
The Munin::Plugin::SNMP module extends L<Net::SNMP> with methods useful for
Munin plugins.
=head1 SNMP CONFIGURATION
SNMP plugins (that use this module) share a common configuration
interface implemented in the function session(). Please see the
documentation for that function for complete instructions and examples
on how to configure SNMP. The documentation is located there to
ensure that it is up to date and matches the code.
=head1 DEBUGGING
Additional debugging messages can be enabled by setting
C<$Munin::Plugin::SNMP::DEBUG>, C<$Munin::Plugin::DEBUG>, or by exporting
the C<MUNIN_DEBUG> environment variable before running the plugin (by
passing the C<--pidebug> option to C<munin-run>, for instance).
=cut
package Munin::Plugin::SNMP;
use strict;
use warnings;
use Net::SNMP;
use Munin::Plugin;
our (@ISA, $DEBUG);
@ISA = qw(Net::SNMP);
# Alias $Munin::Plugin::SNMP::DEBUG to $Munin::Plugin::DEBUG, so SNMP
# plugins don't need to import the latter module to get debug output.
*DEBUG = \$Munin::Plugin::DEBUG;
=head1 METHODS
=cut
sub config_session {
=head2 config_session() - Decode environment to get the needed plugin configuration parameters
($host, $port, $version, $tail) = Munin::Plugin::SNMP->config_session();
This is a convenience function for the "config" part of the plugin -
it decodes the environment/plugin name to retrieve the information
needed in the configuration phase. It returns a 4 tuple consisting of:
=over
=item 1) the host name
=item 2) the udp port to use
=item 3) the SNMP version to use (3 for version 3, 2 for version 1 or 2c)
=item 4) the tail of the plugin name: whatever is left of the plugin
name after "snmp_<host>_".
=back
The tail can be interesting for the "fetch" part of the plugin as
well.
=cut
my ($host, $port, $version, $tail);
# Decode plugin/symlink name and extract meaning from it - if possible.
if ($0 =~ /^(?:.*\/)?snmp(v3)?_([^_]+)_(.*)/) {
$version = '3' if $1;
$host = $2;
$tail = $3;
if ($host =~ /^([^:]+):(\d+)$/) {
$host = $1;
$port = $2;
}
}
# The environment overrides the symlink. The other way around is
# not useful.
$host = $ENV{host} || $host || die "Could not find hostname";
$version = $ENV{version} || $version || '2';
$port = $ENV{port} || $port || 161;
return ($host, $port, $version, $tail);
}
sub session {
my $class = shift;
my (@userargs) = @_;
=head2 session([optional Net::SNMP options]) - create new Munin::Plugin::SNMP object
$session = Munin::Plugin::SNMP->session();
This method overrides the Net::SNMP constructor to get the connection
information from the plugin name and/or environment. Please note that
no error string is returned. The function handles errors internaly -
giving a error message and calling die. Calling die is the right
thing to do.
The host name is taken from the plugin symlink, which must be on the
form C<snmp[v3]_E<lt>hostnameE<gt>_E<lt>plugin_nameE<gt>[_args]>.
The "v3" form is taken to mean that SNMPv3 is to be used. It is also
a name trick providing a separate "namespace" for devices that use
SNMPv3 so it can be configured separately in munin/plugin-conf.d/
files. E.g.:
[snmp_*]
env.version 2
env.community public
[snmpv3_*]
env.v3username snmpoperator
env.v3authpassword s3cr1tpa55w0rd
See below for how to configure for each different case. The first
case above shows Munin's default configuration.
NOTE: munin-node-configure does not yet utilize the "v3" thing.
The following environment variables are consulted:
=over
=item env.host
If the plugin name (symlink) does not contain the host name this is
used as the host name to connect to.
The host name must be specified, but is usually specified in the
plugin name. If the hostname somehow does not resolve in DNS (or the
hosts file) it is possible to do this:
[snmp_*]
env.version 2c
env.community floppa
[snmp_switch1.langfeldt.net]
env.host 192.168.2.45
[snmp_switch2.langfeldt.net]
env.host 192.168.2.46
=item env.port
The port to connect to. Default 161.
=item env.timeout
The timeout in seconds to use. Default 5.
=item env.version
The SNMP version to use for the connection. One of 1, 2, 3, snmpv1,
snmpv2c or snmpv3. SNMP v2 is better as it supports bulk operations.
Therefore 2 is the default in Munin::Plugin::SNMP. If your device
supports v3 that may be even better as it supports proper security -
but the encryption may slow things down.
Security is handled differently for versions 1/2c and 3. See below.
=cut
my ($host, $port, $version, $tail) = config_session();
# Common options.
my @options = (-hostname => $host,
-port => $port,
-version => $version,
);
# User defined options
if (defined($userargs[0])) {
push(@options,@userargs);
}
# Timeout
if (defined($ENV{timeout})) {
push(@options, (-timeout => $ENV{timeout}));
}
if ($version eq '1' or $version eq 'snmpv1' or
$version eq '2' or $version eq 'snmpv2c') {
=item env.community
The community name for version 1 and 2c agents. The default is
'public'. If this works your device is probably very insecure and
needs a security checkup.
=cut
# FIXME: die if $ENV{community} isn't set?
my $community = $ENV{community} || 'public';
push(@options,(-community => $community));
my $object;
my $error;
print STDERR "Setting up a SNMPv$version session\n" if $DEBUG;
($object, $error) = $class->SUPER::session(@options);
unless ($object) {
die "Could not set up SNMP $version session to $host: $error\n";
}
return $object;
} elsif ($version eq '3' or $version eq 'snmpv3') {
=item SNMP 3 authentication
SNMP v3 has three security levels: "noAuthNoPriv". If a username
and password is given it goes up to "authNoPriv".
If privpassword is given the security level becomes "authPriv" - the
connection is authenticated and encrypted.
Note: Encryption can slow down slow or heavily loaded network devices.
For most uses authNoPriv will be secure enough --- in SNMP v3 the
password is sent over the network encrypted in any case.
Munin::Plugin::SNMP does not support ContextEngineIDs and such for
authentication/privacy. If you see the need and know how it should be
done please send patches!
For further reading on SNMP v3 security models please consult RFC3414
and the documentation for Net::SNMP.
If version is set to 3 or snmpv3 these variables are used to define
authentication:
=over
=item env.v3username
SNMPv3 username. There is no default. Empty username ('') is allowed.
=item env.v3authpassword
SNMPv3 authentication password. Optional when encryption is also
enabled, in which case defaults to the privacy password.
Authentication requires a v3authprotocol, but this defaults to "md5"
and may therefore be left unspecified.
The password is sent encrypted (one way hash) over the network.
=item env.v3authprotocol
SNMPv3 authentication protocol. One of 'md5' or 'sha' (HMAC-MD5-96,
RFC1321 and SHA-1/HMAC-SHA-96, NIST FIPS PIB 180, RFC2264). The
default is 'md5'.
=item env.v3privpassword
SNMPv3 privacy password to enable encryption. An empty ('') password
is considered as no password and will not enable encryption.
Privacy requires a v3privprotocol as well as a v3authprotocol and a
v3authpassword, but all of these are defaulted (to 'des', 'md5', and
the v3privpassword value, respectively) and may therefore be left
unspecified.
=item env.v3privprotocol
If the v3privpassword is set this setting controls what kind of
encryption is used to achive privacy in the session. Only the very
weak 'des' encryption method is supported officially. The default is
'des'.
The implementing perl module (Net::SNMP) also supports '3des'
(CBC-3DES-EDE aka Triple-DES, NIST FIPS 46-3) as specified in IETF
draft-reeder-snmpv3-usm-3desede. Whether or not this works with any
particular device, we do not know.
=back
=cut
my $privpw = $ENV{'v3privpassword'} || '';
my $privproto = $ENV{'v3privprotocol'} || 'des';
my $authpw = $ENV{'v3authpassword'} || '';
my $authproto = $ENV{'v3authprotocol'} || 'md5';
my $username = $ENV{'v3username'};
if (defined($username)) {
# FIXME: isn't it an error if no username was specified?
push( @options, (-username => $username));
}
if ($privpw) {
# Privacy is a stronger demand and should be checked first.
push( @options, ( -privpassword => $privpw,
-privprotocol => $privproto,
-authpassword => ($authpw || $privpw),
-authprotocol => $authproto ));
# Note how Net::SNMP demands authentication options when
# privacy is invoked.
} elsif ($authpw) {
# Authenticated only.
push( @options,
( -authpassword => $authpw,
-authprotocol => $authproto ));
}
my ($object, $error) = $class->SUPER::session(@options);
if (!defined($object)) {
die "Could not set up SNMPv3 session to $host: $error\n";
}
return $object;
} else {
die "Unknown SNMP version: '$version'.";
}
}
=back
=head2 get_hash() - retrieve a table as a hash of hashes
$result = $session->get_hash(
[-callback => sub {},] # non-blocking
[-delay => $seconds,] # non-blocking
[-contextengineid => $engine_id,] # v3
[-contextname => $name,] # v3
-baseoid => $oid,
-cols => \%columns
);
This method transforms the -baseoid and -cols to a array of -columns
and calls C<get_entries()> with all the other arguments. It then
transforms the data into a hash of hashes in the following manner:
The keys of the main hash are the last element(s) of the OIDs, after
C<$oid> and the matching keys from C<%columns> are removed. The values
are hashes with keys corresponding to the values of C<%columns> hash and
values from the subtables corresonding to the keys of C<%columns>.
For this to work, all the keys of C<-cols> must have the same number
of elements. Also, don't try to specify a next-to-next-to-leaf-node
baseoid, the principle it breaks both C<get_entries> and the logic in
C<get_hash>.
If (all) the OIDs are unavailable a defined but empty hashref is
returned.
Example:
$session->get_hash(
-baseoid => '1.3.6.1.2.1.2.2.1', # IF-MIB
-cols => {
1 => 'index',
2 => 'descr',
4 => 'mtu',
}
);
given the following SNMP table:
IF-MIB::ifIndex.1 = INTEGER: 1
IF-MIB::ifIndex.2 = INTEGER: 2
IF-MIB::ifDescr.1 = STRING: lo0
IF-MIB::ifDescr.2 = STRING: lna0
IF-MIB::ifType.1 = INTEGER: softwareLoopback(24)
IF-MIB::ifType.2 = INTEGER: ethernetCsmacd(6)
IF-MIB::ifMtu.1 = INTEGER: 32768
IF-MIB::ifMtu.2 = INTEGER: 1500
...
will return a hash like this:
'1' => {
'index' => '1',
'mtu' => '32768',
'descr' => 'lo0'
},
'2' => {
'index' => '2',
'descr' => 'lna0',
'mtu' => '1500'
}
=cut
sub get_hash {
my $self = shift;
my %args = @_;
my %ret;
my $base = delete $args{'-baseoid'};
my $cols = delete $args{'-cols'} or return;
my @bases = map { $base.'.'.$_; } keys %{$cols};
$args{-columns} = \@bases;
my $table = $self->get_entries(-columns => \@bases)
or return;
my $subtabs = join '|', keys %$cols;
my $re = qr/^\Q$base.\E($subtabs)\.(.*)/;
for my $key (keys %$table) {
$key =~ $re;
next unless defined($1 && $2);
$ret{$2}{$cols->{$1}} = $table->{$key};
}
return \%ret;
}
=head2 get_single() - Retrieve a single value by OID
$uptime = $session->get_single("1.3.6.1.2.1.1.3.0") || 'U';
If the call fails to get a value the above call sets $uptime to 'U'
which Munin interprets as "Undefined" and handles accordingly.
If you stop to think about it you should probably use C<get_hash()> (it
gets too much, but is good for arrays) or C<get_entries()> - it gets
exactly what you want, so you mus
=cut
# FIXME: how was that last sentence meant to finish?
sub get_single {
my $handle = shift;
my $oid = shift;
print STDERR "# Getting single $oid...\n" if $DEBUG;
my $response = $handle->get_request($oid);
if (!defined $response->{$oid} or $handle->error_status or $response->{$oid} eq 'noSuchObject') {
print STDERR "# Error getting $oid: ",$handle->error(),"\n"
if $DEBUG;
return;
}
return $response->{$oid};
}
=head2 get_by_regex() - Retrive table of values filtered by regex applied to the value
This example shows the usage for a netstat plugin.
my $tcpConnState = "1.3.6.1.2.1.6.13.1.1.";
my $connections = $session->get_by_regex($tcpConnState, "[1-9]");
It gets all OIDs based at $tcpConnState and only returns the ones that
contain a number in the value.
=cut
sub get_by_regex
{
my ($self, $baseoid, $regex) = @_;
my %result;
print "# Starting browse of table at $baseoid\n" if $DEBUG;
$baseoid =~ s/\.$//; # no trailing dots
my $response = $self->get_table(-baseoid => $baseoid);
unless ($response) {
print "# Failed to get table at $baseoid\n" if $DEBUG;
return;
}
while (my ($oid, $value) = each %$response) {
unless ($value =~ /$regex/) {
print "# '$value' doesn't match /$regex/. Ignoring\n" if $DEBUG;
next;
}
my ($index) = ($oid =~ m{^\Q$baseoid.\E(.*)})
or die "$oid doesn't appear to be a descendent of $baseoid";
$result{$index} = $value;
print "# Index '$index', value $value\n" if $DEBUG;
}
return \%result;
}
1;
=head1 TODO
Lots.
=head1 BUGS
Ilmari wrote: C<get_hash()> doesn't handle tables with sparse indices.
Nicolai Langfeldt: Actually I think it does.
=head1 SEE ALSO
L<Net::SNMP>
=head1 AUTHOR
Dagfinn Ilmari Mannsåker, Nicolai Langfeldt
Rune Nordbøe Skillingstad added timeout support.
=head1 COPYRIGHT/License.
Copyright (c) 2004-2009 Dagfinn Ilmari Mannsåker and Nicolai Langfeldt.
All rights reserved. This program is free software; you can
redistribute it and/or modify it under the terms of the GNU General
Public License as published by the Free Software Foundation; version 2
dated June, 1991.
=cut
1;
|