Skip to content

Instantly share code, notes, and snippets.

@kzk
Created September 15, 2009 08:21
Show Gist options
  • Save kzk/187180 to your computer and use it in GitHub Desktop.
Save kzk/187180 to your computer and use it in GitHub Desktop.
#include <string.h>
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/types.h>
#include <net-snmp/library/snmp_api.h>
#include <net-snmp/library/snmp_transport.h>
#include <net-snmp/library/snmp_client.h>
#include <net-snmp/library/system.h>
#include <string>
using namespace std;
int
snmp_input(int operation,
netsnmp_session * session,
int reqid, netsnmp_pdu *pdu, void *magic)
{
return 1;
}
oid objid_sysuptime[] = { 1, 3, 6, 1, 2, 1, 1, 3, 0 };
oid objid_snmptrap[] = { 1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0 };
int
main()
{
string peername = "localhost";
string oid = ".1.3.6.1.4.1.8072.99999";
string community = "trapprivate";
netsnmp_session session;
snmp_sess_init(&session);
session.peername = (char*)peername.c_str();
session.community = (u_char*)community.c_str();
session.community_len = community.size();
session.version = SNMP_VERSION_2c;
session.callback = snmp_input;
session.callback_magic = NULL;
netsnmp_session *ss = snmp_add(&session,
netsnmp_transport_open_client("snmptrap", session.peername),
NULL, NULL);
if (ss == NULL) {
snmp_sess_perror("snmptrap", &session);
return -1;
}
netsnmp_pdu *pdu = snmp_pdu_create(SNMP_MSG_TRAP2);
if (pdu == NULL) {
return -1;
}
char csysuptime[20];
sprintf(csysuptime, "%ld", get_uptime());
snmp_add_var(pdu, objid_sysuptime,
sizeof(objid_sysuptime) / sizeof(oid), 't',
csysuptime);
snmp_add_var(pdu, objid_snmptrap,
sizeof(objid_snmptrap) / sizeof(oid), 'o',
oid.c_str());
int status = snmp_send(ss, pdu);
fprintf(stderr, "status = %d\n", status);
snmp_close(ss);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment