windows下使用net-snmp实现agent扩展(一)
项目上需要用snmp来做告警监控管理,达到对系统的运行状态的监测。这几天研究了一下,发现网上资料比较少,大多数抄来抄去,能够正确运行的更少。所以,总结了一下,把相关的代码放上来,希望能够帮助同样遇到困惑的朋友。 havenzhao http://vcsky.net
项目名称为DCS系统,采用VS2010开发,DCS作为被监测的对象,因此需要实现snmp的Agent扩展。最开始的方法,采用了WinSnmp,发现步骤很繁琐,需要编写dll,需要手动修改注册表,需要安装snmp协议服务,可行性和意义都不大。又研究了开源软件net-snmp,snmp++,agent++。网上说net-snmp主要适用于Linux平台,在这几天的开发中,它完全能够胜任Windows平台,只是网上的资料偏少而已。
DCS就是使用net-snmp(版本是5.7.1),实现了Windows平台下的Agent端,实现了get、set、trap命令。
接下来的工作,我们这样展开:
1、首先要得到四个lib库以及一个dll文件:netsnmp.lib netsnmpagent.lib netsnmpmibs.lib netsnmptrapd.lib,netsnmp.dll。在下载了net-snmp的源码后,编译,需要阅读源码下的“README.win32”文件,工程默认的是VC6.0的,但由于要装SDK之类的,我直接转化为VS2010的了,实际上,还可以转为VS2003、VS2005、VS2008,看你手头上用的哪个版本的编译器了。只编译win32dll.dsw工程就可以得到想要的东西了。
2、下载net-snmp的5.4版msi文件安装,默认路径C:\usr安装,安装过程中选择开发支持。没有找到5.7的,随便下个5.4的,安装上就行,但要主要选用默认的安装路径(更改安装路径,不知道可不可行,没有尝试),一路默认即可。安装的目的在于我们要配置后缀名为conf的文件,没有这个配置选项,将不能使用net-snmp的服务,我怀疑这个文件在源码中有个对应关系,不然怎么知道配置文件在路径C:\usr\etc\snmp下呢?如果只实现trap命令,这步可以省略。
3、建立C++的控制台工程,一个空的工程就可以。设置工程的包含头文件和lib文件。这里需要注意的一点是:include头文件,我把net-snmp5.7.1下的头文件都包含过来了,lib库文件,就是上面4个lib库,别忘了设置连接器:在项目属性的--连接器--输入--"附加依赖项":添加netsnmp.lib netsnmpagent.lib netsnmpmibs.lib netsnmptrapd.lib wsock32.lib; 如果环境变量没有起作用,netsnmp.dll与exe文件放在同一文件夹下就可以了。注意:wsock32.lib是其它lib库里用到的,缺少它会报错。
4、工程建立完了,配置好了,那就写代码吧!新建3个文件,一个入口函数文件,一个mib库的实现文件,一个头文件。我们先拿网上的例子试一下,参照这篇文章;http://hi.baidu.com/haven2002/item/d2d0bac02eed32bd0d0a7b9d
example-demon.c
- #include <net-snmp/net-snmp-config.h>
- #include <net-snmp/net-snmp-includes.h>
- #include <net-snmp/agent/net-snmp-agent-includes.h>
- #include <signal.h>
- #include "nstAgentSubagentObject.h"
- static int keep_running;
- RETSIGTYPE
- stop_server(int a) {
- keep_running = 0;
- }
- int main (int argc, char **argv) {
- //int agentx_subagent=1; /* change this if you want to be a SNMP master agent */
- int agentx_subagent=0;
- int background = 0; /* change this if you want to run in the background */
- int syslog = 0; /* change this if you want to use syslog */
- /* print log errors to syslog or stderr */
- if (syslog)
- ;
- //snmp_enable_calllog();
- else
- snmp_enable_stderrlog();
- /* we're an agentx subagent? */
- if (agentx_subagent) {
- /* make us a agentx client. */
- netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE, 1);
- }
- /* run in background, if requested */
- if (background && netsnmp_daemonize(1, !syslog))
- exit(1);
- /* initialize tcpip, if necessary */
- SOCK_STARTUP;
- /* initialize the agent library */
- init_agent("example-demon");
- /* initialize mib code here */
- /* mib code: init_nstAgentSubagentObject from nstAgentSubagentObject.C */
- init_nstAgentSubagentObject();
- /* initialize vacm/usm access control */
- if (!agentx_subagent) {
- init_vacm_vars();
- init_usmUser();
- }
- /* example-demon will be used to read example-demon.conf files. */
- /*在这里读取一个example-demon.conf的配置文件,这是关键*/
- init_snmp("example-demon");
- /* If we're going to be a snmp master agent, initial the ports */
- if (!agentx_subagent)
- init_master_agent(); /* open the port to listen on (defaults to udp:161) */
- /* In case we recevie a request to stop (kill -TERM or kill -INT) */
- keep_running = 1;
- signal(SIGTERM, stop_server);
- signal(SIGINT, stop_server);
- snmp_log(LOG_INFO,"example-demon is up and running.\n");
- /* your main loop here... */
- while(keep_running) {
- /* if you use select(), see snmp_select_info() in snmp_api(3) */
- /* --- OR --- */
- agent_check_and_process(1); /* 0 == don't block */
- }
- /* at shutdown time */
- snmp_shutdown("example-demon");
- SOCK_CLEANUP;
- return 0;
- }
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include <signal.h>
#include "nstAgentSubagentObject.h"
static int keep_running;
RETSIGTYPE
stop_server(int a) { keep_running = 0;
}
int main (int argc, char **argv) { //int agentx_subagent=1; /* change this if you want to be a SNMP master agent */ int agentx_subagent=0; int background = 0; /* change this if you want to run in the background */ int syslog = 0; /* change this if you want to use syslog *//* print log errors to syslog or stderr */ if (syslog) ; //snmp_enable_calllog(); else snmp_enable_stderrlog();/* we're an agentx subagent? */ if (agentx_subagent) { /* make us a agentx client. */ netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE, 1); }/* run in background, if requested */ if (background && netsnmp_daemonize(1, !syslog)) exit(1);/* initialize tcpip, if necessary */ SOCK_STARTUP;/* initialize the agent library */ init_agent("example-demon");/* initialize mib code here *//* mib code: init_nstAgentSubagentObject from nstAgentSubagentObject.C */ init_nstAgentSubagentObject(); /* initialize vacm/usm access control */ if (!agentx_subagent) { init_vacm_vars(); init_usmUser(); }/* example-demon will be used to read example-demon.conf files. */ /*在这里读取一个example-demon.conf的配置文件,这是关键*/ init_snmp("example-demon");/* If we're going to be a snmp master agent, initial the ports */ if (!agentx_subagent) init_master_agent(); /* open the port to listen on (defaults to udp:161) *//* In case we recevie a request to stop (kill -TERM or kill -INT) */ keep_running = 1; signal(SIGTERM, stop_server); signal(SIGINT, stop_server);snmp_log(LOG_INFO,"example-demon is up and running.\n");/* your main loop here... */ while(keep_running) { /* if you use select(), see snmp_select_info() in snmp_api(3) */ /* --- OR --- */ agent_check_and_process(1); /* 0 == don't block */ }/* at shutdown time */ snmp_shutdown("example-demon"); SOCK_CLEANUP;return 0;
}
nstAgentSubagentObject.h
- /*
- * Note: this file originally auto-generated by mib2c using
- * : mib2c.int_watch.conf,v 5.0 2002/04/20 07:30:13 hardaker Exp $
- */
- #ifndef NSTAGENTSUBAGENTOBJECT_H
- #define NSTAGENTSUBAGENTOBJECT_H
- /*
- * function declarations
- */
- void init_nstAgentSubagentObject(void);
- #endif /* NSTAGENTSUBAGENTOBJECT_H */
/*
* Note: this file originally auto-generated by mib2c using
* : mib2c.int_watch.conf,v 5.0 2002/04/20 07:30:13 hardaker Exp $
*/
#ifndef NSTAGENTSUBAGENTOBJECT_H
#define NSTAGENTSUBAGENTOBJECT_H
/*
* function declarations
*/
void init_nstAgentSubagentObject(void);
#endif /* NSTAGENTSUBAGENTOBJECT_H */
nstAgentSubagentObject.c
- /*
- * Note: this file originally auto-generated by mib2c using
- * : mib2c.int_watch.conf,v 5.0 2002/04/20 07:30:13 hardaker Exp $
- */
- #include <net-snmp/net-snmp-config.h>
- #include <net-snmp/net-snmp-includes.h>
- #include <net-snmp/agent/net-snmp-agent-includes.h>
- #include "nstAgentSubagentObject.h"
- /*
- * the variable we want to tie an OID to. The agent will handle all
- * * GET and SET requests to this variable changing it's value as needed.
- */
- static int nstAgentSubagentObject = 6;
- /*
- * our initialization routine, automatically called by the agent
- * (to get called, the function name must match init_FILENAME())
- */
- void
- init_nstAgentSubagentObject(void)
- {
- static oid nstAgentSubagentObject_oid[] =
- { 1, 3, 6, 1, 4, 1, 8072, 2, 4, 1, 1, 2, 0 };
- /*
- * a debugging statement. Run the agent with -DnstAgentSubagentObject to see
- * the output of this debugging statement.
- */
- DEBUGMSGTL(("nstAgentSubagentObject",
- "Initializing the nstAgentSubagentObject module\n"));
- /*
- * the line below registers our variables defined above as
- * accessible and makes it writable. A read only version of any
- * of these registration would merely call
- * register_read_only_int_instance() instead. The functions
- * called below should be consistent with your MIB, however.
- *
- * 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.
- */
- DEBUGMSGTL(("nstAgentSubagentObject",
- "Initalizing nstAgentSubagentObject scalar integer. Default value = %d\n",
- nstAgentSubagentObject));
- netsnmp_register_int_instance("nstAgentSubagentObject",
- nstAgentSubagentObject_oid,
- OID_LENGTH(nstAgentSubagentObject_oid),
- &nstAgentSubagentObject, NULL);
- DEBUGMSGTL(("nstAgentSubagentObject",
- "Done initalizing nstAgentSubagentObject module\n"));
- }
/*
* Note: this file originally auto-generated by mib2c using
* : mib2c.int_watch.conf,v 5.0 2002/04/20 07:30:13 hardaker Exp $
*/
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include "nstAgentSubagentObject.h"
/*
* the variable we want to tie an OID to. The agent will handle all
* * GET and SET requests to this variable changing it's value as needed.
*/
static int nstAgentSubagentObject = 6;
/*
* our initialization routine, automatically called by the agent
* (to get called, the function name must match init_FILENAME())
*/
void
init_nstAgentSubagentObject(void)
{ static oid nstAgentSubagentObject_oid[] = { 1, 3, 6, 1, 4, 1, 8072, 2, 4, 1, 1, 2, 0 };/* * a debugging statement. Run the agent with -DnstAgentSubagentObject to see * the output of this debugging statement. */ DEBUGMSGTL(("nstAgentSubagentObject", "Initializing the nstAgentSubagentObject module\n"));/* * the line below registers our variables defined above as * accessible and makes it writable. A read only version of any * of these registration would merely call * register_read_only_int_instance() instead. The functions * called below should be consistent with your MIB, however. * * 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. */ DEBUGMSGTL(("nstAgentSubagentObject", "Initalizing nstAgentSubagentObject scalar integer. Default value = %d\n", nstAgentSubagentObject));netsnmp_register_int_instance("nstAgentSubagentObject", nstAgentSubagentObject_oid, OID_LENGTH(nstAgentSubagentObject_oid), &nstAgentSubagentObject, NULL);DEBUGMSGTL(("nstAgentSubagentObject", "Done initalizing nstAgentSubagentObject module\n"));
}
编译链接通过!恭喜你!
5、 在C:\usr\etc'\snmp下建立example-demon.conf。内容如下:
#community 的读写根据需要设,
rwcommunity public
agentaddress 161
6、如果开启snmpd服务,先关闭。启动上面项目的可执行文件。
7、启动cmd,执行:
snmpget -v1 -c public localhost 1.3.6.1.4.1.8072.2.4.1.1.2.0
结果:NET-SNMP-MIB::netSnmp.2.4.1.1.2.0 = INTEGER: 6
snmpset -v1 -c public localhost 1.3.6.1.4.1.8072.2.4.1.1.2.0 i 5
结果:NET-SNMP-MIB::netSnmp.2.4.1.1.2.0 = INTEGER: 5
snmpget -v1 -c public localhost 1.3.6.1.4.1.8072.2.4.1.1.2.0
结果:NET-SNMP-MIB::netSnmp.2.4.1.1.2.0 = INTEGER: 5
OK!大功告成!
至此,havenzhao http://vcsky.net 我们实现了windows下利用net-snmp扩展。这只是一个最简单的例子,能够使用agent接收一个int变量的get/set命令。接下来,我们发现这只是万里长征的第一步……
windows下使用net-snmp实现agent扩展(一)相关推荐
- Windows下Memcache的安装及PHP扩展配置方法
这篇文章主要介绍了Windows下Memcache的安装及PHP扩展配置方法,需要的朋友可以参考下 一.下载 找到完整的memcache的Windows安装包,解压放在硬盘上,比如 F:\memcac ...
- windows下安装php5.5的redis扩展
windows下开发用的xampp集成的环境,想装个php-redis扩展,扩展的github地址: https://github.com/nicolasff/phpredis php_redis.d ...
- php编码和c语言,急求windows下用c语言开发PHP扩展时,在C语言里把字符串转成utf-8编码再打印的方法。...
因我是用windows下用c开发PHP扩展时, 想在C的函数内直接打印php_printf某字符串(其中含有汉字字符), 但在PHP调用这个函数时,看到的时汉字乱码.PHP页面用的是utf-8编码格式 ...
- 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 ...
- windows下php连接oracle安装oci8扩展报错(PHP Startup: Unable to load dynamic library ‘oci8_11g‘)
记录一下php7.29安装oci8的艰苦过程,简直就是唐僧西天取经历经九九八十一难. 使用的是phpstudy_pro安装的ph扩展wnmp环境下: 1 .安装oralce Instant Clien ...
- windows 下xampp集成环境安装mongodb扩展
http://www.thegeekstuff.com/2015/10/php-mongodb-for-xampp 见这文章,写的很详细. 1.打开PHPINFO界面查看PHP版本,编译器版本,还有架 ...
- Snmp在Windows下的实现----WinSNMP编程原理
在Windows 下实现SNMP协议的编程,可以采用Winsock接口,在161,162端口通过udp传送信息.在Windows 2000中,Microsoft已经封装了SNMP协议的实现,提供了一套 ...
- 在Windows下为PHP安装redis扩展
在网上找了许多教程, 在Windows下都无法安装 PHP redis 扩展,自己搞了两个小时才终于找到下载的地址,现归纳总结如下. 1.查看自己的PHP版本 echo phpinfo(); PHP ...
- Windows下PHP多线程扩展pthreads的安装
线程都需要开启线程安全 及(ZTS版本) 不是(NTS版本) phpinfo(); Thread Safety 要为 enabled pthreads扩展安装步骤 1.查看phpinfo() 获取PH ...
最新文章
- [转] Gradle: 此时不应有 Androidandroid-studiosdk oolslib\find_java.exe。解决方法
- 幸运 - 一种可以学习的简单技能
- 基于XMPP协议的aSmack源码分析
- Python之tkinter:动态演示调用python库的tkinter带你进入GUI世界(Menu/Menu的Command)
- 动态 SQL、EXECUTE IMMEDIATE、using、into、returning
- ubuntu服务器安装指南
- BurpSuite v2021.8.2安装使用
- C#使用SQLite数据库的代码示例
- 10 个非常有用的 SVG 动画的 JavaScript 库
- Kali在Vmware中通过Bridge联网
- 修改鼠标手形 闪烁 在填写文字内容后也一直在闪烁
- 计算机网络(北京理工大学出版社)课后习题答案
- 步进式解读Apache许可证
- dBm与功率(w)换算关系!
- 基于Scrapy的IP代理池搭建
- 地图,GPS位置地图坐标系:WGS-84(GPS)、GCJ-02(Google地图)、BD-09(百度地图),OpenGIS
- 四分位数的数学计算以及使用pandas计算
- 新手搭建自己的网站(1)
- 微信小程序测试点汇总
- pspice破解完显示 license not found 的解决方法
热门文章
- oracle宣传视频下载,1300首 Audiomachine 背景音乐电影宣传预告片配乐合辑(23集)...
- Spark Streaming 技术看点!
- 电脑怎么卸载软件干净_不要说你的电脑卸载很干净!分享两款卸载神器!
- 狗狗1岁相当于人类31岁!基于基因组甲基化图谱建立首个跨物种年龄转换公式...
- 英雄?好汉?可否有人站出来回应下?
- 风湿病年鉴 | scRNA-seq研究揭示骨关节炎患者的半月板退变新机制
- Mac用户装机必备——让 Mac 也能右键新建/剪切文件,多达 10 余项实用功能
- 微机原理和计算机组成原理一样吗_计算机组成原理:计算机的层次与编程语言...
- 简单英文题 25 Sequence Search(python)
- 第44课 角谷猜想 动动脑 第2题 阅读程序写结果