windows下使用net-snmp實現agent擴展(二)

剛剛實現了int型的get命令,可能更多的情況下,我們更需要的是字符串類型的。在實現int型的時候,用到了netsnmp_register_int_instance這個函數,很自然想到如果是string型的,用類似的netsnmp_register_string_instance,或者netsnmp_register_char_instance不就行了?很可惜的是:net-snmp並沒有提供這兩個函數。通過查找,在net-snmp5.7.1/agent/mibgroup/examples下面,有個watched.c文件,這裏提供了對字符串類型的操作。實際上,net-snmp將string型歸到了scalar類型裏,做了統一的處理。好,看代碼:

watched.h文件:

#ifndef EXAMPLES_WATCHED_H
#define EXAMPLES_WATCHED_H

#ifdef __cplusplus
extern "C" {
#endif

void init_watched(void);

#ifdef __cplusplus
}
#endif

#endif /* EXAMPLES_WATCHED_H */

 

watched.c文件: /* * start by including the appropriate header files */ #include <net-snmp/net-snmp-config.h> #include <net-snmp/net-snmp-includes.h> #include <net-snmp/agent/net-snmp-agent-includes.h> void init_watched_string(void); void init_watched(void) { init_watched_string(); } void init_watched_string(void) { /* * the storage for our string. It must be static or allocated. * we use static here for simplicity. */ static char my_string[256] = "welcome to vcsky.net!"; /* * the OID we want to register our string at. This should be a * fully qualified instance. In our case, it's a scalar at: * NET-SNMP-EXAMPLES-MIB::netSnmpExampleString.0 (note the trailing * 0 which is required for any instantiation of any scalar object) */ oid my_registration_oid[] = { 1, 3, 6, 1, 4, 1, 8072, 2, 1, 3, 0 }; /* * variables needed for registration */ netsnmp_handler_registration *reginfo; static netsnmp_watcher_info watcher_info; int watcher_flags; /* * a debugging statement. Run the agent with -Dexample_string_instance * to see the output of this debugging statement. */ DEBUGMSGTL(("example_string_instance", "Initalizing example string instance. Default value = %s\n", my_string)); /* * If we wanted a callback when the value was retrieved or set * (even though the details of doing this are handled for you), * you could change the NULL pointer below to a valid handler * function. * * Change RWRITE to RONLY for a read-only string. */ reginfo = netsnmp_create_handler_registration("my example string", NULL, my_registration_oid, OID_LENGTH(my_registration_oid), HANDLER_CAN_RWRITE); /* * the three options for a string watcher are: * fixed size string (length never changes) * variable size (length can be 0 - MAX, for some MAX) * c string (length can be 0 - MAX-1 for some max, \0 is not a valid * character in the string, the length is provided by strlen) * * we'll use a variable length string. */ watcher_flags = WATCHER_MAX_SIZE; /* * create the watcher info for our string. */ netsnmp_init_watcher_info6(&watcher_info, my_string, strlen(my_string), ASN_OCTET_STR, watcher_flags, sizeof(my_string), NULL); /* * the line below registers our "my_string" variable above as * accessible and makes it writable. */ netsnmp_register_watched_instance(reginfo, &watcher_info); DEBUGMSGTL(("example_string_instance", "Done initalizing example string instance\n")); } 編譯運行(注意在入口函數中將init_nstAgentSubagentObject替換爲init_watched)。
打開cmd,測試一下:snmpget -v1 -c public localhost 1.3.6.1.4.1.8072.2.1.3.0
返回結果:NET-SNMP-MIB::netSnmp.2.1.3.0 = STRING: "welcome to vcsky.net!" snmpset -v1 -c public localhost 1.3.6.1.4.1.8072.2.1.3.0 s "havenzhao”
返回結果:NET-SNMP-MIB::netSnmp.2.1.3.0 = STRING: "havenzhao" OK,沒問題! 接下來,我們想改變my_string這個變量的值,這個值需要從程序的其它地方獲得,這樣才能起到監測的作用。先簡單改變一下試試看,能否正確運行: 修改下watched.c文件,添加一行爲my_string改變值的語句,標紅的代碼即是。 #include <net-snmp/net-snmp-config.h> #include <net-snmp/net-snmp-includes.h> #include <net-snmp/agent/net-snmp-agent-includes.h> static char my_string[256] = "welcome to vcsky.net!"; //作爲全局變量

