擴展MIB庫
關於MIB庫的擴展網絡文章很是多,這裏我主要參考了http://blog.csdn.net/qq_27204267/article/details/51595708,這篇文章介紹的比較簡單,流程清晰,內容全面,沒有太多的理論講解。
閒言少敘,參考前人經驗直接在個人環境裏來個「傻瓜」操做並記錄步驟,防止忘記。哎,年紀大了健忘啊^^^^^^同時爲了後續的移植操做和深刻分析作基礎。
1. 編譯MIB庫文件網絡
一. 編譯MIB庫文件
app
-- Test-MIB.my Test-MIB DEFINITIONS ::= BEGIN IMPORTS OBJECT-GROUP, MODULE-COMPLIANCE, NOTIFICATION-GROUP FROM SNMPv2-CONF enterprises, Integer32, Unsigned32, OBJECT-TYPE, MODULE-IDENTITY, NOTIFICATION-TYPE FROM SNMPv2-SMI DisplayString FROM SNMPv2-TC; -- October 09, 2002 at 14:50 GMT -- 1.3.6.1.4.1.16535 Test MODULE-IDENTITY LAST-UPDATED "200210091450Z" -- October 09, 2002 at 14:50 GMT ORGANIZATION "" CONTACT-INFO "" DESCRIPTION "Video's Server MIB." ::= { enterprises 16535 } -- Node definitions -- This part will include all details about the Test. -- 1.3.6.1.4.1.16535.1 Time OBJECT IDENTIFIER ::= { Test 1 } -- 1.3.6.1.4.1.16535.1.1 GetTime OBJECT-TYPE SYNTAX DisplayString (SIZE (0..100)) MAX-ACCESS read-only STATUS current DESCRIPTION "Example : 2013/4/11" ::= { Time 1 } END -- Test-MIB.my
二. 生成源代碼並修改實現的函數
ide
/* * 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 "Test.h" #include <time.h> /** Initializes the Test module */ void init_Test(void) { const oid GetTime_oid[] = { 1, 3, 6, 1, 4, 1, 16535, 1, 1 }; DEBUGMSGTL(("Test", "Initializing\n")); netsnmp_register_scalar(netsnmp_create_handler_registration ("GetTime", handle_GetTime, GetTime_oid, OID_LENGTH(GetTime_oid), HANDLER_CAN_RONLY)); } int handle_GetTime(netsnmp_mib_handler *handler, netsnmp_handler_registration *reginfo, netsnmp_agent_request_info *reqinfo, netsnmp_request_info *requests) { /* * 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. */ time_t t; switch (reqinfo->mode) { case MODE_GET: time(&t); char szTime[100]; snprintf(szTime,100,"%s",ctime(&t)); snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR, /* * XXX: a pointer to the scalar's data */ szTime, /* * XXX: the length of the data in bytes */ strlen(szTime)); break; default: /* * we should never get here, so this is a really bad error */ snmp_log(LOG_ERR, "unknown mode (%d) in handle_GetTime\n", reqinfo->mode); return SNMP_ERR_GENERR; } return SNMP_ERR_NOERROR; }
三. 靜態庫編譯安裝
函數
./configure --prefix=/usr/local/net-snmp --with-default-snmp-version=2c --with-sys-contact="xxx@163.com" --with-sys-location="China" --with-logfile="/var/log/snmpd.log" --with-persistent-directory=/var/net-snmp/ --enable-applications --enable-ipv6 --with-out-transports="TCP TCPv6 Unix" --with-transports="Callback UDP UDPIPv6" --disable-nls --enable-shared --enable-static --disable-embedded-perl --with-mib-modules=Test
將.c 和.h文件移動到源碼目錄下,個人是/opt/net-snmp-5.7.3/agent/mibgroup。
運行configure命令,而且添加--with-mib-modules=Test,進行MIB庫文件的配置。
運行make && make install編譯安裝
測試:
執行命令:snmpget -v2c -c public localhost Test-MIB:GetTime.0測試oop
四. 生成共享庫編譯
具體編譯命令爲: 測試
gcc -I `net-snmp-config --cflags` -fPIC -shared -g -O0 -o Test.so Test.c `net-snmp-config --libs`
修改snmpd.conf,添加一行:
dlmod Test /usr/local/net-snmp/share/snmp/mibs/Test.so
測試:
從新configure(不要--with-mib-modules=Test),make,make install,安裝snmpd程序
執行命令:snmpget -v2c -c public localhost Test-MIB:GetTime.0測試this