void snmp_start_read_rssi(char *host) { struct snmp_pdu *pdu; struct snmp_session session; const char *community = "public"; if (oid_rssi_ts1_length == 0 || oid_rssi_ts2_length == 0) return; snmp_rssi_received = 0; if (snmp_session_rssi != NULL) { snmp_close(snmp_session_rssi); snmp_session_rssi = NULL; } snmp_sess_init(&session); session.version = SNMP_VERSION_1; session.peername = strdup(host); session.community = (unsigned char *)strdup(community); session.community_len = strlen(community); session.callback = snmp_get_rssi_cb; if (!(snmp_session_rssi = snmp_open(&session))) { console_log("snmp error: error opening session to host %s\n", host); return; } pdu = snmp_pdu_create(SNMP_MSG_GET); snmp_add_null_var(pdu, oid_rssi_ts1, oid_rssi_ts1_length); snmp_add_null_var(pdu, oid_rssi_ts2, oid_rssi_ts2_length); if (!snmp_send(snmp_session_rssi, pdu)) console_log("snmp error: error sending rssi request to host %s\n", host); free(session.peername); free(session.community); }
void simpleSNMPsend(struct snmp_session *session, oid *name, size_t name_length) { struct snmp_pdu *pdu; oid uptime[MAX_OID_LEN]; size_t uptime_length; /* * Create PDU for GET request and add object names to request. */ pdu = snmp_pdu_create(SNMP_MSG_GET); /* * First insert uptime request into PDU. */ uptime_length = MAX_OID_LEN; if (!snmp_parse_oid("system.sysUpTime.0", uptime, &uptime_length)) { printf("error parsing oid: system.sysUpTime.0\n"); } snmp_add_null_var(pdu, uptime, uptime_length); snmp_add_null_var(pdu, name, name_length); /* * Perform the request. */ snmp_send(session, pdu); }
int ups_mib_mgr_get_upsBypassEntry(struct snmp_session *s, upsBypassEntry_t **upsBypassEntry) { struct snmp_session *peer; struct snmp_pdu *request, *response; struct variable_list *vars; int status; request = snmp_pdu_create(SNMP_MSG_GETNEXT); snmp_add_null_var(request, upsBypassVoltage, sizeof(upsBypassVoltage)/sizeof(oid)); snmp_add_null_var(request, upsBypassCurrent, sizeof(upsBypassCurrent)/sizeof(oid)); snmp_add_null_var(request, upsBypassPower, sizeof(upsBypassPower)/sizeof(oid)); peer = snmp_open(s); if (!peer) { return -1; } status = snmp_synch_response(peer, request, &response); if (status != STAT_SUCCESS) { if (response) snmp_free_pdu(response); snmp_close(peer); return -2; } *upsBypassEntry = (upsBypassEntry_t *) malloc(sizeof(upsBypassEntry_t)); if (! *upsBypassEntry) { if (response) snmp_free_pdu(response); snmp_close(peer); return -4; } for (vars = response->variables; vars; vars = vars->next_variable) { if (vars->name_length > sizeof(upsBypassVoltage)/sizeof(oid) && memcmp(vars->name, upsBypassVoltage, sizeof(upsBypassVoltage)) == 0) { (*upsBypassEntry)->__upsBypassVoltage = *vars->val.integer; (*upsBypassEntry)->upsBypassVoltage = &((*upsBypassEntry)->__upsBypassVoltage); } if (vars->name_length > sizeof(upsBypassCurrent)/sizeof(oid) && memcmp(vars->name, upsBypassCurrent, sizeof(upsBypassCurrent)) == 0) { (*upsBypassEntry)->__upsBypassCurrent = *vars->val.integer; (*upsBypassEntry)->upsBypassCurrent = &((*upsBypassEntry)->__upsBypassCurrent); } if (vars->name_length > sizeof(upsBypassPower)/sizeof(oid) && memcmp(vars->name, upsBypassPower, sizeof(upsBypassPower)) == 0) { (*upsBypassEntry)->__upsBypassPower = *vars->val.integer; (*upsBypassEntry)->upsBypassPower = &((*upsBypassEntry)->__upsBypassPower); } } if (response) snmp_free_pdu(response); if (snmp_close(peer) == 0) { return -5; } return 0; }
int ups_mib_mgr_get_upsOutput(struct snmp_session *s, upsOutput_t **upsOutput) { struct snmp_session *peer; struct snmp_pdu *request, *response; struct variable_list *vars; int status; request = snmp_pdu_create(SNMP_MSG_GETNEXT); snmp_add_null_var(request, upsOutputSource, sizeof(upsOutputSource)/sizeof(oid)); snmp_add_null_var(request, upsOutputFrequency, sizeof(upsOutputFrequency)/sizeof(oid)); snmp_add_null_var(request, upsOutputNumLines, sizeof(upsOutputNumLines)/sizeof(oid)); peer = snmp_open(s); if (!peer) { return -1; } status = snmp_synch_response(peer, request, &response); if (status != STAT_SUCCESS) { if (response) snmp_free_pdu(response); snmp_close(peer); return -2; } *upsOutput = (upsOutput_t *) malloc(sizeof(upsOutput_t)); if (! *upsOutput) { if (response) snmp_free_pdu(response); snmp_close(peer); return -4; } for (vars = response->variables; vars; vars = vars->next_variable) { if (vars->name_length > sizeof(upsOutputSource)/sizeof(oid) && memcmp(vars->name, upsOutputSource, sizeof(upsOutputSource)) == 0) { (*upsOutput)->__upsOutputSource = *vars->val.integer; (*upsOutput)->upsOutputSource = &((*upsOutput)->__upsOutputSource); } if (vars->name_length > sizeof(upsOutputFrequency)/sizeof(oid) && memcmp(vars->name, upsOutputFrequency, sizeof(upsOutputFrequency)) == 0) { (*upsOutput)->__upsOutputFrequency = *vars->val.integer; (*upsOutput)->upsOutputFrequency = &((*upsOutput)->__upsOutputFrequency); } if (vars->name_length > sizeof(upsOutputNumLines)/sizeof(oid) && memcmp(vars->name, upsOutputNumLines, sizeof(upsOutputNumLines)) == 0) { (*upsOutput)->__upsOutputNumLines = *vars->val.integer; (*upsOutput)->upsOutputNumLines = &((*upsOutput)->__upsOutputNumLines); } } if (response) snmp_free_pdu(response); if (snmp_close(peer) == 0) { return -5; } return 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 prsnmpstr(char *stroid) { struct snmp_pdu *pdu, *resp; oid tmp_oid[MAX_OID_LEN]; size_t tmp_oid_len=MAX_OID_LEN; int stat; char *tmp; pdu=snmp_pdu_create(SNMP_MSG_GET); read_objid(stroid, tmp_oid, &tmp_oid_len); snmp_add_null_var(pdu, tmp_oid, tmp_oid_len); stat=snmp_synch_response(ses, pdu, &resp); if (stat != STAT_SUCCESS || resp->errstat != SNMP_ERR_NOERROR) perr(resp); if(resp->variables->val_len && strlen((char *)resp->variables->val.string)) { tmp=malloc((resp->variables->val_len+1) * sizeof(char)); memcpy(tmp, resp->variables->val.string, resp->variables->val_len); tmp[resp->variables->val_len]=0; printf("%s", tmp); free(tmp); } if(resp) snmp_free_pdu(resp); }
void prifalias(oid inst) { struct snmp_pdu *pdu, *resp; oid tmp_oid[] = { 1,3,6,1,2,1,31,1,1,1,18,0 }; int stat; char *tmp; if(!extended) { fprintf(stderr, "ifalias is only available in eXtended mode\n"); snmp_close(ses); SOCK_CLEANUP; exit(1); } tmp_oid[11]=inst; pdu=snmp_pdu_create(SNMP_MSG_GET); snmp_add_null_var(pdu, tmp_oid, sizeof(tmp_oid)/sizeof(oid)); stat=snmp_synch_response(ses, pdu, &resp); if (stat != STAT_SUCCESS || resp->errstat != SNMP_ERR_NOERROR) perr(resp); if(resp->variables->val_len && strlen((char *)resp->variables->val.string)) { tmp=malloc((resp->variables->val_len+1) * sizeof(char)); memcpy(tmp, resp->variables->val.string, resp->variables->val_len); tmp[resp->variables->val_len]=0; printf(" \"%s\"", tmp); free(tmp); } if(resp) snmp_free_pdu(resp); }
uint32_t getcntr32(int dir, oid inst) { struct snmp_pdu *pdu, *resp; oid iftable_oid[] = { 1,3,6,1,2,1,2,2,1,0,0 }; // dir=9 ; inst=10 int stat; uint32_t tmp; pdu=snmp_pdu_create(SNMP_MSG_GET); iftable_oid[9]=dir; iftable_oid[10]=inst; snmp_add_null_var(pdu, iftable_oid, sizeof(iftable_oid)/sizeof(oid)); stat=snmp_synch_response(ses, pdu, &resp); if (stat != STAT_SUCCESS || resp->errstat != SNMP_ERR_NOERROR) perr(resp); if(resp->variables->type != ASN_COUNTER) { fprintf(stderr, "\nError: unsupported data type (only 32bit counter is supported in normal mode)\n"); snmp_close(ses); SOCK_CLEANUP; exit(1); } tmp=resp->variables->val.counter64->high; if(resp) snmp_free_pdu(resp); return tmp; }
/* * 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; }
/** * snmp_getn_bulk: Builds and sends a SNMP_BET_BULK pdu using snmp * @sessp: snmp session * @bulk_objid: string containing the OID entry. * @bulk_objid_len: len of OID string * @bulk_pdu: pointer to the PDU to be created * @bulk_response: pointer to the response for this PDU request * @num_repetitions: max OIDs for this request. * Note: this is the max requested. Actual number returned can be less * than maximum. That depends on the target snmp agent. * * Return value: status from snmp_sess_synch_response(). See snmp_client.h of net-snmp library. * STAT_SUCCESS * STAT_ERROR * STAT_TIMEOUT * * Additionally, if status == STAT_SUCCESS, consumer of this routine needs to check for exception * conditions. * Possible exception conditions are SNMP_ENDOFMIBVIEW, SNMP_NOSUCHOBJECT, SNMP_NOSUCHINSTANCE **/ int snmp_getn_bulk( void *sessp, oid *bulk_objid, size_t bulk_objid_len, struct snmp_pdu *bulk_pdu, struct snmp_pdu **bulk_response, int num_repetitions ) { int status; bulk_pdu = snmp_pdu_create(SNMP_MSG_GETBULK); bulk_pdu->non_repeaters = 0; bulk_pdu->max_repetitions = num_repetitions; snmp_add_null_var(bulk_pdu, bulk_objid, bulk_objid_len); /* Send the Request out.*/ status = snmp_sess_synch_response(sessp, bulk_pdu, bulk_response); /* * Return the status. Consumer of this util has to process the response. */ return(status); }
int agentx_remove_agentcaps(netsnmp_session * ss, oid * agent_cap, size_t agent_caplen) { netsnmp_pdu *pdu, *response; if (ss == NULL || !IS_AGENTX_VERSION(ss->version)) { return 0; } pdu = snmp_pdu_create(AGENTX_MSG_REMOVE_AGENT_CAPS); if (pdu == NULL) return 0; pdu->time = 0; pdu->sessid = ss->sessid; snmp_add_null_var(pdu, agent_cap, agent_caplen); if (agentx_synch_response(ss, pdu, &response) != STAT_SUCCESS) return 0; if (response->errstat != SNMP_ERR_NOERROR) { snmp_free_pdu(response); return 0; } snmp_free_pdu(response); return 1; }
/* report the value interfaces.ifNumber.0, actually the number of interfaces */ static int snmp_get_ifcount(struct snmp_session *ss) { int nifaces = -1; oid ifcount[] = { 1, 3, 6, 1, 2, 1, 2, 1, 0 }; struct snmp_pdu *pdu; struct snmp_pdu *response = NULL; int status; if ((pdu = snmp_pdu_create(SNMP_MSG_GET)) == NULL) { ifstat_error("snmp_pdu_create: %s", snmp_api_errstring(snmp_errno)); return -1; } snmp_add_null_var(pdu, ifcount, sizeof(ifcount) / sizeof(oid)); if ((status = snmp_synch_response(ss, pdu, &response)) != STAT_SUCCESS || response->errstat != SNMP_ERR_NOERROR || response->variables == NULL || response->variables->type != ASN_INTEGER) { if (status == STAT_SUCCESS) ifstat_error("snmp: Error: %s", snmp_errstring(response->errstat)); else ifstat_error("snmpget(interfaces.ifNumber.0): %s", snmp_sess_errstring(ss)); if (response) snmp_free_pdu(response); return -1; } nifaces = *(response->variables->val.integer); snmp_free_pdu(response); if (nifaces < 0) return -1; return nifaces; }
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 ) ; }
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 */ }
void session_add_null_var(struct snmp_pdu *pdu, const char *name) { oid anOID[MAX_OID_LEN+1]; size_t anOID_len = MAX_OID_LEN; get_node(name, anOID, &anOID_len); snmp_add_null_var(pdu, anOID, anOID_len); }
/* * 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); } }
int snmp_get_bulk( struct snmp_session *ss, const char *bulk_objid, struct snmp_pdu *bulk_pdu, struct snmp_pdu **bulk_response ) { size_t anOID_len = MAX_OID_LEN; oid anOID[MAX_OID_LEN]; int status; /* Create the PDU for theenrty_count data for our request. */ read_objid(bulk_objid, anOID, &anOID_len); bulk_pdu = snmp_pdu_create(SNMP_MSG_GETBULK); bulk_pdu->non_repeaters = 0; bulk_pdu->max_repetitions = NUM_REPITIONS; snmp_add_null_var(bulk_pdu, anOID, anOID_len); /* Send the Request out.*/ status = snmp_synch_response(ss, bulk_pdu, bulk_response); return(status); }
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; }
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); } }
void processSnmpGet(char * oid){ read_objid(oid, id_oid, &id_len); snmp_add_null_var(pdu, id_oid, id_len); int status = snmp_synch_response(session_handle, pdu, &response); for(vars = response->variables; vars; vars = vars->next_variable){ snprint_variable(outbuff, 256, vars->name, vars->name_length, vars); resultString = strrchr(outbuff, ':'); } }
int agentx_register(netsnmp_session * ss, oid start[], size_t startlen, int priority, int range_subid, oid range_ubound, int timeout, u_char flags, const char *contextName) { netsnmp_pdu *pdu, *response; DEBUGMSGTL(("agentx/subagent", "registering: ")); DEBUGMSGOIDRANGE(("agentx/subagent", start, startlen, range_subid, range_ubound)); DEBUGMSG(("agentx/subagent", "\n")); if (ss == NULL || !IS_AGENTX_VERSION(ss->version)) { return 0; } pdu = snmp_pdu_create(AGENTX_MSG_REGISTER); if (pdu == NULL) { return 0; } pdu->time = timeout; pdu->priority = priority; pdu->sessid = ss->sessid; pdu->range_subid = range_subid; if (contextName) { pdu->flags |= AGENTX_MSG_FLAG_NON_DEFAULT_CONTEXT; pdu->community = strdup(contextName); pdu->community_len = strlen(contextName); } if (flags & FULLY_QUALIFIED_INSTANCE) { pdu->flags |= AGENTX_MSG_FLAG_INSTANCE_REGISTER; } if (range_subid) { snmp_pdu_add_variable(pdu, start, startlen, ASN_OBJECT_ID, (u_char *) start, startlen * sizeof(oid)); pdu->variables->val.objid[range_subid - 1] = range_ubound; } else { snmp_add_null_var(pdu, start, startlen); } if (agentx_synch_response(ss, pdu, &response) != STAT_SUCCESS) { DEBUGMSGTL(("agentx/subagent", "registering failed!\n")); return 0; } if (response->errstat != SNMP_ERR_NOERROR) { snmp_log(LOG_ERR,"registering pdu failed: %d!\n", response->errstat); snmp_free_pdu(response); return 0; } snmp_free_pdu(response); DEBUGMSGTL(("agentx/subagent", "registered\n")); return 1; }
/** Get current from power supply */ double getCurrentMeasurement(HSNMP m_sessp, int channel) { double value; struct snmp_pdu* pdu = snmp_pdu_create(SNMP_MSG_GET); // prepare get-request pdu // for(each GET request to one crate) { snmp_add_null_var(pdu,oidOutputMeasurementCurrent[channel],lengthOutputMeasurementCurrent[channel]); // generate request data // } // endfor struct snmp_pdu* response; int status = snmp_sess_synch_response(m_sessp,pdu,&response); /* * Process the response. */ if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR) { /* * SUCCESS: Print the result variables */ struct variable_list *vars; // debug print //for(vars = response->variables; vars; vars = vars->next_variable) // print_variable(vars->name, vars->name_length, vars); /* manipuate the information ourselves */ for(vars = response->variables; vars; vars = vars->next_variable) { if (vars->type == ASN_OPAQUE_FLOAT) { // 0x78 value = *vars->val.floatVal; } else if (vars->type == ASN_OPAQUE_DOUBLE) { // 0x79 value = *vars->val.doubleVal; } else if(vars->type == ASN_INTEGER) { // 0x02 value = (double)*vars->val.integer; } } } else { /* * FAILURE: print what went wrong! */ if (status == STAT_SUCCESS) fprintf(stderr, "Error in packet\nReason: %s\n", snmp_errstring(response->errstat)); else snmp_sess_perror("snmpget",snmp_sess_session(m_sessp)); return 0; } snmp_free_pdu(response); return value; }
bool Session::getVariables(const QStringList &oids, QValueVector<QVariant> &retvars, uint32_t &status, int startIndex) { struct snmp_pdu *response = NULL; if ( ! m_session ) { status = 0xFFFFFFFF; return false; } struct snmp_pdu *pdu = snmp_pdu_create(SNMP_MSG_GET); u_long anOID[MAX_OID_LEN]; QStringList::const_iterator it = oids.begin(); for(int i = 0; i < startIndex; i++) it++; // rough hack, but works for(int i = 0; it != oids.end() && i < 5; it++, i++ ) { size_t anOID_len = MAX_OID_LEN; get_node((*it).latin1(), anOID, &anOID_len); snmp_add_null_var(pdu, anOID, anOID_len); } status = snmp_synch_response(m_session, pdu, &response); //! @todo Error handling should be changed in a more OO way. if ( status != STAT_SUCCESS ) { snmp_sess_perror("snmpget", m_session); return false; } if ( response->errstat != SNMP_ERR_NOERROR ) { kdWarning() << "Error in packet: " << snmp_errstring(response->errstat) << endl; snmp_free_pdu(response); return false; } variable_list *var = response->variables; int i = startIndex; while( var ) { retvars[i] = snmpvarToVariant(var); i++; var = var->next_variable; } snmp_free_pdu(response); return true; }
netsnmp_variable_list * collect(netsnmp_session * ss, netsnmp_pdu *pdu, oid * base, size_t base_length) { netsnmp_pdu *response; int running = 1; netsnmp_variable_list *saved = NULL, **vlpp = &saved; int status; while (running) { /* * gotta catch em all, gotta catch em all! */ status = snmp_synch_response(ss, pdu, &response); if (status != STAT_SUCCESS || !response) { snmp_sess_perror("snmpdf", ss); exit(1); } if (response->errstat != SNMP_ERR_NOERROR) { fprintf(stderr, "snmpdf: Error in packet: %s\n", snmp_errstring(response->errstat)); exit(1); } if (response && snmp_oid_compare(response->variables->name, SNMP_MIN(base_length, response->variables-> name_length), base, base_length) != 0) running = 0; else { /* * get response */ *vlpp = response->variables; (*vlpp)->next_variable = NULL; /* shouldn't be any, but just in case */ /* * create the next request */ pdu = snmp_pdu_create(SNMP_MSG_GETNEXT); snmp_add_null_var(pdu, (*vlpp)->name, (*vlpp)->name_length); /* * finish loop setup */ vlpp = &((*vlpp)->next_variable); response->variables = NULL; /* ahh, forget about it */ } snmp_free_pdu(response); } return saved; }
int main(int argc, char ** argv) { struct snmp_session session; struct snmp_session *sess_handle; struct snmp_pdu *pdu; struct snmp_pdu *response; struct variable_list *vars; oid id_oid[MAX_OID_LEN]; oid serial_oid[MAX_OID_LEN]; size_t id_len = MAX_OID_LEN; size_t serial_len = MAX_OID_LEN; int status; struct tree * mib_tree; /*********************/ if(argv[1] == NULL){ printf("Please supply a hostname\n"); exit(1); } init_snmp("Main check"); snmp_sess_init( &session ); session.version = SNMP_VERSION_1; session.community = "public"; session.community_len = strlen(session.community); session.peername = argv[1]; sess_handle = snmp_open(&session); add_mibdir("."); mib_tree = read_mib("mibs/SNMPv2-MIB.txt"); pdu = snmp_pdu_create(SNMP_MSG_GET); read_objid("SNMPv2-MIB::sysDescr.0", id_oid, &id_len); snmp_add_null_var(pdu, id_oid, id_len); read_objid("SNMPv2-MIB::sysObjectID.0", serial_oid, &serial_len); snmp_add_null_var(pdu, serial_oid, serial_len); status = snmp_synch_response(sess_handle, pdu, &response); for(vars = response->variables; vars; vars = vars->next_variable) print_value(vars->name, vars->name_length, vars); snmp_free_pdu(response); snmp_close(sess_handle); return (0); }
/** Get channel status */ int getChannelStatus(HSNMP m_sessp, int channel) { double value; struct snmp_pdu* pdu = snmp_pdu_create(SNMP_MSG_GET); // prepare get-request pdu // for(each GET request to one crate) { snmp_add_null_var(pdu,oidOutputStatus[channel],lengthOutputStatus[channel]); // generate request data // } // endfor struct snmp_pdu* response; int status = snmp_sess_synch_response(m_sessp,pdu,&response); /* * Process the response. */ if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR) { /* * SUCCESS: Print the result variables */ struct variable_list *vars; // debug print //for(vars = response->variables; vars; vars = vars->next_variable) // print_variable(vars->name, vars->name_length, vars); /* manipuate the information ourselves */ for(vars = response->variables; vars; vars = vars->next_variable) { if (vars->type == ASN_OPAQUE_FLOAT) { // 0x78 value = *vars->val.floatVal; } else if (vars->type == ASN_OPAQUE_DOUBLE) { // 0x79 value = *vars->val.doubleVal; } else if(vars->type == ASN_INTEGER) { // 0x02 value = (double)*vars->val.integer; } else if(vars->type == ASN_OCTET_STR) { // 0x04 unsigned long bitstring = 0; for(int cpos = vars->val_len-1;cpos >= 0;cpos--) { unsigned char octet = vars->val.string[cpos]; for(int bpos = 0;bpos < 8;bpos++) { // convert one character bitstring <<= 1; if(octet&0x01) bitstring |= 1; octet >>= 1; } } value = bitstring; } } } else {
SaErrorT snmp_getn_bulk( struct snmp_session *ss, oid *bulk_objid, size_t bulk_objid_len, struct snmp_pdu *bulk_pdu, struct snmp_pdu **bulk_response, int num_repetitions ) { int status; SaErrorT rtncode = SA_OK; // struct variable_list *vars; bulk_pdu = snmp_pdu_create(SNMP_MSG_GETBULK); bulk_pdu->non_repeaters = 0; bulk_pdu->max_repetitions = num_repetitions; snmp_add_null_var(bulk_pdu, bulk_objid, bulk_objid_len); /* Send the Request out.*/ status = snmp_synch_response(ss, bulk_pdu, bulk_response); /* * Process the response. */ #if 0 if (status == STAT_SUCCESS) { vars = (*bulk_response)->variables; if ((*bulk_response)->errstat == SNMP_ERR_NOERROR) { if (!CHECH_END(vars->type)) { /* This is one of the exception condition */ rtncode = SA_ERR_HPI_NOT_PRESENT; dbg("snmp exception %d \n",vars->type); } } else { fprintf(stderr, "Error in packet %s\nReason: %s\n", (char *)bulk_objid, snmp_errstring((*bulk_response)->errstat)); if ((*bulk_response)->errstat == SNMP_ERR_NOSUCHNAME) (*bulk_response)->errstat = SNMP_NOSUCHOBJECT; rtncode = (SaErrorT) (SA_ERR_SNMP_BASE - (*bulk_response)->errstat); } } else { snmp_sess_perror("snmpset", ss); rtncode = (SaErrorT) (SA_ERR_SNMP_BASE - status); } #endif return(rtncode); }
void snmp::session::get_bulk(std::map<snmp::oid, snmp::variant> & variables) { auto request = snmp_pdu_create(SNMP_MSG_GET); for (auto var : variables) { snmp_add_null_var(request, var.first.data(), var.first.size()); } auto response = perform_request(cast_session(_ss), request); for (auto var = response->variables; var; var = var->next_variable) { save_variable(variables, var); } }
/* * 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; }
int mau_mib_mgr_get_ifJackEntry(struct snmp_session *s, ifJackEntry_t **ifJackEntry) { struct snmp_session *peer; struct snmp_pdu *request, *response; struct variable_list *vars; int status; request = snmp_pdu_create(SNMP_MSG_GETNEXT); snmp_add_null_var(request, ifJackType, sizeof(ifJackType)/sizeof(oid)); peer = snmp_open(s); if (!peer) { snmp_free_pdu(request); return -1; } status = snmp_synch_response(peer, request, &response); if (status != STAT_SUCCESS) { if (response) snmp_free_pdu(response); snmp_close(peer); return -2; } *ifJackEntry = (ifJackEntry_t *) malloc(sizeof(ifJackEntry_t)); if (! *ifJackEntry) { if (response) snmp_free_pdu(response); snmp_close(peer); return -4; } for (vars = response->variables; vars; vars = vars->next_variable) { if (vars->name_length > sizeof(ifJackType)/sizeof(oid) && memcmp(vars->name, ifJackType, sizeof(ifJackType)) == 0) { (*ifJackEntry)->__ifJackType = *vars->val.integer; (*ifJackEntry)->ifJackType = &((*ifJackEntry)->__ifJackType); } } if (response) snmp_free_pdu(response); if (snmp_close(peer) == 0) { return -5; } return 0; }