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

问题描述 投票:1回答:1

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

snmp net-snmp snmp-trap snmpd snmptrapd
1个回答
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和unpriviledge)。陷阱将通过端口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 localhost: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

并通过奇迹对snmptrapd进程

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

咆哮结束......

希望能帮助到你

© www.soinside.com 2019 - 2024. All rights reserved.