1. 程式人生 > >Net-snmp 使用c 擴充套件 sub-agent

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 protected] snmp]# pwd

/root/snmp

[[email protected] snmp]# ls

net-snmp-5.7.2.1.tar.gz

[[email protected] snmp]# tarxzvf net-snmp-5.7.2.1.tar.gz

[[email protected]

snmp]# ls

net-snmp-5.7.2.1  net-snmp-5.7.2.1.tar.gz

配置

[[email protected] snmp]# cdnet-snmp-5.7.2.1

[[email protected]]# pwd

/root/snmp/net-snmp-5.7.2.1

[[email protected]]# ./configure

配置完成後,會顯示如下configure摘要資訊

---------------------------------------------------------

            Net-SNMP configuration summary:

---------------------------------------------------------

  SNMP Versions Supported:    1 2c 3

  Building for:               linux

  Net-SNMP Version:           5.7.2.1

  Network transport support:  Callback Unix Alias TCP UDP IPv4BaseSocketBase TCPBase UDPIPv4Base UDPBase

  SNMPv3 Security Modules:     usm

  Agent MIB code:             default_modules =>  snmpv3mibs mibII ucd_snmp notificationnotification-log-mib target agent_mibs agentx disman/event disman/scheduleutilities host

  MYSQL Trap Logging:         unavailable

  Embedded Perl support:      enabled

  SNMP Perl modules:          building -- embeddable

  SNMP Python modules:        disabled

  Crypto support from:        crypto

  Authentication support:     MD5 SHA1

  Encryption support:         DES AES

  Local DNSSEC validation:    disabled

編譯、安裝

 make && make install

配置snmpd.conf

[[email protected] snmp]# pwd

/usr/local/share/snmp

[[email protected] snmp]#snmpconf

The following installedconfiguration files were found:

   1: ./snmpd.conf

   2: ./snmptrapd.conf

   3: /usr/local/share/snmp/snmpd.conf

   4: /usr/local/share/snmp/snmptrapd.conf

Would you like me to read themin?  Their content will be merged withthe

output files created by thissession.

Valid answer examples:"all", "none","3","1,2,5"

Read in which (default = all): none

I can create the followingtypes of configuration files for you.

Select the file type you wishto create:

(you can create more than oneas you run this program)

   1: snmpd.conf

   2: snmptrapd.conf

   3: snmp.conf

Other options: quit

Select File: 1

The configuration informationwhich can be put into snmpd.conf is divided

into sections.  Select a configuration section for snmpd.conf

that you wish to create:

   1: Access Control Setup

   2: Extending the Agent

   3: Trap Destinations

   4: Monitor Various Aspects of the Running Host

   5: Agent Operating Mode

   6: System Information Setup

Other options: finished

Select section: 5

Section: Agent Operating Mode

Description:

  This section defines how the agent willoperate when it

  is running.

Select from:

   1: Should the agent operate as a master agent or not.

   2: The system user that the agent runs as.

   3: The system group that the agent runs as.

   4: The IP address and port number that the agent will listen on.

Other options: finished, list

Select section: 1

Configuring: master

Description:

  Should the agent operate as a master agent ornot.

    Currently, the only supported master agenttype for this token

    is "agentx".

    arguments: (on|yes|agentx|all|off|no)

Should the agent run as a AgentX masteragent?: agentx

Finished Output: master  agentx

Section: Agent Operating Mode

Description:

  This section defines how the agent willoperate when it

  is running.

Select from:

   1: Should the agent operate as a master agent or not.

   2: The system user that the agent runs as.

   3: The system group that the agent runs as.

   4: The IP address and port number that the agent will listen on.

Other options: finished, list

Select section: finished

The configuration informationwhich can be put into snmpd.conf is divided

into sections.  Select a configuration section for snmpd.conf

that you wish to create:

   1: Access Control Setup

   2: Extending the Agent

   3: Trap Destinations

   4: Monitor Various Aspects of the Running Host

   5: Agent Operating Mode

   6: System Information Setup

Other options: finished

Select section: 1

Section: Access Control Setup

Description:

  This section defines who is allowed to talkto your running

  snmp agent.

Select from:

   1:  aSNMPv3 read-write user

   2:  aSNMPv3 read-only user

   3:  aSNMPv1/SNMPv2c read-only access community name

   4:  aSNMPv1/SNMPv2c read-write access community name

Other options: finished, list

Select section: 3

Configuring: rocommunity

Description:

  a SNMPv1/SNMPv2c read-only access communityname

    arguments: community [default|hostname|network/bits] [oid]

The community name to add read-only accessfor: public

The hostname or networkaddress to accept this community name from [RETURN for all]:

The OID that this communityshould be restricted to [RETURN for no-restriction]:

Finished Output:rocommunity  public 

