net-snmp扩展自己的程序

本案例为起到简单明了作用,只对CPU进行的监控。包括snmpget和trap。在CSDN上也发布了此文。http://topic.csdn.net/u/20110705/17/74727b1c-3aef-4729-a537-04e748e71e85.html

首先编写mib文件:BRD-SYS-MIB.txt 采用ASN.1编码,我是一的是工具生成:mgMibBrowser

 

  BRD-SYS-MIB.txt内容:
--
-- BRD-SYS-MIB.my
-- MIB generated by MG-SOFT Visual MIB Builder Version 6.0 Build 88
-- Monday, July 11, 2011 at 17:14:41
--

BRD-SYS-MIB DEFINITIONS ::= BEGIN

IMPORTS
OBJECT-GROUP, NOTIFICATION-GROUP
FROM SNMPv2-CONF
enterprises, OBJECT-TYPE, MODULE-IDENTITY, OBJECT-IDENTITY, NOTIFICATION-TYPE
FROM SNMPv2-SMI;


-- 1.3.6.1.4.1.30000.1
brdModule MODULE-IDENTITY
LAST-UPDATED "201007061242Z" -- July 06, 2010 at 12:42 GMT
ORGANIZATION
"Organization."
CONTACT-INFO
"Contact-info."
DESCRIPTION
"Description."
::= { broadtech 1 }



--
-- Node definitions
--

-- 1.3.6.1.4.1.30000
broadtech OBJECT-IDENTITY
STATUS current
DESCRIPTION
"The root of the OID sub-tree assigned to Company by the Internet Assigned Numbers Authority (IANA)"
::= { enterprises 30000 }


-- 1.3.6.1.4.1.30000.1.1
system OBJECT IDENTIFIER ::= { brdModule 1 }


-- 1.3.6.1.4.1.30000.1.1.1
realvalue OBJECT IDENTIFIER ::= { system 1 }


-- 1.3.6.1.4.1.30000.1.1.1.1
cpu OBJECT-TYPE
SYNTAX INTEGER (0..100)
MAX-ACCESS read-only
STATUS current
DESCRIPTION
"Description."
::= { realvalue 1 }


-- 1.3.6.1.4.1.30000.1.1.1.2
maxcpu OBJECT-TYPE
SYNTAX INTEGER (50..100)
MAX-ACCESS read-write
STATUS current
DESCRIPTION
"Description."
::= { realvalue 2 }


-- 1.3.6.1.4.1.30000.1.1.1.3
notifycpu NOTIFICATION-TYPE
OBJECTS { cpu }
STATUS current
DESCRIPTION
"Description."
::= { realvalue 3 }


-- 1.3.6.1.4.1.30000.1.1.1.4
realgroup OBJECT-GROUP
OBJECTS { cpu, maxcpu }
STATUS current
DESCRIPTION
"Description."
::= { realvalue 4 }


-- 1.3.6.1.4.1.30000.1.1.1.5
notifygroup NOTIFICATION-GROUP
NOTIFICATIONS { notifycpu }
STATUS current
DESCRIPTION
"Description."
::= { realvalue 5 }



END
--+--brdModule(1)
-- |
-- +--system(1)
-- |
-- +--realvalue(1)
-- |
-- +-- -R-- INTEGER cpu(1)
-- | Range: 0..100
-- +-- -RW- INTEGER maxcpu(2)
-- | Range: 50..100
-- +--notifycpu(3)
-- +--realgroup(4)
-- +--notifygroup(5)
--
-- BRD-SYS-MIB.my
--

 下面是程序代码:

brdModule.h

/*
 * Note: this file originally auto-generated by mib2c using
 *        : mib2c.int_watch.conf 17587 2009-04-30 06:57:27Z magfr $
 */
#ifndef BRDMODULE_H
#define BRDMODULE_H


/*
 * function declarations 
 */
void            init_brdModule(void);
int             send_notifycpu_trap(void);
void        updateValueOfCpu(unsigned int clientreg, void *clientarg);

#endif                          /* BRDMODULE_H */
 

brdModule.c

/*
 * Note: this file originally auto-generated by mib2c using
 *        : mib2c.int_watch.conf 17587 2009-04-30 06:57:27Z magfr $
 */

#include 
#include 
#include 
#include "brdModule.h"
#include "getcpu.h"       //for get cpu rate

/*
 * The variables we want to tie the relevant OIDs to.
 * The agent will handle all GET and (if applicable) SET requests
 * to these variables automatically, changing the values as needed.
 */

long            cpu = 0;        /* XXX: set default value */
long            maxcpu = 0;     /* XXX: set default value */



static const oid snmptrap_oid[] = { 1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0 };

int     //mib2c auto make and modifyed
send_notifycpu_trap(void)
{
    netsnmp_variable_list *var_list = NULL;
    const oid       notifycpu_oid[] =
        { 1, 3, 6, 1, 4, 1, 30000, 1, 1, 1, 3 };
    const oid       cpu_oid[] = { 1, 3, 6, 1, 4, 1, 30000, 1, 1, 1, 1, 0 };

    /*
     * Set the snmpTrapOid.0 value
     */
    snmp_varlist_add_variable(&var_list,
                              snmptrap_oid, OID_LENGTH(snmptrap_oid),
                              ASN_OBJECT_ID,
                              notifycpu_oid, sizeof(notifycpu_oid));

    /*
     * Add any objects from the trap definition
     */
    snmp_varlist_add_variable(&var_list,
                              cpu_oid, OID_LENGTH(cpu_oid), ASN_INTEGER,
                              /*
                               * Set an appropriate value for cpu 
                               */
                              (long*)&cpu, sizeof(cpu));  //modifyed by ligang

    /*
     * Add any extra (optional) objects here
     */

    /*
     * Send the trap to the list of configured destinations
     *  and clean up
     */
    send_v2trap(var_list);
    snmp_free_varbind(var_list);

    return SNMP_ERR_NOERROR;
}


