/**
 * The 'raw' SNMP set function. It should not be used directly in external
 * files. Rather, a non-static wrapper function should be created here that
 * makes the appropriate call in order to preserve source code readability.
 *
 * @return 1 if successful, 0 if not
 */
static int snmp_set(netsnmp_session *s, char *oid_str, char type, const char *val)
{
	netsnmp_pdu *pdu;
	oid the_oid[MAX_OID_LEN];
	size_t oid_len;

	pdu = snmp_pdu_create(SNMP_MSG_SET);
	oid_len = MAX_OID_LEN;

	// Parse the OID
	if (snmp_parse_oid(oid_str, the_oid, &oid_len) == 0) {
		snmp_perror(oid_str);
		return 0;
	}

	// Build the packet to be sent
	if (snmp_add_var(pdu, the_oid, oid_len, type, val) != 0) {
		printf("type: %c, val: %s, oid_str: %s\n", type, val, oid_str);
		snmp_perror("SNMP: Could not add var!");
		return 0;
	}

	// Send the request
	if (snmp_send(s, pdu) == 0) {
		snmp_perror("SNMP: Error while sending!");
		snmp_free_pdu(pdu);
		return 0;
	}

	return 1;
}
int set_snmp(void *precord, const char *sval)
{
  snmpRecord *psnmp = (snmpRecord *)precord;
  SNMP_INFO *snmpinfo = (SNMP_INFO*)psnmp->dpvt;

  char type = 'i';

  if (!(snmpinfo->sess = snmp_open(&snmpinfo->ss))) {
    snmp_perror("snmp_open");
    return -1;
  };

  snmpinfo->setreq = snmp_pdu_create(SNMP_MSG_SET);	/* send the first GET */
  if (! snmpinfo->setreq) {
    snmp_close(snmpinfo->sess);                 /* cleanup */
    return -1;
  }

  snmp_add_var(snmpinfo->setreq, snmpinfo->oid_info.Oid, snmpinfo->oid_info.OidLen, type, sval);       //snmp_add_var(netsnmp_pdu *, const oid *, size_t, char, const char *)

  if (snmp_send(snmpinfo->sess, snmpinfo->setreq))
    hosts++;
  else {
    snmp_perror("snmp_getsend");
    snmp_free_pdu(snmpinfo->setreq);
    return -1;
  }

  active_hosts();
  snmp_close(snmpinfo->sess);                 /* cleanup */
  return 0;
}
void get_snmp(void *precord)
{
  snmpRecord *psnmp = (snmpRecord *)precord;
  SNMP_INFO *snmpinfo = (SNMP_INFO*)psnmp->dpvt;

  if (!(snmpinfo->sess = snmp_open(&snmpinfo->ss))) {
    snmp_perror("snmp_open");
  };

  snmpinfo->getreq = snmp_pdu_create(SNMP_MSG_GET);	/* send the first GET */
  if (! snmpinfo->getreq) {
    snmp_close(snmpinfo->sess);                 /* cleanup */
  }

  snmp_add_null_var(snmpinfo->getreq, snmpinfo->oid_info.Oid, snmpinfo->oid_info.OidLen);

  if (snmp_send(snmpinfo->sess, snmpinfo->getreq))
    hosts++;
  else {
    snmp_perror("snmp_setsend");
    snmp_free_pdu(snmpinfo->getreq);
  }

  active_hosts();
  snmp_close(snmpinfo->sess);                 /* cleanup */
}
/* 
 * This routine loads MIB files, and computes the key-OID values.
 * We defer this until the mib-definition is actually being referred to 
 * in snmphosts.cfg, because lots of MIB's are not being used
 * (and probably do not exist on the host where we're running) and
 * to avoid spending a lot of time to load MIB's that are not used.
 */