Section: Access Control Setup

Description:

  This section defines who is allowed to talkto your running

  snmp agent.

Select from:

   1:  aSNMPv3 read-write user

   2:  aSNMPv3 read-only user

   3:  aSNMPv1/SNMPv2c read-only access community name

   4:  aSNMPv1/SNMPv2c read-write access community name

Other options: finished, list

Select section: 4

Configuring: rwcommunity

Description:

  a SNMPv1/SNMPv2c read-write access communityname

    arguments: community [default|hostname|network/bits] [oid]

Enter the community name to add read-writeaccess for: private

The hostname or networkaddress to accept this community name from [RETURN for all]:

The OID that this communityshould be restricted to [RETURN for no-restriction]:

Finished Output:rwcommunity  private 

Section: Access Control Setup

Description:

  This section defines who is allowed to talkto your running

  snmp agent.

Select from:

   1:  aSNMPv3 read-write user

   2:  aSNMPv3 read-only user

   3:  aSNMPv1/SNMPv2c read-only access community name

   4:  aSNMPv1/SNMPv2c read-write access community name

Other options: finished, list

Select section: finished

The configuration informationwhich can be put into snmpd.conf is divided

into sections.  Select a configuration section for snmpd.conf

that you wish to create:

   1: Access Control Setup

   2: Extending the Agent

   3: Trap Destinations

   4: Monitor Various Aspects of the Running Host

   5: Agent Operating Mode

   6: System Information Setup

Other options: finished

Selectsection: finished

I can create the followingtypes of configuration files for you.

Select the file type you wishto create:

(you can create more than oneas you run this program)

   1: snmpd.conf

   2: snmptrapd.conf

   3: snmp.conf

Other options: quit

Select File: quit

Error: An snmpd.conf filealready exists in this directory.

'overwrite', 'skip', 'rename' or 'append'?: overwrite

The following files werecreated:

  snmpd.conf 

These files should be moved to /usr/local/share/snmp if you

want them used by everyone onthe system.  In the future, if you add

the -i option to the commandline I'll copy them there automatically for you.

Or, if you want them for yourpersonal use only, copy them to

/root/.snmp .  In the future, if you add the -p option tothe

command line I'll copy themthere automatically for you.

snmpd.conf配置檔案

[[email protected] snmp]# pwd

/usr/local/share/snmp

[[email protected] snmp]# catsnmpd.conf

rocommunity  public 

rwcommunity  private 

master  agentx

[[email protected] snmp]#

編寫MIB檔案

[[email protected] snmp_c]# pwd

/root/snmp/test/snmp_c

[[email protected] snmp_c]# catInspur-MIB.my

-- Inspur-MIB.my

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

-- October 09, 2002 at 14:50GMT

        -- 1.3.6.1.4.1.37945

            LAST-UPDATED"200210091450Z"        -- October 09, 2002 at 14:50 GMT

            ORGANIZATION

                "" 

            CONTACT-INFO

                "" 

            DESCRIPTION

                "Video's Server MIB."

            ::= { enterprises 37945 }

--  Node definitions

-- This part will include alldetails about the Test.

        -- 1.3.6.1.4.1.37945.1

        Time OBJECT IDENTIFIER ::= { Test 1 }

        -- 1.3.6.1.4.1.37945.1.1

        GetTime OBJECT-TYPE

            SYNTAX DisplayString (SIZE (0..100))

            STATUS current

            DESCRIPTION

                "Example : 2013/4/11"

            ::= { Time 1 }

    END

-- Inspur-MIB.my

複製mib檔案到指定目錄

[[email protected] snmp_c]# cpInspur-MIB.my /usr/local/share/snmp/mibs/

測試mib檔案

+--Test(37945)

   |

   +--Time(1)

      |

      +-- -R-- String    GetTime(1)

               Textual Convention:DisplayString

               Size: 0..100

生成原始碼

[[email protected] snmp_c]# pwd

/root/snmp/test/snmp_c

[[email protected] snmp_c]# envMIBS="+/usr/local/share/snmp/mibs/Inspur-MIB.my" mib2c Test

writing to -

mib2c has multipleconfiguration files depending on the type of

code you need to write.  You must pick one depending on your need.

You requested mib2c to be runon the following part of the MIB tree:

  OID:                                   Test

  numeric translation:                 .1.3.6.1.4.1.37945

  number of scalars within:         1

  number of tables within:          0

  number of notifications within:   0

First, do you want to generatecode that is compatible with the

ucd-snmp 4.X line of code, orcode for the newer Net-SNMP 5.X code

base (which provides a muchgreater choice of APIs to pick from):

  1) ucd-snmp style code

  2) Net-SNMP style code

Select your choice : 2

**********************************************************************

                    GENERATINGCODE FOR SCALAR OBJECTS:

