`
axengine
  • 浏览: 143171 次
  • 性别: 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的Agent而写的一个开发流程文档,压缩包中也包含程序的源代码,和配置文件。根据Net-SNMP官方的实例程序,详细介绍了SNMP代理开发的各个步骤,各位读者按照步骤可以轻松的完成一...

    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 agent开发详解-

    扩展一个子代理,让SNMPD以主代理的模式运行,对于SNMPD我们只要让它启动就可以,不需要任何的更改和配置,把子代理编译生成的程序运行起来就可以扩展自定义的MIB库

    Net-snmp agent扩展开发示例

    在net-snmp环境下的agent扩展开发示例,包括net-snmp环境的安装与配置,agent程序开发的一般流程示例,有详尽的说明。

    NET-SNMP网管软件V5.7.3 C语言源代码

    Net-SNMP是一个免费的、开放源码的简单网络管理协议...软件构成命令行应用程序从支持SNMP的设备获得数据.支持独立请求 (snmpget, snmpgetnext), 与重复请求 (snmpwalk, snmptable, snmpdelta).对支持SNMP的设备配置属

    snmp-shell:通过Net-SNMP进行Shell仿真并具有扩展功能

    SNMP外壳通过Net-SNMP进行Shell仿真并具有扩展功能这是一个预览:描述如果您的目标具有具有“扩展”功能的Net-SNMP实例,并且您获得了一个SNMP社区字符串,该字符串可为您提供写访问权限,则可以使用此工具来自动...

    net-snmp-extend-mailstats:为 EximPostfixSendmail 和许多扫描程序 (VirusSpam) 提供带有统计信息的 snmp 扩展的脚本

    snmp-扩展-mxstats 版本:1.x-beta3-rc1 - David "Dinde" 17/11/2013 用于提供带有 Exim/Postfix/Sendmail 和许多支持 DKIM/SPF/Greylist 的扫描程序(病毒/垃圾邮件)的统计信息的 snmp 扩展的脚本。 使用的 OID:...

    lba-snmp-interface

    该项目的目的是扩展 SNMP 代理(在本例中为 Net-SNMP),以提供允许访问和更改游戏数据的自定义 MIB。 为此,选择了开源项目 TwinEngine,它是对由 Adeline Software International 于 1994 年开发的 Little Big ...

    SNMP协议基础,网络学习必备

    2.4 V2版本中的SMI扩展 13 2.5进一步了解MIB-II 15 2.6 SNMP 操作 17 2.6.1 get 17 2.6.2 getnext 20 2.6.3 getbulk 23 2.6.4 set操作 25 2.6.5 get, getnext, getbulk, 以及set 错误响应 26 2.6.6 SNMP trap 27 ...

    SNMP(Simple Network Management Protocol,简单网络管理协议)

    SNMP的设计原则是简单性和扩展性。简单性是通过信息类型限制、请求响应或协议而取得。扩展性是通过将管理信息模型与协议、被管理对象的详细规定(MIB)分离而实现的。 网络管理体系结构 SNMP的网络管理模型包括以下...

    基于Java的SNMP网络管理系统

    在实现其基本功能的同时运用Java技术建立并部署应用程序,采用封装技术提高系统的扩展性和灵活性。基于用户界面层、中间层和存储层的3层体系结构,设计并实现该网络管理系统。本设计基于简单网络管理协议(Simple ...

    SNMP协议详解,下载看看吧

    SNMPv3的一个目标是支持一种容易扩展的模块化体系结构,将以前版本中的代理和管理站统一为SNMP实体。SNMP实体由两部分组成:SNMP引擎和SNMP应用程序

    JAVA基于SNMP网络设备MIB信息采集(论文+源代码).zip

    同时,阐述了JAVA语言在SNMP网络设备MIB信息采集方面的优势,包括跨平台性、易于维护和扩展性。 源代码部分包含了JAVA语言实现的SNMP网络设备MIB信息采集程序,可用于获取网络设备的各种信息,如CPU利用率、内存使用...

    devops-infrustructure-monitoring-nagios-installation-rhel8

    安装Nagios 4.4.5和Nagios插件2.2.1 Nagios核心- Nagios Core(以前称为Nagios)是一种免费的开源计算机软件应用程序,用于监视系统,网络和基础结构。 Nagios插件- 插件是Nagios Core的独立扩展,可以使用Core监视...

    Agent中英文对照译文

    Agent++是在Snmp++的基础上,扩展了Snmp++中的概念,它是用来开发SNMP代理的一套C++类的集合。它继承了Snmp++的优点,封装了绝大部分的SNMP的标准操作,并且利用面向对象的特性,使开发者能够通过派生的子类,重载...

    TrackNode:基于SNMP的网络节点轮询套件-开源

    TrackNode旨在成为一种健壮的,可扩展的体系结构,用于通过SNMP轮询任意系统指标。 该项目仍处于实验初期,因此功能受到限制(当前代码比PoC更为重要)。 本代码的功能不提供任何保证。 可以在...

    snmpAdaptor4j:用于JMX的SNMP适配器。-开源

    snmpAdaptor4j是Java管理扩展(JMX)的适配器,可通过SNMP协议轻松访问MBean。 因此,您可以使用此适配器将大多数监视工具(如Nagios和Cacti)连接到Java应用程序。 对于每个MBean,XML映射文件都允许在SNMP适配器的...

    01-运维监控-zabbix

    它能够监控各种网络设备、服务器和应用程序,并提供了丰富的报警和通知功能,是企业 IT 管理人员进行监控和管理的重要工具。 支持多种监控方式:Zabbix 支持 SNMP、JMX、IPMI、SSH、Telnet 和 HTTP 等多种协议和方法...

    cmd操作命令和linux命令大全收集

    tftp -i 自己IP(用肉机作跳板时这用肉机IP) get server.exec:server.exe 登陆后,将“IP”的server.exe下载到目标主机c:server.exe 参数:-i指以二进制模式传送,如传送exe文件时用,如不加-i 则以ASCII模式(传送...

    随身典k.jar 集成到J2EE、JINI甚至是SNMP应用中。

    随身典k.jar 此外,通过Log4j其他语言接口,您可以在C、C++、.Net、PL/SQL程序中使用Log4j,其语法和用法与在Java程序中...而且,通过使用各种第三方扩展,您可以很方便地将Log4j集成到J2EE、JINI甚至是SNMP应用中。

Global site tag (gtag.js) - Google Analytics