void setupmib(mibdef_t *mib, int verbose)
{
	mibidx_t *iwalk;
	size_t sz, len;

	if (mib->loadstatus != MIB_STATUS_NOTLOADED) return;

	if (mib->mibfn && (read_mib(mib->mibfn) == NULL)) {
		mib->loadstatus = MIB_STATUS_LOADFAILED;
		if (verbose) {
			errprintf("Failed to read MIB file %s\n", mib->mibfn);
			snmp_perror("read_objid");
		}
	}

	for (iwalk = mib->idxlist; (iwalk); iwalk = iwalk->next) {
		iwalk->rootoid = calloc(MAX_OID_LEN, sizeof(oid));
		iwalk->rootoidlen = MAX_OID_LEN;
		if (read_objid(iwalk->keyoid, iwalk->rootoid, &iwalk->rootoidlen)) {
			/* Re-use the iwalk->keyoid buffer */
			sz = strlen(iwalk->keyoid) + 1; len = 0;
			sprint_realloc_objid((unsigned char **)&iwalk->keyoid, &sz, &len, 1, iwalk->rootoid, iwalk->rootoidlen);
		}
		else {
			mib->loadstatus = MIB_STATUS_LOADFAILED;
			if (verbose) {
				errprintf("Cannot determine OID for %s\n", iwalk->keyoid);
				snmp_perror("read_objid");
			}
		}
	}

	mib->loadstatus = MIB_STATUS_LOADED;
}
Example #5
0
void asynchronous(void)
{
  struct session *hs;
  struct host *hp;

  /* startup all hosts */

  for (hs = sessions, hp = hosts; hp->name; hs++, hp++) {
    struct snmp_pdu *req;
    struct snmp_session sess;
    snmp_sess_init(&sess);			/* initialize session */
    sess.version = SNMP_VERSION_2c;
    sess.peername = strdup(hp->name);
    sess.community = strdup(hp->community);
    sess.community_len = strlen(sess.community);
    sess.callback = asynch_response;		/* default callback */
    sess.callback_magic = hs;
    if (!(hs->sess = snmp_open(&sess))) {
      snmp_perror("snmp_open");
      continue;
    }
    hs->current_oid = oids;
    req = snmp_pdu_create(SNMP_MSG_GET);	/* send the first GET */
    snmp_add_null_var(req, hs->current_oid->Oid, hs->current_oid->OidLen);
    if (snmp_send(hs->sess, req))
      active_hosts++;
    else {
      snmp_perror("snmp_send");
      snmp_free_pdu(req);
    }
  }

  /* loop while any active hosts */

  while (active_hosts) {
    int fds = 0, block = 1;
    fd_set fdset;
    struct timeval timeout;

    FD_ZERO(&fdset);
    snmp_select_info(&fds, &fdset, &timeout, &block);
    fds = select(fds, &fdset, NULL, NULL, block ? NULL : &timeout);
    if (fds < 0) {
        perror("select failed");
        exit(1);
    }
    if (fds)
        snmp_read(&fdset);
    else
        snmp_timeout();
  }

  /* cleanup */

  for (hp = hosts, hs = sessions; hp->name; hs++, hp++) {
    if (hs->sess) snmp_close(hs->sess);
  }
}
Example #6
0
int
sh_count_regexp_procs(char *procname, pcre *regexp)
{
    int             fd, total = 0;
    struct psinfo   info;
    char            fbuf[32];
    struct dirent  *ent;
    DIR            *dir;

    if (!(dir = opendir("/proc"))) {
        snmp_perror("/proc");
        return -1;
    }

    while ((ent = readdir(dir))) {
        if (!strcmp(ent->d_name, "..") || !strcmp(ent->d_name, "."))
            continue;

        snprintf(fbuf, sizeof fbuf, "/proc/%s/psinfo", ent->d_name);
        if ((fd = open(fbuf, O_RDONLY)) < 0) {  /* Continue or return error? */
            snmp_perror(fbuf);
	    continue;
        }

        if (read(fd, (char *) &info, sizeof(struct psinfo)) !=
            sizeof(struct psinfo)) {
            snmp_perror(fbuf);
            close(fd);
            closedir(dir);
            return -1;
        }

        if (!info.pr_nlwp && !info.pr_lwp.pr_lwpid) {
            /*
             * Zombie process 
             */
        } else {
            DEBUGMSGTL(("proc","Comparing wanted %s against %s\n",
                        procname, info.pr_fname));
            if (!strcmp(procname, info.pr_fname)) {
                total++;
                DEBUGMSGTL(("proc", " Matched.  total count now=%d\n", total));
            }
        }

        close(fd);
    }
    closedir(dir);
    return total;
}
Example #7
0
void
send_trap_pdu(struct snmp_pdu *pdu)
{
  struct snmp_pdu *mypdu;
  
  struct trap_sink *sink = sinks;

  if ((snmp_enableauthentraps == 1) && sink != NULL) {
    DEBUGMSGTL(("snmpd", "sending v2 trap pdu...\n"));
    while (sink) {
      if (sink->ses.version == SNMP_VERSION_2c) {
        DEBUGMSGTL(("snmpd", " found v2 session...\n"));
        mypdu = snmp_clone_pdu(pdu);
        if (snmp_send(sink->sesp, mypdu) == 0) {
          snmp_perror ("snmpd: send_trap_pdu");
        }
#ifdef USING_MIBII_SNMP_MIB_MODULE       
        snmp_outtraps++;
#endif
      }
      sink = sink->next;
    }
    DEBUGMSGTL(("snmpd", "  done\n"));
  }
}
Example #8
0
static struct snmp_pdu *
snmp_get_item(char *host, char *community, char *mib_item)
{
	struct snmp_session session, *ss;
	struct snmp_pdu *request = NULL, *result = NULL;
	oid Oid[MAX_OID_LEN];
	unsigned int oid_len = MAX_OID_LEN;

	/* initialize the SNMP session */
	snmp_sess_init(&session);
	session.peername = host;
	session.community = (uchar_t *)community;
	session.community_len = strlen((const char *)session.community);
	session.version = SNMP_VERSION_1;
	session.retries = 0;

	if ((ss = snmp_open(&session)) == NULL)
		return (NULL);

	/* add the requested data */
	if (!read_objid(mib_item, Oid, &oid_len))
		snmp_perror(mib_item);

	/* initialize the request PDU */
	request = snmp_pdu_create(SNMP_MSG_GET);
	snmp_add_null_var(request, Oid, oid_len);

	(void) snmp_synch_response(ss, request, &result);

	snmp_close(ss);

	return (result);
}
void perr(struct snmp_pdu *resp) {
    if(resp) fprintf(stderr, "error: %s\n", snmp_errstring(resp->errstat));
    snmp_perror("error");
    snmp_close(ses);
    SOCK_CLEANUP;
    exit(1);
}
Example #10
0
int
sysctl_read_icmp6_msg_stat(struct icmp6_mib *mib,
                           struct icmp6_msg_mib *msgmib,
			   int *support)
{
    struct icmp6stat icmpstat;
    size_t   size = sizeof(icmpstat);
    int      i;
    static int      sname[4] =
        { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_STATS };

    sysctl_read_icmp6_stat(mib);

    if (-1 == sysctl(sname, 4, &icmpstat, &size, NULL, 0)) {
	snmp_perror("sysctl_read_icmp6_stat: net.inet6.icmp6.stats");
        return -1;
    }

    for (i = 0; i < 256; i++) {
	msgmib->vals[i].InType = icmpstat.icp6s_inhist[i];
	msgmib->vals[i].OutType = icmpstat.icp6s_outhist[i];
    }
    *support = 1;
    return 0;
}
Example #11
0
/*
 * response handler
 */
int asynch_response(int operation, struct snmp_session *sp, int reqid,
		    struct snmp_pdu *pdu, void *magic)
{
  struct session *host = (struct session *)magic;
  struct snmp_pdu *req;

  if (operation == NETSNMP_CALLBACK_OP_RECEIVED_MESSAGE) {
    if (print_result(STAT_SUCCESS, host->sess, pdu)) {
      host->current_oid++;			/* send next GET (if any) */
      if (host->current_oid->Name) {
	req = snmp_pdu_create(SNMP_MSG_GET);
	snmp_add_null_var(req, host->current_oid->Oid, host->current_oid->OidLen);
	if (snmp_send(host->sess, req))
	  return 1;
	else {
	  snmp_perror("snmp_send");
	  snmp_free_pdu(req);
	}
      }
    }
  }
  else
    print_result(STAT_TIMEOUT, host->sess, pdu);

