| 1 |
package SNMP::Extension::PassPersist; |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
|
| 5 |
use parent qw< Class::Accessor >; |
| 6 |
|
| 7 |
use Carp; |
| 8 |
use Getopt::Long; |
| 9 |
use File::Basename; |
| 10 |
use IO::Handle; |
| 11 |
use IO::Pipe; |
| 12 |
use IO::Select; |
| 13 |
use List::MoreUtils qw< any >; |
| 14 |
use Storable qw< nfreeze thaw >; |
| 15 |
use Sys::Syslog; |
| 16 |
|
| 17 |
|
| 18 |
{ |
| 19 |
no strict "vars"; |
| 20 |
$VERSION = '0.07'; |
| 21 |
} |
| 22 |
|
| 23 |
use constant HAVE_SORT_KEY_OID |
| 24 |
=> eval "use Sort::Key::OID 0.04 qw<oidsort>; 1" ? 1 : 0; |
| 25 |
|
| 26 |
|
| 27 |
# early initialisations -------------------------------------------------------- |
| 28 |
my @attributes = qw< |
| 29 |
backend_collect |
| 30 |
backend_fork |
| 31 |
backend_init |
| 32 |
backend_pipe |
| 33 |
heap |
| 34 |
idle_count |
| 35 |
input |
| 36 |
oid_tree |
| 37 |
sorted_entries |
| 38 |
output |
| 39 |
refresh |
| 40 |
dispatch |
| 41 |
>; |
| 42 |
|
| 43 |
__PACKAGE__->mk_accessors(@attributes); |
| 44 |
|
| 45 |
|
| 46 |
# constants -------------------------------------------------------------------- |
| 47 |
use constant SNMP_NONE => "NONE"; |
| 48 |
use constant SNMP_PING => "PING"; |
| 49 |
use constant SNMP_PONG => "PONG"; |
| 50 |
use constant SNMP_GET => "get"; |
| 51 |
use constant SNMP_GETNEXT => "getnext"; |
| 52 |
use constant SNMP_SET => "set"; |
| 53 |
use constant SNMP_NOT_WRITABLE => "not-writable"; |
| 54 |
use constant SNMP_WRONG_TYPE => "wrong-type"; |
| 55 |
use constant SNMP_WRONG_LENGTH => "wrong-length"; |
| 56 |
use constant SNMP_WRONG_VALUE => "wrong-value"; |
| 57 |
use constant SNMP_INCONSISTENT_VALUE => "inconsistent-value"; |
| 58 |
|
| 59 |
|
| 60 |
# global variables ------------------------------------------------------------- |
| 61 |
my %snmp_ext_type = ( |
| 62 |
counter => "counter", |
| 63 |
counter64 => "counter64", |
| 64 |
gauge => "gauge", |
| 65 |
integer => "integer", |
| 66 |
ipaddr => "ipaddress", |
| 67 |
ipaddress => "ipaddress", |
| 68 |
netaddr => "ipaddress", |
| 69 |
objectid => "objectid", |
| 70 |
octetstr => "string", |
| 71 |
# opaque => "opaque", |
| 72 |
string => "string", |
| 73 |
timeticks => "timeticks", |
| 74 |
); |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
# |
| 79 |
# new() |
| 80 |
# --- |
| 81 |
sub new { |
| 82 |
my ($class, @args) = @_; |
| 83 |
my %attrs; |
| 84 |
my $ref = ref $args[0]; |
| 85 |
|
| 86 |
# see how arguments were passed |
| 87 |
if ($ref and $ref eq "HASH") { |
| 88 |
%attrs = %{$args[0]}; |
| 89 |
} |
| 90 |
else { |
| 91 |
croak "error: Don't know how to handle \L$ref reference" if $ref; |
| 92 |
croak "error: Odd number of arguments" if @args % 2 == 1; |
| 93 |
%attrs = @args; |
| 94 |
} |
| 95 |
|
| 96 |
# filter out unknown attributes |
| 97 |
my %known_attr; |
| 98 |
@known_attr{@attributes} = (1) x @attributes; |
| 99 |
!$known_attr{$_} && delete $attrs{$_} for keys %attrs; |
| 100 |
|
| 101 |
# check that code attributes are coderefs |
| 102 |
for my $code_attr (qw<backend_init backend_collect>) { |
| 103 |
croak "error: Attribute $code_attr must be a code reference" |
| 104 |
if defined $attrs{$code_attr} and ref $attrs{$code_attr} ne "CODE"; |
| 105 |
} |
| 106 |
|
| 107 |
# default values |
| 108 |
%attrs = ( |
| 109 |
backend_collect => sub {}, |
| 110 |
backend_fork => 0, |
| 111 |
backend_init => sub {}, |
| 112 |
heap => {}, |
| 113 |
input => \*STDIN, |
| 114 |
output => \*STDOUT, |
| 115 |
oid_tree => {}, |
| 116 |
sorted_entries => [], |
| 117 |
idle_count => 5, |
| 118 |
refresh => 10, |
| 119 |
dispatch => { |
| 120 |
lc(SNMP_PING) => { nargs => 0, code => \&ping }, |
| 121 |
lc(SNMP_GET) => { nargs => 1, code => \&get_oid }, |
| 122 |
lc(SNMP_GETNEXT) => { nargs => 1, code => \&getnext_oid }, |
| 123 |
lc(SNMP_SET) => { nargs => 2, code => \&set_oid }, |
| 124 |
}, |
| 125 |
%attrs, |
| 126 |
); |
| 127 |
|
| 128 |
# create the object with Class::Accessor |
| 129 |
my $self = $class->SUPER::new(\%attrs); |
| 130 |
|
| 131 |
return $self |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
# |
| 136 |
# run() |
| 137 |
# --- |
| 138 |
sub run { |
| 139 |
my ($self) = @_; |
| 140 |
|
| 141 |
# process command-line arguments |
| 142 |
Getopt::Long::Configure(qw<no_auto_abbrev no_ignore_case>); |
| 143 |
GetOptions(\my %options, qw<get|g=s getnext|n=s set|s=s>) |
| 144 |
or croak "fatal: An error occured while processing runtime arguments"; |
| 145 |
|
| 146 |
my $name = $::COMMAND || basename($0); |
| 147 |
openlog($name, "ndelay,pid", "local0"); |
| 148 |
|
| 149 |
my ($mode_pass, $mode_passpersist); |
| 150 |
my $backend_fork = $self->backend_fork; |
| 151 |
|
| 152 |
# determine the run mode |
| 153 |
if (any { defined $options{$_} } "get", "getnext", "set") { |
| 154 |
$mode_pass = 1; |
| 155 |
$mode_passpersist = 0; |
| 156 |
} |
| 157 |
else { |
| 158 |
$mode_pass = 0; |
| 159 |
$mode_passpersist = 1; |
| 160 |
} |
| 161 |
|
| 162 |
# execute the init and collect callback once, except in the case |
| 163 |
# where the backend run in a separate process |
| 164 |
unless ($mode_passpersist and $backend_fork) { |
| 165 |
# initialise the backend |
| 166 |
eval { $self->backend_init->($self); 1 } |
| 167 |
or croak "fatal: An error occurred while executing the backend " |
| 168 |
."initialisation callback: $@"; |
| 169 |
|
| 170 |
# collect the information |
| 171 |
eval { $self->backend_collect->($self); 1 } |
| 172 |
or croak "fatal: An error occurred while executing the backend " |
| 173 |
."collecting callback: $@"; |
| 174 |
} |
| 175 |
|
| 176 |
# Net-SNMP "pass" mode |
| 177 |
if ($mode_pass) { |
| 178 |
for my $op (qw<get getnext set>) { |
| 179 |
if ($options{$op}) { |
| 180 |
my @args = split /,/, $options{$op}; |
| 181 |
my $coderef = $self->dispatch->{$op}{code}; |
| 182 |
my @result = $coderef->($self, @args); |
| 183 |
$self->output->print(join "\n", @result, ""); |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
# Net-SNMP "pass_persist" mode |
| 188 |
else { |
| 189 |
my $needed = 1; |
| 190 |
my $delay = $self->refresh; |
| 191 |
my $counter = $self->idle_count; |
| 192 |
my ($pipe, $child); |
| 193 |
|
| 194 |
# if the backend is to be run in a separate process, |
| 195 |
# create a pipe and fork |
| 196 |
if ($backend_fork) { |
| 197 |
$pipe = IO::Pipe->new; |
| 198 |
$self->backend_pipe($pipe); |
| 199 |
|
| 200 |
$child = fork; |
| 201 |
my $msg = "fatal: can't fork: $!"; |
| 202 |
syslog err => $msg and die $msg |
| 203 |
unless defined $child; |
| 204 |
|
| 205 |
# child setup is handled in run_backend_loop() |
| 206 |
goto &run_backend_loop if $child == 0; |
| 207 |
|
| 208 |
# parent setup |
| 209 |
$pipe->reader; # declare this end of the pipe as the reader |
| 210 |
$pipe->autoflush(1); |
| 211 |
} |
| 212 |
|
| 213 |
my $io = IO::Select->new; |
| 214 |
$io->add($self->input); |
| 215 |
$self->output->autoflush(1); |
| 216 |
|
| 217 |
if ($backend_fork) { |
| 218 |
$io->add($pipe); |
| 219 |
$SIG{CHLD} = sub { $io->remove($pipe); waitpid($child, 0); }; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
# main loop |
| 224 |
while ($needed and $counter > 0) { |
| 225 |
my $start_time = time; |
| 226 |
|
| 227 |
# wait for some input data |
| 228 |
my @ready = $io->can_read($delay); |
| 229 |
|
| 230 |
for my $fh (@ready) { |
| 231 |
# handle input data from netsnmpd |
| 232 |
if ($fh == $self->input) { |
| 233 |
if (my $cmd = <$fh>) { |
| 234 |
$self->process_cmd(lc($cmd), $fh); |
| 235 |
$counter = $self->idle_count; |
| 236 |
} |
| 237 |
else { |
| 238 |
$needed = 0 |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
# handle input data from the backend process |
| 243 |
if ($backend_fork and $fh == $pipe) { |
| 244 |
use bytes; |
| 245 |
|
| 246 |
# read a first chunk from the child |
| 247 |
$fh->sysread(my $buffer, 20); |
| 248 |
last unless length $buffer; |
| 249 |
|
| 250 |
# extract the header |
| 251 |
my $headline= substr($buffer, 0, index($buffer, "\n")+1, ""); |
| 252 |
chomp $headline; |
| 253 |
my %header = map { split /=/, $_, 2 } split /\|/, $headline; |
| 254 |
|
| 255 |
# read the date in Storable format |
| 256 |
my $length = $header{length}; |
| 257 |
$fh->sysread(my $freezed, $length); |
| 258 |
$freezed = $buffer.$freezed; |
| 259 |
|
| 260 |
# decode the freezed data |
| 261 |
my $struct = thaw($freezed); |
| 262 |
$self->add_oid_tree($struct); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
$delay = $delay - (time() - $start_time); |
| 267 |
|
| 268 |
if ($delay <= 0) { |
| 269 |
if (not $backend_fork) { |
| 270 |
# collect information when the timeout has expired |
| 271 |
eval { $self->backend_collect->($self); 1 } |
| 272 |
or croak "fatal: An error occurred while executing " |
| 273 |
."the backend collecting callback: $@"; |
| 274 |
} |
| 275 |
|
| 276 |
# reset delay |
| 277 |
$delay = $self->refresh; |
| 278 |
$counter--; |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
if ($backend_fork) { |
| 283 |
kill TERM => $child; |
| 284 |
sleep 1; |
| 285 |
kill KILL => $child; |
| 286 |
waitpid($child, 0); |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
# |
| 293 |
# run_backend_loop() |
| 294 |
# ---------------- |
| 295 |
sub run_backend_loop { |
| 296 |
my ($self) = @_; |
| 297 |
|
| 298 |
my $pipe = $self->backend_pipe; |
| 299 |
$pipe->writer; # declare this end of the pipe as the writer |
| 300 |
$pipe->autoflush(1); |
| 301 |
|
| 302 |
# execute the initialisation callback |
| 303 |
eval { $self->backend_init->($self); 1 } |
| 304 |
or croak "fatal: An error occurred while executing the backend " |
| 305 |
."initialisation callback: $@"; |
| 306 |
|
| 307 |
while (1) { |
| 308 |
my $start_time = time; |
| 309 |
|
| 310 |
# execute the collect callback |
| 311 |
eval { $self->backend_collect->($self); 1 } |
| 312 |
or croak "fatal: An error occurred while executing the backend " |
| 313 |
."collecting callback: $@"; |
| 314 |
|
| 315 |
# freeze the OID tree using Storable |
| 316 |
use bytes; |
| 317 |
my $freezed = nfreeze($self->oid_tree); |
| 318 |
my $length = length $freezed; |
| 319 |
my $output = "length=$length\n$freezed"; |
| 320 |
|
| 321 |
# send it to the parent via the pipe |
| 322 |
$pipe->syswrite($output); |
| 323 |
select(undef, undef, undef, .000_001); |
| 324 |
|
| 325 |
# wait before next execution |
| 326 |
my $delay = $self->refresh() - (time() - $start_time); |
| 327 |
sleep $delay; |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
# |
| 333 |
# add_oid_entry() |
| 334 |
# ------------- |
| 335 |
sub add_oid_entry { |
| 336 |
my ($self, $oid, $type, $value) = @_; |
| 337 |
|
| 338 |
croak "error: Unknown type '$type'" unless exists $snmp_ext_type{$type}; |
| 339 |
$self->oid_tree->{$oid} = [$type => $value]; |
| 340 |
|
| 341 |
# need to resort |
| 342 |
@{$self->sorted_entries} = (); |
| 343 |
|
| 344 |
return 1 |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
# |
| 349 |
# add_oid_tree() |
| 350 |
# ------------ |
| 351 |
sub add_oid_tree { |
| 352 |
my ($self, $new_tree) = @_; |
| 353 |
|
| 354 |
croak "error: Unknown type" |
| 355 |
if any { !$snmp_ext_type{$_->[0]} } values %$new_tree; |
| 356 |
my $oid_tree = $self->oid_tree; |
| 357 |
@{$oid_tree}{keys %$new_tree} = values %$new_tree; |
| 358 |
|
| 359 |
# need to resort |
| 360 |
@{$self->sorted_entries} = (); |
| 361 |
|
| 362 |
return 1 |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
# |
| 367 |
# dump_oid_tree() |
| 368 |
# ------------- |
| 369 |
sub dump_oid_tree { |
| 370 |
my ($self) = @_; |
| 371 |
|
| 372 |
my $oid_tree = $self->oid_tree; |
| 373 |
my $output = $self->output; |
| 374 |
|
| 375 |
for my $oid (sort by_oid keys %$oid_tree) { |
| 376 |
my ($type, $value) = @{ $oid_tree->{$oid} }; |
| 377 |
$output->print("$oid ($type) = $value\n"); |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
# |
| 383 |
# ping() |
| 384 |
# ---- |
| 385 |
sub ping { |
| 386 |
return SNMP_PONG |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
# |
| 391 |
# get_oid() |
| 392 |
# ------- |
| 393 |
sub get_oid { |
| 394 |
my ($self, $req_oid) = @_; |
| 395 |
|
| 396 |
my $oid_tree = $self->oid_tree; |
| 397 |
my @result = (); |
| 398 |
|
| 399 |
if ($oid_tree->{$req_oid}) { |
| 400 |
my ($type, $value) = @{ $oid_tree->{$req_oid} }; |
| 401 |
@result = ($req_oid, $type, $value); |
| 402 |
} |
| 403 |
else { |
| 404 |
@result = (SNMP_NONE) |
| 405 |
} |
| 406 |
|
| 407 |
return @result |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
# |
| 412 |
# getnext_oid() |
| 413 |
# ----------- |
| 414 |
sub getnext_oid { |
| 415 |
my ($self, $req_oid) = @_; |
| 416 |
|
| 417 |
my $next_oid = $self->fetch_next_entry($req_oid) |
| 418 |
|| $self->fetch_first_entry(); |
| 419 |
|
| 420 |
return $self->get_oid($next_oid) |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
# |
| 425 |
# set_oid() |
| 426 |
# ------- |
| 427 |
sub set_oid { |
| 428 |
my ($self, $req_oid, $value) = @_; |
| 429 |
return SNMP_NOT_WRITABLE |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
# |
| 434 |
# process_cmd() |
| 435 |
# ----------- |
| 436 |
# Process and dispatch Net-SNMP commands when in pass_persist mode. |
| 437 |
# |
| 438 |
sub process_cmd { |
| 439 |
my ($self, $cmd, $fh) = @_; |
| 440 |
my @result = (); |
| 441 |
|
| 442 |
chomp $cmd; |
| 443 |
my $dispatch = $self->dispatch; |
| 444 |
|
| 445 |
if (exists $dispatch->{$cmd}) { |
| 446 |
|
| 447 |
# read the command arguments |
| 448 |
my @args = (); |
| 449 |
my $n = $dispatch->{$cmd}{nargs}; |
| 450 |
|
| 451 |
while ($n-- > 0) { |
| 452 |
chomp(my $arg = <$fh>); |
| 453 |
push @args, $arg; |
| 454 |
} |
| 455 |
|
| 456 |
# call the command handler |
| 457 |
my $coderef = $dispatch->{$cmd}{code}; |
| 458 |
@result = $coderef->($self, @args); |
| 459 |
} |
| 460 |
else { |
| 461 |
@result = SNMP_NONE; |
| 462 |
} |
| 463 |
|
| 464 |
# output the result |
| 465 |
$self->output->print(join "\n", @result, ""); |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
# |
| 470 |
# fetch_next_entry() |
| 471 |
# ---------------- |
| 472 |
sub fetch_next_entry { |
| 473 |
my ($self, $req_oid) = @_; |
| 474 |
|
| 475 |
my $entries = $self->sorted_entries; |
| 476 |
|
| 477 |
if (!@$entries) { |
| 478 |
@$entries = HAVE_SORT_KEY_OID |
| 479 |
? oidsort(keys %{ $self->oid_tree }) |
| 480 |
: sort by_oid keys %{ $self->oid_tree }; |
| 481 |
} |
| 482 |
|
| 483 |
# find the index of the current entry |
| 484 |
my $curr_entry_idx = -1; |
| 485 |
|
| 486 |
for my $i (0..$#$entries) { |
| 487 |
# exact match of the requested entry |
| 488 |
$curr_entry_idx = $i and last if $entries->[$i] eq $req_oid; |
| 489 |
|
| 490 |
# prefix match of the requested entry |
| 491 |
$curr_entry_idx = $i - 1 and last |
| 492 |
if $curr_entry_idx == -1 and index($entries->[$i], $req_oid) >= 0; |
| 493 |
} |
| 494 |
|
| 495 |
# get the next entry if it exists, otherwise none |
| 496 |
my $next_entry_oid = $entries->[$curr_entry_idx + 1] || SNMP_NONE; |
| 497 |
|
| 498 |
return $next_entry_oid |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
# |
| 503 |
# fetch_first_entry() |
| 504 |
# ----------------- |
| 505 |
sub fetch_first_entry { |
| 506 |
my ($self) = @_; |
| 507 |
|
| 508 |
my $entries = $self->sorted_entries; |
| 509 |
|
| 510 |
if (!@$entries) { |
| 511 |
@$entries = HAVE_SORT_KEY_OID |
| 512 |
? oidsort(keys %{ $self->oid_tree }) |
| 513 |
: sort by_oid keys %{ $self->oid_tree }; |
| 514 |
} |
| 515 |
my $first_entry_oid = $entries->[0]; |
| 516 |
|
| 517 |
return $first_entry_oid |
| 518 |
} |
| 519 |
|
| 520 |
|
| 521 |
# |
| 522 |
# by_oid() |
| 523 |
# ------ |
| 524 |
# sort() sub-function, for sorting by OID |
| 525 |
# |
| 526 |
sub by_oid ($$) { |
| 527 |
my (undef, @a) = split /\./, $_[0]; |
| 528 |
my (undef, @b) = split /\./, $_[1]; |
| 529 |
my $v = 0; |
| 530 |
$v ||= $a[$_] <=> $b[$_] for 0 .. $#a; |
| 531 |
return $v |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
__PACKAGE__ |
| 536 |
|
| 537 |
__END__ |
| 538 |
|
| 539 |
|
| 540 |
=head1 NAME |
| 541 |
|
| 542 |
SNMP::Extension::PassPersist - Generic pass/pass_persist extension framework |
| 543 |
for Net-SNMP |
| 544 |
|
| 545 |
|
| 546 |
=head1 VERSION |
| 547 |
|
| 548 |
This is the documentation of C<SNMP::Extension::PassPersist> version 0.07 |
| 549 |
|
| 550 |
|
| 551 |
=head1 SYNOPSIS |
| 552 |
|
| 553 |
Typical setup for a C<pass> program: |
| 554 |
|
| 555 |
use strict; |
| 556 |
use SNMP::Extension::PassPersist; |
| 557 |
|
| 558 |
# create the object |
| 559 |
my $extsnmp = SNMP::Extension::PassPersist->new; |
| 560 |
|
| 561 |
# add a few OID entries |
| 562 |
$extsnmp->add_oid_entry($oid, $type, $value); |
| 563 |
$extsnmp->add_oid_entry($oid, $type, $value); |
| 564 |
|
| 565 |
# run the program |
| 566 |
$extsnmp->run; |
| 567 |
|
| 568 |
Typical setup for a C<pass_persist> program: |
| 569 |
|
| 570 |
use strict; |
| 571 |
use SNMP::Extension::PassPersist; |
| 572 |
|
| 573 |
my $extsnmp = SNMP::Extension::PassPersist->new( |
| 574 |
backend_collect => \&update_tree |
| 575 |
); |
| 576 |
$extsnmp->run; |
| 577 |
|
| 578 |
|
| 579 |
sub update_tree { |
| 580 |
my ($self) = @_; |
| 581 |
|
| 582 |
# add a serie of OID entries |
| 583 |
$self->add_oid_entry($oid, $type, $value); |
| 584 |
... |
| 585 |
|
| 586 |
# or directly add a whole OID tree |
| 587 |
$self->add_oid_tree(\%oid_tree); |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
=head1 DESCRIPTION |
| 592 |
|
| 593 |
This module is a framework for writing Net-SNMP extensions using the |
| 594 |
C<pass> or C<pass_persist> mechanisms. |
| 595 |
|
| 596 |
When in C<pass_persist> mode, it provides a mechanism to spare |
| 597 |
ressources by quitting from the main loop after a given number of |
| 598 |
idle cycles. |
| 599 |
|
| 600 |
This module can use C<Sort::Key::OID> when it is available, for sorting |
| 601 |
OIDs faster than with the internal pure Perl function. |
| 602 |
|
| 603 |
|
| 604 |
=head1 METHODS |
| 605 |
|
| 606 |
=head2 new() |
| 607 |
|
| 608 |
Creates a new object. Can be given any attributes as a hash or hashref. |
| 609 |
See L<"ATTRIBUTES"> for the list of available attributes. |
| 610 |
|
| 611 |
B<Examples:> |
| 612 |
|
| 613 |
For a C<pass> command, most attributes are useless: |
| 614 |
|
| 615 |
my $extsnmp = SNMP::Extension::PassPersist->new; |
| 616 |
|
| 617 |
For a C<pass_persist> command, you'll usually want to at least set the |
| 618 |
C<backend_collect> callback: |
| 619 |
|
| 620 |
my $extsnmp = SNMP::Extension::PassPersist->new( |
| 621 |
backend_collect => \&update_tree, |
| 622 |
idle_count => 10, # no more than 10 idle cycles |
| 623 |
refresh => 10, # refresh every 10 sec |
| 624 |
); |
| 625 |
|
| 626 |
=head2 run() |
| 627 |
|
| 628 |
This method does the following things: |
| 629 |
|
| 630 |
=over |
| 631 |
|
| 632 |
=item * |
| 633 |
|
| 634 |
process the command line arguments in order to decide in which mode |
| 635 |
the program has to be executed |
| 636 |
|
| 637 |
=item * |
| 638 |
|
| 639 |
call the backend init callback |
| 640 |
|
| 641 |
=item * |
| 642 |
|
| 643 |
call the backend collect callback a first time |
| 644 |
|
| 645 |
=back |
| 646 |
|
| 647 |
Then, when in C<pass> mode, the corresponding SNMP command is executed, |
| 648 |
its result is printed on the output filehandle, and C<run()> returns. |
| 649 |
|
| 650 |
When in C<pass_persist> mode, C<run()> enters a loop, reading Net-SNMP |
| 651 |
queries on its input filehandle, processing them, and printing result |
| 652 |
on its output filehandle. The backend collect callback is called every |
| 653 |
C<refresh> seconds. If no query is read from the input after C<idle_count> |
| 654 |
cycles, C<run()> returns. |
| 655 |
|
| 656 |
=head2 add_oid_entry(FUNC_OID, FUNC_TYPE, FUNC_VALUE) |
| 657 |
|
| 658 |
Add an entry to the OID tree. |
| 659 |
|
| 660 |
=head2 add_oid_tree(HASH) |
| 661 |
|
| 662 |
Merge an OID tree to the main OID tree, using the same structure as |
| 663 |
the one of the OID tree itself. |
| 664 |
|
| 665 |
=head2 dump_oid_tree() |
| 666 |
|
| 667 |
Print a complete listing of the OID tree on the output file handle. |
| 668 |
|
| 669 |
|
| 670 |
=head1 ATTRIBUTES |
| 671 |
|
| 672 |
This module's attributes are generated by C<Class::Accessor>, and can |
| 673 |
therefore be passed as arguments to C<new()> or called as object methods. |
| 674 |
|
| 675 |
=head2 backend_collect |
| 676 |
|
| 677 |
Set the code reference for the I<collect> callback. See also L<"CALLBACKS">. |
| 678 |
|
| 679 |
=head2 backend_fork |
| 680 |
|
| 681 |
When set to true, the backend callbacks will be executed in a separate |
| 682 |
process. Default value is false. |
| 683 |
|
| 684 |
=head2 backend_init |
| 685 |
|
| 686 |
Set the code reference for the I<init> callback. See also L<"CALLBACKS">. |
| 687 |
|
| 688 |
=head2 backend_pipe |
| 689 |
|
| 690 |
Contains the pipe used to communicate with the backend child, when executed |
| 691 |
in a separate process. |
| 692 |
|
| 693 |
=head2 dispatch |
| 694 |
|
| 695 |
Gives access to the internal dispatch table, stored as a hash with the |
| 696 |
following structure: |
| 697 |
|
| 698 |
dispatch => { |
| 699 |
SNMP_CMD => { nargs => NUMBER_ARGS, code => CODEREF }, |
| 700 |
... |
| 701 |
} |
| 702 |
|
| 703 |
where the SNMP command is always in lowercase, C<nargs> gives the number |
| 704 |
of arguments expected by the command and C<code> the callback reference. |
| 705 |
|
| 706 |
You should not modify this table unless you really know what you're doing. |
| 707 |
|
| 708 |
=head2 heap |
| 709 |
|
| 710 |
Give access to the heap. |
| 711 |
|
| 712 |
=head2 idle_count |
| 713 |
|
| 714 |
Get/set the number of idle cycles before ending the run loop. |
| 715 |
|
| 716 |
=head2 input |
| 717 |
|
| 718 |
Get/set the input filehandle. |
| 719 |
|
| 720 |
=head2 oid_tree |
| 721 |
|
| 722 |
Gives access to the internal OID tree, stored as a hash with the |
| 723 |
following structure: |
| 724 |
|
| 725 |
oid_tree => { |
| 726 |
FUNC_OID => [ FUNC_TYPE, FUNC_VALUE ], |
| 727 |
... |
| 728 |
} |
| 729 |
|
| 730 |
where C<FUNC_OID> is the absolute OID of the SNMP function, C<FUNC_TYPE> |
| 731 |
the function type (C<"integer">, C<"counter">, C<"gauge">, etc), and |
| 732 |
C<FUNC_VALUE> the function value. |
| 733 |
|
| 734 |
You should not directly modify this hash but instead use the appropriate |
| 735 |
methods for adding OID entries. |
| 736 |
|
| 737 |
=head2 output |
| 738 |
|
| 739 |
Get/set the output filehandle. |
| 740 |
|
| 741 |
=head2 refresh |
| 742 |
|
| 743 |
Get/set the refresh delay before calling the backend collect callback |
| 744 |
to update the OID tree. |
| 745 |
|
| 746 |
|
| 747 |
=head1 CALLBACKS |
| 748 |
|
| 749 |
The callbacks are invoked with the corresponding object as first argument, |
| 750 |
as for a normal method. A heap is available for storing user-defined data. |
| 751 |
|
| 752 |
In the specific case of a programm running in C<pass_persist> mode with |
| 753 |
a forked backend, the callbacks are only executed in the child process |
| 754 |
(the forked backend). |
| 755 |
|
| 756 |
The currently implemented callbacks are: |
| 757 |
|
| 758 |
=over |
| 759 |
|
| 760 |
=item * init |
| 761 |
|
| 762 |
This callback is called once, before the first I<collect> invocation |
| 763 |
and before the main loop. It can be accessed and modified through the |
| 764 |
C<backend_init> attribute. |
| 765 |
|
| 766 |
=item * collect |
| 767 |
|
| 768 |
This callback is called every C<refresh> seconds so the user can update |
| 769 |
the OID tree using the C<add_oid_entry()> and C<add_oid_tree()> methods. |
| 770 |
|
| 771 |
=back |
| 772 |
|
| 773 |
=head2 Examples |
| 774 |
|
| 775 |
For simple needs, only the I<collect> callback needs to be defined: |
| 776 |
|
| 777 |
my $extsnmp = SNMP::Extension::PassPersist->new( |
| 778 |
backend_collect => \&update_tree, |
| 779 |
); |
| 780 |
|
| 781 |
sub update_tree { |
| 782 |
my ($self) = @_; |
| 783 |
|
| 784 |
# fetch the number of running processes |
| 785 |
my $nb_proc = @{ Proc::ProcessTable->new->table }; |
| 786 |
|
| 787 |
$self->add_oid_entry(".1.3.6.1.4.1.32272.10", gauge", $nb_proc); |
| 788 |
} |
| 789 |
|
| 790 |
A more advanced example is when there is a need to connect to a database, |
| 791 |
in which case both the I<init> and I<collect> callback need to be defined: |
| 792 |
|
| 793 |
my $extsnmp = SNMP::Extension::PassPersist->new( |
| 794 |
backend_init => \&connect_db, |
| 795 |
backend_collect => \&update_tree, |
| 796 |
); |
| 797 |
|
| 798 |
sub connect_db { |
| 799 |
my ($self) = @_; |
| 800 |
my $heap = $self->heap; |
| 801 |
|
| 802 |
# connect to a database |
| 803 |
my $dbh = DBI->connect($dsn, $user, $password); |
| 804 |
$heap->{dbh} = $dbh; |
| 805 |
} |
| 806 |
|
| 807 |
sub update_tree { |
| 808 |
my ($self) = @_; |
| 809 |
my $heap = $self->heap; |
| 810 |
|
| 811 |
# fetch the number of records from a given table |
| 812 |
my $dbh = $heap->{dbh}; |
| 813 |
my $sth = $dbh->prepare_cached("SELECT count(*) FROM whatever"); |
| 814 |
$sth->execute; |
| 815 |
my ($count) = $sth->fetchrow_array; |
| 816 |
|
| 817 |
$self->add_oid_entry(".1.3.6.1.4.1.32272.20", "gauge", $count); |
| 818 |
} |
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
=head1 SEE ALSO |
| 823 |
|
| 824 |
L<SNMP::Persist> is another pass_persist backend for writing Net-SNMP |
| 825 |
extensions, but relies on threads. |
| 826 |
|
| 827 |
The documentation of Net-SNMP, especially the part on how to configure |
| 828 |
a C<pass> or C<pass_persist> extension: |
| 829 |
|
| 830 |
=over |
| 831 |
|
| 832 |
=item * |
| 833 |
|
| 834 |
main site: L<http://www.net-snmp.org/> |
| 835 |
|
| 836 |
=item * |
| 837 |
|
| 838 |
configuring a pass or pass_persist extension: |
| 839 |
L<http://www.net-snmp.org/docs/man/snmpd.conf.html#lbBB> |
| 840 |
|
| 841 |
=back |
| 842 |
|
| 843 |
|
| 844 |
=head1 BUGS |
| 845 |
|
| 846 |
Please report any bugs or feature requests to |
| 847 |
C<bug-snmp-extension-passpersist at rt.cpan.org>, |
| 848 |
or through the web interface at |
| 849 |
L<http://rt.cpan.org/Public/Dist/Display.html?Name=SNMP-Extension-PassPersist>. |
| 850 |
I will be notified, and then you'll automatically be notified of |
| 851 |
progress on your bug as I make changes. |
| 852 |
|
| 853 |
|
| 854 |
=head1 SUPPORT |
| 855 |
|
| 856 |
You can find documentation for this module with the perldoc command. |
| 857 |
|
| 858 |
perldoc SNMP::Extension::PassPersist |
| 859 |
|
| 860 |
|
| 861 |
You can also look for information at: |
| 862 |
|
| 863 |
=over |
| 864 |
|
| 865 |
=item * Search CPAN |
| 866 |
|
| 867 |
L<http://search.cpan.org/dist/SNMP-Extension-PassPersist> |
| 868 |
|
| 869 |
=item * Meta CPAN |
| 870 |
|
| 871 |
L<https://metacpan.org/release/SNMP-Extension-PassPersist> |
| 872 |
|
| 873 |
=item * RT: CPAN's request tracker |
| 874 |
|
| 875 |
L<http://rt.cpan.org/Public/Dist/Display.html?Name=SNMP-Extension-PassPersist> |
| 876 |
|
| 877 |
=item * AnnoCPAN: Annotated CPAN documentation |
| 878 |
|
| 879 |
L<http://annocpan.org/dist/SNMP-Extension-PassPersist> |
| 880 |
|
| 881 |
=item * CPAN Ratings |
| 882 |
|
| 883 |
L<http://cpanratings.perl.org/d/SNMP-Extension-PassPersist> |
| 884 |
|
| 885 |
=back |
| 886 |
|
| 887 |
|
| 888 |
=head1 AUTHOR |
| 889 |
|
| 890 |
SE<eacute>bastien Aperghis-Tramoni, C<< <sebastien at aperghis.net> >> |
| 891 |
|
| 892 |
|
| 893 |
=head1 COPYRIGHT & LICENSE |
| 894 |
|
| 895 |
Copyright 2008-2011 SE<eacute>bastien Aperghis-Tramoni, all rights reserved. |
| 896 |
|
| 897 |
This program is free software; you can redistribute it and/or modify it |
| 898 |
under the same terms as Perl itself. |
| 899 |
|
| 900 |
=cut |
| 901 |
|