SNMP Libraries for programming languages

(some  examples)    

SNMPv2 library for C/C++

The SNMPv2 Tool kits for C++ include:

The applications created with the tool kits are royalty free.

SNMP C++ Library

The C++ library supports SNMP v2c and v1. The library is very efficient, robust and native for 32-bin Windows environment. It is designed to provide the maximum flexibility in network management software development. It does not coerce developers into a particular architecture but rather is easy to adjust to any paradigm chosen by developers.

The C++ library is equally suitable to develop graphical and console mode applications. It is designed to handle a variety of tasks in:

etc.
 

The SNMP C++ library fully implements SNMPv1 and SNMPv2c. It allows to send commands and/or notifications to single or multiple destinations concurrently with or without automatic retransmission. It automatically creates listening thread(s) on the background to enable blocking and non-blocking receive mode for replies and notifications.

A unique feature of the library is sending the same or different message to multiple destinations with automatic retransmission using single function call. The library takes care about matching requests sent and replies received and automatically retransmits requests to the non-responding nodes. This feature is very handy when an agent is configured to work with multiple managers and needs to send the same notification to multiple destinations. Sending to multiple destinations is also very valuable for network discovery when node manager sends requests to multiple agents.

The following SNMP Protocol Data Unit (PDU) types are supported:

The following variable types are supported:

Full support of SNMP v2c and v1 allows developing version 1, 2 or bilingual manager applications and proxy agents.

The library is using the TCP/IP User Datagram Protocol as a transport. However, it's not limited to this transport only. It can be used with any other standard or proprietary network transport.

The toolkit comes with two GUI applications to assist in developing a variety of network management applications. The utilities are built using the LogiSoft AR C++ class library.
 
 
 


java advent SNMP Package Classes

The Advent SNMP Package is designed to enable writing object-oriented Java applets and Java applications that use SNMP to talk to managed nodes.
 

SNMP Variable classes

The ancestor of all SNMP variable classes is an abstract class called SnmpVar. This class contains abstract methods for printing, ASN encoding, ASN decoding, etc., not all of which are public, i.e. some of the abstract methods are used only by the other classes in the package.

The SnmpVar class has five direct sub-classes. They are:

 

 SNMP Communication classes

The Advent SNMP package uses the SnmpAPI class to manage sessions created by the user application, manage the MIB modules that have been loaded, and store some key parameters for SNMP communication, e.g. SNMP ports to be used.

The SnmpSession class is used to manage a session with an SNMP peer. You can talk to more than one host via a single session, but it makes sense to use separate sessions for hosts you talk to often in an application. Each session runs as a separate thread (primarily to do receive tasks) and provides functions to open sessions (on a particular local port if needed), synchronously or asynchronously send and receive SNMP requests, check for responses and timeouts, and close sessions.

The SnmpCallback class is used when an asynchronous response to a snmprequest is to be delivered to the user application from a separate thread other than the SnmpSession receiver thread. If the user sets the SnmpSession.CALLBACK_THREAD to true then the users callback method will be called from a separate thread when a response arrives. If the SnmpSesssion.CALLBACK_THREAD is set to false (default), then the callback method is called from the same thread as the receiver thread and subsequent responses can be received only if the user returns from the callback method. But calling from a separate thread, the performance of the receiver thread in receiving responses or traps is little bit poorer.

Interaction between the SNMP manager and the agent is via SNMP protocol data units, PDUs. The SnmpPDU class will be used to provide the variables and methods to create and use the SNMP PDU. The methods include adding null valued variable bindings and printing all variable bindings.

In order to get around the security restriction in browsers where socket access from the browser is not allowed, except to communicate with the applet host, we've added an SASClient class. This class does not need to be directly used. The open method in the SnmpSession class can be used to specify whether this class should be used to communicate with an SNMP peer.

In order to save applet data to a file for use later (for example to save user configuration for the next time the applet is started), the saveFile method in SASClient can be used. It saves the specified data to the specified file in the SASusers sub-directory on the applet host.
 

 Miscellaneous classes


SNMP libraries in PERL

SNMP support for Perl 5

The SNMP operations currently supported are "get", "get-next", "get-bulk" and "set", as well as trap generation and reception.
 

Usage

The basic usage of these routines works like this:

use BER;
require 'SNMP_Session.pm';

# Set $host to the name of the host whose SNMP agent you want
# to talk to.  Set $community to the community name under
# which you want to talk to the agent.  Set port to the UDP
# port on which the agent listens (usually 161).

$session = SNMP_Session->open ($host, $community, $port)
    || die "couldn't open SNMP session to $host";

# Set $oid1, $oid2... to the BER-encoded OIDs of the MIB
# variables you want to get.

if ($session->get_request_response ($oid1, $oid2, ...)) {
    ($bindings) = $session->decode_get_response ($session->{pdu_buffer});

    while ($bindings ne '') {
        ($binding,$bindings) = &decode_sequence ($bindings);
        ($oid,$value) = &decode_by_template ($binding, "%O%@");
        print $pretty_oids{$oid}," => ",
              &pretty_print ($value), "\n";
    }
} else {
    die "No response from agent on $host";
}

Set Requests

