Вопрос

I'm trying to create a simple SNMP trap generator program so that I can understand the Net-SNMP library better. With the following code I keep getting the error

snmptrap: Configuration data found but the transport can't be configured

Session did not open

#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>

#include "snmptrap_simple.h"

int main (int argc, char **argv) {
  oid             objid_enterprise[] = { 1, 3, 6, 1, 4, 1, 3, 1, 1 };
  oid             objid_sysdescr[] = { 1, 3, 6, 1, 2, 1, 1, 1, 0 };
  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 };

  netsnmp_session session, *ss;
  netsnmp_pdu    *pdu, *response;
  oid             name[MAX_OID_LEN];
  size_t          name_length;
  int             arg;
  int             status;
  char           *trap = NULL;
  char           *prognam;
  int             exitval = 0;

  session.version       = SNMP_VERSION_1;
  session.retries       = 2;
  char address[]        = "127.0.0.1";
  char *ptrAddress      = address;
  session.peername      = ptrAddress;
  u_char comm[]         = "public";
  const u_char *community = comm;
  session.community     = community;
  session.community_len = strlen(session.community);

  /* windows32 specific initialization (is a noop on unix) */
  SOCK_STARTUP;
  // open snmp session
  ss = snmp_open(&session);
  if (!ss) {
    snmp_perror("ack");
    snmp_sess_perror("snmptrap", &session);
    snmp_log(LOG_DEBUG, "Session did not open\n");
    exit(2);
  }
  snmp_log(LOG_DEBUG, "DEBUG OUTPUT");
  // create the PDU
  pdu = snmp_pdu_create(SNMP_MSG_TRAP);
  if ( !pdu ) {
      fprintf(stderr, "Failed to create trap PDU\n");
      SOCK_CLEANUP;
      exit(1);
  }

  // add variables to the PDU
  char *value;
  *value = '42';
  if(snmp_add_var (pdu, name, name_length, 'i', value)){
    //void snmp_perror(const char *prog_string)
    snmp_perror("add variable");
    SOCK_CLEANUP;
    exit(1);
  };

  send_trap_to_sess (&session, pdu);

  return 0;
}

What am I doing wrong?

I have a github repo of the code here: https://github.com/EhevuTov/tcp2snmp/blob/master/test/snmptrap_simple.c

Это было полезно?

Решение

You probably just need to call init_snmp("snmptrap_simple"); at the top of the code before the snmp_open().

Другие советы

You need to use this

snmp_sess_init(&ss);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top