  /* something went wrong (or end of variables) 
   * this host not active any more
   */
  active_hosts--;
  return 1;
}
Example #12
0
/*
 * simple synchronous loop
 */
void synchronous (void)
{
  struct host *hp;

  for (hp = hosts; hp->name; hp++) {
    struct snmp_session ss, *sp;
    struct oid *op;

    snmp_sess_init(&ss);			/* initialize session */
    ss.version = SNMP_VERSION_2c;
    ss.peername = strdup(hp->name);
    ss.community = strdup(hp->community);
    ss.community_len = strlen(ss.community);
    if (!(sp = snmp_open(&ss))) {
      snmp_perror("snmp_open");
      continue;
    }
    for (op = oids; op->Name; op++) {
      struct snmp_pdu *req, *resp;
      int status;
      req = snmp_pdu_create(SNMP_MSG_GET);
      snmp_add_null_var(req, op->Oid, op->OidLen);
      status = snmp_synch_response(sp, req, &resp);
      if (!print_result(status, sp, resp)) break;
      snmp_free_pdu(resp);
    }
    snmp_close(sp);
  }
}
Example #13
0
void * snmp_poll ( void * request_details ) {
	snmp_request_t* req = (snmp_request_t*)request_details;

	struct snmp_session ss, *sp;
	struct oid *op;
 
	snmp_sess_init(&ss);                        /* initialize session */
	ss.version = SNMP_VERSION_2c;
	ss.peername = req->target_host;
	ss.community = req->community;
	ss.community_len = strlen(ss.community);
	snmp_synch_setup(&ss);
	if (!(sp = snmp_open(&ss))) 
		snmp_perror("snmp_open");
	struct snmp_pdu *req2, *resp;
	int status;
	req = snmp_pdu_create(SNMP_MSG_GET);
	snmp_add_null_var(req2, req->oid, strlen(req->oid));
	status = snmp_synch_response(sp, req2, &resp);
	snmp_free_pdu(resp);
	snmp_close(sp);

	// Push the results back to the result queue..
	pthread_mutex_lock ( &result_lock ) ;
	// TODO: create linked array for a result buffer
	pthread_mutex_unlock ( &result_lock ) ;
}
Example #14
0
int
add(netsnmp_pdu *pdu, const char *mibnodename,
    oid * index, size_t indexlen)
{
    oid             base[MAX_OID_LEN];
    size_t          base_length = MAX_OID_LEN;

    memset(base, 0, MAX_OID_LEN * sizeof(oid));

    if (!snmp_parse_oid(mibnodename, base, &base_length)) {
        snmp_perror(mibnodename);
        fprintf(stderr, "couldn't find mib node %s, giving up\n",
                mibnodename);
#if HAVE_CURSES_H
        endwin();
#endif
        exit(1);
    }

    if (index && indexlen) {
        memcpy(&(base[base_length]), index, indexlen * sizeof(oid));
        base_length += indexlen;
    }
    DEBUGMSGTL(("add", "created: "));
    DEBUGMSGOID(("add", base, base_length));
    DEBUGMSG(("add", "\n"));
    snmp_add_null_var(pdu, base, base_length);

    return base_length;
}
Example #15
0
int
netbsd_read_icmp6_stat(struct icmp6_mib *mib)
{
    uint64_t icmpstat[ICMP6_NSTATS];
    size_t   size = sizeof(icmpstat);
    int      i;

    (void)memset(mib, 0, sizeof(*mib));

    if (-1 == sysctlbyname("net.inet6.icmp6.stats", icmpstat, &size, NULL, 0)) {
	snmp_perror("netbsd_read_icmp6_stat: net.inet6.icmp6.stats");
        return -1;
    }

    mib->icmp6InMsgs = icmpstat[ICMP6_STAT_BADCODE]
            + icmpstat[ICMP_STAT_TOOSHORT]
	    + icmpstat[ICMP_STAT_CHECKSUM]
            + icmpstat[ICMP_STAT_BADLEN];
    for (i = 0; i <= ICMP6_MAXTYPE; i++)
        mib->icmp6InMsgs  += icmpstat[ICMP6_STAT_INHIST + i];
    mib->icmp6InErrors = icmpstat[ICMP6_STAT_BADCODE]
        + icmpstat[ICMP6_STAT_TOOSHORT]
        + icmpstat[ICMP6_STAT_CHECKSUM]
        + icmpstat[ICMP6_STAT_BADLEN];
    mib->icmp6InDestUnreachs = icmpstat[ICMP6_STAT_INHIST + ICMP6_DST_UNREACH];
    mib->icmp6InPktTooBigs = icmpstat[ICMP6_STAT_INHIST + ICMP6_PACKET_TOO_BIG];
    mib->icmp6InTimeExcds = icmpstat[ICMP6_STAT_INHIST + ICMP6_TIME_EXCEEDED];
    mib->icmp6InParmProblems = icmpstat[ICMP6_STAT_INHIST + ICMP6_PARAM_PROB];
    mib->icmp6InEchos = icmpstat[ICMP6_STAT_INHIST + ICMP6_ECHO_REQUEST];
    mib->icmp6InEchoReplies = icmpstat[ICMP6_STAT_INHIST + ICMP6_ECHO_REPLY];
    mib->icmp6InGroupMembQueries = icmpstat[ICMP6_STAT_INHIST + MLD_LISTENER_QUERY];
    mib->icmp6InGroupMembResponses = icmpstat[ICMP6_STAT_INHIST + MLD_LISTENER_REPORT];
    mib->icmp6InRouterSolicits = icmpstat[ICMP6_STAT_INHIST + ND_ROUTER_SOLICIT];
    mib->icmp6InRouterAdvertisements = icmpstat[ICMP6_STAT_INHIST + ND_ROUTER_ADVERT];
    mib->icmp6InNeighborSolicits = icmpstat[ICMP6_STAT_INHIST + ND_NEIGHBOR_SOLICIT];
    mib->icmp6InNeighborAdvertisements = icmpstat[ICMP6_STAT_INHIST + ND_NEIGHBOR_ADVERT];
    mib->icmp6InRedirects = icmpstat[ICMP6_STAT_INHIST + ND_REDIRECT];

    mib->icmp6OutMsgs = icmpstat[ICMP6_STAT_BADCODE]
        + icmpstat[ICMP6_STAT_TOOSHORT]
        + icmpstat[ICMP6_STAT_CHECKSUM]
        + icmpstat[ICMP6_STAT_BADLEN];
    for (i = 0; i <= ICMP6_MAXTYPE; i++)
        mib->icmp6OutMsgs += icmpstat[ICMP6_STAT_OUTHIST + i];
    mib->icmp6OutDestUnreachs = icmpstat[ICMP6_STAT_OUTHIST + ICMP6_DST_UNREACH];
    mib->icmp6OutPktTooBigs =  icmpstat[ICMP6_STAT_OUTHIST + ICMP6_PACKET_TOO_BIG];
    mib->icmp6OutTimeExcds = icmpstat[ICMP6_STAT_OUTHIST + ICMP6_TIME_EXCEEDED];
    mib->icmp6OutParmProblems = icmpstat[ICMP6_STAT_OUTHIST + ICMP6_PARAM_PROB];
    mib->icmp6OutEchos = icmpstat[ICMP6_STAT_OUTHIST + ICMP6_ECHO_REQUEST];
    mib->icmp6OutEchoReplies = icmpstat[ICMP6_STAT_OUTHIST + ICMP6_ECHO_REPLY];
    mib->icmp6OutRouterSolicits =  icmpstat[ICMP6_STAT_OUTHIST + ND_ROUTER_SOLICIT];
    mib->icmp6OutNeighborSolicits =  icmpstat[ICMP6_STAT_OUTHIST + ND_NEIGHBOR_SOLICIT];
    mib->icmp6OutNeighborAdvertisements =  icmpstat[ICMP6_STAT_OUTHIST + ND_NEIGHBOR_ADVERT];
    mib->icmp6OutRedirects = icmpstat[ICMP6_STAT_OUTHIST + ND_REDIRECT];
    mib->icmp6OutGroupMembResponses =  icmpstat[ICMP6_STAT_OUTHIST + MLD_LISTENER_REPORT];
    mib->icmp6OutGroupMembReductions =  icmpstat[ICMP6_STAT_OUTHIST + MLD_LISTENER_DONE];

    return 0;
}
void snmp_get(void *precord)
{
  snmpRecord *psnmp = (snmpRecord *)precord;
  /* SNMP_INFO *gsnmpInfo = (SNMP_INFO*)psnmp->dpvt;       */

  printf("snmpget()****** Message:%s, version: %s, ip: %s, name: %s, securityname: %s, authpass: %s, privpass: %s, secuauth: %s, secupriv: %s\n", snmpinfo->msg, snmpinfo->ss.version, snmpinfo->ss.peername, snmpinfo->username, snmpinfo->ss.securityName, snmpinfo->authpass, snmpinfo->privpass, snmpinfo->ss.securityAuthKey, snmpinfo->ss.securityPrivKey);

  printf("oids: %s\n", psnmp->oids);

  if (!(snmpinfo->sess = snmp_open(&snmpinfo->ss))) {
    snmp_perror("snmp_open");
  };

  snmpinfo->getreq = snmp_pdu_create(SNMP_MSG_GET);	/* send the first GET */
  if (! snmpinfo->getreq) {
    snmp_close(snmpinfo->sess);                 /* cleanup */
  }

  snmp_add_null_var(snmpinfo->getreq, snmpinfo->oid_info.Oid, snmpinfo->oid_info.OidLen);

/* int snmp_async_send(netsnmp_session *, netsnmp_pdu *, netsnmp_callback, void *) */
/* int snmp_send(netsnmp_session *, netsnmp_pdu *) */


  /* if (snmp_async_send(snmpinfo->sess, snmpinfo->getreq, asynch_response, NULL))   */
  /*   { */
  /*     hosts++; */
  /*   } else { */
  /*   snmp_perror("snmp_get->async_Send Error "); */
  /*   snmp_free_pdu(snmpinfo->getreq); */
  /* } */


  if (snmp_send(snmpinfo->sess, snmpinfo->getreq))
    {
      hosts++;
    } else {
    snmp_perror("snmp_get->Send Error ");
    snmp_free_pdu(snmpinfo->getreq);
  }



  active_hosts();
  snmp_close(snmpinfo->sess);                 /* cleanup */
}
Example #17
0
int
netbsd_read_icmp_stat(struct icmp_mib *mib)
{
    uint64_t icmpstat[ICMP_NSTATS];
    size_t   size = sizeof(icmpstat);
    int      i;

    (void)memset(mib, 0, sizeof(*mib));

    if (-1 == sysctlbyname("net.inet.icmp.stats", icmpstat, &size, NULL, 0)) {
	snmp_perror("netbsd_read_icmp_stat: net.inet.icmp.stats");
        return -1;
    }

    mib->icmpInMsgs = icmpstat[ICMP_STAT_BADCODE]
        + icmpstat[ICMP_STAT_TOOSHORT]
        + icmpstat[ICMP_STAT_CHECKSUM]
        + icmpstat[ICMP_STAT_BADLEN];
    for (i = 0; i <= ICMP_MAXTYPE; i++)
        mib->icmpInMsgs  += icmpstat[ICMP_STAT_INHIST + i];
    mib->icmpInErrors = icmpstat[ICMP_STAT_BADCODE]
        + icmpstat[ICMP_STAT_TOOSHORT]
        + icmpstat[ICMP_STAT_CHECKSUM]
        + icmpstat[ICMP_STAT_BADLEN];
    mib->icmpInDestUnreachs = icmpstat[ICMP_STAT_INHIST + ICMP_UNREACH];
    mib->icmpInTimeExcds = icmpstat[ICMP_STAT_INHIST + ICMP_TIMXCEED];
    mib->icmpInParmProbs = icmpstat[ICMP_STAT_INHIST + ICMP_PARAMPROB];
    mib->icmpInSrcQuenchs = icmpstat[ICMP_STAT_INHIST + ICMP_SOURCEQUENCH];
    mib->icmpInRedirects = icmpstat[ICMP_STAT_INHIST + ICMP_REDIRECT];
    mib->icmpInEchos = icmpstat[ICMP_STAT_INHIST + ICMP_ECHO];
    mib->icmpInEchoReps = icmpstat[ICMP_STAT_INHIST + ICMP_ECHOREPLY];
    mib->icmpInTimestamps = icmpstat[ICMP_STAT_INHIST + ICMP_TSTAMP];
    mib->icmpInTimestampReps
        = icmpstat[ICMP_STAT_INHIST + ICMP_TSTAMPREPLY];
    mib->icmpInAddrMasks = icmpstat[ICMP_STAT_INHIST + ICMP_MASKREQ];
    mib->icmpInAddrMaskReps = icmpstat[ICMP_STAT_INHIST + ICMP_MASKREPLY];
    mib->icmpOutMsgs = icmpstat[ICMP_STAT_OLDSHORT]
        + icmpstat[ICMP_STAT_OLDICMP];
    for (i = 0; i <= ICMP_MAXTYPE; i++)
        mib->icmpOutMsgs += icmpstat[ICMP_STAT_OUTHIST + i];
    mib->icmpOutErrors = icmpstat[ICMP_STAT_OLDSHORT]
        + icmpstat[ICMP_STAT_OLDICMP];
    mib->icmpOutDestUnreachs = icmpstat[ICMP_STAT_OUTHIST + ICMP_UNREACH];
    mib->icmpOutTimeExcds = icmpstat[ICMP_STAT_OUTHIST + ICMP_TIMXCEED];
    mib->icmpOutParmProbs = icmpstat[ICMP_STAT_OUTHIST + ICMP_PARAMPROB];
    mib->icmpOutSrcQuenchs
        = icmpstat[ICMP_STAT_OUTHIST + ICMP_SOURCEQUENCH];
    mib->icmpOutRedirects = icmpstat[ICMP_STAT_OUTHIST + ICMP_REDIRECT];
    mib->icmpOutEchos = icmpstat[ICMP_STAT_OUTHIST + ICMP_ECHO];
    mib->icmpOutEchoReps = icmpstat[ICMP_STAT_OUTHIST + ICMP_ECHOREPLY];
    mib->icmpOutTimestamps = icmpstat[ICMP_STAT_OUTHIST + ICMP_TSTAMP];
    mib->icmpOutTimestampReps
        = icmpstat[ICMP_STAT_OUTHIST + ICMP_TSTAMPREPLY];
    mib->icmpOutAddrMasks = icmpstat[ICMP_STAT_OUTHIST + ICMP_MASKREQ];
    mib->icmpOutAddrMaskReps = icmpstat[ICMP_STAT_OUTHIST + ICMP_MASKREPLY];

    return 0;
}
Example #18
0
int
sysctl_read_icmp6_stat(struct icmp6_mib *mib)
{
    struct icmp6stat icmpstat;
    size_t   size = sizeof(icmpstat);
    int      i;
    static int      sname[4] =
        { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_STATS };

    (void)memset(mib, 0, sizeof(*mib));

    if (-1 == sysctl(sname, 4, &icmpstat, &size, NULL, 0)) {
	snmp_perror("sysctl_read_icmp6_stat: net.inet6.icmp6.stats");
        return -1;
    }

    mib->icmp6InMsgs = icmpstat.icp6s_badcode
            + icmpstat.icp6s_tooshort
	    + icmpstat.icp6s_checksum
            + icmpstat.icp6s_badlen;
    mib->icmp6InErrors = mib->icmp6InMsgs;
    for (i = 0; i <= ICMP6_MAXTYPE; i++)
        mib->icmp6InMsgs  += icmpstat.icp6s_inhist[i];
    mib->icmp6InDestUnreachs = icmpstat.icp6s_inhist[ICMP6_DST_UNREACH];
    mib->icmp6InPktTooBigs = icmpstat.icp6s_inhist[ICMP6_PACKET_TOO_BIG];
    mib->icmp6InTimeExcds = icmpstat.icp6s_inhist[ICMP6_TIME_EXCEEDED];
    mib->icmp6InParmProblems = icmpstat.icp6s_inhist[ICMP6_PARAM_PROB];
    mib->icmp6InEchos = icmpstat.icp6s_inhist[ICMP6_ECHO_REQUEST];
    mib->icmp6InEchoReplies = icmpstat.icp6s_inhist[ICMP6_ECHO_REPLY];
    mib->icmp6InGroupMembQueries = icmpstat.icp6s_inhist[MLD_LISTENER_QUERY];
    mib->icmp6InGroupMembResponses = icmpstat.icp6s_inhist[MLD_LISTENER_REPORT];
    mib->icmp6InRouterSolicits = icmpstat.icp6s_inhist[ND_ROUTER_SOLICIT];
    mib->icmp6InRouterAdvertisements = icmpstat.icp6s_inhist[ND_ROUTER_ADVERT];
    mib->icmp6InNeighborSolicits = icmpstat.icp6s_inhist[ND_NEIGHBOR_SOLICIT];
    mib->icmp6InNeighborAdvertisements = icmpstat.icp6s_inhist[ND_NEIGHBOR_ADVERT];
    mib->icmp6InRedirects = icmpstat.icp6s_inhist[ND_REDIRECT];

    mib->icmp6OutMsgs = icmpstat.icp6s_canterror
        + icmpstat.icp6s_toofreq;
    for (i = 0; i <= ICMP6_MAXTYPE; i++)
        mib->icmp6OutMsgs += icmpstat.icp6s_outhist[i];
    mib->icmp6OutDestUnreachs = icmpstat.icp6s_outhist[ICMP6_DST_UNREACH];
    mib->icmp6OutPktTooBigs =  icmpstat.icp6s_outhist[ICMP6_PACKET_TOO_BIG];
    mib->icmp6OutTimeExcds = icmpstat.icp6s_outhist[ICMP6_TIME_EXCEEDED];
    mib->icmp6OutParmProblems = icmpstat.icp6s_outhist[ICMP6_PARAM_PROB];
    mib->icmp6OutEchos = icmpstat.icp6s_outhist[ICMP6_ECHO_REQUEST];
    mib->icmp6OutEchoReplies = icmpstat.icp6s_outhist[ICMP6_ECHO_REPLY];
    mib->icmp6OutRouterSolicits =  icmpstat.icp6s_outhist[ND_ROUTER_SOLICIT];
    mib->icmp6OutNeighborSolicits =  icmpstat.icp6s_outhist[ND_NEIGHBOR_SOLICIT];
    mib->icmp6OutNeighborAdvertisements =  icmpstat.icp6s_outhist[ND_NEIGHBOR_ADVERT];
    mib->icmp6OutRedirects = icmpstat.icp6s_outhist[ND_REDIRECT];
    mib->icmp6OutGroupMembResponses =  icmpstat.icp6s_outhist[MLD_LISTENER_REPORT];
    mib->icmp6OutGroupMembReductions =  icmpstat.icp6s_outhist[MLD_LISTENER_DONE];

    return 0;
}
int return_data(int status, struct snmp_session *sp, struct snmp_pdu *pdu)
{
  char buf[1024];
  struct variable_list *vp;
  int ix;
  char *getdata = NULL;
 
  switch (status) {
  case STAT_SUCCESS:
    vp = pdu->variables;
    if (pdu->errstat == SNMP_ERR_NOERROR) {
      while (vp) {
	snprint_variable(buf, sizeof(buf), vp->name, vp->name_length, vp);
	/*mjpark------------------------------------------*/
	getdata = (char *)malloc(1 + vp->val_len);
	memcpy(getdata, vp->val.string, vp->val_len);
	getdata[vp->val_len] = '\0';
#if 0
	//Data over flow...
	int nVal;
	memcpy((void *)&nVal, getdata, 4);
#else
	unsigned long int nVal;
	memcpy((void *)&nVal, getdata, sizeof(unsigned long int));
#endif
	gpSnmp->val = nVal;

	//debug print
	/* printf("data length(%d), unsigned long int size(%d), int(%d)\n",vp->val_len, sizeof(unsigned long int), sizeof(int)); */
	printf("snmpVal(%d), psnmp(%p)\n", nVal, gpSnmp);
	/*------------------------------------------------*/
	vp = vp->next_variable;
      }
      free(getdata);
    }
    else {
      for (ix = 1; vp && ix != pdu->errindex; vp = vp->next_variable, ix++);
      if (vp)
	snprint_objid(buf, sizeof(buf), vp->name, vp->name_length);
      else
	strcpy(buf, "(none)");
      fprintf(stdout, "%s: %s: %s\n", sp->peername, buf, snmp_errstring(pdu->errstat));
    }
    return 1;
  case STAT_TIMEOUT:
    fprintf(stdout, "%s: Timeout\n", sp->peername);
    return 0;
  
  case STAT_ERROR:
    snmp_perror(sp->peername);
    return 0;
  }
  return 0;
}
Example #20
0
static void add_pdu_var(netsnmp_pdu *pdu, const char *mib_name, int id, const char *value)
{
	oid oid_name[MAX_OID_LEN];
	size_t name_length;

	char buf[4096];
	buf[4095] = '\0';
	snprintf(buf, sizeof(buf)-1, "%s.%d", mib_name, id);

	name_length = MAX_OID_LEN;
	if (snmp_parse_oid(buf, oid_name, &name_length) == NULL) {
		snmp_perror(buf);
		return;
	}

	if (snmp_add_var(pdu, oid_name, name_length, '=', value)) {
		snmp_perror(buf);
		return;
	}
}
Example #21
0
int
netbsd_read_ip_stat(struct ip_mib *mib)
{
    uint64_t ipstat[IP_NSTATS];
    size_t   size = sizeof(ipstat);
    int      i;
    static   int sname[4] = { 4, 2, 0, 0 }; /* CTL_NET, PF_INET, IPPROTO_IP, 0 */
    size_t   len;

    (void)memset(mib, 0, sizeof(*mib));

    if (-1 == sysctlbyname("net.inet.ip.stats", ipstat, &size, NULL, 0)) {
	snmp_perror("netbsd_read_ip_stat: net.inet.ip.stats");
        return -1;
    }

    mib->ipForwarding = 0;
    len = sizeof i;
    sname[3] = IPCTL_FORWARDING;
    if (0 == sysctl(sname, 4, &i, &len, 0, 0)) {
        mib->ipForwarding = (long)i;
    }

    mib->ipDefaultTTL = 0;
    sname[3] = IPCTL_DEFTTL;     
    if (0 == sysctl(sname, 4, &i, &len, 0, 0)) {
        mib->ipDefaultTTL = (long)i;
    }

    mib->ipInReceives = ipstat[IP_STAT_TOTAL];
    mib->ipInHdrErrors = ipstat[IP_STAT_BADSUM]
        + ipstat[IP_STAT_TOOSHORT] + ipstat[IP_STAT_TOOSMALL]
        + ipstat[IP_STAT_BADHLEN] + ipstat[IP_STAT_BADLEN];
    mib->ipInAddrErrors = ipstat[IP_STAT_CANTFORWARD];
    mib->ipForwDatagrams = ipstat[IP_STAT_FORWARD];
    mib->ipInUnknownProtos = ipstat[IP_STAT_NOPROTO];
    mib->ipInDiscards = ipstat[IP_STAT_FRAGDROPPED]; /* FIXME */
    mib->ipInDelivers = ipstat[IP_STAT_DELIVERED];
    mib->ipOutRequests = ipstat[IP_STAT_LOCALOUT];
    mib->ipOutDiscards = ipstat[IP_STAT_ODROPPED];
    mib->ipOutNoRoutes = 0; /* FIXME */
    mib->ipReasmTimeout = 0; /* IPFRAGTTL; */
    mib->ipReasmReqds = ipstat[IP_STAT_FRAGMENTS];
    mib->ipReasmOKs = ipstat[IP_STAT_REASSEMBLED];
    mib->ipReasmFails = ipstat[IP_STAT_FRAGDROPPED]
        + ipstat[IP_STAT_FRAGTIMEOUT];
    mib->ipFragOKs = ipstat[IP_STAT_FRAGMENTS];
    mib->ipFragFails = ipstat[IP_STAT_CANTFRAG];
    mib->ipFragCreates = ipstat[IP_STAT_OFRAGMENTS];
    mib->ipRoutingDiscards = ipstat[IP_STAT_NOROUTE];
    
    return 0;
}
Example #22
0
int
sysctl_read_icmp_stat(struct icmp_mib *mib)
{
    struct icmpstat icmpstat;
    size_t   size = sizeof(icmpstat);
    int      i;
    static int      sname[4] =
        { CTL_NET, PF_INET, IPPROTO_ICMP, ICMPCTL_STATS };

    (void)memset(mib, 0, sizeof(*mib));

    if (-1 == sysctl(sname, 4, &icmpstat, &size, NULL, 0)) {
	snmp_perror("sysctl_read_icmp_stat: net.inet.icmp.stats");
        return -1;
    }

    mib->icmpInMsgs = icmpstat.icps_badcode
        + icmpstat.icps_tooshort
        + icmpstat.icps_checksum
        + icmpstat.icps_badlen;
    mib->icmpInErrors = mib->icmpInMsgs;
    for (i = 0; i <= ICMP_MAXTYPE; i++)
        mib->icmpInMsgs  += icmpstat.icps_inhist[i];
    mib->icmpInDestUnreachs = icmpstat.icps_inhist[ICMP_UNREACH];
    mib->icmpInTimeExcds = icmpstat.icps_inhist[ICMP_TIMXCEED];
    mib->icmpInParmProbs = icmpstat.icps_inhist[ICMP_PARAMPROB];
    mib->icmpInSrcQuenchs = icmpstat.icps_inhist[ICMP_SOURCEQUENCH];
    mib->icmpInRedirects = icmpstat.icps_inhist[ICMP_REDIRECT];
    mib->icmpInEchos = icmpstat.icps_inhist[ICMP_ECHO];
    mib->icmpInEchoReps = icmpstat.icps_inhist[ICMP_ECHOREPLY];
    mib->icmpInTimestamps = icmpstat.icps_inhist[ICMP_TSTAMP];
    mib->icmpInTimestampReps = icmpstat.icps_inhist[ICMP_TSTAMPREPLY];
    mib->icmpInAddrMasks = icmpstat.icps_inhist[ICMP_MASKREQ];
    mib->icmpInAddrMaskReps = icmpstat.icps_inhist[ICMP_MASKREPLY];
    mib->icmpOutMsgs = icmpstat.icps_oldshort + icmpstat.icps_oldicmp;
    for (i = 0; i <= ICMP_MAXTYPE; i++)
        mib->icmpOutMsgs += icmpstat.icps_outhist[i];
    mib->icmpOutErrors = icmpstat.icps_oldshort + icmpstat.icps_oldicmp;
    mib->icmpOutDestUnreachs = icmpstat.icps_outhist[ICMP_UNREACH];
    mib->icmpOutTimeExcds = icmpstat.icps_outhist[ICMP_TIMXCEED];
    mib->icmpOutParmProbs = icmpstat.icps_outhist[ICMP_PARAMPROB];
    mib->icmpOutSrcQuenchs = icmpstat.icps_outhist[ICMP_SOURCEQUENCH];
    mib->icmpOutRedirects = icmpstat.icps_outhist[ICMP_REDIRECT];
    mib->icmpOutEchos = icmpstat.icps_outhist[ICMP_ECHO];
    mib->icmpOutEchoReps = icmpstat.icps_outhist[ICMP_ECHOREPLY];
    mib->icmpOutTimestamps = icmpstat.icps_outhist[ICMP_TSTAMP];
    mib->icmpOutTimestampReps = icmpstat.icps_outhist[ICMP_TSTAMPREPLY];
    mib->icmpOutAddrMasks = icmpstat.icps_outhist[ICMP_MASKREQ];
    mib->icmpOutAddrMaskReps = icmpstat.icps_outhist[ICMP_MASKREPLY];

    return 0;
}
Example #23
0
int oflops_snmp_get(struct oflops_context * ctx, oid query[], size_t len)
{
	struct snmp_channel* ch = ctx->snmp_channel_info;
	struct snmp_session* sess;

	//Open session for async request
	if(!(sess = snmp_open(&(ch->session))))
	{
		snmp_perror("snmp_open");
		return 1;
	}

	//Build and send packet
	if (ch->req != NULL)
		snmp_free_pdu(ch->req);
	ch->req = snmp_pdu_create(SNMP_MSG_GET);
	snmp_add_null_var(ch->req, query, len);
	if (!snmp_send(sess, ch->req))
		snmp_perror("snmp_send");

	return 0;
}
Example #24
0
/* ---------------------------------------------------------------------
 */
