子代理扩搌

来源:互联网 发布:只有我知2在线看 编辑:程序博客网 时间:2024/05/06 14:57

Net-SNMP的第一个agent实现

经过查找多方资料,终于成功让Net-SNMP的第一个agent运行起来,但尚有写问题没有明白,欢迎讨论:

作者:阿飞

完成时间:2007-7-22

Email:

Web:www.dearstock.com

参考:http://www.net-snmp.org/wiki/index.php/Tutorials

          http://bbs.itgoal.com/viewthread.php?tid=2605

过程如下:

1, 下载net-snmp的5.4版msi文件安装,默认路径C:\usr安装,安装过程中选择开发支持。

2, 在VS2005上,新建C++ console工程SnmpServer,在工具->选项->VC++目录中,在“包含文件”,添加C:\usr\include ; 在“库文件”添加C:\usr\lib; 在项目属性的--连接器--输入--"附加依赖项":添加netsnmp.lib netsnmpagent.lib netsnmphelpers.lib netsnmpmibs.lib netsnmptrapd.lib wsock32.lib;在“忽略特定库”中添加:msvcrt.lib。

建立一下3个文件:

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;
}

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"));
}
编译链接通过。

3, 在C:\usr\etc'\snmp下建立example-demon.conf。内容如下:

#community 的读写根据需要设,
rwcommunity        public
agentaddress        161

4,如果开启snmpd,先关闭。启动上面项目的可执行文件。

5,启动cmd,执行:

snmpget -v1 -c public localhost 1.3.6.1.4.1.8072.2.4.1.1

结果: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

问题;

1, SnmpServer.exe 怎么知道要去C:\usr\etc\snmp下早配置文件呢?

2, SnmpServer.exe 要用到C:\usr\bin\netsnmp.dll,这些都是怎么知道的呢?


http://hi.baidu.com/lieyu063/item/e00625c245ee2544a9ba94a0
0 0
原创粉丝点击