【snmp】net-snmp添加自定義MIB(標量)


 

安裝net-snmp見:【snmp】centos6.5安裝和配置snmp5.7.1

net-snmp添加自定義MIB(表格) 見:【snmp】net-snmp添加自定義MIB(表格)

一、編寫MIB文件

 My-MIB.txt文件內容如下,新增的葉子節點myNode的oid為1.3.6.1.4.1.310.1.1

My-MIB DEFINITIONS::= BEGIN
	IMPORTS
		enterprises, OBJECT-TYPE, Integer32
    FROM SNMPv2-SMI
		TEXTUAL-CONVENTION, DisplayString
    FROM SNMPv2-TC;

	myModule MODULE-IDENTITY 
		LAST-UPDATED "202007231640Z"		
		ORGANIZATION 
			"Organization."
		CONTACT-INFO 
			"Contact-info."
		DESCRIPTION 
			"Description."
		::= { enterprises 310 }

	myObj OBJECT IDENTIFIER::={myModule 1}
 
	myNode OBJECT-TYPE
		SYNTAX      DisplayString
		ACCESS      read-write
		STATUS      current
		DESCRIPTION "my node"
		::={myObj 1}
END

 

 

二、使用mib2c命令生成.c和.h文件

上傳My-MIB.txt到linux機器的/usr/local/snmp/share/snmp/mibs目錄下

 

1、使用如下命令查看文件格式是否正確

/usr/local/snmp/bin/snmptranslate -Tp -IR My-MIB::myModule

 

2、執行如下命令生成標量文件

env MIBS="+/usr/local/snmp/share/snmp/mibs/My-MIB.txt" /usr/local/snmp/bin/mib2c -c mib2c.scalar.conf My-MIB::myModule

以上命令執行完后會生成myModule.hmyModule.c文件

生成的原代碼(myModule.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 "myModule.h"

/** Initializes the myModule module */
void
init_myModule(void)
{
    const oid       myNode_oid[] = { 1, 3, 6, 1, 4, 1, 310, 1, 1 };

    DEBUGMSGTL(("myModule", "Initializing\n"));

    netsnmp_register_scalar(netsnmp_create_handler_registration
                            ("myNode", handle_myNode, myNode_oid,
                             OID_LENGTH(myNode_oid), HANDLER_CAN_RWRITE));
}

int
handle_myNode(netsnmp_mib_handler *handler,
              netsnmp_handler_registration *reginfo,
              netsnmp_agent_request_info *reqinfo,
              netsnmp_request_info *requests)
{
    int             ret;
    /*
     * We are never called for a GETNEXT if it's registered as a
     * "instance", as it's "magically" handled for us.  
     */

    /*
     * a instance handler also only hands us one request at a time, so
     * we don't need to loop over a list of requests; we'll only get one. 
     */

    switch (reqinfo->mode) {

    case MODE_GET:
        snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
                                 /*
                                  * XXX: a pointer to the scalar's data 
                                  */ ,
                                 /*
                                  * XXX: the length of the data in bytes 
                                  */ );
        break;

        /*
         * SET REQUEST
         *
         * multiple states in the transaction.  See:
         * http://www.net-snmp.org/tutorial-5/toolkit/mib_module/set-actions.jpg
         */
    case MODE_SET_RESERVE1:
        /*
         * or you could use netsnmp_check_vb_type_and_size instead 
         */
        ret = netsnmp_check_vb_type(requests->requestvb, ASN_OCTET_STR);
        if (ret != SNMP_ERR_NOERROR) {
            netsnmp_set_request_error(reqinfo, requests, ret);
        }
        break;

    case MODE_SET_RESERVE2:
        /*
         * XXX malloc "undo" storage buffer 
         */
        if ( /* XXX if malloc, or whatever, failed: */ ) {
            netsnmp_set_request_error(reqinfo, requests,
                                      SNMP_ERR_RESOURCEUNAVAILABLE);
        }
        break;

    case MODE_SET_FREE:
        /*
         * XXX: free resources allocated in RESERVE1 and/or
         * RESERVE2.  Something failed somewhere, and the states
         * below won't be called. 
         */
        break;

    case MODE_SET_ACTION:
        /*
         * XXX: perform the value change here 
         */
        if ( /* XXX: error? */ ) {
            netsnmp_set_request_error(reqinfo, requests, /* some error */
                                      );
        }
        break;

    case MODE_SET_COMMIT:
        /*
         * XXX: delete temporary storage 
         */
        if ( /* XXX: error? */ ) {
            /*
             * try _really_really_ hard to never get to this point 
             */
            netsnmp_set_request_error(reqinfo, requests,
                                      SNMP_ERR_COMMITFAILED);
        }
        break;

    case MODE_SET_UNDO:
        /*
         * XXX: UNDO and return to previous value for the object 
         */
        if ( /* XXX: error? */ ) {
            /*
             * try _really_really_ hard to never get to this point 
             */
            netsnmp_set_request_error(reqinfo, requests,
                                      SNMP_ERR_UNDOFAILED);
        }
        break;

    default:
        /*
         * we should never get here, so this is a really bad error 
         */
        snmp_log(LOG_ERR, "unknown mode (%d) in handle_myNode\n",
                 reqinfo->mode);
        return SNMP_ERR_GENERR;
    }

    return SNMP_ERR_NOERROR;
}
View Code

 

