-= Network Protocol Tutorial using Perl =-
-= Network Protocol Tutorial using Perl =-

SNMP Protocol Client

Chris Huyler

  1. Statement of Purpose
  2. Introduction to SNMP
  3. Using the SNMP Protocol with Perl - Intro to Net-SNMP
  4. Appendix - Code


Statement of Purpose

This tutorial is designed to give you an overview of the SNMP protocol and using the net-snmp Perl module to write a client script. Included is a brief introduction to SNMP, a sample conversation between client and server, and a short programming exercise using Perl.


Introduction to SNMP

Simple Network Management Protocol (SNMP) is a network management protocol which was developed to provide a common framework for network management. Management applications are written using SNMP to:

  • monitor printer queues
  • set up addresses for devices
  • assign priorities for communication
  • install software on the network
  • manage databases
  • manage power on the network

SNMP interacts with management information bases (MIBs) of devices on the network. Information from these devices can be retrieved by issuing SNMP control commands which allow a network manager to monitor and control the network. A device could be anything on the network which can communicate using SNMP such as servers, UPS, routers and switches. SNMP can also be used to handle hardware traps by messaging the network management station if something important occurs (such as a fatal error or power loss).

SNMP is part of the TCP/IP suite of protocols. The primary protocols that SNMP implements are the User Datagram Protocol (UDP) and the Internet Protocol (IP). SNMP also requires Data Link Layer protocols such as Ethernet or TokenRing to implement the communication channel from the management to the managed agent.

Each device or product on a network has what is called a Management information base (MIBs), or a collection of definitions, which define the properties of the managed object within the device to be managed. Every managed device keeps a database of values for each of the definitions written in the MIB. It is not the actual database itself - it is implementation dependant. You can think of a MIB as an information warehouse. MIB-II, the latest generation of network management MIBs, stores data on TCP/IP traffic, routing, configuration and errors. MIB-II has improved support for multi-protocol devices and allows the network management system to control SNMP operation.

Network management system contain two primary elements: a manager and agents. The Manager is the console through which the network administrator performs network management functions. Agents are the objects that relate to actual device being managed. Bridges, Hubs, Routers or network servers are examples of agents. SNMP allows managers and agents to communicate for the purpose of accessing objects within the agent's MIB

A typical agent usually:
  • Implements full SNMP protocol.
  • Stores and retrieves management data as defined by the Management Information Base
  • Can asynchronously signal an event to the manager
  • Can be a proxy for some non-SNMP manageable network node.
A typical manager usually:
  • Implemented as a Network Management Station (the NMS)
  • Implements full SNMP Protocol
  • Able to
    • Query agents
    • Get responses from agents
    • Set variables in agents
    • Acknowledge asynchronous events from agents
Each SNMP message has the format:
  • Version Number
  • Community Name - kind of a password
  • One or more SNMP PDUs - assuming trivial authentication
Each SNMP PDU except trap has the following format:
  • request id - request sequence number
  • error status - zero if no error otherwise one of a small set
  • error index - if non zero indicates which of the OIDs in the PDU caused the error2
  • list of OIDs and values - values are null for get and get next
Trap PDUs have the following format:
  • enterprise - identifies the type of object causing the trap
  • agent address - IP address of agent which sent the trap
  • generic trap id - the common standard traps
  • specific trap id - proprietary or enterprise trap
  • time stamp - when trap occurred in time ticks
  • list of OIDs and values - OIDs that may be relevant to send to the NMS

Coding a SNMP Client with Perl

In order to implement a SNMP client using Perl, we must first install the Net-SNMP module. Open a DOS command window and enter the following:

C:\> cd \Perl\bin
C:\Perl\bin\> ppm
PPM> install net-snmp
PPM> exit

Once you have the net-snmp module installed, create a new text file titled snmpDemo.pl. Start out by typing the following lines to create a new SNMP data structure and start a new session. You may substitute a SNMP enabled host for "localhost" if you know of one. The community is similar to a password and 161 is the default port on which SNMP commands are sent.

use strict;
use Net::SNMP;
my ($session, $error) = Net::SNMP->session(
          -hostname => shift || 'localhost',
          -community => shift || 'public',
          -port => shift || 161
);

If a connection is not made, we should exit by adding the following check:

if (!defined($session)) {
printf("ERROR: %s.\n", $error);
exit 1; }

If a connection is established, we can use the following code to retrieve the uptime of an agent on the network. Uptime is the elapsed time since the device was last restarted.

my $sysUpTime = '1.3.6.1.2.1.1.3.0';
my $result = $session->get_request(
             -varbindlist => [$sysUpTime]
);

If we get a valid result we can print it to the screen, otherwise we should print an error and exit.

if (!defined($result)) {
    printf("ERROR: %s.\n", $session->error);
    $session->close;
    exit 1;
}

printf("sysUpTime for host '%s' is %s\n",
        $session->hostname, $result->{$sysUpTime}
);

$session->close;
exit 0;

Appendix

snmpDemo.pl

#! /usr/local/bin/perl
use strict;
use Net::SNMP;

my ($session, $error) = Net::SNMP->session(
      -hostname  => shift || 'localhost',
      -community => shift || 'public',
      -port      => shift || 161
);

if (!defined($session)) {
    printf("ERROR: %s.\n", $error);
    exit 1;
}

my $sysUpTime = '1.3.6.1.2.1.1.3.0';
my $result = $session->get_request(
             -varbindlist => [$sysUpTime]
);

if (!defined($result)) {
    printf("ERROR: %s.\n", $session->error);
    $session->close;
    exit 1;
}

printf("sysUpTime for host '%s' is %s\n",
    $session->hostname, $result->{$sysUpTime}
);

$session->close;
exit 0;
www.bright-blade.net
[ Email: | chuyler1@ic3.ithaca.edu | jbirkin1@ic3.ithaca.edu ]