int
netsnmp_swinst_arch_load( netsnmp_container *container, u_int flags)
{
    FILE *p = popen("dpkg-query --show --showformat '${Package}#${Version}#${Section}#${Priority}#${Essential}#${Status}\n'", "r");
    char package[SNMP_MAXBUF];
    char version[SNMP_MAXBUF];
    char section[SNMP_MAXBUF];
    char priority[SNMP_MAXBUF];
    char essential[SNMP_MAXBUF];
    char status[SNMP_MAXBUF];
    char buf[BUFSIZ];
    netsnmp_swinst_entry *entry;
    int i = 0;

    if (p == NULL) {
	snmp_perror("dpkg-list");
	return 1;
    }

    while (fgets(buf, BUFSIZ, p)) {
	DEBUGMSG(("swinst_apt", "entry: %s\n", buf));
        entry = netsnmp_swinst_entry_create( i++ );
        if (NULL == entry)
            continue;   /* error already logged by function */
        CONTAINER_INSERT(container, entry);

	sscanf(buf, apt_fmt, package, version, section, priority, essential, status);
	if (strstr(status, "not-installed"))
	    continue;

        entry->swName_len = snprintf( entry->swName, sizeof(entry->swName),
                                      "%s-%s", package, version);
	if (entry->swName_len >= sizeof(entry->swName))
	    entry->swName_len = sizeof(entry->swName)-1;
        entry->swType = (strcmp(essential, "yes") == 0)
                        ? 2      /* operatingSystem */
                        : 4;     /*  application    */

        entry->swDate_len = 8;
	memcpy(entry->swDate, "\0\0\1\1\0\0\0\0", 8);
    }
    pclose(p);
    DEBUGMSGTL(("swinst:load:arch"," loaded %d entries\n",
                (int) CONTAINER_SIZE(container)));

    return 0;
}
Example #25
0
File: snmp.c Project: Shmuma/z
/*
 * response handler
 */
