我做了一个陷阱接收器应用程序。我只是以下一种方式修改了snmptrapd_handler:
int print_handler( netsnmp_pdu *pdu,
netsnmp_transport *transport,
netsnmp_trapd_handler *handler)
{
int iii= ems_flush_traps(pdu,transport);
return NETSNMPTRAPD_HANDLER_OK;
}
int ems_flush_traps(netsnmp_pdu *pdu, netsnmp_transport *transport) {
display(pdu,transport);
return 0;
}
int display(netsnmp_pdu *pdu, netsnmp_transport *transport) {
oid OID1[]={1,3,6,1,4,1,24024,200,200,1};
oid OID2[]={1,3,6,1,4,1,24024,200,200,2};
oid OID4[]={1,3,6,1,4,1,24024,200,200,4};
netsnmp_variable_list *vars;
char alarmaName[40];
char dateIn[40];
//long id[40];
long idSeverity[40];
char strIdSeverity[2];
char strTime[20];
char clean[] = "";
netsnmp_pdu * internal_pdu;
internal_pdu = snmp_clone_pdu(pdu);
int ii=0;
char stringBuffer[20];
for (vars = internal_pdu->variables; vars != NULL; vars = vars->next_variable){
// Alarm name
if(memcmp(OID1, vars->name_loc, sizeof(OID1))==0){
memcpy (alarmaName, vars->val.string, strlen(vars->val.string)+1);
printf("alarm Name: %s\n",alarmaName);
printf("alarm Name: %s\n",vars->val.string);
//memcpy ( vars->val.string, clean, strlen(clean)+1 );
//vars->val.string=NULL;
}
if(memcmp(OID2, vars->name_loc, sizeof(OID2))==0){
memcpy (idSeverity, vars->val.integer, sizeof(vars->val.integer) );
sprintf(strIdSeverity, "%lu", idSeverity[0]);
printf("Severity: %s\n",strIdSeverity);
printf("Severity: %lu\n",vars->val.integer[0]);
}
if(memcmp(OID4, vars->name_loc, sizeof(OID4))==0){
memcpy ( dateIn , vars->val.string, strlen(vars->val.string)+1 );
printf("Date: %s\n",dateIn);
printf("Date: %s\n",vars->val.string);
//memcpy ( vars->val.string, clean, strlen(clean)+1 );
//vars->val.string=NULL;
}
//strcpy(vars->val.string, "");
}
free(vars);
snmp_free_pdu( internal_pdu );
return 0;
}
使用下一个命令行我多次执行该程序:
$ sudo snmptrapd -f -Le -F "%v\n"
$ sudo snmptrap -v 2c -c public 192.168.0.40 "" 1.3.6.1.4.1.24024.200.200.1 1.3.6.1.4.1.24024.200.200.1 s latenciawan 1.3.6.1.4.1.24024.200.200.2 i 2 1.3.6.1.4.1.24024.200.200.3 s MINOR 1.3.6.1.4.1.24024.200.200.4 s "2013/02/18 15:36:00"
结果是:
alarm Name: latenciawan
alarm Name: latenciawan
Severity: 2
Severity: 2
Date: 2013/02/18 15:36:00
Date: 2013/02/18 15:36:00
在几次执行后我有一些错误的结果:
alarm Name: latenciawan15:36:00
alarm Name: latenciawan15:36:00
Severity: 2
Severity: 2
Date: 2013/02/18 15:36:00
Date: 2013/02/18 15:36:00