Re: SNMP Trap

This is a discussion on Re: SNMP Trap within the SNMP Users forums, part of the Networking and Network Related category; --Boundary-00=_KGjXHcxXaCA13Yb Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline ,---- [...


Go Back   Usenet Forums > Networking and Network Related > SNMP Users

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-11-2007
Sharad Chandra
 
Posts: n/a
Default Re: SNMP Trap

--Boundary-00=_KGjXHcxXaCA13Yb
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

,---- [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

--Boundary-00=_KGjXHcxXaCA13Yb
Content-Type: text/x-csrc;
charset="iso-8859-1";
name="trap.c"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="trap.c"


#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");
}

--Boundary-00=_KGjXHcxXaCA13Yb
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

-------------------------------------------------------------------------
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
--Boundary-00=_KGjXHcxXaCA13Yb
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Net-snmp-users mailing list
Net-snmp-users@lists.sourceforge.net
Please see the following page to unsubscribe or change other options:
https://lists.sourceforge.net/lists/...net-snmp-users

--Boundary-00=_KGjXHcxXaCA13Yb--

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 08:34 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0