11-Aug-2012

Example SNMP trap

This is example C code of how to generate an SNMP trap via the eSNMP API on OpenVMS. The SNMP example that ships with the operating system is far too complicated to take in one gulp, particularly if all you want to do is trap.

First, create a file called TRAP_MIB.MY containing the following code:


TRAP-MIB DEFINITIONS ::= BEGIN

--  trap MIB

IMPORTS
        enterprises
                FROM RFC1155-SMI
        TRAP-TYPE
                FROM RFC-1215
        OBJECT-TYPE
                FROM RFC-1212;


dec     OBJECT IDENTIFIER ::= { enterprises 36 }
ema     OBJECT IDENTIFIER ::= { dec 2 }
sysobjectids
        OBJECT IDENTIFIER ::= { ema 15 }
decosf  OBJECT IDENTIFIER ::= { sysobjectids 2 }

exampleTraps OBJECT IDENTIFIER ::= { decosf 100 }

trapTime OBJECT-TYPE
        SYNTAX  OCTET STRING (SIZE (0..14))
        ACCESS  not-accessible
        STATUS  mandatory
        DESCRIPTION
                "Trap creation time (YYYYMMDDhhmmss)"
        ::= { exampleTraps 1 }

trapMessage OBJECT-TYPE
        SYNTAX  OCTET STRING (SIZE (0..255))
        ACCESS  not-accessible
        STATUS  mandatory
        DESCRIPTION
                "Message"
        ::= { exampleTraps 2 }

exampleTrap TRAP-TYPE
        ENTERPRISE exampleTraps
        VARIABLES {
                trapTime,
                trapMessage }
        DESCRIPTION
                ""
        ::= 1

END

To compile, copy TCPIP$SNMP_EXAMPLES:RFC1442.MY to your directory and issue the following:


$ mibcomp rfc1442.my,trap_mib.my "exampleTraps"/print_tree

This will generate two files called EXAMPLETRAPS_TBL.C and EXAMPLETRAPS_TBL.H.

To generate a trap, create a file called TRAP.C and include the following code:


#include <stdlib.h>
#include <stdio.h>
#include <ssdef.h>
#include <time.h>
#include <string.h>

#include "esnmp.h"
#include "exampletraps_tbl.h"

extern SUBTREE exampleTraps_subtree;

static char *prod_oid_str = "1.3.6.1.3.4.36.2.15.2.100";


/******************************************************************************/
int main (int argc, char *argv[]) {

VARBIND trap_vb[MAX_EXAMPLETRAPS_TBL_VARIABLES];
OBJECT *ot;

time_t timer;
char now[14+1];

int i;
int rc;
int subsocket = 0;
int debug_level = 0;

#ifdef DEBUG
debug_level |= TRACE;
#endif /* DEBUG */

    if (argc < 2) {
        (void)fprintf (stderr, "Usage: TRAP \"message\"\n");
        exit (EXIT_SUCCESS);
    }

    if (strlen (argv[1]) > 255) {
        (void)fprintf (stderr, "Message maximum length is 255\n");
        exit (EXIT_FAILURE);
    }

    set_debug_level (debug_level, 0);

    ot = exampleTraps_subtree.object_tbl;

    (void)bzero((char *)trap_vb, sizeof(trap_vb));
    for (i = 0; i < MAX_EXAMPLETRAPS_TBL_VARIABLES - 1; i++) {
        trap_vb[i].next = &trap_vb[i+1];
    }

    rc = esnmp_init (&subsocket, "exampletraps");
    if (rc != ESNMP_LIB_OK) {
        (void)fprintf (stderr, "esnmp init failed with code %d\n", rc);
        exit (EXIT_FAILURE);
    }

    rc = esnmp_poll ();
    if (rc != ESNMP_LIB_OK) {
        (void)fprintf (stderr, "esnmp poll failed with code %d\n", rc);
        exit (EXIT_FAILURE);
    }

    timer = time (NULL);
    strftime (now, sizeof (now), "%Y%m%d%H%M%S", localtime (&timer));
    (void)o_string (&trap_vb[0],
                    &ot[I_trapTime],
                    (unsigned char *)now,
                    strlen (now));
    (void)clone_oid(&trap_vb[0].name, &ot[I_trapTime].oid);

    (void)o_string (&trap_vb[1],
                    &ot[I_trapMessage],
                    (unsigned char *)argv[1],
                    strlen (argv[1]));
    (void)clone_oid(&trap_vb[1].name, &ot[I_trapMessage].oid);

    rc = esnmp_trap(6, 1, prod_oid_str, trap_vb);
    if (rc != ESNMP_LIB_OK) {
        (void)fprintf (stderr, "esnmp trap failed with code %d\n", rc);
        exit (EXIT_FAILURE);
    }

    for (i = 0; i < MAX_EXAMPLETRAPS_TBL_VARIABLES; i++) {
        free_varbind_data (&trap_vb[i]);
    }

    (void)esnmp_term ();
    exit (EXIT_SUCCESS);
}

Compile and link with the following:


$ cc trap+exampletraps_tbl/incl=([],tcpip$snmp)
$ link trap,sys$input/opt
sys$share:tcpip$esnmp_shr/share
^Z
$

Set up the local host as a trap receiver if you haven't already done so:


$ tcpip
TCPIP> set config snmp/address=127.0.0.1/type=trap/community="public"
TCPIP> exit
$

Stop and start SNMP (if you needed to perform the previous step):


$ @sys$startup:tcpip$snmp_shutdown
$ @sys$startup:tcpip$snmp_startup

To test, in a seperate process, you can run the trap receiver program:


$ mc tcpip$snmp_traprcv

And test by running the program with a single text string argument:


$ mc []trap "Test trap"
$

Posted at August 11, 2012 3:33 PM
Tag Set:

Comments are closed