利用NET-SNMP开发代理(原创)

  SNMP代理开发:
  by Flyfish <zxm0718@yahoo.com.cn>


=================================================================================
添加自定义MIB库:
1、把自定义的MIB库拷贝到c:/usr/share/snmp/mibs/下;
2、使用snmpconf这个perl工具配置MIB库到NET-SNMP系统;
C:/usr/bin>snmpconf -i

The following installed configuration files were found:
  1:  ./snmpd.conf
  2:  c:/usr/etc/snmp/snmp.conf
  3:  c:/usr/etc/snmp/snmpd.conf
  4:  c:/usr/share/snmp/snmp.conf
  5:  c:/usr/share/snmp/snmptrapd.conf
Would you like me to read them in?  Their content will be merged with the
output files created by this session.
Valid answer examples: "all", "none","3","1,2,5"
Read in which (default = all):
I can create the following types of configuration files for you.
Select the file type you wish to create:
(you can create more than one as you run this program)
  1:  snmpd.conf
  2:  snmptrapd.conf
  3:  snmp.conf
Other options: quit
Select File: 3
The configuration information which can be put into snmp.conf is divided
into sections.  Select a configuration section for snmp.conf
that you wish to create:
  1:  Default Authentication Options
  2:  Debugging output options
  3:  Textual mib parsing
  4:  Output style options
Other options: finished
Select section: 3
Section: Textual mib parsing
Description:
 This section controls the textual mib parser.  Textual
 mibs are parsed in order to convert OIDs, enumerated
 lists, and ... to and from textual representations
 and numerical representations.
Select from:
  1:  Specifies directories to be searched for mibs.
  2:  Specifies a list of mibs to be searched for and loaded.
  3:  Loads a particular mib file from a particualar path
  4:  Should errors in mibs be displayed when the mibs are loaded
  5:  Should warnings about mibs be displayed when the mibs are loaded
  6:  Be strict about about mib comment termination.
  7:  Should underlines be allowed in mib symbols (illegal)
  8:  Force replacement of older mibs with known updated ones
Other options: finished, list
Select section: 2
Configuring: mibs
Description:
 Specifies a list of mibs to be searched for and loaded.
   Adding a '+' sign to the front of the argument appends the new
   mib name to the list of mibs already being searched for.
   arguments: [+]mibname[:mibname...]
Enter the list of mibs to read: MyMIB /*注意使用的是文件名*/
Finished Output: mibs  +MyMIB
Section: Textual mib parsing
Description:
 This section controls the textual mib parser.  Textual
 mibs are parsed in order to convert OIDs, enumerated
 lists, and ... to and from textual representations
 and numerical representations.
Select from:
  1:  Specifies directories to be searched for mibs.
  2:  Specifies a list of mibs to be searched for and loaded.
  3:  Loads a particular mib file from a particualar path
  4:  Should errors in mibs be displayed when the mibs are loaded
  5:  Should warnings about mibs be displayed when the mibs are loaded
  6:  Be strict about about mib comment termination.
  7:  Should underlines be allowed in mib symbols (illegal)
  8:  Force replacement of older mibs with known updated ones
Other options: finished, list
Select section: finished
The configuration information which can be put into snmp.conf is divided
into sections.  Select a configuration section for snmp.conf
that you wish to create:
  1:  Default Authentication Options
  2:  Debugging output options
  3:  Textual mib parsing
  4:  Output style options
Other options: finished
Select section: finished
I can create the following types of configuration files for you.
Select the file type you wish to create:
(you can create more than one as you run this program)
  1:  snmpd.conf
  2:  snmptrapd.conf
  3:  snmp.conf
Other options: quit
Select File: quit
The following files were created:
 snmp.conf installed in c:/usr/share/snmp
C:/usr/bin>

