,---- [Bojan Andonoski wrote:]
| Hello
|
| Can anyone send me the source code how to send SNMP Trap from an SNMP Agent
| to the Manager(Client). I need only the code for sending a SNMP Trap from
| the Agent
|
| Bojan
`----
Some time back, i needed a trap code, I just wrote one for me, may be this you
want. One is trap sender another is receiver.
Attached: trap.c
--
Thanks
Sharad Chandra
#include <net-snmp/net-snmp-config.h>
#include <string.h>
#include <net-snmp/net-snmp-includes.h>
/*
* gcc -I /usr/local/include/ -L/usr/lib -lutil -lcrypto -L/usr/local/lib
* -lnetsnmp -o trap trap.c
*/
oid objid_snmptrap[] = {1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0};
//char *snmptrap_t = ".1.3.6.3.1.1.4.1.0";
oid objid_sysuptime[] = {1, 3, 6, 1, 2, 1, 1, 3, 0};
int
snmp_input(int operation, netsnmp_session * session, int reqid, netsnmp_pdu * pdu, void *magic)
{
return 1;
}
static void
optProc(int argc, char *const *argv, int opt)
{
switch (opt) {
case 'C':
while (*optarg) {
switch (*optarg++) {
case 'i':
//inform = 1;
break;
default:
fprintf(stderr,
"Unknown flag passed to -C: %c\n", optarg[-1]);
exit(1);
}
}
break;
}
}
int
snd_trap(char *community, char *ip, char *msg)
{
char *argv[10];
int argc = 10, arg;
netsnmp_session session, *ss;
netsnmp_pdu *pdu, *response;
long sysuptime;
char csysuptime[20];
oid my_trap [] = {1, 3, 6, 1, 4, 1, 5713, 3, 10, 10, 1000, 20, 1};
char *my_trap_t = ".1.3.6.1.4.1.5713.3.10.10.1000.20.1";
oid objid_name [] = {1, 3, 6, 1, 4, 1, 5713, 3, 10, 10, 40};
char *objid_name_t = ".1.3.6.1.4.1.5713.3.10.10.40";
char *trap = NULL;
int name_len = 11;
//128;
//max oid len
int error = 0;
argv[0] = "Your Trap";
argv[1] = "-v2c";
argv[2] = "-c";
argv[3] = strdup(community);
argv[4] = strdup(ip);
argv[5] = "";
argv[6] = objid_name_t;
argv[7] = my_trap_t;
argv[8] = "s";
argv[9] = strdup(msg);
if (!(community && ip && msg))
return 0;
switch (arg = snmp_parse_args(argc, argv, &session, "C:", optProc)) {
case -2:
case -1:
return 1;
default:;
}
netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DEFAULT_PORT, SNMP_TRAP_PORT);
session.callback = snmp_input;
session.callback_magic = NULL;
ss = snmp_open(&session);
if (ss == NULL) {
return 1;
}
pdu = snmp_pdu_create(SNMP_MSG_TRAP2);
/*
* pdu_in_addr_t = (in_addr_t *) pdu->agent_addr;
* memcpy(pdu->enterprise, objid_snmptrap, sizeof(objid_snmptrap));
* pdu->enterprise_length = sizeof(objid_snmptrap) / sizeof(oid);
* pdu_in_addr_t = parse_address(ip);
*/
sysuptime = get_uptime();
sprintf(csysuptime, "%ld", sysuptime);
trap = csysuptime;
snmp_add_var(pdu, objid_sysuptime, sizeof(objid_sysuptime) / sizeof(oid), 't', trap);
arg++;
if (snmp_add_var(pdu, objid_snmptrap, sizeof(objid_snmptrap) / sizeof(oid), 'o', objid_name_t) != 0) {
return 1;
}
arg++;
if (snmp_add_var(pdu, my_trap, 13, 's', msg) != 0) {
//name len = 13
return 1;
}
if (snmp_send(ss, pdu) == 0)
error = 1;
snmp_close(ss);
return error == 1;
}
int
main()
{
snd_trap("public", "10.60.50.68", "HI TESTING");
}
-------------------------------------------------------------------------
SF.Net email is sponsored by:
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Net-snmp-users mailing list
[email protected]
Please see the following page to unsubscribe or change other options:
https://lists.sourceforge.net/lists/listinfo/net-snmp-users