net-snmp是如何收集信息的?

net-snmp是如何收集信息的?
2025-04-28 04:43:50
推荐回答(1个)
回答1:

在windows下编译net-snmp其实很简单,我用的是net-snmp5.2.1.2的版本。只需把源码下的win32目录中的几个库的工程编译就行了。但记住,把netsnmp.lib放在最后编译,要不然编译结果不一样,切记,切记。然后再用编译出来的四个库文件: netsnmp.lib,netsnmpagent.lib,netsnmphelpers.lib,netsnmpmibs.lib,来编译netsnmp.dll。注意还要加上wsock32.lib。如果编译出错,提示跟VC默认的库有冲突,按提示在编译环境中用NODEFAULTLIB:XXX来去掉VC的缺省库文件。

下面介绍一下几个例子程序,例子源代码都可以通过net-snmp的帮助超链接连到其网站下载

1.编译例子example-demon;

#include
#include
#include
#include

#include "nstAgentSubagentObject.h"

static int keep_running;

RETSIGTYPE
stop_server(int a) {
keep_running = 0;
}

int
main(int argc, char *argv[])

{
int agentx_subagent=0; /* change this if you want to be a SNMP master agent */
/*为了编译成为主代理,这里设为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) {
void init_vacm_vars();/*---------------小修改一下---------------*/
void 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;
}

这里都有很详细的注释,就不说明了。
在vc的工程中,把所用到的例子mib库文件nstAgentSubagentObject.h,nstAgentSubagentObject.c添加进去。并设置好所需的四个库文件,如文开头所述,看是否需要加wsock32.lib,和NODEFAULTLIB。接着就应该可以编译通过了,无错误无警告。

2.关于运行
运行就必须设置配置文件的路径,最好下一个NET-SNMP的二进制包,按缺省路径安装C:\USR。安装时提示还需要下载一个PERL的安装包,用来运行mib2c工具,这个网上都有介绍。配置文件放在
c:\,c:\usr\etc\snmp或C:\usr\share\snmp都有效。但如果你是运行例子主代理程序,会在etc目录下找配置文件。例子的配置文件应和init_snmp("example-demon");中初始化的名字相同:example-demon.conf。配置文件只需设两个值就够了:

#community 的读写根据需要设,这里设的是readonly
rocommunity public
agentaddress 161

另外,如果是运行二进制包,snmp.conf文件不要手工修改,要通过snmpconf生成,手工修改的好像不起作用。例子程序的配置可以手工设置。
最后,把example-demon.conf放到c:\usr\etc\snmp下。关掉二进制包的snmpd.exe服务(如果在运行的话)。然后运行example-demon.exe。正常情况下,如果安装了二进制包,例子可以在编译目录下运行,不行就把netsnmp.dll拷进来,或直接把例子放到二进制包的bin目录下。有问题检查配置文件的路径和设置。最好下载一个叫getif的管理端软件,很方便小巧的查询工具,来侧试。

3.编译自己的mib库
第一种可以通过mib2c生成模版框架,在相关地方添上数据导入指针及其他。
第二种可以参考nstAgentSubagentObject.c。
第三种可以参考net-snmp-xxx\agent\mibgroup\examples下的几个实现方式。

总结
主要是环境设置,库的编译顺序,配置文件设置。参考例子基本没多大问题。