netsnmp编译
A. net-snmp是如何收集信息的
在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 <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=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下的几个实现方式。
总结
主要是环境设置,库的编译顺序,配置文件设置。参考例子基本没多大问题。
B. linux下简单配置安装cactisnmp再次安装怎么解决
以前确认过机器上安装了snmp,但是这是redhat自带的,据说里面是没有snmpwalk,snmpget这些命令的,所以我这里要重新
编译安装NET-SNMP
官方网站
http://www.net-snmp.org/
下载文件
wget http://nchc.dl.sourceforge.net/project/net-snmp/net-snmp/5.5/net-snmp-5.5.tar.gz
解开压缩包
tar zxvf net-snmp-5.5.tar.gz
配置
./configure --prefix=/usr/local/net-snmp --enable-developer
中间有提示,一路回车下去
make
报错如下:
grep: /usr/lib/libbeecrypt.la: No such file or directory
/bin/sed: can't read /usr/lib/libbeecrypt.la: No such file or directory
libtool: link: `/usr/lib/libbeecrypt.la' is not a valid libtool archive
make[1]: *** [libnetsnmpmibs.la] 错误 1
make[1]: Leaving directory `/var/tmp/net-snmp-5.5/agent'
make: *** [subdirs] 错误 1
解决办法:
安装beecrypt
wget http://nchc.dl.sourceforge.net/sourceforge/beecrypt/beecrypt-4.1.2.tar.gz
tar zxvf beecrypt-4.1.2.tar.gz
./configure --prefix=/usr
make
make install
重新make,又报错了,报错如下:
/usr/bin/ld: cannot find -lelf
collect2: ld returned 1 exit status
make[1]: *** [libnetsnmpmibs.la] 错误 1
make[1]: Leaving directory `/var/tmp/net-snmp-5.5/agent'
make: *** [subdirs] 错误 1
解决办法:
ln -s libelf.so.1 /usr/lib/libelf.so
继续make,成功了:)
make install
安装成功。
链接过去
ln -s /usr/local/net-snmp/bin/* /usr/local/bin/
cp EXAMPLE.conf /usr/local/net-snmp/share/snmp/snmpd.conf
/usr/local/net-snmp/sbin/snmpd //启动SNMP服务
vi /etc/rc.d/rc.local
//在rc.local上加入一行/usr/local/net-snmp/sbin/snmpd,系统启动时启动SNMP服务
现在再回头看看cacti的错误
错误:/usr/local/bin/snmpwalk
错误:/usr/local/bin/snmpget
错误:/usr/local/bin/snmpbulkwalk
错误:/usr/local/bin/snmpgetnext
再次点开,这次就没有以上的错误啦。点Finish
输入用户名和密码admin/admin
登陆后点击graph图标,图没有出来。继续查找下原因,快要成功了:)
查看后台日志cacti里面的log/cacti.log,日志如下:
POLLER: Poller[0] Maximum runtime of 298 seconds exceeded. Exiting
点击控制面板console----->Data Sources
选择列表中的一个Localhost-Load Average
点击右上角的Turn Off Data Source Debug Mode
下面会出现对应的命令Data Source Debug
/usr/local/bin/rrdtool create \
/usr/local/apache2/htdocs/cacti/rra/localhost_load_1min_5.rrd \
--step 300 \
DS:load_1min:GAUGE:600:0:500 \
DS:load_5min:GAUGE:600:0:500 \
DS:load_15min:GAUGE:600:0:500 \
RRA:AVERAGE:0.5:1:600 \
RRA:AVERAGE:0.5:6:700 \
RRA:AVERAGE:0.5:24:775 \
RRA:AVERAGE:0.5:288:797 \
RRA:MAX:0.5:1:600 \
RRA:MAX:0.5:6:700 \
RRA:MAX:0.5:24:775 \
RRA:MAX:0.5:288:797 \
su cacti,执行该命令,报错如下:
ERROR: creating '/usr/local/apache2/htdocs/cacti/rra/localhost_load_1min_5.rrd': 权限不够
晕,前面不是赋予了权限了嘛,难道搞错了。再赋予一次。
chown -R cacti rra
chown -R cacti log
执行命令,OK了,检查一下目录rra,哈哈,已经有一个文件了。去看看图看。图有了,但是是否有正确的每5分钟采集一次
就要等等,等好几个5分钟再来看看了。等了一会儿,还是没有数据,唉,好事多磨啊,有图像了,又没有数据。。。。
检查snmp
snmpwalk -c public -v 2c 10.206.19.59 if 有很多信息返回
检查数据
rrdtool fetch localhost_mem_swap_4.rrd AVERAGE,返回的都是
1261452300: nan
1261452600: nan
文件更新信息
rrdtool lastupdate localhost_mem_swap_4.rrd --template,返回数据
users
1261535656: U
文件信息
rrdtool info localhost_users_6.rrd,返回信息:
filename = "localhost_users_6.rrd"
rrd_version = "0003"
step = 300
last_update = 1261535656
ds[users].type = "GAUGE"
ds[users].minimal_heartbeat = 600
ds[users].min = 0.0000000000e+00
ds[users].max = 5.0000000000e+02
ds[users].last_ds = "U"
ds[users].value = 0.0000000000e+00
网上搜索资料,可能是SNMP没有配置好
vi /usr/local/net-snmp/share/snmp/snmpd.conf
cp /etc/snmp/snmpd.conf /usr/local/net-snmp/share/snmp/snmpd.conf
重启snmp
/usr/local/net-snmp/sbin/snmpd
修改定时任务
[root@dev1 snmp]# crontab -e -u cacti
*/5 * * * * /usr/local/bin/php -q /usr/local/apache2/htdocs/cacti/poller.php > /var/log/poller.log 2>&1
检查定时任务是否启动了
/etc/init.d/crond status
一切正常
检查log/cacti.log日志
12/23/2009 01:14:16 PM - POLLER: Poller[0] WARNING: Cron is out of sync with the Poller Interval! The Poller Interval is '300' seconds, with a maximum of a '300' second Cron, but 4754 seconds have passed since the last poll!
12/23/2009 01:20:01 PM - POLLER: Poller[0] WARNING: Cron is out of sync with the Poller Interval! The Poller Interval is '300' seconds, with a maximum of a '300' second Cron, but 345 seconds have passed since the last poll!
12/23/2009 01:25:00 PM - POLLER: Poller[0] Maximum runtime of 298 seconds exceeded. Exiting.
12/23/2009 01:25:00 PM - SYSTEM STATS: Time:298.7072 Method:cmd.php Processes:1 Threads:N/A Hosts:2 HostsPerProcess:2 DataSources:5 RRDsProcessed:0
12/23/2009 01:30:00 PM - POLLER: Poller[0] Maximum runtime of 298 seconds exceeded. Exiting.
手工执行了几次
php cacti/cmd.php
php cacti/poller.php
PHP Deprecated错误的解决
vi /usr/local/lib/php.ini
加入:
php_flag allow_call_time_pass_reference On
php_value error_reporting "E_ALL & ~E_NOTICE & ~E_DEPRECATED"
终于有数据了,发现好像是执行了php cacti/cmd.php,但是最后还是有些问题
C. net-snmp代理(snmpd)扩展开发:
其实get和set对应的调用函数已经挂载好了,只需要修改对应的函数实现,就可以达到你需要的结果。不过需要在configure文件中添加--with-mib-moles="XXXX"这样的语句,并且重新configure一遍,才能将新添加的模块编译进去。你问的比较宽泛,我回答的可能也不是很清楚,可以追问。
D. 如何用net snmp开发snmp客户端
Linux下net-snmp的安装提供的net-snmp安装包中有以下目录运行install脚本执行安装,但不建立用户运行install_cm脚本执行安装并建立cm用户运行install_vg脚本执行安装并建立vg用户用户也可以用命令:net-snmp-config --create-snmpv3-user -a MD5 -A $snmp_auth -x DES -X $snmp_priv $snmp_usr$snmp_auth 是认证密码,密码必须大于8位$snmp_priv是加密密码,密码必须大于8位$snmp_usr是将要建立的用户名 安装完以后自动启动可以运行 tools/目录下的snmpwalk命令来检查是否安装成功./snmpwalk -v 3 -l authPriv -u cm -aMD5 -A password -xDES -X password 127.0.0.1 Windows下snmp协议的安装(windows2003,windows xp)控制面板-》添加删除程序-》添加组件-》管理和监视工具点详细信息,勾选“简单网络管理协议”安装过程中会提示插入系统盘安装完成后,选择管理工具-》服务在右边的列表中选中“SNMP service”, 右键-》属性里面有一个安全页面,可以自己添加一个团体,也可以用默认的“public”团体。下面选择“接受任意主机的snmp数据包”,或者选择“接受指定主机的snmp数据包”,填入主机的ip地址或主机名。
E. 新人求助windows下netsnmp怎么连接python
由于需要练手,准备将nagios的bash shell/perl监控脚本改用python写,而监控机上yum安装的net-snmp默认没有启用python模块,这里重新编译安装一个。
需要准备的安装包包括:
setuptools-0.6c11-py2.4.egg //提供easy_install指令用来直接在线安装python模块,类似于perl的 cpan install;
net-snmp-5.7.2.tar.gz //net-snmp的源码包。
先安装setuptools-0.6c11-py2.4.egg,否则net-snmp编译安装无法通过。
#chmod 755 setuptools-0.6c11-py2.4.egg
#./setuptools-0.6c11-py2.4.egg
接着,安装net-snmp
#tar -zxvf net-snmp-5.7.2.tar.gz
#cd net-snmp-5.7.2
#./configure --prefix=/usr/local/netsnmp --with-python-moles
#make
#make install
#cd python
#python setup.py build
#python setup.py test
出现以下错误:
ImportError: libnetsnmp.so.30: cannot open shared object file: No such file or directory
由于net-snmp是编译安装,且安装路径为/usr/local/netsnmp,以至于找不到相应模块,解决方法:
#echo "/usr/local/netsnmp/lib" >> /etc/ld.so.conf
#ldconfig
#python setup.py test//出现类似下图内容即正常:
#python setup.py install
测试net-snmp的python模块的导入:
#python
>>>import netsnmp //如果没有报错的话,则net-snmp的python模块启用成功。
F. linux 下的snmp++的安装
1.下载net-snmp -5.3.pre4.tar.gz包.
编译,安装
[[email protected] net-snmp -5.2.1]# ./configure –prefix=/usr/local/net-snmp -5.2.1
[[email protected] net-snmp -5.2.1]#make
[[email protected] net-snmp -5.2.1]#make install
2.创建放配置 文件的目录
[[email protected] net-snmp -5.2.1]# mkdir /etc/snmp
3.拷贝配置 文件
将解压出来的包中的EXAMPLE.conf拷贝到刚创建的目/etc/snmp 中,并命名为snmpd.conf
[[email protected] net-snmp -5.2.1]# cp -R EXAMPLE.conf /etc/snmp /snmpd.conf
4.修改配置 文件
[[email protected] net-snmp -5.2.1]# vi /etc/snmp /snmpd.conf
找到如下行,将其改为你想要的字符串,和网络
# sec.name source community
com2sec local localhost COMMUNITY
com2sec mynetwork NETWORK/24 COMMUNITY
例如
com2sec local localhost COMMUNITY
com2sec mynetwork 192.168.1.0/24 abcpublic
注意要用192.168.1.0/24 MS不能用192.168.1.1/24.另外改后要重启snmp 才能生效.
5.开启snmp
[[email protected] net-snmp -5.2.1]# /usr/local/net-snmp -5.2.1/sbin/snmpd -Lsd -Lf /dev/null -p /var/run/snmpd -a -c /etc/snmp /snmpd.conf
6.重启.
[[email protected] net-snmp -5.2.1]# killall -9 snmpd
[email protected] net-snmp -5.2.1]# /usr/local/net-snmp -5.2.1/sbin/snmpd -Lsd -Lf /dev/null -p /var/run/snmpd -a -c /etc/snmp /snmpd.conf
这样下来应该行了,
大概的思路吧
G. uclinux下net-snmp编译完成后程序在目标板(uclinux+arm)上运行时提示Cannot find mole (SNMPv2-MIB):
你的库文件或模块代码有缺失。重新下载个snmpagent开发包吧。