**********************************************************************

  It looks like you have some scalars in themib you requested, so I

  will now generate code for them if youwish.  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 genericscalars

     (by hand use: "mib2c -cmib2c.scalar.conf Test")

  2) If you want to magically "tie"integer variables to integer

     scalars

     (by hand use: "mib2c -cmib2c.int_watch.conf Test")

  3) Don't generate any code for the scalars

Select your choice: 1

    using the mib2c.scalar.conf configurationfile to generate your code.

writing to Test.h

writing to Test.c

**********************************************************************

* NOTE WELL: The code generatedby mib2c is only a template.  *YOU*  *

* must fill in the code beforeit'll work most of the time.  In many *

* cases, spots that MUST beedited within the files are marked with *

* /* XXX */ or /* TODO */comments.                                  *

**********************************************************************

running indent on Test.h

running indent on Test.c

[[email protected] snmp_c]# ls

Inspur-MIB.my  Test.c Test.h

修改測試程式Test.c

修改40-44行中的XXX:

         case MODE_GET:

            snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,

                      "hello inspur"/* XXX: a pointer to the scalar's data */,

                      strlen("helloinspur")/* XXX: the length of the data in bytes */);                                                                                         

             break;

編譯測試程式

[[email protected] snmp_c]# pwd

/root/snmp/test/snmp_c

[[email protected] snmp_c]#net-snmp-config --compile-subagent Test Test.c

generating the temporary codefile: netsnmptmp.1981.c

void init_Test(void);

checking for init_Test inTest.c

init_Test(void)

checking for shutdown_Test inTest.c

running: gcc  -fno-strict-aliasing -g -O2 -Ulinux-Dlinux=linux  -D_REENTRANT -D_GNU_SOURCE-fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include-D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/lib64/perl5/CORE   -I.-I/usr/local/include -o Test netsnmptmp.1981.c Test.c  -L/usr/local/lib-lnetsnmpmibs -lnetsnmpagent -lnetsnmp -lnetsnmpmibs -ldl  -lnetsnmpagent  -Wl,-E -Wl,-rpath,/usr/lib64/perl5/CORE-lnetsnmp -lrt  -lcrypto

removing the temporary codefile: netsnmptmp.1981.c

subagent program Test created

開啟另一個終端,啟動snmpd服務

顯示debug資訊

[[email protected] snmp]# snmpd-f -Le -d

Turning on AgentX mastersupport.

NET-SNMP version 5.7.2.1

回到原來終端,執行測試程式

[[email protected] snmp_c]#./Test

[[email protected] snmp_c]# ps-ef|grep Test

root      2039    1  0 14:35 ?        00:00:00 ./Test

root      2041 1915  0 14:35 pts/0    00:00:00 grep Test

傳送get請求

[[email protected] snmp_c]#snmpget -v2c -c public localhost .1.3.6.1.4.1.37945.1.1.0

SNMPv2-SMI::enterprises.37945.1.1.0= STRING: "hello inspur"

檢視原來終端的debug資訊

[[email protected] ~]# snmpd -Le -f -d

Turning on AgentX master support.

NET-SNMP version 5.7.2.1

Received 80 byte packet from Local IPC:/var/agentx/master

0000: 01 01 00 00 00 00 00 00  00 00 00 00  86 FB CA 1E   ................

0016: 3C 00 00 00 00 00 00 00  05 04 00 00  01 00 00 00   <...............

0032: 88 1F 00 00 03 00 00 00  02 00 00 00  0A 00 00 00   ................

0048: 19 00 00 00 4E 65 74 2D  53 4E 4D 50  20 41 67 65   ....Net-SNMP Age

0064: 6E 74 58 20 73 75 62 2D  61 67 65 6E  74 00 00 00   ntX sub-agent...

Sending 88 bytes to Local IPC: /var/agentx/master

0000: 01 12 00 00 05 00 00 00  00 00 00 00  86 FB CA 1E   ................

0016: 44 00 00 00 2B 03 00 00  00 00 00 00  04 00 00 00   D...+...........

0032: 05 04 00 00 01 00 00 00  88 1F 00 00  03 00 00 00   ................

0048: 02 00 00 00 0A 00 00 00  19 00 00 00  4E 65 74 2D   ............Net-

0064: 53 4E 4D 50 20 41 67 65  6E 74 58 20  73 75 62 2D   SNMP AgentX sub-

0080: 61 67 65 6E 74 00 00 00                             agent...

Received 72 byte packet from Local IPC:/var/agentx/master

0000: 01 12 00 00 05 00 00 00  01 00 00 00  02 00 00 00   ................

0016: 34 00 00 00 00 00 00 00  00 00 00 00  04 00 00 00   4...............

0032: 05 04 00 00 01 00 00 00  39 94 00 00  01 00 00 00   ........9.......

