Net-snmp 使用c 扩展 sub-agent

来源:互联网 发布:云计算的虚拟化 编辑:程序博客网 时间:2024/06/06 16:39


摘要:

netSNMP开发,使用c开发对SNMP进行扩展,应用程序作为agent执行。

官网:http://www.net-snmp.org

版本:NET-SNMP version 5.7.2.1

系统:CentOS X64

软件包:net-snmp-5.7.2.1.tar.gz

 

解压安装包:

[root@localhost snmp]# pwd

/root/snmp

[root@localhost snmp]# ls

net-snmp-5.7.2.1.tar.gz

[root@localhost snmp]# tarxzvf net-snmp-5.7.2.1.tar.gz

[root@localhost snmp]# ls

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

配置

[root@localhost snmp]# cdnet-snmp-5.7.2.1

[root@localhostnet-snmp-5.7.2.1]# pwd

/root/snmp/net-snmp-5.7.2.1

[root@localhostnet-snmp-5.7.2.1]# ./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

[root@localhost snmp]# pwd

/usr/local/share/snmp

[root@localhost 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配置文件

[root@localhost snmp]# pwd

/usr/local/share/snmp

[root@localhost snmp]# catsnmpd.conf

rocommunity  public 

rwcommunity  private 

master  agentx

[root@localhost snmp]#

 

编写MIB文件

[root@localhost snmp_c]# pwd

/root/snmp/test/snmp_c

[root@localhost 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

        Test MODULE-IDENTITY

            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))

            MAX-ACCESS read-only

            STATUS current

            DESCRIPTION

                "Example : 2013/4/11"

            ::= { Time 1 }

    END

 

-- Inspur-MIB.my

复制mib文件到指定目录

[root@localhost snmp_c]# cpInspur-MIB.my /usr/local/share/snmp/mibs/

 

测试mib文件

[root@localhost snmp_c]# snmptranslate-Tp -IR Inspur-MIB::Test

+--Test(37945)

   |

   +--Time(1)

      |

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

               Textual Convention:DisplayString

               Size: 0..100

 

生成源码

[root@localhost snmp_c]# pwd

/root/snmp/test/snmp_c

[root@localhost 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

[root@localhost 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;

编译测试程序

[root@localhost snmp_c]# pwd

/root/snmp/test/snmp_c

[root@localhost 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信息

[root@localhost snmp]# snmpd-f -Le -d

Turning on AgentX mastersupport.

NET-SNMP version 5.7.2.1

 

回到原来终端,执行测试程序

[root@localhost snmp_c]#./Test

[root@localhost 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请求

[root@localhost 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信息

[root@localhost ~]# 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

0 0
原创粉丝点击