刚刚实现了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

转载于:https://blog.51cto.com/havenzhao/954871

windows下使用net-snmp实现agent扩展(二)相关推荐

  1. Windows下Memcache的安装及PHP扩展配置方法

    这篇文章主要介绍了Windows下Memcache的安装及PHP扩展配置方法,需要的朋友可以参考下 一.下载 找到完整的memcache的Windows安装包,解压放在硬盘上,比如 F:\memcac ...

  2. windows下安装php5.5的redis扩展

    windows下开发用的xampp集成的环境,想装个php-redis扩展,扩展的github地址: https://github.com/nicolasff/phpredis php_redis.d ...

  3. windows下delf配置:delf环境(二)

    windows下delf配置:基础环境(一) windows下delf配置:delf环境(二) 一.delf基础package 1.1.安装tf-Slim cd c:\dlenv git clone ...

  4. php编码和c语言,急求windows下用c语言开发PHP扩展时,在C语言里把字符串转成utf-8编码再打印的方法。...

    因我是用windows下用c开发PHP扩展时, 想在C的函数内直接打印php_printf某字符串(其中含有汉字字符), 但在PHP调用这个函数时,看到的时汉字乱码.PHP页面用的是utf-8编码格式 ...

  5. linux redis 5.6扩展,Windows下为PHP5.6安装Redis扩展和memcached扩展

    2.根据PHP版本号,编译器版本号和CPU架构, 选择php_redis-2.2.5-5.6-ts-vc11-x64.zip和php_igbinary-1.2.1-5.5-ts-vc11-x64.zi ...

  6. windows下php连接oracle安装oci8扩展报错(PHP Startup: Unable to load dynamic library ‘oci8_11g‘)

    记录一下php7.29安装oci8的艰苦过程,简直就是唐僧西天取经历经九九八十一难. 使用的是phpstudy_pro安装的ph扩展wnmp环境下: 1 .安装oralce Instant Clien ...

  7. windows 下xampp集成环境安装mongodb扩展

    http://www.thegeekstuff.com/2015/10/php-mongodb-for-xampp 见这文章,写的很详细. 1.打开PHPINFO界面查看PHP版本,编译器版本,还有架 ...

  8. Windows下Caffe的学习与应用(二)——优化自己训练的模型(fine-tuning)

    前言 在前面的博文中,我们也看到,在进行训练的样本比较少,每个种类的样本只有120张,迭代次数为100代,训练得到的模型在测试那些特征不那么明显图像时,准确率达不到我们想要的效果.如果下图: 测试的结 ...

  9. [Telink泰凌微825x]Windows下开发环境搭建与烧录(二)

    目录 一.IDE的下载 二.IDE安装 1.Telink IDE 安装 ​编辑 2.解压SDK 3.导入SDK 4.编译工程 5.编译 ​编辑 编译成功后,会在工作空间下面多一个和工厂名称一样的文件夹 ...

  10. windows下使用net-snmp实现agent扩展(一)

    项目上需要用snmp来做告警监控管理,达到对系统的运行状态的监测.这几天研究了一下,发现网上资料比较少,大多数抄来抄去,能够正确运行的更少.所以,总结了一下,把相关的代码放上来,希望能够帮助同样遇到困 ...

最新文章

  1. 一次简单的Java服务性能优化,实现压测 QPS 翻倍
  2. 神秘的subsys_initcall【转】
  3. crontab 每5分钟_Crontab安装步骤和命令使用详细解说
  4. *【CodeForces - 791B】Bear and Friendship Condition (图论,判断完全图,dfs乱搞或带权并查集)
  5. Spring Boot笔记-拦截器相关(用户权限方面)
  6. OpenJudge 8782 乘积最大——S.B.S
  7. Pytorch遍历DataLoader时报错BrokenPipeError: [Errno 32] Broken pipe
  8. 原生 JavaScript 实现扫雷
  9. vs2017激活密钥
  10. im服务器开源项目,Oschat IM 开源即时通讯项目介绍
  11. 终于找到YST的BLOG了!!!!
  12. 可口可乐市场调查失败的原因_经典案例可口可乐一次市场调研失败的教训
  13. 无线路由器DNS服务器解析,路由器DNS劫持简单实现和防范分析
  14. .net core 中的[FromBody]和[FromFrom]的区别
  15. UE Gameplay Learning Record
  16. 京东mate10评论分析
  17. SpringCloud项目No qualifying bean of type ‘×××Mapper‘ available:的错误解决
  18. (6)自旋模型基态算法
  19. ID基本操作(标尺,参考线,网格)5.11
  20. 计算机专业去支教学到什么,支教的收获及感悟4篇_大学生支教感想

热门文章

  1. Windows Server 2012 服务器之Web服务器
  2. Android setTag()/getTag()
  3. python用什么运行环境_使用SAE部署Python运行环境的教程
  4. sqlserver 查找某个字段在哪张表里
  5. 解决Ubuntu16.04重启进入initramfs
  6. MariaDB 主从同步与热备(14)
  7. Linux查看CPU相关信息
  8. 解决Apache配置虚拟主机时出现403错误的问题
  9. 初识费用流 模板(spfa+slf优化) 餐巾计划问题
  10. 网站访问过程理解(一点记录)