NET-SNMP使用簡述

來源:互聯網
上載者:User

NET-SNMP使用簡述

 

1. 前言

 

NET-SNMP是一套優秀的開源snmp工具包,使用它可以開發snmp代理程式,也可以開發snmp管理程式,目前最新的版本已經支援snmp v3

 

2. 使用net-snmp開發管理程式簡述

 

使用net-snmp開發管理程式,首先要注意的內容是mib檔案的問題,一般情況下,net-snmp通過環境變數或註冊表指定mib檔案目錄位置,但是在實際編程過程中可以通過api介面直接設定mib檔案路徑。比如:

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

 

在具體的程式中,使用net-snmp的snmp api介面比較關鍵的有3個步驟,首先要定義的個snmp的session,然後建立snmp pdu資料包,最後通過snmp send方法發送snmp資料包。具體發送資料包的過程分為同步發送和非同步發送。

具體的同步和非同步發送過程參考snmptest程式

 

3. Net-snmp v3使用

Net-snmp已經支援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的配置參數,由該引擎直接從相關配置中去擷取。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.