Set requests are generated much like get or getNext requests are, with the exception that you have to specify not just OIDs, but also the values the variables should be set to. Every binding is passed as a reference to a two-element array, the first element being the encoded OID and the second one the encoded value. See the test/set-test.pl script for an example, in particular the subroutine snmpset.

Walking Tables

Beginning with version 0.57 of SNMP_Session.pm, there is API support for walking tables. The map_table method can be used for this as follows:

sub walk_function ($$$) {
  my ($index, $val1, $val3) = @_;
  ...
}

...
$columns = [$base_oid1, $base_oid3];
$n_rows = $session->map_table ($columns, \&walk_function);

The columns argument must be a reference to a list of OIDs for table columns sharing the same index. The method will traverse the table and call the walk_function for each row. The arguments for these calls will be:

  1. the row index as a partial OID in dotted notation, e.g. "1.3", or "10.0.1.34".
  2. the values of the requested table columns in that row, in BER-encoded form. If you want to use the standard pretty_print subroutine to decode the values, you can use the following idiom:
  3.   grep (defined $_ && ($_=pretty_print $_), ($val1, $val3));

Sending Traps

To send a trap, you have to open an SNMP session to the trap receiver. Usually this is a process listening to UDP port 162 on a network management station. Then you can use the trap_request_send method to encode and send the trap. There is no way to find out whether the trap was actually received at the management station - SNMP traps are fundamentally unreliable.

When constructing a trap, you must provide

Here is a short example:

my $trap_receiver = "netman.noc";
my $trap_community = "SNMP_Traps";
my $trap_session = SNMP_Session->open ($trap_receiver, $trap_community, 162);
my $myIpAddress = ...;
my $start_time = time;

...

sub link_down_trap ($) {
  my ($if_index) = @_;
  my $genericTrap = 2;          # linkDown
  my $specificTrap = 0;
  my @ifIndexOID = ( 1,3,6,1,2,1,2,2,1,1 );
  my $upTime = int ((time - $start_time) * 100.0);
  my @myOID = ( 1,3,6,1,4,1,2946,0,8,15 );

  warn "Sending trap failed"
    unless $trap_session->trap_request_send (encode_oid (@myOID),
                                encode_ip_address ($myIpAddress),
                                       encode_int ($genericTrap),
                                      encode_int ($specificTrap),
                                      encode_timeticks ($upTime),
                             [encode_oid (@ifIndexOID,$if_index),
                                        encode_int ($if_index)]);
}

Receiving Traps

Since version 0.60, SNMP_Session.pm supports the receipt and decoding of SNMPv1 trap requests. To receive traps, you have to create a special SNMP session that passively listens on the SNMP trap transport address (usually UDP port 162). Then you can receive traps using the receive_trap method and decode them using decode_trap_request:

my $trap_session = SNMP_Session->open_trap_session ()
  || die "cannot open trap session";
my ($trap, $sender_addr, $sender_port) = $trap_session->receive_trap ()
  || die "cannot receive trap";
my ($community, $enterprise, $agent,
    $generic, $specific, $sysUptime, $bindings)
  = $session->decode_trap_request ($trap)
    || die "cannot decode trap received"
...
my ($binding, $oid, $value);
while ($bindings ne '') {
    ($binding,$bindings) = &decode_sequence ($bindings);
    ($oid, $value) = decode_by_template ("%O%@");
    print BER::pretty_oid ($oid)," => ",pretty_print ($value),"\n";
}


Another package: (monitor, not full package)

NAME

SNMP::Monitor - a Perl package for monitoring remote hosts via SNMP
 
 

SYNOPSIS

    require SNMP::Monitor;
    # Read a configuration file
    my $config = SNMP::Monitor->Configuration("/etc/snmpmon/config");
    # Create a new monitor
    my $monitor = SNMP::Monitor->new($config);
    # Start monitoring (endless loop, never returns)
    $monitor->Loop();

The SNMP::Monitor module is a package for checking and watching arbitrary values via SNMP. Events can be triggered, Logging can be done, whatever you want.
 
 


More libraries

    relating to the SNMP: an extensible agent, a SNMP library, tools to UCD - SNMP package which includes various tools request or set information from SNMP agents, tools to generate and handle SNMP traps, a version of the UNIX  'netstat' command using SNMP and graphical Perl/Tk/SNMP based mib browser, has support for SNMPv3. It is from the California University UC-Davis.
http://ucd-snmp.ucdavis.edu/

    Software from Carnegie Melon University network group (The  CMU SNMP library V1.14)
http://www.net.cmu.edu/groups/netdev/software.html

More SNMP packages from different resources:

http://wwwsnmp.cs.utwente.nl/software/pubdomain.html

Java:
    http://xva2k.bradley.edu/AdventNet/docs/tutorial3.html
    http://www.a2points.com/homepage/YS/sck/html/beans.html
    http://wwwhome.cs.utwente.nl/~nm/projects/ut-laforge/assignment/index.htm

C++:
    http://www.logisoftar.com/snmptk.htm

C:
    http://ucd-snmp.ucdavis.edu/
    http://www.avatar.com/snmp.htm

Perl:
    http://www.switch.ch/misc/leinen/snmp/perl/index.html
    http://theory.uwinnipeg.ca/CPAN/data/SNMP-Monitor/SNMP/Monitor.html