NETSNMP使用简述

NET-SNMP使用简述

1. 前言

NETSNMP是一套优秀的开源snmp工具包,使用它可以开发snmp代理程序,也可以开发snmp管理程序,目前最新的版本已经支持snmp v3

 

2. 使用net-snmp开发管理程序简述

使用netsnmp开发管理程序,首先要注意的内容是mib文件的问题,一般情况下,netsnmp通过环境变量或注册表指定mib文件目录位置,但是在实际编程过程中可以通过api接口直接设定mib文件路径。比如:

     netsnmp_set_mib_directory("C:\\EasyManager\\mibs");

 

在具体的程序中,使用netsnmpsnmp api接口比较关键的有3个步骤,首先要定义的个snmpsession,然后创建snmp pdu数据包,最后通过snmp send方法发送snmp数据包。具体发送数据包的过程分为同步发送和异步发送。

具体的同步和异步发送过程参考snmptest程序

 

3. Net-snmp v3使用

Netsnmp已经支持snmp v3开发,要使用snmp v3功能,首先要安装openssl开发包。

然后编辑win32"net-snmp"net-snmp-config.h 文件添加如下行:

#define USE_OPENSSL 1

link选项中添加libeay32.lib

做了以上设定以后,系统就可以支持snmpv3的相关功能了。

具体snmp v3的开发比较简单,只要创建snmp session时把相关参数设定好即可,其他相关开发和snmp v1一样。

我们可以看一下session的定义就明白了

 

struct snmp_session {

    /*

     * Protocol-version independent fields

     */

    /** snmp version */

    long            version;

    /** Number of retries before timeout. */

    int             retries;

    /** Number of uS until first timeout, then exponential backoff */

    long            timeout;       

    u_long          flags;

    struct snmp_session *subsession;

    struct snmp_session *next;

 

    /** Domain name or dotted IP address of default peer */

    char           *peername;

    /** UDP port number of peer. */

    u_short         remote_port;

    /** My Domain name or dotted IP address, 0 for default */

    char           *localname;

    /** My UDP port number, 0 for default, picked randomly */

    u_short         local_port;    

    /**

     * Authentication function or NULL if null authentication is used

     */

    u_char         *(*authenticator) (u_char *, size_t *, u_char *, size_t);

    /** Function to interpret incoming data */

    netsnmp_callback callback;     

    /**

     * Pointer to data that the callback function may consider important

     */

    void           *callback_magic;

    /** copy of system errno */

    int             s_errno;

    /** copy of library errno */

    int             s_snmp_errno;  

    /** Session id - AgentX only */

    long            sessid;

 

    /*

    * SNMPv1 & SNMPv2c fields

     */

    /** community for outgoing requests. */

    u_char         *community;

    /** Length of community name. */

    size_t          community_len; 

    /** Largest message to try to receive. */

    size_t          rcvMsgMaxSize;

    /** Largest message to try to send. */

    size_t          sndMsgMaxSize; 

 

    /*

     * SNMPv3 fields

     */

    /** are we the authoritative engine? */

    u_char          isAuthoritative;

    /** authoritative snmpEngineID */

    u_char         *contextEngineID;

    /** Length of contextEngineID */

    size_t          contextEngineIDLen;    

    /** initial engineBoots for remote engine */

    u_int           engineBoots;

    /** initial engineTime for remote engine */

    u_int           engineTime;

    /** authoritative contextName */

    char           *contextName;

    /** Length of contextName */

    size_t          contextNameLen;

    /** authoritative snmpEngineID */

    u_char         *securityEngineID;

    /** Length of contextEngineID */

    size_t          securityEngineIDLen;   

    /** on behalf of this principal */

    char           *securityName;

    /** Length of securityName. */

    size_t          securityNameLen;

 

    /** auth protocol oid */ 

    oid            *securityAuthProto;

    /** Length of auth protocol oid */

    size_t          securityAuthProtoLen;

    /** Ku for auth protocol XXX */

    u_char          securityAuthKey[USM_AUTH_KU_LEN];      

    /** Length of Ku for auth protocol */

    size_t          securityAuthKeyLen;

    /** Kul for auth protocol */

    u_char          *securityAuthLocalKey;      

    /** Length of Kul for auth protocol XXX */

    size_t          securityAuthLocalKeyLen;      

 

    /** priv protocol oid */

    oid            *securityPrivProto;

    /** Length of priv protocol oid */

    size_t          securityPrivProtoLen;

    /** Ku for privacy protocol XXX */

    u_char          securityPrivKey[USM_PRIV_KU_LEN];      

    /** Length of Ku for priv protocol */

    size_t          securityPrivKeyLen;

    /** Kul for priv protocol */

    u_char          *securityPrivLocalKey;      

    /** Length of Kul for priv protocol XXX */

    size_t          securityPrivLocalKeyLen;      

 

    /** snmp security model, v1, v2c, usm */

    int             securityModel;

    /** noAuthNoPriv, authNoPriv, authPriv */

    int             securityLevel; 

   

    /**

     * security module specific

     */

    void           *securityInfo;

 

    /**

     * use as you want data

     */

    void           *myvoid;

};

 

 

4. 关于添加支持snmp v3支持的一点设想。

由于snmpv3的支持比较复杂,在目前的系统上添加这些功能,相对比较复杂(主要时snmp v3相关参数比较多)。因此可以考虑,做一个通用的snmp v3读取引擎,把有关snmpv3读取的配置信息放到一个单独的配置文件中或数据库,需要读取snmp v3信息时,只要调用snmp v3读取引擎,就可以直接得到相关数据,相关snmp v3的配置参数,由该引擎直接从相关配置中去获取。

 

原文地址:https://www.cnblogs.com/wishma/p/1251355.html