Net::SNMP::Mixin - mixin framework for Net::SNMP


Net-SNMP-Mixin documentation Contained in the Net-SNMP-Mixin distribution.

Index


Code Index:

NAME

Top

Net::SNMP::Mixin - mixin framework for Net::SNMP

VERSION

Top

Version 0.12

ABSTRACT

Top

Thin framework to access cooked SNMP information from SNMP agents with various mixins to Net::SNMP.

SYNOPSIS

Top

  use Net::SNMP;
  use Net::SNMP::Mixin;

  my $session = Net::SNMP->session( -hostname => 'example.com' );

  # method mixin and initialization
  $session->mixer(qw/Net::SNMP::Mixin::Foo Net::SNMP::Mixin::Bar/);
  $session->init_mixins();

  # event_loop in case of nonblocking sessions
  snmp_dispatcher();

  # check for initialization errors
  die $session->errors(1) if $session->errors;

  # use mixed-in methods to retrieve cooked SNMP info
  my $a = $session->get_foo_a();
  my $b = $session->get_bar_b();

DESCRIPTION

Top

Net::SNMP implements already the methods to retrieve raw SNMP values from the agents. With the help of specialized mixins, the access to these raw SNMP values is simplified and necessary calculations on these values are already done for gaining high level information.

This module provides helper functions in order to mixin methods into the inheritance tree of the Net::SNMP session instances or the Net::SNMP class itself.

The standard Net::SNMP get_... methods are still supported and the mixins fetch itself the needed SNMP values during initialization with these standard get_... methods. Blocking and non blocking sessions are supported. The mixins don't change the Net::SNMP session instance, besides storing additional payload in the object space prefixed with the unique mixin module names as the hash key.

DEFAULT EXPORTS

Top

These methods are exported by default into the Net::SNMP namespace:

Please see the following description for details.

mixer(@module_names)

  # class method
  Net::SNMP->mixer(qw/Net::SNMP::Mixin::Foo/);

  # instance method
  $session->mixer(qw/Net::SNMP::Mixin::Yazz Net::SNMP::Mixin::Brazz/)

Called as class method mixes the methods for all session instances. This is useful for agents supporting the same set of MIBs.

Called as instance method mixes only for the calling session instance. This is useful for SNMP agents not supporting the same set of MIBs and therefore not the same set of mixin modules.

Even the SNMP agents from a big network company don't support the most useful standard MIBs. They always use proprietary private enterprise MIBs (ring, ring, Cisco, do you hear the bells, grrrmmml).

The name of the modules to mix-in is passed to this method as a list. You can mix class and instance mixins as you like, but importing the same mixin module twice is an error.

Returns the invocant for chaining method calls, dies on error.

init_mixins($reload)

  $session->init_mixins();
  $session->init_mixins(1);

This method redispatches to every _init() method in the loaded mixin modules. The raw SNMP values for the mixins are loaded during this call - or via callbacks during the snmp_dispatcher event loop for nonblocking sessions - and stored in the object space. The mixed methods deliver afterwards cooked meal from these values.

The MIB values are reloaded for the mixins if the argument $reload is true. It's an error calling this method twice without forcing $reload.

If there is an error in a mixin, the rest of the initialization is skipped to preserve the current error message.

This method should be called in void context. In order to check successfull initialization the Net::SNMP error method $session->error() should be checked. Please use the following idiom:

  $session->init_mixins;
  snmp_dispatcher;
  die $session->errors(1) if $session->errors;

errors($clear)

  @errors = $session->errors();
  @errors = $session->errors(1);

Net::SNMP::error() has only one slot for errors. During nonblocking calls it's possible that an error followed by a successful transaction is cleared before the user gets the chance to see the error. For the mixin modules we use an error buffer until they are explicit cleared.

This method returns the list of all errors pushed by any mixin module. Called in scalar context returns a string of all @errors joined with "\n".

The error buffer is cleared if the argument $clear is true.

GUIDELINES FOR MIXIN AUTHORS

Top

See the Net::SNMP::Mixin::System module as a blueprint for a simple mixin module.

As a mixin-module author you must respect the following design guidelines:

DEVELOPER INFORMATION

Top

If mixer() is called as a class method, the mixin-methods are just imported into the Net::SNMP package.

If called as an instance method for the first time, the methods are imported into a newly generated, unique package for this session. The session instance is reblessed into this new package. The new package inherits from the Net::SNMP class. Successive calls for this session instance imports just the additional mixin-methods into the already generated package for this instance.

SEE ALSO

Top

Sub::Exporter, and the Net::SNMP::Mixin::... documentations for more details about the provided mixin methods.

REQUIREMENTS

Top

Net::SNMP, Sub::Exporter, Package::Generator, Package::Reaper

BUGS, PATCHES & FIXES

Top

There are no known bugs at the time of this release. However, if you spot a bug or are experiencing difficulties that are not explained within the POD documentation, please submit a bug to the RT system (see link below). However, it would help greatly if you are able to pinpoint problems or even supply a patch.

Fixes are dependant upon their severity and my availablity. Should a fix not be forthcoming, please feel free to (politely) remind me by sending an email to gaissmai@cpan.org .

  RT: http://rt.cpan.org/Public/Dist/Display.html?Name=Net-SNMP-Mixin

AUTHOR

Top

Karl Gaissmaier <karl.gaissmaier at uni-ulm.de>

COPYRIGHT & LICENSE

Top


