下面介紹壹下幾個例子程序,例子源代碼都可以通過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下的幾個實現方式。
總結
主要是環境設置,庫的編譯順序,配置文件設置。參考例子基本沒多大問題。