/*模擬在其它代碼中改變要監測的my_string值的函數,此函數可以放在定時器中,或者其它能夠動態改變my_string值的代碼段裏,以保證獲取的my_string值是變化的。別忘了在頭文件watched.h裏聲明*/
void change_string_value()
{
    strcpy(my_string, "haven zhao");
}

void init_watched_string(void); void init_watched(void) { init_watched_string(); } void init_watched_string(void) { oid my_registration_oid[] = { 1, 3, 6, 1, 4, 1, 8072, 2, 1, 3, 0 }; netsnmp_handler_registration *reginfo; static netsnmp_watcher_info watcher_info; int watcher_flags; DEBUGMSGTL(("example_string_instance", "Initalizing example string instance. Default value = %s\n", my_string)); reginfo = netsnmp_create_handler_registration("my example string", NULL, my_registration_oid, OID_LENGTH(my_registration_oid), HANDLER_CAN_RWRITE); watcher_flags = WATCHER_MAX_SIZE; netsnmp_init_watcher_info6(&watcher_info, my_string, strlen(my_string), ASN_OCTET_STR, watcher_flags, sizeof(my_string), NULL); netsnmp_register_watched_instance(reginfo, &watcher_info); DEBUGMSGTL(("example_string_instance", "Done initalizing example string instance\n")); } 爲了方便,我們把change_string_value()函數放在入口函數文件(example-demon.c)的死循環裏: while(keep_running) {
    /* if you use select(), see snmp_select_info() in snmp_api(3) */
    /*           --- OR ---        */
    change_string_value(); //放在這裏了
    agent_check_and_process(1); /* 0 == don't block */
}

再次編譯運行,看看有沒有達到效果:
輸入:snmpget -v1 -c public localhost 1.3.6.1.4.1.8072.2.1.3.0
返回:NET-SNMP-MIB::netSnmp.2.1.3.0 = Hex-STRING: 68 61 76 65 6E 20 7A 68 61 6F 00 76
出現亂碼,沒有成功!

再試試set命令:snmpset -v1 -c public localhost 1.3.6.1.4.1.8072.2.1.3.0 s "haven"
返回:NET-SNMP-MIB::netSnmp.2.1.3.0 = STRING: "haven"
可見set成功!try again,再輸入get命令:snmpget -v1 -c public localhost 1.3.6.1.4.1.8072.2.1.3.0
返回:NET-SNMP-MIB::netSnmp.2.1.3.0 = STRING: "haven"
這次卻成功了。

難道要先set,再get,才行?什麼情況?再試一次!
輸入:snmpset -v1 -c public localhost 1.3.6.1.4.1.8072.2.1.3.0 s "vcsky.net haven zhao"
返回:NET-SNMP-MIB::netSnmp.2.1.3.0 = STRING: "vcsky.net haven zhao"
輸入:snmpget -v1 -c public localhost 1.3.6.1.4.1.8072.2.1.3.0
返回:NET-SNMP-MIB::netSnmp.2.1.3.0 = Hex-STRING: 68 61 76 65 6E 20 7A 68 61 6F 00 61
照樣失敗!

經過反覆測試,好像和字符串的長度有關係。改變後的字符串與初始化時的字符串長度不一致時,總是達不到我們想要的效果。是的,問題就出在這裏!

解決方案:

看這行代碼:watcher_flags = WATCHER_MAX_SIZE;
就在這個小小的參數上,例子中的WATCHER_MAX_SIZE:variable size (length can be 0 - MAX, for some MAX),If set then the variable data_size_p points to is supposed to hold the current size of the watched object and will be updated on writes.
而我們則需要這個:c string (length can be 0 - MAX-1 for some max, \0 is not a valid  character in the string, the length is provided by strlen),也就是這個:WATCHER_SIZE_STRLEN

好,馬上替換watcher_flags = WATCHER_MAX_SIZE;爲watcher_flags = WATCHER_SIZE_STRLEN;
編譯運行,測試:
輸入:snmpget -v1 -c public localhost 1.3.6.1.4.1.8072.2.1.3.0
返回:NET-SNMP-MIB::netSnmp.2.1.3.0 = STRING: "haven zhao"

OK!到此爲止,我們終於前進了一步,由get/set一個int型變量,到get/set一個字符串型變量,而且這個字符串變量,可由外部的代碼對其進行設置,從而實現了監測變化的字符串變量的目的。

這只是單個的變量,可能要監測的變量不只一個,那怎麼辦呢?未完待續:http://vcsky.net




 

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章