0048: 01 00 00 00 00 00 00 00  0C 00 00 00  68 65 6C 6C    ............hell

0064: 6F 20 69 6E 73 70 75 72                             o inspur

Sending 58 bytes to UDP:[127.0.0.1]:37760->[127.0.0.1]:161

0000: 30 38 02 01 01 04 06 70  75 62 6C 69  63 A2 2B 02   08.....public.+.

0016: 04 39 99 34 1F 02 01 00  02 01 00 30  1D 30 1B 06   .9.4.......0.0..

0032: 0B 2B 06 01 04 01 82 A8  39 01 01 00  04 0C 68 65   .+......9.....he

0048: 6C 6C 6F 20 69 6E 73 70  75 72                       llo inspur

http://blog.csdn.net/qq_27204267/article/category/6261728

http://www.cnblogs.com/shenlinken/p/5316021.html

相關推薦

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

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

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

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

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

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

C#/.NET 列舉特性擴充套件——系統特性及自定義特性

C#列舉特性擴充套件——系統特性及自定義特性 系統自帶的特性 public static class EnumHelperExtensions { public static List<T> GetAllEnumMembers<T>() {

net-snmp agent開發詳解,非常簡單

轉載請標明出處 原文地址:http://blog.csdn.net/hepeng597/article/details/8782868 花了一兩天時間測試和整理一下。 用net-snmp擴充套件MIB庫,實現方法可歸結為四種: 1)一是靜態庫方式,通過修改配置標頭檔案,

net-snmp agent開發詳解

轉載請標明出處 原文地址:http://blog.csdn.net/hepeng597/article/details/8782868 花了一兩天時間測試和整理一下。 用net-snmp擴充套件MIB庫,實現方法可歸結為四種: 1)一是靜態庫方式,通

基於net-snmp的代理agent開發詳解

花了一兩天時間測試和整理一下。 用net-snmp擴充套件MIB庫,實現方法可歸結為四種: 1)一是靜態庫方式,通過修改配置標頭檔案,在相應地方包含新引入的mib模組的.c和.h檔案,然後重新編譯庫檔案和擴充套件程式碼;這種方式不夠靈活,每次修改擴

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

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

linux net-snmp agent manager windows snmp

這段時間一直在弄snmp相關的東西,網上的資料有用的很少,在這說下自己遇到的問題避免很多人都走回彎路。 snmp分為manager管理端(一般為一臺主機),和agent代理端(一般為一些執行SNMP的裝置);對應到net-snmp為client 和 server端(agen

ASP.NETC#生成二維碼

保存 mss 軟件 clear 所有 str drawing for visual ASP.NET中用C#語言編寫網頁,將自己輸入的文字生成二維碼。 工具/原料 已安裝好VS2010或其他版本VS軟件的電腦一臺 1、新建 1

zabbix-3.2.6之snmp監控(無需安裝agent也能監控)

6.0 send 是否 available 安全 scroll sna object 2.2.0 一、snmp監控介紹 概述 如果我們需要監控打印機、路由器、UPS等設備,肯定不能使用zabbix agentd,因為他們不能安裝軟件的,還好他們一般都支持SNMP協議,這

【.Netc# 讓double保留兩位小數

con orm tr1 ima 一個 dbd for tin git 1、Math.Round(0.333333,2);//按照四舍五入的國際標準2、 double dbdata=0.335333; string str1=String.Format("{0:F

.NETC#和ASP.NET三才之間的區別(轉)

編程 tail 基於web 發現 我認 ava 微軟 框架 framwork 經常有同學會在.NET、C#和ASP.NET這三者之間區別不清楚,到底它們之間有什麽聯系呢?在這裏我給大家歸納如下: 1、.NET是一個平臺,一個抽象的平臺的概念。

JSON之Asp.net MVC C#對象轉JSON,DataTable轉JSON,List<T>轉JSON,JSON轉List<T>,JSON轉C#對象

技術分享 toolbar index 基本 枚舉 對象轉json tostring cab 拼接 一、JSON解析與字符串化   JSON.stringify()  序列化對象、數組或原始值   語法:JSON.stringify(o,filter,indent)     

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 Core C#代碼分享

date dom factory reat throw must ret off ont 1、即使是相同的短信簽名與短信正文模板,也需要針對“國內文本短信”與“海外文本短信”分別申請。開始不知道,以為只要申請一次,給國外手機發

MongoDB .Net Driver(C#驅動) - 內嵌數組/嵌入文檔的操作(增加、刪除、修改、查詢(Linq 分頁))

odbc article err native linq protocol remove mit 實體類 目錄 一、前言 (一) 運行環境 二、前期準備工作 (一) 創建 MongoDBContext MongoDb操作上下文類 (二)創建測試類 (三) 創建測試代碼