snmp学习笔记之二trap——分析notification.c文件

需要文件:

NET-SNMP-EXAMPLES-MIB.txt
notification.c

这两个文件都在net-snmp源码包里,我的版本是5.4.1

NET-SNMP-EXAMPLES-MIB.txt net-snmp-5.4.1.2/mibs目录下
notification.c net-snmp-5.4.1.2/agent/mibgroup/examples目录下

  1. 安装notification:

    ./configure --with-mib-modules="examples/notification"
    make
    sudo make install
  2. 配置snmp.conf文件,在文件中增加NET-SNMP-EXAMPLES-MIB mib库

    sudo vim /usr/local/share/snmp/snmp.conf

    在文件中增加: mibs +NET-SNMP-EXAMPLES-MIB

  3. 验证netSnmpExampleNotifications mib库是否正常加载:

    snmptranslate -IR -Tp netSnmpExampleNotifications
    +--netSnmpExampleNotifications(3)
       |
       +--netSnmpExampleNotificationPrefix(0)
       |  |
       |  +--netSnmpExampleHeartbeatNotification(1)
       |
       +-- ---N String    netSnmpExampleNotification(1)
       |        Textual Convention: SnmpAdminString
       |        Size: 0..255
       |
       +--netSnmpExampleNotificationObjects(2)
          |
          +-- ---N Integer32 netSnmpExampleHeartbeatRate(1)
          +-- ---N String    netSnmpExampleHeartbeatName(2)
                   Textual Convention: SnmpAdminString
                   Size: 0..255
  4. 配置snmptrapd.conf

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

    authcommunity execute,log,net public

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

如果相对接受到的信息处理可以增加:

traphandle .1.3.6.1.4.1.2021.251.2  page_me down
# 默认处理函数
traphandle default                  log_it
  1. agent自动产生trap

配置agent的snmpd.conf,加入以下几行:(参考:
http://www.net-snmp.org/wiki/index.php/FAQ:Agent_17 ):

# From: http://www.net-snmp.org/wiki/index.php/FAQ:Agent_17
# send v1 traps
trapsink   127.0.0.1:162
# also send v2 traps
trap2sink  127.0.0.1:162
informsink 127.0.0.1:162
  1. 启动snmptrapd

    sudo snmptrapd –d –f –Lo

  2. 启动snmpd

    sudo snmpd -f -L

snmpd 会每隔30秒给snmptrapd发送一个信息。收到的信息如下:

Received 64 bytes from UDP: [127.0.0.1]:56929
0000: 30 3E 02 01  00 04 06 70  75 62 6C 69  63 A4 31 06    0>.....public.1.
0016: 09 2B 06 01  04 01 BF 08  02 03 40 04  AC 10 81 01    .+........@.....
0032: 02 01 06 02  01 01 43 03  03 CC BC 30  13 30 11 06    ......C....0.0..
0048: 0C 2B 06 01  04 01 BF 08  02 03 02 01  00 02 01 1E    .+..............

2008-11-11 15:43:11 172.16.129.1(via UDP: [127.0.0.1]:56929) TRAP, SNMP v1, community public
        NET-SNMP-EXAMPLES-MIB::netSnmpExampleNotifications Enterprise Specific Trap
        (NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatNotification) Uptime: 0:41:30.20
        NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatRate.0 = INTEGER: 30
  1. notification.c 源码如下:

    /** @example notification.c
     *  This example shows how to send a notification from inside the
     *  agent.  In this case we do something really boring to decide
     *  whether to send a notification or not: we simply sleep for 30
     *  seconds and send it, then we sleep for 30 more and send it again.
     *  We do this through the snmp_alarm mechanisms (which are safe to
     *  use within the agent.  Don't use the system alarm() call, it won't
     *  work properly).  Normally, you would probably want to do something
     *  to test whether or not to send an alarm, based on the type of mib
     *  module you were creating.
     *
     *  When this module is compiled into the agent (run configure with
     *  --with-mib-modules="examples/notification") then it should send
     *  out traps, which when received by the snmptrapd demon will look
     *  roughly like:
     *
     *   可以通过 --with-mib-modules="examples/notification" 把这个模块
     * 编译到agent模块中,snmptrapd可以接收到他发送的traps, 接收到的信息
     * 如下:
     *
     *  2002-05-08 08:57:05 localhost.localdomain [udp:127.0.0.1:32865]:
     *      sysUpTimeInstance = Timeticks: (3803) 0:00:38.03 /
     *      snmpTrapOID.0 = OID: netSnmpExampleNotification
     *
     */
    
    /*
     * start be including the appropriate header files
     */
    #include 
    #include 
    #include 
    
    /*
     * contains prototypes
     */
    #include "notification.h"
    
    /*
     * our initialization routine 初始化
     * (to get called, the function name must match init_FILENAME()
     * 函数的名字必须是 init_FILENAME() 这种格式
     */
    void
    init_notification(void)
    {
        DEBUGMSGTL(("example_notification",
                    "initializing (setting callback alarm)/n"));
        snmp_alarm_register(30,     /* seconds, 秒 */
                            SA_REPEAT,      /* repeat (every 30 seconds). 每隔30秒发送一个trap*/
                            send_example_notification,      /* our callback 我们的回调函数 */
                            NULL    /* no callback data needed */
            );
    }
    
    /** here we send a SNMP v2 trap (which can be sent through snmpv3 and
     *  snmpv1 as well) and send it out.
     *
     *     The various "send_trap()" calls allow you to specify traps in different
     *  formats.  And the various "trapsink" directives allow you to specify
     *  destinations to receive different formats.
     *  But *all* traps are sent to *all* destinations, regardless of how they
     *  were specified.
     *
     *
     *  I.e. it's
     * @verbatim
     *                                           ___  trapsink
     *                                          /
     *      send_easy_trap /___  [  Trap      ] ____  trap2sink
     *                      ___  [ Generator  ]
     *      send_v2trap    /     [            ] ----- informsink
     *                                          /____
     *                                                trapsess
     *
     *  *Not*
     *       send_easy_trap  ------------------->  trapsink
     *       send_v2trap     ------------------->  trap2sink
     *       ????            ------------------->  informsink
     *       ????            ------------------->  trapsess
     * @endverbatim
     */
    void
    send_example_notification(unsigned int clientreg, void *clientarg)
    {
        /*
         * define the OID for the notification we're going to send
         * NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatNotification
         */
        oid             notification_oid[] =
            { 1, 3, 6, 1, 4, 1, 8072, 2, 3, 0, 1 };
        size_t          notification_oid_len = OID_LENGTH(notification_oid);
        static u_long count = 0;
    
        /*
         * In the notification, we have to assign our notification OID to
         * the snmpTrapOID.0 object. Here is it's definition.
         */
        oid             objid_snmptrap[] = { 1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0 };
        size_t          objid_snmptrap_len = OID_LENGTH(objid_snmptrap);
    
        /*
         * define the OIDs for the varbinds we're going to include
         *  with the notification -
         * NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatRate  and
         * NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatName
         */
        oid      hbeat_rate_oid[]   = { 1, 3, 6, 1, 4, 1, 8072, 2, 3, 2, 1, 0 };
        size_t   hbeat_rate_oid_len = OID_LENGTH(hbeat_rate_oid);
        oid      hbeat_name_oid[]   = { 1, 3, 6, 1, 4, 1, 8072, 2, 3, 2, 2, 0 };
        size_t   hbeat_name_oid_len = OID_LENGTH(hbeat_name_oid);
    
        /*
         * here is where we store the variables to be sent in the trap
         */
        netsnmp_variable_list *notification_vars = NULL;
        const char *heartbeat_name = "A girl named Maria";
        #ifdef  RANDOM_HEARTBEAT
        int  heartbeat_rate = rand() % 60;
        #else
        int  heartbeat_rate = 30;
        #endif
    
        DEBUGMSGTL(("example_notification", "defining the trap/n"));
    
        /*
         * add in the trap definition object
         */
        snmp_varlist_add_variable(¬ification_vars,
                                  /*
                                   * the snmpTrapOID.0 variable
                                   */
                                  objid_snmptrap, objid_snmptrap_len,
                                  /*
                                   * value type is an OID
                                   */
                                  ASN_OBJECT_ID,
                                  /*
                                   * value contents is our notification OID
                                   */
                                  (u_char *) notification_oid,
                                  /*
                                   * size in bytes = oid length * sizeof(oid)
                                   */
                                  notification_oid_len * sizeof(oid));
    
        /*
         * add in the additional objects defined as part of the trap
         */
    
        snmp_varlist_add_variable(¬ification_vars,
                                   hbeat_rate_oid, hbeat_rate_oid_len,
                                   ASN_INTEGER,
                                  (u_char *)&heartbeat_rate,
                                      sizeof(heartbeat_rate));
    
        /*
         * if we want to insert additional objects, we do it here
         */
        if (heartbeat_rate < 30 ) {
            snmp_varlist_add_variable(¬ification_vars,
                                   hbeat_name_oid, hbeat_name_oid_len,
                                   ASN_OCTET_STR,
                                   heartbeat_name, strlen(heartbeat_name));
        }
    
        /*
         * send the trap out.  This will send it to all registered
         * receivers (see the "SETTING UP TRAP AND/OR INFORM DESTINATIONS"
         * section of the snmpd.conf manual page.
         */
        ++count;
        DEBUGMSGTL(("example_notification", "sending trap %ld/n",count));
        send_v2trap(notification_vars); // 发送snmpv2的trap
    
        /*
         * free the created notification variable list
         */
        DEBUGMSGTL(("example_notification", "cleaning up/n"));
        snmp_free_varbind(notification_vars);
    }
参考:
  1. How can I get the agent to generate a trap http://www.net-snmp.org/wiki/index.php/FAQ:Coding_15
  2. http://www.net-snmp.org/docs/mibs/NET-SNMP-EXAMPLES-MIB.txt
  3. http://www.net-snmp.org/dev/agent/notification_8c-example.html
  4. http://www.net-snmp.org/wiki/index.php/FAQ:Agent_17

你可能感兴趣的:(网络管理)