net-snmp添加自定義MIB

来源:http://www.cnblogs.com/shenlinken/archive/2016/03/24/5316021.html
-Advertisement-
Play Games

我所知道的添加自定義MIB的方法有三種 1.靜態載入,將生成的.c和.h文件加入到相應的位置,重新編譯snmp庫,優點是不需要修改配置文件,缺點是每次添加都得重新編譯; 2.動態載入,將生成的.c和.h文件再編譯成.so庫,修改snmpd.conf配置文件。優點是每次添加不需要重新編譯,缺點是必須支 ...


我所知道的添加自定義MIB的方法有三種   1.靜態載入,將生成的.c和.h文件加入到相應的位置,重新編譯snmp庫,優點是不需要修改配置文件,缺點是每次添加都得重新編譯; 2.動態載入,將生成的.c和.h文件再編譯成.so庫,修改snmpd.conf配置文件。優點是每次添加不需要重新編譯,缺點是必須支持dlmod命令; 3.子代理擴展,將生成的.c和.h文件編譯成可執行程式,運行該程式和snmpd即可,優點是操作簡單,缺點是需要運行兩個程式才行。   三種方法的前幾步是一樣的,都是編寫MIB,生成.c和.h文件,補全.c文件。   1.編寫MIB       MIB的語法見http://blog.csdn.net/shanzhizi/article/details/15340305,寫得很清楚,很詳細。 下麵給出我自己的MIB文件。 -- Test-SLK-MIB.txt      Test-SLK-MIB DEFINITIONS ::= BEGIN            IMPORTS              OBJECT-GROUP, MODULE-COMPLIANCE, NOTIFICATION-GROUP                  FROM SNMPv2-CONF              enterprises, Integer32, Unsigned32, OBJECT-TYPE, MODULE-IDENTITY,              NOTIFICATION-TYPE                  FROM SNMPv2-SMI              DisplayString                  FROM SNMPv2-TC;            Test MODULE-IDENTITY              LAST-UPDATED "201601221450Z"       --必須以Z結尾              ORGANIZATION                  ""              CONTACT-INFO                  ""              DESCRIPTION                  "Video's Server MIB."              ::= { enterprises 745352 }            Time OBJECT IDENTIFIER ::= { Test 1 }              GetTime OBJECT-TYPE              SYNTAX DisplayString               MAX-ACCESS read-only              STATUS current              DESCRIPTION                  "Example : 2016/1/22"             ::= { Time 1 }     END -- Test-SLK-MIB.txt   這個MIB文件很簡單,只有一個OID 1.3.6.1.4.1.745352.1.1,把這個MIB放入MIBS文件夾,我的位於/usr/local/snmp/share/snmp/mibs。   2.生成.c和.h文件       運行命令mib2c Test-SLK-MIB::Test 出現的選項依次選2和1.   [root@localhost mibs]# env MIBS="+/etc/snmp/mibs/Test-MIB.my" mib2c Test 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:                              Test   numeric translation:              .1.3.6.1.4.1.16535   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 Test")     2) If you want to magically "tie" integer variables to integer      scalars      (by hand use: "mib2c -c mib2c.int_watch.conf Test")     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 Test.h writing to Test.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 Test.h running indent on Test.c     生成的Test.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 "Test.h"   /** Initializes the Test module */ void init_Test(void) {     const oid       GetTime_oid[] = { 1, 3, 6, 1, 4, 1, 745352, 1, 1 };       DEBUGMSGTL(("Test", "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; }   3.補全Test.c 在代碼中XXX處添加相應的值,代碼中都有說明,XXX: a pointer to the scalar's data,這個要我們填一個指針,XXX: the length of the data in bytes 這個要我們填數據的大小,當然要先定義,然後去獲取啊。   補全後 /*  * 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 "Test.h" #include <time.h>   /** Initializes the Test module */ void init_Test(void) {     const oid       GetTime_oid[] = { 1, 3, 6, 1, 4, 1, 745352, 1, 1 };       DEBUGMSGTL(("Test", "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; }   接下來根據方法不同,步驟也不同。 一、靜態鏈接     把Test.c和Test.h複製到/net-snmp-5.7.3/agent/mibgroups,這裡是說net-snmp源碼里。     編譯./configure --prefix=/usr/local/snmp --with-mib-modules=Test,make && make install。     靜態載入成功   二、動態載入     編寫makefile文件   CC=gcc FLAGS=-I. `net-snmp-config --cflags` -g DLFLAGS=-shared -fPIC -g   Test.so: Test.c     $(CC) $(CFLAGS) $(DLFLAGS) -c -o Test.o Test.c     $(CC) $(CFLAGS) $(DLFLAGS) -o Test.so Test.o   .PHONY : clean clean :     rm -f *.so *.o   編譯生成.so庫。 修改snmpd.conf配置文件,在文件末尾加入dlmod Test ${Test.so所在絕對路徑}/Test.so 啟動snmpd,      /usr/local/snmpd -f -L -DTest,dlmod -c /usr/local/snmp/etc/snmpd.conf 動態載入完成   三、子代理擴展 生成Test程式:  net-snmp-config --compile-subagent Test Test.c 啟動snmpd,Test /usr/local/snmpd -c /usr/local/snmp/etc/snmpd.conf ./Test 完成
您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 一、shell 電腦硬體的直接控制者是操作系統的內核(kernel),因為內核的重要性,所以作為用戶的我們是無法直接操作內核的,所以我們需要shell調用應用程式或者雙擊打開安裝的應用軟體與內核之間進行通信。 shell就相當於是一個介面,連接程式與內核;或者可以說shell是一個辦公桌,給工作的 ...
  • 我的內核版本是: 所以接下來就是先安裝內核源碼: 執行後,/usr/src / 目錄就多了兩個文件夾: 這樣源碼就下載下來了,然後將源碼解壓: 解壓之後 /usr/src/linux-3.13.0/文件夾裡面的就是內核源碼了. 然後再對源碼進行編譯. 先進入/usr/src/linux-3.13.0 ...
  • 最近因為一個監控相關的項目,深入研究了一下 windows 的 遠程桌面的相關知識。 1. 如何讓關閉了遠程桌面連接的用戶,對應的 session 立即退出 windows server。 大家使用 mstsc.exe 遠程桌面登錄windows server時,退出時,99.99%的人會直接關閉 ...
  • gdb
    原文鏈接:http://www.orlion.ga/762/ 一、單步執行和跟蹤函數調用 對於以下程式 最後列印出的結果是55和5105,而第二個結果應該是5050。現在用gdb調試下。 在編譯時要加上-g選項生成的目標文件才能用gdb進行調試,-g作用是在目標文件中加入源代碼的信息,比如目標文件第 ...
  • 原文鏈接:http://www.orlion.ga/698/ ab是個什麼就不說了搞lamp的都會知道。主要看一下結果都是什麼意義。 ab 的用法是:ab [options] [http://]hostname[:port]/path 例如:ab -n 5000 -c 200 http://loca ...
  • 頭記:vim作為被大多數程式員所推崇的編輯器,是源於它的自由靈活以及令人舒服的輸入模式,但對於新手來說無疑是個噩夢(需要記太多的命令), 而作為使用了vim有一段時間的我來說,總結下常用的命令,以備新手快速進入vim,感受它的強大以及令人著迷的處理方式。 以下只介紹常用的使用方式,對於繁重的概念以及 ...
  • 如何恢復以前的ie版本-控制面板,程式和功能-查看已安裝的更新-搜索Internet explorer,然後卸載更新就Ok. ...
  • http://www.cnblogs.com/kissdodog/category/580666.html ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...