1. 程式人生 > >Net-snmp總結(三)-net-snmp的MIBs擴充套件_新增get

Net-snmp總結(三)-net-snmp的MIBs擴充套件_新增get

一、編寫MIB檔案

MIB檔案是用 ASN.1 語法來描述的,所以為了精確定義MIB中各管理物件,使用者不得不參考一些ASN.1語法的有關文件如RFC1155、RFC1212等等來定義裝置自己的MIB。ASN.1是抽象句法表示法一 (Abstract SyntaxNotation One) 的簡稱,對於每個管理物件它都用文字來描述,一般檔案的字尾名都用“.mib”,在net-snmp中字尾名是“.mib.txt”。

關於MIB檔案示例,可以見編譯安裝後的net-snmp目錄,一般是 /usr/local/net-snmp/share/snmp/mibs/。  

ls/usr/local/net-snmp/share/snmp/mibs/

AGENTX-MIB.txt                       IPV6-TCP-MIB.txt                     SNMP-NOTIFICATION-MIB.txt

...

IPV6-MIB.txt                         SNMP-MPD-MIB.txt                     UDP-MIB.txt

這裡我們建立一個mib檔案,命名為TEST-GET-MIB.txt,放在/usr/local/net-snmp/share/snmp/mibs/目錄下因為這個目錄是snmpd的預設目錄,只要把MIB庫放入該目錄就可以自動載入MIB庫,否則需要修改snmpd.conf檔案,自定義的MIB檔案如下:

--開始
TEST-GET-MIB DEFINITIONS ::= BEGIN

--引入部分
IMPORTS
    enterprises
        FROM RFC1155-SMI            
    Integer32,OBJECT-TYPE
        FROM SNMPv2-SMI            
    DisplayString
        FROM SNMPv2-TC
    TEXTUAL-CONVENTION
        FROM SNMPv2-TC; --引用結束,用分號


--定義節點
--enterprises的OID是1.3.6.1.4
testGet    OBJECT IDENTIFIER ::= { enterprises 77695 }

GetTime     OBJECT IDENTIFIER ::= { testGet   1}

GetTime OBJECT-TYPE       --物件名稱
SYNTAX DisplayString      --型別
MAX-ACCESS read-only      --訪問方式
STATUS current            --狀態
DESCRIPTION               --描述
"get current time"   
::= { testGet  1 }       --父節點

--結束定義
END 

寫完後我們測一個MIB庫有沒有問題,在linux機器上用snmptranslate-Tp -IR TEST-GET-MIB::testGet顯示結果如下:(這個測試不需要啟動snmpd程序

[[email protected]]# ./snmptranslate -Tp -IR TEST-GET-MIB::testGet

+--testGet(77695)
   |
   +-- -R-- String    GetTime(1)
            Textual Convention: DisplayString
            Size: 0..255

OK,snmp自動發現了這個MIB庫, 有了自定義的OID,接下來開始新增處理程式。

二、生成原始碼

我們可以先來獲取一下前面定義的 testGet 節點的值試試。 因為 enterprises 的OID是 1.3.6.1.4 ,而 testGet是 enterprises 的葉子(77695),而 GetTime 又是 testGet 的葉子節點(1)。所以其OID為 1.3.6.1.4.77695.1 。 
下面使用snmpget來測試一下(測試之前要先啟動snmpd程序

[[email protected]]# ./snmpget -c public -v 2c localhost 1.3.6.1.4.1.77695.1.0

SNMPv2-SMI::enterprises.77695.1= No Such Object available on this agent at this OID  

結果是No Such Object available on this agent at this OID,我們需要用mib2c程式生成所需要的.c和.h檔案。

執行envMIBS="+/usr/local/net-snmp/share/snmp/mibs/TEST-GET-MIB.txt" ./mib2ctestGet,會引導你逐漸生成testGet.h和testGet.c,先選2再選1,過程如下:

[[email protected] bin]# env MIBS="+/usr/local/net-snmp/share/snmp/mibs/TEST-GET-MIB.txt" ./mib2c  testGet
writing to -
mib2c has multiple configuration files depending on the type of
code you need to write.  You must pick one depending on your need.

You requested mib2c to be run on the following part of the MIB tree:
  OID:                              testGet
  numeric translation:              .1.3.6.1.4.1.77695
  number of scalars within:         1
  number of tables within:          0
  number of notifications within:   0

First, do you want to generate code that is compatible with the
ucd-snmp 4.X line of code, or code for the newer Net-SNMP 5.X code
base (which provides a much greater choice of APIs to pick from):

  1) ucd-snmp style code
  2) Net-SNMP style code

