snmp trap配置


一、trap的用途
TRAP是提供从代理进程到管理站的异步报告机制。
为了使管理站能够及时而又有效地对被管理设备进行监控,同时又不过分增加网络的通信负载,必须使用陷入(TRAP)制导的轮讯过程。代理进程负责在必要时 向管理站报告异常事件,得到异常事件的报告后,管理站可以查询有关的代理,以便得到更具体的信息,对事件的原因做进一步的分析


二、trap的工作流程
1、agent端:
  A) 编写mib文件,确定好trap名称等信息。
  B) 命令方式:发送各种trap命令(manager地址,默认端口号162),在manager端进行监听,在agent端进行命令测试。
  C) 自动触发:配置snmpd.conf设置触发trap,系统发生某类错误时会自动触发相应类型的trap,发送给manager
  D) 程序方式:一部份trap需要写c语言程序,用相应的api(send_easy_trap 或 send_v2trap)发送
2、manager端:
  A) 配置snmptrapd.conf文件,设置访问权限
  B) 将mib导入到mibs文件夹中
  C) 用perl等脚本语言编写处理trap的程序
  D) 配置snmptrapd.conf文件,添加traphandler项,将不同的trap对应到不同的处理程序上


三、trap的环境配置
   在manager端建立/etc/snmp/snmptrapd.conf(我的机器上是这个,不同机器不同,可能有的放在/etc/snmp,/usr/local/share/snmp/下,视不同情况慢慢实验),加入以下一行:
  

  A) authcommunity 设置所有用户的访问权限:可执行,记录,传递

这条指令指明以“public”为“community”请求的snmp “notification”允许的操作。
各变量意义如下:

      log: log the details of the notification - either in a specified file, to standard output (or stderr), or via syslog(or similar).
      execute: pass the details of the trap to a specified handler program, including embedded perl.
      net: forward the trap to another notification receiver.

  B) 若想对接收到的信息进行处理,可以使用traphandle,示例如上图:

