Guest User

Untitled

a guest
Apr 16th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. #include <string.h>
  2. #include <net-snmp/net-snmp-config.h>
  3. #include <net-snmp/types.h>
  4. #include <net-snmp/library/snmp_api.h>
  5. #include <net-snmp/library/snmp_transport.h>
  6. #include <net-snmp/library/snmp_client.h>
  7. #include <net-snmp/library/system.h>
  8.  
  9. #include <string>
  10. using namespace std;
  11.  
  12. int
  13. snmp_input(int operation,
  14. netsnmp_session * session,
  15. int reqid, netsnmp_pdu *pdu, void *magic)
  16. {
  17. return 1;
  18. }
  19.  
  20. oid objid_sysuptime[] = { 1, 3, 6, 1, 2, 1, 1, 3, 0 };
  21. oid objid_snmptrap[] = { 1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0 };
  22.  
  23. int
  24. main()
  25. {
  26. string peername = "localhost";
  27. string oid = ".1.3.6.1.4.1.8072.99999";
  28. string community = "trapprivate";
  29.  
  30. netsnmp_session session;
  31. snmp_sess_init(&session);
  32. session.peername = (char*)peername.c_str();
  33. session.community = (u_char*)community.c_str();
  34. session.community_len = community.size();
  35. session.version = SNMP_VERSION_2c;
  36. session.callback = snmp_input;
  37. session.callback_magic = NULL;
  38.  
  39. netsnmp_session *ss = snmp_add(&session,
  40. netsnmp_transport_open_client("snmptrap", session.peername),
  41. NULL, NULL);
  42. if (ss == NULL) {
  43. snmp_sess_perror("snmptrap", &session);
  44. return -1;
  45. }
  46.  
  47. netsnmp_pdu *pdu = snmp_pdu_create(SNMP_MSG_TRAP2);
  48. if (pdu == NULL) {
  49. return -1;
  50. }
  51. char csysuptime[20];
  52. sprintf(csysuptime, "%ld", get_uptime());
  53. snmp_add_var(pdu, objid_sysuptime,
  54. sizeof(objid_sysuptime) / sizeof(oid), 't',
  55. csysuptime);
  56. snmp_add_var(pdu, objid_snmptrap,
  57. sizeof(objid_snmptrap) / sizeof(oid), 'o',
  58. oid.c_str());
  59.  
  60. int status = snmp_send(ss, pdu);
  61. fprintf(stderr, "status = %d\n", status);
  62.  
  63. snmp_close(ss);
  64. }
Add Comment
Please, Sign In to add comment