net-snmp扩展trap类型的私有mib

注:本文介绍的是静态编译的方法扩展的私有mib,别的方法请看本人整理的《 net-snmp agent开发(用net-snmp扩展MIB库)

1. 首先创建一个简单的含有table变量的mib文件取名test-trap.mib,后缀名也可以是.txt 实际操作没有区别;

   TEST-TRAP-MIB DEFINITIONS ::= BEGIN
   
   IMPORTS
       MODULE-IDENTITY, OBJECT-TYPE, TimeTicks FROM SNMPv2-SMI
          DisplayString, FROM SNMPv2-TC
       enterprises
           FROM RFC1155-SMI;
       test OBJECT IDENTIFIER ::= { enterprises 1000 }
       TestTraps OBJECT IDENTIFIER ::= { test 1 }
       
  
      cpuRatioHigh NOTIFICATION-TYPE
             OBJECTS  {SystemTrapDescription}
             STATUS  current
             DESCRIPTION
                 "."
         ::= { TestTraps 1 }    
         
  
      TestDescription OBJECT IDENTIFIER ::= { TestTraps 2 }
      
      TestTrapDescription  OBJECT-TYPE
          SYNTAX  DisplayString (SIZE (0..256))
          MAX-ACCESS read-only
          STATUS current
          DESCRIPTION "  "
          ::= { TestDescription 1 }
           
  END
注:其中TestTrapDescription是trap触发时上送的信息,内容可以自定义,trap cpuRatioHigh中object{ }中可以添加多个对象,都将是trap触发时上送的信息;


2. 将创建好的mib文件放入默认目录中,一般默认目录为/usr/local/net-snmp, 或者放到别的目录中,在配置文件snmpd.conf中增加mibs +/home//mibs/mytrap test-trap.mib,或者直接修改默认的mib路径。


3.使用mib2c -c mib2c.notify.conf TEST-TRAP-MIB::TestTraps命令生成相应的.c和.h文件,如果该命令出错(ERROR: You don't have the SNMP perl module installed.  Please obtain
this by getting the latest source release of the net-snmp toolkit from
http://www.net-snmp.org/download/ .  Once you download the source and
unpack it, the perl module is contained in the perl/SNMP directory.
See the README file there for instructions.)请正常make    make install重新安装net-snmp,原因未知。

生成的.c文件如下

/*
 * Note: this file originally auto-generated by mib2c using
 *        $
 */

#include 
#include 
#include 
#include "TestTraps.h"

extern const oid snmptrap_oid[];
extern const size_t snmptrap_oid_len;

int
send_cpuRatioHigh_trap( void )
{
    netsnmp_variable_list  *var_list = NULL;
    const oid cpuRatioHigh_oid[] = { 1,3,6,1,4,1,1000,1,1 };
    const oid TestTrapDescription_oid[] = { 1,3,6,1,4,1,1000,1,2,1, 0 };

    /*
     * Set the snmpTrapOid.0 value
     */
    snmp_varlist_add_variable(&var_list,
        snmptrap_oid, snmptrap_oid_len,
        ASN_OBJECT_ID,
        cpuRatioHigh_oid, sizeof(cpuRatioHigh_oid));
    
    /*
     * Add any objects from the trap definition
     */
    snmp_varlist_add_variable(&var_list,
        TestTrapDescription_oid, OID_LENGTH(TestTrapDescription_oid),
        ASN_OCTET_STR,
        /* Set an appropriate value for TestTrapDescription */
        NULL, 0);

    /*
     * 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;
}

4.修改.c文件,添加函数读取cpu实际使用率和判断是否产生告警函数,每过5秒产生一次报警,这里就直接将cpu使用率定为90,判断告警触发函数写成80,添加初始化函数,注册函数每过一秒读取一下cpu使用率,修改.h文件声明读取cpu使用率函数,修改后的.c函数如下:

/*
 * Note: this file originally auto-generated by mib2c using
 *        $
 */

#include 
#include 
#include 
#include "TestTraps.h"

extern const oid snmptrap_oid[] = {1,3,6,1,6,3,1,1,4,1,0};
const size_t snmptrap_oid_len = OID_LENGTH(snmptrap_oid);

void init_TestTraps(void)
{
    DEBUGMSGTL(("TestTraps","Initializing\n"));
    snmp_alarm_register(1,SA_REPEAT,read_cpudata_repeat, NULL);
}

int
send_cpuRatioHigh_trap( void )
{
    netsnmp_variable_list  *var_list = NULL;
    const oid cpuRatioHigh_oid[] = { 1,3,6,1,4,1,1000,1,1 };
    const oid TestTrapDescription_oid[] = { 1,3,6,1,4,1,1000,1,2,1, 0 };

    static char TestTrapDescription[50];
    strcpy(TestTrapDescription,"CPU使用率过高");
    /*
     * Set the snmpTrapOid.0 value
     */
    snmp_varlist_add_variable(&var_list,
        snmptrap_oid, snmptrap_oid_len,
        ASN_OBJECT_ID,
        cpuRatioHigh_oid, sizeof(cpuRatioHigh_oid));
    
    /*
     * Add any objects from the trap definition
     */
    snmp_varlist_add_variable(&var_list,
        TestTrapDescription_oid, OID_LENGTH(TestTrapDescription_oid),
        ASN_OCTET_STR,
        /* Set an appropriate value for TestTrapDescription */
        NULL, 0);

    /*
     * 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;
}

void judge_send_cputrap(int cpu)
{
    static unsigned int cputrap_clientreg = 0;
    if(cpu > 80)
    {
        if(cputrap_clientreg == 0){
            send_cpuRatioHigh_trap();
            cputrap_clientreg = snmp_alarm_register(5,SA_REPEAT,send_cpuRatioHigh_trap,NULL);

        }
    }
    else
    {
        if(cputrap_clientreg != 0)
        {
            snmp_alarm_unregister(cputrap_clientreg);
            cputrap_clientreg = 0;
        }
    }
}

void read_cpudata_repeat(unsigned int clientreg, void *clientarg)
{
    int cpu = 90;
    judge_send_cputrap(cpu);
}

5. .然后把相应的.c和.h文件添加到net-snmp源码里的 agent/mibgroup路径下,在configure的时候添加   --with-mib-modules="TestTrap" 选项,如果有多个table,则可以再mibgroup中创建一个文件夹例如testMIB文件夹,然后把相应的.c和.h文件都移到testMIB文件夹下,然后在mibgroup路径下新建一个test.h文件,添加config_require(testMIB/TestTable);依次添加,编译的时候添加configure选项为--weith-mib-modules="test"  则可以一次全部编译;最后make   make install


6. 配置trap触发环境,在snmpd.conf添加trap2sink localhost或者trap2sink 127.0.0.1:162,重新启动snmpd 并用-c执行snmpd.conf

    然后新建snmptrapd的配置文件snmptrapd.conf添加authcommunity log,execute,net public,为了能清楚看到trap的打印消息用命令

snmptrapd -d -f -Lo -c snmptrapd.conf启动snmptrapd 

  则每过5秒可以看到trap返回来的告警信息;


注:本人的一点见解,不足之处可以多多交流指正;




你可能感兴趣的:(net-snmp)