日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > windows >内容正文

windows

windows下使用net-snmp实现agent扩展(二)

發(fā)布時(shí)間:2025/7/14 windows 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 windows下使用net-snmp实现agent扩展(二) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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

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")); } 編譯運(yùn)行(注意在入口函數(shù)中將init_nstAgentSubagentObject替換為init_watched)。
打開cmd,測(cè)試一下:snmpget -v1 -c public localhost 1.3.6.1.4.1.8072.2.1.3.0
返回結(jié)果: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”
返回結(jié)果:NET-SNMP-MIB::netSnmp.2.1.3.0 = STRING: "havenzhao" OK,沒問題! 接下來,我們想改變my_string這個(gè)變量的值,這個(gè)值需要從程序的其它地方獲得,這樣才能起到監(jiān)測(cè)的作用。先簡(jiǎn)單改變一下試試看,能否正確運(yùn)行: 修改下watched.c文件,添加一行為my_string改變值的語(yǔ)句,標(biāo)紅的代碼即是。 #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!"; //作為全局變量

/*模擬在其它代碼中改變要監(jiān)測(cè)的my_string值的函數(shù),此函數(shù)可以放在定時(shí)器中,或者其它能夠動(dòng)態(tài)改變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()函數(shù)放在入口函數(shù)文件(example-demon.c)的死循環(huán)里: 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 */
}

再次編譯運(yùn)行,看看有沒有達(dá)到效果:
輸入: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
出現(xiàn)亂碼,沒有成功!

再試試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
照樣失敗!

經(jīng)過反復(fù)測(cè)試,好像和字符串的長(zhǎng)度有關(guān)系。改變后的字符串與初始化時(shí)的字符串長(zhǎng)度不一致時(shí),總是達(dá)不到我們想要的效果。是的,問題就出在這里!

解決方案:

看這行代碼:watcher_flags = WATCHER_MAX_SIZE;
就在這個(gè)小小的參數(shù)上,例子中的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.
而我們則需要這個(gè):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),也就是這個(gè):WATCHER_SIZE_STRLEN

好,馬上替換watcher_flags = WATCHER_MAX_SIZE;為watcher_flags = WATCHER_SIZE_STRLEN;
編譯運(yùn)行,測(cè)試:
輸入: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!到此為止,我們終于前進(jìn)了一步,由get/set一個(gè)int型變量,到get/set一個(gè)字符串型變量,而且這個(gè)字符串變量,可由外部的代碼對(duì)其進(jìn)行設(shè)置,從而實(shí)現(xiàn)了監(jiān)測(cè)變化的字符串變量的目的。

這只是單個(gè)的變量,可能要監(jiān)測(cè)的變量不只一個(gè),那怎么辦呢?未完待續(xù):http://vcsky.net




?

?

轉(zhuǎn)載于:https://blog.51cto.com/havenzhao/954871

總結(jié)

以上是生活随笔為你收集整理的windows下使用net-snmp实现agent扩展(二)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。