刚刚实现了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.c文件:
编译运行(注意在入口函数中将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”
/* 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 */
}
- #ifndef EXAMPLES_WATCHED_H
- #define EXAMPLES_WATCHED_H
- #ifdef __cplusplus
- extern "C" {
- #endif
- void init_watched(void);
- #ifdef __cplusplus
- }
- #endif
- #endif /* EXAMPLES_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"));
- }
/*
* 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>
#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!"; //作为全局变量
- 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”));
- }
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”));
}
/* 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 */
}