/*
 * Our initialization routine, called automatically by the agent 
 * (Note that the function name must match init_FILENAME()) 
 */
void   //mib2c auto make and modifyed
init_brdModule(void)
{
    netsnmp_handler_registration *reg;

    const oid       cpu_oid[] = { 1, 3, 6, 1, 4, 1, 30000, 1, 1, 1, 1 };
    static netsnmp_watcher_info cpu_winfo;
    const oid       maxcpu_oid[] = { 1, 3, 6, 1, 4, 1, 30000, 1, 1, 1, 2 };
    static netsnmp_watcher_info maxcpu_winfo;

    /*
     * a debugging statement.  Run the agent with -DbrdModule to see
     * the output of this debugging statement. 
     */
    DEBUGMSGTL(("brdModule", "Initializing the brdModule module\n"));


    /*
     * Register scalar watchers for each of the MIB objects.
     * The ASN type and RO/RW status are taken from the MIB definition,
     * but can be adjusted if needed.
     *
     * In most circumstances, the scalar watcher will handle all
     * of the necessary processing.  But the NULL parameter in the
     * netsnmp_create_handler_registration() call can be used to
     * supply a user-provided handler if necessary.
     *
     * This approach can also be used to handle Counter64, string-
     * and OID-based watched scalars (although variable-sized writeable
     * objects will need some more specialised initialisation).
     */
    DEBUGMSGTL(("brdModule",
                "Initializing cpu scalar integer.  Default value = %d\n",
                cpu));
    reg = netsnmp_create_handler_registration("cpu", NULL,
                                              cpu_oid, OID_LENGTH(cpu_oid),
                                              HANDLER_CAN_RONLY);
    netsnmp_init_watcher_info(&cpu_winfo, &cpu, sizeof(long),
                              ASN_INTEGER, WATCHER_FIXED_SIZE);
    if (netsnmp_register_watched_scalar(reg, &cpu_winfo) < 0) {
        snmp_log(LOG_ERR, "Failed to register watched cpu");
    }

    DEBUGMSGTL(("brdModule",
                "Initializing maxcpu scalar integer.  Default value = %d\n",
                maxcpu));
    reg = netsnmp_create_handler_registration("maxcpu", NULL,
                                              maxcpu_oid,
                                              OID_LENGTH(maxcpu_oid),
                                              HANDLER_CAN_RWRITE);
    netsnmp_init_watcher_info(&maxcpu_winfo, &maxcpu, sizeof(long),
                              ASN_INTEGER, WATCHER_FIXED_SIZE);
    if (netsnmp_register_watched_scalar(reg, &maxcpu_winfo) < 0) {
        snmp_log(LOG_ERR, "Failed to register watched maxcpu");
    }


    DEBUGMSGTL(("brdModule", "Done initalizing brdModule module\n"));
    /*add a timer 3s:HANDLE FUNCTION IS updateValueOfCpu WITHOUT ARGS*/
    snmp_alarm_register(3,SA_REPEAT,updateValueOfCpu,NULL);
}
/*timer handle function*/
void updateValueOfCpu(unsigned int clientreg, void *clientarg)
{
    cpu=getcpurate(NULL);
    printf("%d %d\n",cpu,maxcpu);
    if(cpu>maxcpu)//all oid's value in memery
       send_notifycpu_trap();//send trap
    return;
}

 Makefile:

#
# Warning: you may need more libraries than are included here on the
# build line.  The agent frequently needs various libraries in order
# to compile pieces of it, but is OS dependent and we can't list all
# the combinations here.  Instead, look at the libraries that were
# used when linking the snmpd master agent and copy those to this
# file.
#

CC=gcc

OBJS1=snmpdemoapp.o
OBJS2=example-demon.o nstAgentSubagentObject.o
OBJS3=asyncapp.o
TARGETS=example-demon snmpdemoapp asyncapp

CFLAGS=-I. `net-snmp-config --cflags`
BUILDLIBS=`net-snmp-config --libs`
BUILDAGENTLIBS=`net-snmp-config --agent-libs`

# shared library flags (assumes gcc)
DLFLAGS=-fPIC -shared

all: $(TARGETS)

snmpdemoapp: $(OBJS1)
    $(CC) -o snmpdemoapp $(OBJS1) $(BUILDLIBS)

asyncapp: $(OBJS3)
    $(CC) -o asyncapp $(OBJS3) $(BUILDLIBS)

example-demon: $(OBJS2)
    $(CC) -o example-demon $(OBJS2)  $(BUILDAGENTLIBS)

clean:
    rm $(OBJS2) $(OBJS2) $(TARGETS)
brdModule.so: brdModule.c Makefile
    $(CC) $(CFLAGS) $(DLFLAGS) -c -o brdModule.o brdModule.c
    $(CC) $(CFLAGS) $(DLFLAGS) -c -o getcpu.o getcpu.c
    $(CC) $(CFLAGS) $(DLFLAGS) -o brdModule.so brdModule.o getcpu.o
 取CPU值具体部分就不用贴了,随你怎么实现都可以。

你可能感兴趣的:(SNMP)