int asynch_response(int operation, struct snmp_session *sp, int reqid,
		    struct snmp_pdu *pdu, void *magic)
{
	struct session *host = (struct session *)magic;
	struct snmp_pdu *req;
	struct oid *op;

	if (operation == NETSNMP_CALLBACK_OP_RECEIVED_MESSAGE) {
		if (print_result(STAT_SUCCESS, host->sess, pdu)) {
/*			host->current_oid++;			*/ /* send next GET (if any) */
			op = host->current_oid;
			op++;
			while(op->hostname)
			{
/*				printf("[%s] [%s]\n",op->hostname, host->current_oid->hostname); */
				if(strcmp(op->hostname,host->current_oid->hostname)==0) {
					host->current_oid = op;
					break;
				}
				op++;
			}

			if (op->hostname && host->current_oid->Name) {
				req = snmp_pdu_create(SNMP_MSG_GET);
				snmp_add_null_var(req, host->current_oid->Oid, host->current_oid->OidLen);
				if (snmp_send(host->sess, req))
					return 1;
				else {
					snmp_perror("snmp_send");
					snmp_free_pdu(req);
				}
			}
			else
			{
/*				printf("No more OIDs for [%s]\n", host->current_oid->hostname); */
			}
		}
	}
	else
		print_result(STAT_TIMEOUT, host->sess, pdu);

/* something went wrong (or end of variables) 
* this host not active any more
*/
	active_hosts--;
	return 1;
}
Example #26
0
QSnmp::QSnmp(const QString &peername, const QByteArray &community) {
	init_snmp("door_control");
	snmp_sess_init(&session);
	session.peername = strdup(peername.toUtf8().constData()); //"192.168.0.100";
	session.version = SNMP_VERSION_1;
	session.community = (unsigned char*)strdup(community.constData()); //(unsigned char*)"private";
	session.community_len = strlen((char*)session.community);

	ss = snmp_open(&session);
	if (!ss) {
		snmp_perror("ack");
		snmp_log(LOG_ERR, "something horrible happened!!!\n");
		return;
	}

	qDebug("QSnmp: session established");
}
Example #27
0
int snmp_mtp_peer_name(struct snmp_mtp_session *session, char *host)
{
	if (session->ss) {
		snmp_close(session->ss);
		session->ss = NULL;
	}

	session->session.peername = host;

	session->ss = snmp_open(&session->session);
	if (!session->ss) {
		snmp_perror("create failure");
		snmp_log(LOG_ERR, "Could not connect to the remote.\n");
		LOGP(DINP, LOGL_ERROR, "Failed to open a SNMP session.\n");
		return -1;
	}

	return 0;
}
Example #28
0
int print_result(int status, struct snmp_session *sp, struct snmp_pdu *pdu)
{
	char buf[1024];
	struct variable_list *vp;
	int ix;

	struct timeval now;
	struct timezone tz;
	struct tm *tm;

	gettimeofday(&now, &tz);
	tm = localtime(&now.tv_sec);
	fprintf(stdout, "%.2d:%.2d:%.2d.%.6d, status: %d, ", tm->tm_hour, tm->tm_min, tm->tm_sec,
	        now.tv_usec, status);
	switch (status) {
		case STAT_SUCCESS:
			vp = pdu->variables;
			if (pdu->errstat == SNMP_ERR_NOERROR) {
				while (vp) {
					snprint_variable(buf, sizeof(buf), vp->name, vp->name_length, vp);
					fprintf(stdout, ":%s: %s\n", sp->peername, buf);
					vp = vp->next_variable;
				}
			}
			else {
				for (ix = 1; vp && ix != pdu->errindex; vp = vp->next_variable, ix++)
					;
				if (vp) snprint_objid(buf, sizeof(buf), vp->name, vp->name_length);
				else strcpy(buf, "(none)");
				fprintf(stdout, ":%s: %s: %s\n",
				        sp->peername, buf, snmp_errstring(pdu->errstat));
			}
			return 1;
		case STAT_TIMEOUT:
			fprintf(stdout, ":%s: Timeout\n", sp -> peername);
			return 0;
		case STAT_ERROR:
			snmp_perror(sp->peername);
			return 0;
	}
	return 0;
}
Example #29
0
/*
 * initialize
 */