Net-SNMP-Mixin documentation Contained in the Net-SNMP-Mixin distribution.
package Net::SNMP::Mixin;

use 5.006;
use strict;
use warnings;

our $VERSION = '0.12';

#
# store this package name in a handy variable,
# used for unique prefix of mixin attributes
# storage in instance hash
#
my $prefix = __PACKAGE__;

#
# this module import config
#
use Carp ();
use Scalar::Util 'refaddr';
use Package::Generator;
use Package::Reaper;

#
# this module export config
#
my @mixin_methods;

BEGIN {
  @mixin_methods = (qw/ mixer init_mixins errors /);
}

use Sub::Exporter -setup => {
  into    => 'Net::SNMP',
  exports => [@mixin_methods],
  groups  => { default => [@mixin_methods] }
};

# needed for housekeeping of already mixed in modules
# in order to find double mixins
my @class_mixins;

sub mixer {
  my ( $self, @mixins ) = @_;

  for my $mixin (@mixins) {

    # check: already mixed-in as class-mixin?
    Carp::croak "$mixin already mixed into class,"
      if grep m/^$mixin$/, @class_mixins;

    # instance- or class-mixin?
    if ( ref $self ) {

      # register array for instance mixins
      $self->{$prefix}{mixins} ||= [];

      # check: already mixed-in as instance-mixin?
      Carp::croak "$mixin already mixed into instance $self,"
        if grep m/^$mixin$/, @{ $self->{$prefix}{mixins} };

      _obj_mixer( $self, $mixin );

      # register instance mixins in the object itself
      push @{ $self->{$prefix}{mixins} }, $mixin;
    }
    else {
      _class_mixer( $self, $mixin );

      # register class mixins in a package variable
      push @class_mixins, $mixin;
    }
  }

  return $self;
}

#
# Mix the module into Net::SNMP with the help of Sub::Exporter.
#
sub _class_mixer {
  my ( $class, $mixin ) = @_;

  eval "use $mixin {into => 'Net::SNMP'}";
  Carp::croak $@ if $@;
  return;
}

#
# Create a new package as a subclass of Net::SNMP
# Rebless $session in the new package.
# Mix the module into the new package with the help of Sub::Exporter.
#
sub _obj_mixer {
  my ( $session, $mixin )      = @_;
  my ( $package, $pkg_reaper ) = _make_package($session);

  # created a new PACKAGE with an armed reaper,
  # this is the first call to mixer for this $session
  if ($pkg_reaper) {

    # rebless $session to new PACKAGE, this is still a
    # subclass of Net::SNMP
    bless $session, $package;

    # When this instance is garbage collected, the $pkg_reaper
    # is DESTROYed and the PACKAGE is deleted from the symbol table.
    $session->{$prefix}{reaper} = $pkg_reaper;
  }

  eval "use $mixin {into => '$package'}";
  Carp::croak $@ if $@;
  return;
}

#
# Make unique mixin subclass for this session with name.
# Net::SNMP::<refaddr $session> und make it a subclass of Net::SNMP.
# Arm a package reaper, see perldoc Package::Reaper.
#
sub _make_package {
  my $session  = shift;
  my $pkg_name = 'Net::SNMP::__mixin__' . '::' . refaddr $session;

  # already buildt this package for this session object,
  # just return the package name
  return $pkg_name if Package::Generator->package_exists($pkg_name);

  # build this package, make it a subclass of Net::SNMP and ...
  my $package = Package::Generator->new_package(
    {
      make_unique => sub { return $pkg_name },
      isa         => ['Net::SNMP'],
    }
  );

  # ... arm a package reaper
  my $pkg_reaper = Package::Reaper->new($package);

  return ( $package, $pkg_reaper );
}

sub init_mixins {
  my ( $session, $reload ) = @_;

  Carp::croak "pure instance method called as class method,"
    unless ref $session;

  my @instance_mixins = @{ $session->{$prefix}{mixins} }
    if defined $session->{$prefix}{mixins};

  # loop over all class-mixins and instance-mixins
  my @all_mixins = ( @class_mixins, @instance_mixins );

  unless ( scalar @all_mixins ) {
    Carp::carp "please use first the mixer() method, nothing to init\n";
    return;
  }

  # for each mixin module ...
  foreach my $mixin (@all_mixins) {

    # call the _init() method in module $mixin
    my $mixin_init = $mixin . '::_init';
    eval { $session->$mixin_init($reload) };

    # fatal error during mixin initialization, normally wrong
    # calling convention with $reload
    Carp::croak $@ if $@;

  }
  return;
}

sub errors {
  my ( $session, $clear ) = @_;

  # prepare the error buffer if not already done
  $session->{'Net::SNMP::Mixin'}{errors} ||= [];
  my @errors = @{ $session->{'Net::SNMP::Mixin'}{errors} };

  # show also the last Net::SNMP::error if available
  # and if not already included in the mixin error buffer
  if ( my $net_snmp_error = $session->error ) {
    push @errors, $net_snmp_error
      unless grep m/\Q$net_snmp_error\E$/, @errors;
  }

  if ($clear) {

    # clear the mixin error accumulator
    $session->{'Net::SNMP::Mixin'}{errors} = [];

    # clear the Net::SNMP error; with a private method, sigh.
    $session->_error_clear;
  }

  return wantarray ? @errors : join( "\n", @errors );
}

unless ( caller() ) {
  print __PACKAGE__ . " compiles and initializes successful.\n";
}

1;

# vim: sw=2