********************************************************************************
如下语句可概览MIB库中某节点开始的信息:
snmptranslate -Tp -OS 1.3.6.1.4.1
输出至文本:
snmptranslate -Tp -OS 1.3.6.1.4.1 >enterpriseView.txt
例如:
运行snmptranslate -Tp -OS 1.3.6.1.2.1.1会输出如下:
+--system(1)
  |
  +-- -R-- String    sysDescr(1)
  |        Textual Convention: DisplayString
  |        Size: 0..255
  +-- -R-- ObjID     sysObjectID(2)
  +-- -R-- TimeTicks sysUpTime(3)
  |  |
  |  +--sysUpTimeInstance(0)
  |
  +-- -RW- String    sysContact(4)
  |        Textual Convention: DisplayString
  |        Size: 0..255
  +-- -RW- String    sysName(5)
  |        Textual Convention: DisplayString
  |        Size: 0..255
  +-- -RW- String    sysLocation(6)
  |        Textual Convention: DisplayString
  |        Size: 0..255
  +-- -R-- INTEGER   sysServices(7)
  |        Range: 0..127
  +-- -R-- TimeTicks sysORLastChange(8)
  |        Textual Convention: TimeStamp
  |
  +--sysORTable(9)
     |
     +--sysOREntry(1)
        |  Index: sysORIndex
        |
        +-- ---- INTEGER   sysORIndex(1)
        |        Range: 1..2147483647
        +-- -R-- ObjID     sysORID(2)
        +-- -R-- String    sysORDescr(3)
        |        Textual Convention: DisplayString
        |        Size: 0..255
        +-- -R-- TimeTicks sysORUpTime(4)
                 Textual Convention: TimeStamp
********************************************************************************


========================================================================================
1 按照《用NET-SNMP软件包开发简单客户端代理》一文进行扩展开发代理(自定义Agent程序方式)。
自定义的MIB库如下:
+--foxmail(310)
  |
  +-- -RW- Integer32 SecondCounter(1)
  +-- -R-- TimeTicks WeekTime(2)
  |
  +--ExampleTable(3)
     |
     +--ExampleEntry(1)
        |  Index: UserIndex
        |
        +-- -R-- Integer32 UserIndex(1)
        |        Textual Convention: InterfaceIndex
        |        Range: 1..2147483647
        +-- -R-- String    UserStatus(2)
        |        Textual Convention: DisplayString
        |        Size: 0..255
        +-- -R-- TimeTicks CheckTime(3)
        +-- -RW- Integer32 MonSet(4)
使用MIB2C工具生成*.c和*.h文件模板,在get和set中添加自己的实现代码。
新建工程,加入上述自动生成的文件,并编写主函数文件。
使用静态库版本的netsnmp.lib, netsnmpagent.lib, netsnmpmibs.lib, netsnmphelpers.lib加上wsock32.lib编译链接生成agent。
把此agent拷到c:/usr/bin下,编写相应的配置文件,即可运行。
配置文件写法与snmpd.exe的配置文件类似。

2 按例子生成的agent: FoxMail好像不能指定运行端口,只能运行于默认的161端口

3 使用proxy功能:
在snmpd.conf中加入:
proxy -v 1 -c public localhost:161 .1.3.6.1.4.1.310
或proxy -v 1 -c public -c private localhost:161 .1.3.6.1.4.1.310
先运行foxmail.exe, 再运行snmpd.exe (需指定端口,如:snmpd -f -Le -d 192.168.0.33:10161)
4 foxmail中,只能查到标量对象值,表对象查不到值,提示"Error:Can't open data.txt!"
原来他采用了读文件来实现表: 需要data.txt文件,格式为"%d %s %d %d",分别代表UserIndex,UserStatus,CheckTime,MonSet
手动编写一个此文件,放到foxmail.exe所在目录下,重启foxmail.exe,重启snmpd.exe,通过!
5 SET测试,表对象通过:set 1.3.6.1.4.1.310.3.1.4.4(MonSet.4) 53
但标量对象(SecondCounter.0)没实现?
============================================================================================================

扩展代理(嵌入到snmpd.exe方式):
按照官方手册《Readme.win32》中"Microsoft Visual C++ - Extending the Agent"一节。
1、首先使用mib2c工具生成*.c和*.h文件,合并模板,并编写相应实现代码。以下采用上述MIB库foxmail项目中已经生成的文件。
2、把生成的*.c和*.h文件拷到$source/agent/mibgroup/foxmail/目录下。
3、把这些文件添加到netsnmpmibssdk工程中去。
4、编辑$source/win32/mib_module_includes.h文件:添加自定义库的 .h 文件。
5、编辑$source/win32/mib_module_inits.h文件添加初始化函数:
if (should_init("Foxmail")) init_foxmail();
6、重新编译netsnmpmibssdk和snmpdsdk工程,注意一定要使用静态库方式,依赖的其余几个库也要使用静态库。
7、success!
把编译后的snmpd.exe和netsnmpmibs.lib拷到安装目录,照常使用snmpd.exe即可。

===============================================================================================================

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值