`
axengine
  • 浏览: 145617 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
社区版块
存档分类
最新评论

net-snmp扩展自己的程序

    博客分类:
  • 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 <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#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值具体部分就不用贴了,随你怎么实现都可以。
分享到:
评论

相关推荐

    net-snmp开始教程

    Net-SNMP 是一个开源的基于 SNMP(Simple Network Management Protocol,简单网络管理协议)的开发库,允许开发者创建自己的 SNMP 应用程序。本文将详细介绍 Net-SNMP 的开发过程、开发环境配置和相关工具的使用。 ...

    Net-SNMP代理开发实例程序

    这个是本人由于公司需要扩展Net-SNMP的Agent而写的一个开发流程文档,压缩包中也包含程序的源代码,和配置文件。根据Net-SNMP官方的实例程序,详细介绍了SNMP代理开发的各个步骤,各位读者按照步骤可以轻松的完成一...

    net-snmp源码包,学习开发

    2. 库支持:包含C语言的库,便于开发者在自己的应用程序中集成SNMP功能。 3. 工具集:提供了一系列命令行工具,如snmpget、snmpset、snmpwalk等,用于查询和操作网络设备。 4. 可移植性:Net-SNMP可以在多种操作系统...

    net-snmp-5.8.tar.gz

    在实际应用中,Net-SNMP插件可以扩展SNMP代理的功能,例如添加自定义的MIB(Management Information Base)模块,用于监控特定的硬件、软件或服务状态。通过编写MIB文件和相应的代理代码,我们可以定制SNMP代理以...

    net-snmp-5.7.3.tar.gz

    《深入理解net-snmp-5.7.3:源码编译与安装指南》 net-snmp是一款功能强大的网络管理软件套件,主要用于网络设备的监控、管理和数据收集。其5.7.3版本提供了丰富的功能和改进,适用于各种网络环境。在本文中,我们...

    net-snmp agent开发详解-

    描述中提到的"扩展一个子代理"是指编写一个新的程序,该程序实现了特定的MIB对象,然后将其集成到SNMP服务中。MIB库是SNMP代理的核心部分,它定义了可被管理的对象(如接口状态、路由信息等)。"扩展自定义的MIB库...

    NET-SNMP 5.6 安装包

    6. 自定义MIB模块:允许扩展SNMP功能,以适应特定网络环境。 NET-SNMP 5.6在实际应用中常见于以下场景: 1. 网络监控:通过SNMP获取网络设备的性能数据,如CPU利用率、内存使用情况、接口流量等。 2. 故障排查:当...

    net-snmp-5.4.3

    - **可扩展性**:用户可以通过编写自己的MIB模块来扩展其功能,适应特定的网络环境。 - **社区支持**:作为一个开源项目,Net-SNMP拥有活跃的社区,提供及时的技术支持和更新。 **应用场景** 1. **网络监控**:...

    net-snmp-5.7.3.pre1.tar.gz_NET-SNMP_net_snmp

    3. 应用程序编程接口(API):NET-SNMP提供了C语言的API,允许开发者集成SNMP功能到自己的应用程序中。 六、实战案例 1. 监控网络设备状态:通过snmpget定期检查路由器、交换机的CPU利用率、内存使用情况,及时发现...

    深入理解Net-SNMP代码.zip

    2. **Chapter 7**:这一章可能深入到Net-SNMP库的使用,包括如何编译和安装Net-SNMP,以及如何编写简单的SNMP代理程序。可能会讲解如何定义MIB对象,并将其映射到C语言结构体。 3. **Chapter 8**:可能涵盖了SNMP...

    net-snmp-5.4.tar.gz

    - **增强的MIB支持**:增加了对更多网络设备和应用程序的MIB(Management Information Base)支持,扩展了可监控的网络资源范围。 - **API改进**:提供了更稳定的编程接口,便于开发者集成到自定义应用中。 - **...

    net-snmp-5.2.1.2.tar.gz

    Net-SNMP支持自定义MIB扩展,允许用户根据需求创建和维护自己的MIB模块,以适应特定的网络环境。 3. **管理工具**:除了代理之外,Net-SNMP还提供了一系列的命令行工具,例如`snmpwalk`、`snmpget`和`snmpset`,...

    net-snmp编译好的lib库

    net-snmp5.7.1版本的lib库及dll文件,使用VS2010编译出来的,...包含netsnmp.lib netsnmpagent.lib netsnmpmibs.lib netsnmptrapd.lib netsnmp.dll,以及相应的头文件,可以在windows平台下开发net-snmp的Agent扩展程序

    net-snmp version 5.5

    此外,net-snmp还提供了C语言的API,使得开发者能够轻松地在应用程序中集成SNMP功能。 2. **版本5.5的改进与特性** - **安全增强**:net-snmp 5.5对安全性进行了加强,支持更高级别的认证和加密机制,如USM(User-...

    windows下安装net-snmp代理

    首先,需要下载 Net-SNMP 的安装程序,网址为 &lt;http://www.net-snmp.org/download.html&gt;。选择适合的版本,例如 net-snmp-5.7.0-1.x86.exe,然后选择 Windows external DLL 扩展支持。安装过程中,保持所有默认设置...

    net-snmp-5.7 安装包

    3. **库函数**:net-snmp提供了C语言编写的库,供开发人员在自己的应用程序中实现SNMP功能,支持SNMPv1, SNMPv2c, 和 SNMPv3 三种版本。 4. **MIB支持**:MIB(Management Information Base)是SNMP管理信息的结构...

    net-snmp,很好的snmp工具

    这个工具集包含了各种用于管理和监控网络设备的实用程序,如snmpd(SNMP代理),snmpwalk,snmpget,snmpset等。在本文中,我们将深入探讨Net-SNMP及其主要组件、安装过程以及如何有效地使用它。 首先,SNMP是一种...

    net-snmp软件包的应用

    1. **代理扩展实例**:NET-SNMP允许开发者通过C语言编写代码来扩展代理的功能,如增加新的MIB对象或修改现有对象的行为。通过自定义MIB文件,可以定义特定于应用程序的对象类型和访问控制策略。 2. **MIB文件**:...

    net-snmp在linux上的移植

    `snmp.conf`文件用于配置net-snmp的其他小程序,如snmptranslate,而`snmpd.conf`则是snmpd服务的配置文件。 移植到ARM11板上时,需使用arm-linux-gcc交叉编译器重新编译net-snmp,确保配置时指定正确的交叉编译...

    snmp 源代码 net-snmp-5.3.2.zip

    10. **扩展MIB模块**:Net-SNMP允许用户通过编写自己的MIB模块来扩展SNMP代理的功能,这些模块可以暴露自定义的网络设备信息。 通过学习和使用Net-SNMP源代码,开发者可以深入了解SNMP的工作原理,同时也可以定制...

Global site tag (gtag.js) - Google Analytics