Select your choice : 2

**********************************************************************
                 GENERATING CODE FOR SCALAR OBJECTS:
**********************************************************************

  It looks like you have some scalars in the mib you requested, so I
  will now generate code for them if you wish.  You have two choices
  for scalar API styles currently.  Pick between them, or choose not
  to generate any code for the scalars:

  1) If you're writing code for some generic scalars
     (by hand use: "mib2c -c mib2c.scalar.conf testGet")

  2) If you want to magically "tie" integer variables to integer
     scalars
     (by hand use: "mib2c -c mib2c.int_watch.conf testGet")

  3) Don't generate any code for the scalars

Select your choice: 1
    using the mib2c.scalar.conf configuration file to generate your code.
writing to testGet.h
writing to testGet.c



**********************************************************************
* NOTE WELL: The code generated by mib2c is only a template.  *YOU*  *
* must fill in the code before it'll work most of the time.  In many *
* cases, spots that MUST be edited within the files are marked with  *
* /* XXX */ or /* TODO */ comments.                                  *
**********************************************************************
running indent on testGet.c
running indent on testGet.h

mib2c已經統計出我們的mib庫包含1個scalar變數,0個table變數,0個通知變數,Scalar就是包含我們常用的整型,字串,時間等等資料型別。table就是scalar的一種集合,有一個和多個列組成,類似於資料庫中的表。它必須具有索引項,用來按一定順序檢索表項,當然我們只寫了一個標量的OID,不是表結構也不是通知結構

生成的testGet.h如下

/*
 * Note: this file originally auto-generated by mib2c using
 *        $
 */
#ifndef TESTGET_H
#define TESTGET_H

/*
 * function declarations
 */
void            init_testGet(void);
Netsnmp_Node_Handler handle_GetTime;

#endif                          /* TESTGET_H */

生成的testGet.c檔案如下:

/*
 * Note: this file originally auto-generated by mib2c using
 *        $
 */

#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include "testGet.h"

/** Initializes the testGet module */
void
init_testGet(void)
{
/*
 * Note: this file originally auto-generated by mib2c using
 *        $
 */

#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include "testGet.h"

/** Initializes the testGet module */
void
init_testGet(void)
{
    const oid       GetTime_oid[] = { 1, 3, 6, 1, 4, 1, 77695, 1 };

    DEBUGMSGTL(("testGet", "Initializing\n"));

    netsnmp_register_scalar(netsnmp_create_handler_registration
                            ("GetTime", handle_GetTime, GetTime_oid,
                             OID_LENGTH(GetTime_oid), HANDLER_CAN_RONLY));
}

int
handle_GetTime(netsnmp_mib_handler *handler,
               netsnmp_handler_registration *reginfo,
               netsnmp_agent_request_info *reqinfo,
               netsnmp_request_info *requests)
{
    /*
     * We are never called for a GETNEXT if it's registered as a
     * "instance", as it's "magically" handled for us.
     */

    /*
     * a instance handler also only hands us one request at a time, so
     * we don't need to loop over a list of requests; we'll only get one.
     */

    switch (reqinfo->mode) {

    case MODE_GET:
        snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
                                 /*
                                  * XXX: a pointer to the scalar's data
                                  */ ,
                                 /*
                                  * XXX: the length of the data in bytes
                                  */ );
        break;


    default:
        /*
         * we should never get here, so this is a really bad error
         */
        snmp_log(LOG_ERR, "unknown mode (%d) in handle_GetTime\n",
                 reqinfo->mode);
        return SNMP_ERR_GENERR;
    }

    return SNMP_ERR_NOERROR;
}
     

以上的程式碼都是自動生成的,我們沒有寫一行程式碼,到了這一步,我們需要把testGet.c裡面的XXX改成自己的值,也就兩行,修改後testGet.c檔案程式碼如下:

/*
 * Note: this file originally auto-generated by mib2c using
 *        $
 */

#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include "testGet.h"

/** Initializes the testGet module */
void
init_testGet(void)
{
    const oid       GetTime_oid[] = { 1, 3, 6, 1, 4, 1, 77695, 1 };

    DEBUGMSGTL(("testGet", "Initializing\n"));

    netsnmp_register_scalar(netsnmp_create_handler_registration
                            ("GetTime", handle_GetTime, GetTime_oid,
                             OID_LENGTH(GetTime_oid), HANDLER_CAN_RONLY));
}