void initialize (void)
{
  struct oid *op = oids;
  
  /* Win32: init winsock */
  SOCK_STARTUP;

  /* initialize library */
  init_snmp("asynchapp");

  /* parse the oids */
  while (op->Name) {
    op->OidLen = sizeof(op->Oid)/sizeof(op->Oid[0]);
    if (!read_objid(op->Name, op->Oid, &op->OidLen)) {
      snmp_perror("read_objid");
      exit(1);
    }
    op++;
  }
}
Example #30
0
int
netbsd_read_udp_stat(struct udp_mib *mib)
{
    uint64_t udpstat[UDP_NSTATS];
    size_t   size = sizeof(udpstat);

    (void)memset(mib, 0, sizeof(*mib));

    if (-1 == sysctlbyname("net.inet.udp.stats", udpstat, &size, NULL, 0)) {
	snmp_perror("netbsd_read_udp_stat: net.inet.udp.stats");
        return -1;
    }

    mib->udpInDatagrams = udpstat[UDP_STAT_IPACKETS];
    mib->udpNoPorts = udpstat[UDP_STAT_NOPORT];
    mib->udpOutDatagrams = udpstat[UDP_STAT_OPACKETS];
    mib->udpInErrors = udpstat[UDP_STAT_HDROPS]
        + udpstat[UDP_STAT_BADSUM] /* + udpstat[UDP_STAT_DISCARD] */ /* FIXME */
        + udpstat[UDP_STAT_FULLSOCK] + udpstat[UDP_STAT_BADLEN];

    return 0;
}