需要對myModule.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 "myModule.h"

/** Initializes the myModule module */
void
init_myModule(void)
{
    const oid       myNode_oid[] = { 1, 3, 6, 1, 4, 1, 310, 1, 1 };

    DEBUGMSGTL(("myModule", "Initializing\n"));

    netsnmp_register_scalar(netsnmp_create_handler_registration
                            ("myNode", handle_myNode, myNode_oid,
                             OID_LENGTH(myNode_oid), HANDLER_CAN_RWRITE));
}



/**定義一個變量*/
static char buff[256]="";

int
handle_myNode(netsnmp_mib_handler *handler,
              netsnmp_handler_registration *reginfo,
              netsnmp_agent_request_info *reqinfo,
              netsnmp_request_info *requests)
{
    int             ret;
    /*
     * We are never called for a GETNEXT if it's registered as a
     * "instance", as it's "magically" handled for us.  
     */

    /*
     * a instance handler also only hands us one request at a time, so
     * we don't need to loop over a list of requests; we'll only get one. 
     */

    switch (reqinfo->mode) {

    case MODE_GET:
        snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
                                  buff, //定義的變量名稱buff
                                  strlen(buff) //buff的長度
                                  );
        break;

        /*
         * SET REQUEST
         *
         * multiple states in the transaction.  See:
         * http://www.net-snmp.org/tutorial-5/toolkit/mib_module/set-actions.jpg
         */
    case MODE_SET_RESERVE1:
        /*
         * or you could use netsnmp_check_vb_type_and_size instead 
         */
        ret = netsnmp_check_vb_type(requests->requestvb, ASN_OCTET_STR);
        if (ret != SNMP_ERR_NOERROR) {
            netsnmp_set_request_error(reqinfo, requests, ret);
        }
        break;

    case MODE_SET_RESERVE2:
        /*
         * XXX malloc "undo" storage buffer 
         */
        if ( 0 /* 出錯條件 if malloc, or whatever, failed: */ ) {
            netsnmp_set_request_error(reqinfo, requests,
                                      SNMP_ERR_RESOURCEUNAVAILABLE);
        }
        break;

    case MODE_SET_FREE:
        /*
         * XXX: free resources allocated in RESERVE1 and/or
         * RESERVE2.  Something failed somewhere, and the states
         * below won't be called. 
         */
        break;

    case MODE_SET_ACTION:
        /*
         * XXX: perform the value change here 
         */
        if ( 0/* 出錯條件 */ ) {
            netsnmp_set_request_error(reqinfo, requests, 0/* 錯誤 error */
                                      );
        }
        break;

    case MODE_SET_COMMIT:
        /*
         * XXX: delete temporary storage 
         */
        memcpy(buff,requests->requestvb->buf,requests->requestvb->val_len); // 將set的值賦值給變量buff
        buff[requests->requestvb->val_len] ='\0';   //設置字符串結束符
        if ( 0/* 出錯條件 */ ) {
            /*
             * try _really_really_ hard to never get to this point 
             */
            netsnmp_set_request_error(reqinfo, requests,
                                      SNMP_ERR_COMMITFAILED);
        }
        break;

    case MODE_SET_UNDO:
        /*
         * XXX: UNDO and return to previous value for the object 
         */
        if ( 0/* 出錯條件 */ ) {
            /*
             * try _really_really_ hard to never get to this point 
             */
            netsnmp_set_request_error(reqinfo, requests,
                                      SNMP_ERR_UNDOFAILED);
        }
        break;

    default:
        /*
         * we should never get here, so this is a really bad error 
         */
        snmp_log(LOG_ERR, "unknown mode (%d) in handle_myNode\n",
                 reqinfo->mode);
        return SNMP_ERR_GENERR;
    }

    return SNMP_ERR_NOERROR;
}

 

三、載入自定義的MIB庫

 

1、將myModule.h 和修改后的myModule.c文件復制到linux機器的net-snmp-5.7.1/agent/mibgroup/目錄下

 

2、如果snmp服務在運行,停止snmp服務

 

3、/root/net-snmp-5.7.1目錄下依次執行下面3個命令編譯安裝

 ./configure --prefix=/usr/local/snmp --with-mib-modules="myModule"

 make

 make install

 

 

四、測試

 

1、執行/usr/local/snmp/sbin/snmpd -c /usr/local/snmp/etc/snmpd.conf 命令啟動snmp服務

 

2、設置myNode的值為"harara"

/usr/local/snmp/bin/snmpset -v 2c -c public localhost 1.3.6.1.4.1.310.1.1.0 s harara

 

 

3、查看myNode的值

/usr/local/snmp/bin/snmpget -v 2c -c public localhost 1.3.6.1.4.1.310.1.1.0

 

 

 

 

 

 

參考地址

Net-SNMP中MIB庫的擴展

linux下snmpset

net-snmp添加自定義MIB

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM