如何在C中使用net-snmp发送snmptrap?

时间:2019-02-11 12:24:42

标签: snmp net-snmp snmp-trap snmpd snmptrapd

我正在为嵌入式设备开发应用程序。我想在某些情况下发送陷阱。我找到了一些例子,但并没有真正帮助我。 net-snmp中有一个名为send_v2trap()的函数。有人能帮我吗? snmpd.confsnmptrapd.conf是否需要做?

1 个答案:

答案 0 :(得分:0)

我们将尝试使某些事情更接近实际需求:触摸OID时发送陷阱/通知

让我们举个例子... net-snmp-5.7.x / agent / mibgroup / examples / watched.c

我们更改:

reginfo = netsnmp_create_handler_registration("my example string", NULL,

作者

reginfo = netsnmp_create_handler_registration("my example string", handler_for_changes,

handler_for_changes(...)的定义是

int handler_for_changes (   netsnmp_mib_handler * p_handler,
                            netsnmp_handler_registration * p_reginfo,
                            netsnmp_agent_request_info * p_requestinfo,
                            netsnmp_request_info * p_requests)
{
    u_char * data_ptr = NULL;

    switch ( p_requestinfo->mode )
    {
        case MODE_SET_COMMIT: 
        {
            switch ( p_requests->requestvb->type )
            {
                case ASN_INTEGER:
                //...
                {
                    data_ptr = (u_char*)p_requests->requestvb->val.integer;
                }
                break;
                case ASN_OCTET_STR:
                //...
                {
                    data_ptr = (u_char*)p_requests->requestvb->val.string;
                }
                break;
            }
            break;
        }

        default:
            break;
    }

    if (  data_ptr )
    {

    //This is likely not the place to do this but this is for example

    netsnmp_variable_list * notification_vars =  NULL;
    static const oid objid_snmptrap[] = {1,3,6,1,6,3,1,1,4,1,0};

    //you will need your own notif OID defined in your own MIB
    static const oid notification_oid[] = {1,3,6,1,4,1,8072,2,3,0,1};
    snmp_varlist_add_variable ( &notification_vars,
                                objid_snmptrap, OID_LENGTH(objid_snmptrap),
                                ASN_OBJECT_ID,
                                (u_char *) notification_oid,
                                OID_LENGTH(notification_oid) * sizeof(oid));

    //the data that changed
    snmp_varlist_add_variable ( &notification_vars,
                                p_reginfo->rootoid,p_reginfo->rootoid_len,
                                p_requests->requestvb->type,
                                data_ptr, p_requests->requestvb->val_len);

    //send the trap is now one line ( + void return )
    send_v2trap(notification_vars);

    snmp_free_varbind(notification_vars);

    }

    return SNMPERR_SUCCESS;
}

有net-snmp-config实用程序,可让我们编译代理(请参阅Net-SNMP教程)

  

[nils @ localhost trapMCVE] $ net-snmp-config --compile-subagent mysubagent --norm watched.c

generating the temporary code file: netsnmptmp.24494.c
void init_watched(void);
checking for init_watched in watched.c
void init_watched_string(void);
void init_watched(void)
init_watched_string();
void init_watched_string(void)
checking for shutdown_watched in watched.c
running: gcc  -fno-strict-aliasing -g -O2 -Ulinux -Dlinux=linux  -I. -I/usr/local/include -o mysubagent netsnmptmp.24494.c  watched.c  -L/usr/local/lib -lnetsnmpmibs -lnetsnmpagent -lnetsnmp -lnetsnmpmibs -ldl  -lnetsnmpagent   -lnetsnmp  
leaving the temporary code file: netsnmptmp.24494.c
subagent program mysubagent created

然后我们可以使用其本地conf文件启动 SNMP守护程序

#likely not a best practise but for example only
rwcommunity public localhost
#inform Request
#informsink localhost:16200
trapsess -Ci -v 2c -c private localhost:16200

,然后启动它,以便它侦听localhost和端口1161上的传入SNMP请求(随机选择,以便> 1024且无特权)。陷阱将通过端口16200发送到localhost(随机...)

  

[nils @ localhost trapMCVE] $ snmpd -f -Lo -C -c local_snmpd.conf --master = agentx --agentXSocket = tcp:localhost:1705 udp:localhost:1161

我们启动我们的子代理,该子代理通过端口1705上的tcp套接字(随机...)与SNMP守护程序通信。

  

./ mysubagent -f -Lo -x tcp:localhost:1705

这时,我们还可以为 SNMP TRAP守护程序

定义本地配置文件。
#likely not a best practise but for example only
authCommunity log,execute,net private

然后我们开始:

  

snmptrapd -f -Lo -C -c local_snmptrapd.conf本地主机:16200

所以回到测试我们的子代理,我们可以尝试 snmpget

[nils@localhost trapMCVE]$ snmpget -v 2c -c public localhost:1161 NET-SNMP-EXAMPLES-MIB::netSnmpExampleString.0
NET-SNMP-EXAMPLES-MIB::netSnmpExampleString.0 = STRING: So long, and thanks for all the fish!

,现在我们要修改此字符串,并查看是否生成了陷阱/通知。因此,我们进行了 snmpset

[nils@localhost trapMCVE]$ snmpset -v 2c -c public localhost:1161 NET-SNMP-EXAMPLES-MIB::netSnmpExampleString.0 s "Hello world: 42"
NET-SNMP-EXAMPLES-MIB::netSnmpExampleString.0 = STRING: Hello world: 42

并奇迹般地被困过程

2019-02-16 01:49:10 localhost [UDP: [127.0.0.1]:45864->[127.0.0.1]:16200]:
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (54342) 0:09:03.42 SNMPv2- 
MIB::snmpTrapOID.0 = OID: NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatNotification     
NET-SNMP-EXAMPLES-MIB::netSnmpExampleString.0 = STRING: Hello world: 42

VOILA !!!

咆哮的结尾...

希望有帮助