test.pl的内容:
Perl代码 
#!/usr/bin/perl 
use strict; 
my $file="file.trap"; 
open(HANDOUT,">>./$file"); 
while(

    print HANDOUT "$_"; 

第一个参数为从snmptrapd接收的OID,第二个参数为调用的程序。

四、命令方式的过程
  1.在manager端用命令snmptrapd -d -f -Lo启动snmptrapd来监听162端口

            -a                    ignore authentication failure traps
            -c FILE                read FILE as a configuration file
            -C                    do not read the default configuration files
            -d                    dump sent and received SNMP packets
            -f                     do not fork from the shell
            -n                     use numeric addresses instead of attempting  hostname lookups (no DNS)
            -t                    Prevent traps from being logged to syslog
            -O           toggle options controlling output display
            -L            toggle options controlling where to log to
                        e:           log to standard error
                        o:           log to standard output
                        n:           don't log at all
                        f file:        log to the specified file
                        s facility:    log to syslog (via the specified facility)

        例:snmptrapd -d -f -Lo

            snmptrapd -C -c /etc/snmp/snmptrapd.conf -Lf /tmp/trapd.log

  2.然后在agent端输入命令:

       版本1:snmptrap -v 1 [COMMON OPTIONS] [-Ci] enterprise-oid agent generic-trap specific-trap uptime [OID TYPE VALUE]...
       版本2:snmptrap -v [2c|3] [COMMON OPTIONS] [-Ci] uptime trap-oid [OID TYPE VALUE]...

       格式:命令  版本   -c 共同体  管理机  Enterprise-OID  snmp代理地址   陷阱类型 oid   时间  1.3.6.1.9.9.44.1.2.1 i 12 1.3.4.1.2.3.1 s aaa(被发送参数的OID 数据类型 值 被发送参数OID 。。。。。)
 
数据类型: i  ××× u  无符号型    c  COUNTER32    s  字符串   x  16进制字符串   d  10进制字符串  n  空对象 o  对象ID  t  计时器  a  IP地址 b  比特
       generic-trap:包括7种, 分别是:0  coldStart  1  warmStart  2  linkDown  3  linkUp 4  authenticationFailure          5  egpNeighborLoss        6  enterpriseSpecific
 

     例: snmptrap -v1 -c public *.*.*.* .1.3.6.1.4.1.1 192.168.2.125 2 3 1000 1.3.6.1.9.9.44.1.2.1 i 12 1.3.4.1.2.3.1 s zzz

          snmptrap -v 2c -c public *.*.*.* "" .1.3.6.1.4.1.2021.251.1 sysLocation.0 s "longtengfei"  

 
而后在manager管理方会接收到如下信息: 
 
Received 98 bytes from UDP: [221.176.14.88]:58750  
0000: 30 60 02 01  01 04 06 70  75 62 6C 69  63 A7 53 02    0`.....public.S.  
0016: 04 1B CE 4F  F1 02 01 00  02 01 00 30  45 30 10 06    ...O.......0E0..  
0032: 08 2B 06 01  02 01 01 03  00 43 04 28  8D B0 5B 30    .+.......C.(..[0  
0048: 18 06 0A 2B  06 01 06 03  01 01 04 01  00 06 0A 2B    ...+...........+  
0064: 06 01 04 01  8F 65 81 7B  01 30 17 06  08 2B 06 01    .....e.{.0...+..  
0080: 02 01 01 06  00 04 0B 6C  6F 6E 67 74  65 6E 67 66    .......longtengf  
0096: 65 69                                                 ei  
 
2013-03-12 10:44:17 [UDP: [221.176.14.88]:58750]:  
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (680374363) 78 days, 17:55:43.63   SNMPv2-MIB::snmpTrapOID.0 = OID: UCD-SNMP-MIB::ucdStart SNMPv2-MIB::sysLocation.0 = STRING: longtengfei  
 
至此,最简单的snmptrap的发送与接收就完成了。

五、处理自定义trap

        (参考http://www.net-snmp.org/wiki/index.php/TUT:snmptrap)
编写两个mib文件,包括snmp1和snmp2两种trap
Snmp1的mib:TRAP-TEST-MIB.txt
TRAP-TEST-MIB DEFINITIONS ::= BEGIN       IMPORTS ucdExperimental FROM UCD-SNMP-MIB; demotraps OBJECT IDENTIFIER ::= { ucdExperimental 990 } demo-trap TRAP-TYPE       STATUS current       ENTERPRISE demotraps       VARIABLES { sysLocation }       DESCRIPTION "This is just a demo"       ::= 17 END


Snmp2的mib:NOTIFICATION-TEST-MIB.txt
NOTIFICATION-TEST-MIB DEFINITIONS ::= BEGIN       IMPORTS ucdavis FROM UCD-SNMP-MIB; demonotifs OBJECT IDENTIFIER ::= { ucdavis 991 } demo-notif NOTIFICATION-TYPE         STATUS current         OBJECTS { sysLocation }         DESCRIPTION "Just a test notification"         ::= { demonotifs 17 } END              然后放入到mibs文件夹中


在agent端敲入命令:
snmptrap -v 2c -c public 192.168.213.64:162 "" NOTIFICATION-TEST-MIB::demo-notif          SNMPv2-MIB::sysLocation.0 s "just here"


manager端输出:
Received 96 bytes from UDP: [192.168.213.64]:32808
0000: 30 5E 02 01  01 04 06 70  75 62 6C 69  63 A7 51 02    0^.....public.Q.
0016: 04 17 27 54  32 02 01 00  02 01 00 30  43 30 10 06    ..'T2......0C0..
0032: 08 2B 06 01  02 01 01 03  00 43 04 03  F0 3A 1A 30    .+.......C...:.0
0048: 18 06 0A 2B  06 01 06 03  01 01 04 01  00 06 0A 2B    ...+...........+
0064: 06 01 04 01  8F 65 87 5F  11 30 15 06  08 2B 06 01    .....e._.0...+..
0080: 02 01 01 06  00 04 09 6A  75 73 74 20  68 65 72 65    .......just here

192.168.213.64 [UDP: [192.168.213.64]:32808]: Trap , DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (66075162) 7 days, 15:32:31.62, SNMPv2-MIB::snmpTrapOID.0 = OID: UCD-SNMP-MIB::ucdavis.991.17, SNMPv2-MIB::sysLocation.0 = STRING: just here
sh: log_it: command not found
其中just here就是我们想要的结果

 六、 让agent自动产生trap
配置agent的snmpd.conf,配置如下:

trapsink *.*.*.*:162 public
trap2sink *.*.*.*:162 public
authtrapenable 1
rwuser administrator
iquerySecName administrator
linkUpDownNotifications yes
defaultMonitors yes

#Process checks
proc sendmail 10 1

#disk checks
disk / 100000

#Check for loads
load 5 5 5

#CPU usage
notificationEvent userCPU ssCpuRawUser
notificationEvent sysCPU ssCpuRawSystem
monitor -r 60 -e userCPU "User CPU use percentage" ssCpuRawUser > 100
monitor -r 60 -e sysCPU "System CPU use percentage" ssCpuRawSystem > 100

#Memory usage
notificationEvent memTotalTrap memTotalReal memTotalSwap
notificationEvent memAvailTrap memAvailReal memAvailSwap memTotalFree
monitor -r 10 -e memTotalTrap "Total memory" memTotalReal < 1000000000000
monitor -r 10 -e memAvailTrap "Available memory" memTotalFree < 1000000000000

指令简介:
·#号后为内容注释。
·trapsink: 指明信息发送的snmptrapd地址,1版本。
·trap2sink:指明信息发送的snmptrapd地址,2c版本。
·authtrapenable:决定是否发送身份验证失败信息。1为发送,2为不发送。
·rwuser:建立读写权限用户。
·iquerySecName:指定内部查询用户名。
·linkUpDownNotifications:在建立断开链接的时候发送通知。
·defaultMonitors:打开默认监视项。其中包括进程,磁盘等的监视,必须打开此项,否则进程,磁盘等的监视信息不会被发出。
·proc:监视指定进程,当进程数不在指定的范围时将特定标志位置1,但是不会发出trap信号,需要打开defaultMonitors,由其发送trap信号。
·disk:监视磁盘剩余空间,当剩余空间小于指定值时将特定标志位置1,同proc。
·load:系统负载,指定1,5,15分钟的系统负载,超过阈值后处理方法同proc。
·notificationEvent:自定义通知事件。
·monitor:监视指定事件。


七、程序方式
思路:让一个程序监控设备,如果设备产生了发送trap的条件,那么就编写一条snmptrap命令,新建一个进程将其发送出去。
本demo模拟这一过程,每2秒钟发送一条trap,用到的是上面最后一条命令:
snmptrap -v 1 -c public localhost:162 TRAP-TEST-MIB::demotraps localhost 2 0 ""        IF-MIB::ifIndex i 1
manager端trap处理程序不变,mib不变。
Mytrap.c:

#include
#include

int main()
{
        while(1){
                int pid ;
                int status;
                if( (pid = fork()) == 0){
execl("/usr/bin/snmptrap","snmptrap","-v","1","-c","public","localhost:162","TRAP-TEST-MIB::demotraps","localhost","2","0","\"\"","IF-MIB::ifIndex","i","1",NULL);
              
printf("snmptrap\n");
                    return 0;
                }
                else if (pid
                        perror("fork:");
                }
                else{
                        printf("send a trap\n");
                        sleep(2);
                }
        }
        return 0;
}