1. 程式人生 > >net-snmp擴充套件trap型別的私有mib

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 <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#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 <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#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返回來的告警資訊;

注:本人的一點見解,不足之處可以多多交流指正;