int
handle_GetTime(netsnmp_mib_handler *handler,
               netsnmp_handler_registration *reginfo,
               netsnmp_agent_request_info *reqinfo,
               netsnmp_request_info *requests)
{
    /*
     * We are never called for a GETNEXT if it's registered as a
     * "instance", as it's "magically" handled for us.
     */

    /*
     * a instance handler also only hands us one request at a time, so
     * we don't need to loop over a list of requests; we'll only get one.
     */
         time_t t;
    switch (reqinfo->mode) {

    case MODE_GET:
                time(&t);
            char szTime[100];
        snprintf(szTime,100,"%s",ctime(&t));
                snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
                                         /*
                                          * XXX: a pointer to the scalar's data
                                          */ szTime,
                                         /*
                                          * XXX: the length of the data in bytes
                                          */ strlen(szTime));

        break;


    default:
        /*
         * we should never get here, so this is a really bad error
         */
        snmp_log(LOG_ERR, "unknown mode (%d) in handle_GetTime\n",
                 reqinfo->mode);
        return SNMP_ERR_GENERR;
    }

    return SNMP_ERR_NOERROR;
}

三、配置編譯

把testGet.c和testGet.h複製到/net-snmp-5.7.3/agent/mibgroups/路徑下

設定編譯引數(紅色部分即為加上我們自己的mib模組)

[[email protected]]# ./configure  --prefix=/usr/local/net-snmp   --with-mib-modules="testGet"

檢視檔案net-snmp-5.7.3/agent/mibgroup/mib_module_inits.h,發現已經新增到初始化中。

/*This file is automatically generated by configure.  Do not modify by hand. */

  if (should_init("testGet"))init_testGet();

編譯並安裝

[[email protected]]# make

[[email protected]]# make install

四、測試新加的MIB

啟動snmpd服務

[[email protected]]# ./snmpd -f -Le

Turning on AgentXmaster support.

NET-SNMP version5.7.3

No Shmem line in/proc/meminfo

我們再呼叫snmpget來測試結果:

[[email protected]]# ./snmpget  -v2c -c publiclocalhost  1.3.6.1.4.1.77695.1.0

SNMPv2-SMI::enterprises.77695.1.0= STRING: "Fri Nov  3 14:02:13 2017

"

顯示出了當前的時間,說明我們新增自定義MIB get操作成功!

相關推薦

Net-snmp總結()-net-snmp的MIBs擴充套件_新增get

一、編寫MIB檔案 MIB檔案是用 ASN.1 語法來描述的,所以為了精確定義MIB中各管理物件,使用者不得不參考一些ASN.1語法的有關文件如RFC1155、RFC1212等等來定義裝置自己的MIB。ASN.1是抽象句法表示法一 (Abstract SyntaxNotat

asp.net學習總結——ADO.net(對Sql Server進行操作的資料訪問類)

ADO.net物件     System.Data.SqlClient(對Sql Server進行操作的資料訪問類): 1)SqlConnection:資料庫聯結器2)SqlCommand:資料庫命名

windows下使用net-snmp實現agent擴充套件

時間隔得太長了,我都快忘了什麼是snmp了,知識啊知識,很容易在不用的時候忘卻,也可能是自己腦袋不好使了吧?翻了翻程式碼,趕緊總結下,不然真不會了…… 在上篇部落格中,實現了get/set一個字串型變數,現在來實現對多個字串變數的get/set。假設要實現獲取CPU利用率、

.Net MVC 導入導出Excel總結(種導出Excel方法,一種導入Excel方法) 通過MVC控制器導出導入Excel文件(可用於java SSH架構)

ets esp llb pat lencod cnblogs 創建 etime mmd public class ExcelController : Controller { // // GET: /Excel/ M

Asp.Net中的種分頁方式總結

rom chang clas 綁定 select proc dll xtend tinc 本人ASP.net初學,網上找了一些分頁的資料,看到這篇文章,沒看到作者在名字,我轉了你的文章,只為我可以用的時候方便查看,2010的文章了,不知道這技術是否過期。 以下才是正文

windows下使用net-snmp實現agent擴充套件(二)

剛剛實現了int型的get命令,可能更多的情況下,我們更需要的是字串型別的。在實現int型的時候,用到了netsnmp_register_int_instance這個函式,很自然想到如果是string型的,用類似的netsnmp_register_string_instanc

windows下使用net-snmp實現agent擴充套件(四)

在前三篇的內容裡,介紹了使用net-snmp實現agent的Get/Set命令,下面介紹一下發送trap訊息。傳送trap訊息時,系統預設的埠是162。使用下面的程式碼,可以實現trap訊息的傳送。 //該函式傳送實時報警資訊。與傳送一般資訊埠不同 void init_al

net-snmp移植專案總結

1、       專案移植總結 1.1  交叉編譯的configure引數的配置 交叉編譯引數的設定,這個部分是最基本的,如果引數設定的有問題,snmpd執行起來就會有問題。 在x86環境下進行測試的時候,選用一個簡單的編譯引數,可以工作: LDFLAGS="-L

net-snmp擴充套件trap型別的私有mib

注:本文介紹的是靜態編譯的方法擴充套件的私有mib,別的方法請看本人整理的《net-snmp agent開發(用net-snmp擴充套件MIB庫)》 1. 首先建立一個簡單的含有table變數的mib檔案取名test-trap.mib,字尾名也可以是.txt 實際操作

.Net MVC 匯入匯出Excel總結(種匯出Excel方法,一種匯入Excel方法)

public class ExcelController : Controller { // // GET: /Excel/ Models.zbwxglEntities myMdl = new Models.zbwxglEntities();

C#/.NET整數的種強制型別轉換(int)、Convert.ToInt32()、int.Parse()的區別總結

1.(int) 適合簡單資料型別之間的轉換,C#的預設整型是int32(不支援bool型)。 2. int.Parse(string sParameter) 是個建構函式,引數型別只支援string型別,Parse就是把String型別轉換成int,char,doubl

Net-snmp 使用c 擴充套件 sub-agent

摘要: netSNMP開發,使用c開發對SNMP進行擴充套件,應用程式作為agent執行。 版本:NET-SNMP version 5.7.2.1 系統:CentOS X64 軟體包:net-snmp-5.7.2.1.tar.gz 解壓安裝包: [[email

net-snmp交叉編譯時問題總結

遇到的問題: 一、配置出錯 錯誤如下:     ./configure --build=i686-linux --host=arm-linux  CC=arm-arago-linux-gnueabi-gcc --disable-ipv6 --with-endianness=

ASP.NET MVC5():表單和HTML輔助方法

http get 暴露 sta 選擇 .text 響應 pos 多行文本 二進制 表單的使用 Action和Method特性   Action特性用以告知瀏覽器信息發往何處,因此,Action特性後面需要包含一個Url地址。這裏的Url地址可以是相對的,也可以是絕對的。如

.NET MVC與層架構

增刪改查 ews 數據的操作 求反 註意 image http pla 業務 雖然接觸了兩者有一段時間了,但是有時還是會混淆概念,在此處不打算說明二者的區別,因為二者都是架構模式,並且也有一定的共存度,在實際開發中,嚴格區分意義不大。基於最近涉及到這部分知識就在復習下,編程

net-snmp-5.7.3配置編譯安裝》

設置 bus 重命名 tree control examples password 取消 mark 先看一下系統環境 [email protected]pc:~/work/_snmp/net-snmp-5.7.3$ uname -a Linux o-pc 3.16

net-snmp: introduction

walk eve index effective take which machine del ber SNMP is a protocol that enables server remote-info-exchange. Which according to wikip

.net 新手上路,已瘋->_<-

for math 面積 面積並 mat col spa 判斷 align 今天.net 上機實驗,已哭死在電腦前 把一些能簡單的全都簡單處理了。 1. 編寫一個控制臺應用程序,輸入三角形或者長方形邊長,計算其周長和面積並輸出。 代碼如下: using Syste

ASP.NET WebApi總結之自定義權限驗證

use bar cor 介紹 string roles 獲得 ext status 在.NET中有兩個AuthorizeAttribute類, 一個定義在System.Web.Http命名空間下 #region 程序集 System.Web.Http, Version=5.

搭建層 .Net Core Mvc + 層 過程中的不解,想要完善項目

過程 pan 重復 ise 疑惑 開發效率 不能 第一次 由於 在學習.Net Core Mvc 的基礎知識後想寫一個Demo,但是在這過程中遇到了很多的問題,我的記憶很差,聽取了意見,記錄下,針對問題來解決。 由於公司都是用的以前的技術,都沒有分層的這個說法,我才從