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); }
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); }
/* * 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); } }
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; }
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 ) ; }
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); }
/* 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 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 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; }
int powernet_snmp_kill_ups_power(UPSINFO *ups) { /* Was 1} change submitted by Kastus Shchuka ([email protected]) 10Dec03 */ oid upsBasicControlConserveBattery[] = { 1, 3, 6, 1, 4, 1, 318, 1, 1, 1, 6, 1, 1, 0 }; struct snmp_ups_internal_data *Sid = (struct snmp_ups_internal_data *)ups->driver_internal_data; struct snmp_session *s = &Sid->session; struct snmp_session *peer; struct snmp_pdu *request, *response; int status; /* * Set up the SET request. */ request = snmp_pdu_create(SNMP_MSG_SET); /* * Set upsBasicControlConserveBattery variable (INTEGER) to * turnOffUpsToConserveBattery(2) value. Will turn on the UPS only * when power returns. */ if (snmp_add_var(request, upsBasicControlConserveBattery, sizeof(upsBasicControlConserveBattery) / sizeof(oid), 'i', "2")) { return 0; } peer = snmp_open(s); if (!peer) { Dmsg0(0, "Can not open the SNMP connection.\n"); return 0; } status = snmp_synch_response(peer, request, &response); if (status != STAT_SUCCESS) { Dmsg0(0, "Unable to communicate with UPS.\n"); return 0; } if (response->errstat != SNMP_ERR_NOERROR) { Dmsg1(0, "Unable to kill UPS power: can not set SNMP variable (%d).\n", response->errstat); return 0; } if (response) snmp_free_pdu(response); snmp_close(peer); return 1; }
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; }
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; }
int collect_mem(netsnmp_session *ss, struct memStats *mem) { netsnmp_pdu *pdu; netsnmp_pdu *response; int status; int ret = 0; pdu = snmp_pdu_create(SNMP_MSG_GET); add(pdu, "UCD-SNMP-MIB:memTotalSwap.0", NULL, 0); add(pdu, "UCD-SNMP-MIB:memAvailSwap.0", NULL, 0); add(pdu, "UCD-SNMP-MIB:memTotalReal.0", NULL, 0); add(pdu, "UCD-SNMP-MIB:memAvailReal.0", NULL, 0); add(pdu, "UCD-SNMP-MIB:memShared.0", NULL, 0); add(pdu, "UCD-SNMP-MIB:memBuffer.0", NULL, 0); add(pdu, "UCD-SNMP-MIB:memCached.0", NULL, 0); status = snmp_synch_response(ss, pdu, &response); memset(mem, 0, sizeof(*mem)); if (status != STAT_SUCCESS || !response || response->errstat != SNMP_ERR_NOERROR) { goto out; } else { netsnmp_variable_list *vlp = response->variables; if (vlp->type == SNMP_NOSUCHOBJECT) goto out; mem->totalSwap = *vlp->val.integer; vlp = vlp->next_variable; if (vlp->type == SNMP_NOSUCHOBJECT) goto out; mem->availSwap = *vlp->val.integer; vlp = vlp->next_variable; if (vlp->type == SNMP_NOSUCHOBJECT) goto out; mem->totalReal = *vlp->val.integer; vlp = vlp->next_variable; if (vlp->type == SNMP_NOSUCHOBJECT) goto out; mem->availReal = *vlp->val.integer; vlp = vlp->next_variable; ret = 1; if (vlp->type == SNMP_NOSUCHOBJECT) goto out; mem->shared = *vlp->val.integer; vlp = vlp->next_variable; if (vlp->type == SNMP_NOSUCHOBJECT) goto out; mem->buffer = *vlp->val.integer; vlp = vlp->next_variable; if (vlp->type == SNMP_NOSUCHOBJECT) goto out; mem->cached = *vlp->val.integer; } out: if (response) snmp_free_pdu(response); return ret; }
static std::unique_ptr<::snmp_pdu, pdu_deleter> perform_request(::snmp_session * session, ::snmp_pdu * request) { struct snmp_pdu * unsafe_response; auto status = snmp_synch_response(session, request, &unsafe_response); std::unique_ptr<::snmp_pdu, pdu_deleter> response(unsafe_response); if (status != STAT_SUCCESS) throw snmp::session_error(session); if (response->errstat != SNMP_ERR_NOERROR) throw snmp::protocol_error(response->errstat); return response; }
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); }
/* * write value of given oid */ static int MPC_write(struct snmp_session *sptr, const char *objname, char type, char *value) { oid name[MAX_OID_LEN]; size_t namelen = MAX_OID_LEN; struct snmp_pdu *pdu; struct snmp_pdu *resp; DEBUGCALL; /* convert objname into oid; return FALSE if invalid */ if (!read_objid(objname, name, &namelen)) { LOG(PIL_CRIT, "%s: cannot convert %s to oid.", __FUNCTION__, objname); return (FALSE); } /* create pdu */ if ((pdu = snmp_pdu_create(SNMP_MSG_SET)) != NULL) { /* add to be written value to pdu */ snmp_add_var(pdu, name, namelen, type, value); /* send pdu and get response; return NULL if error */ if (snmp_synch_response(sptr, pdu, &resp) == STAT_SUCCESS) { /* go through the returned vars */ if (resp->errstat == SNMP_ERR_NOERROR) { /* request successful done */ snmp_free_pdu(resp); return (TRUE); } else { LOG(PIL_CRIT, "%s: error in response packet, reason %ld [%s]." , __FUNCTION__, resp->errstat, snmp_errstring(resp->errstat)); } } else { MPC_error(sptr, __FUNCTION__, "error sending/receiving pdu"); } /* free pdu (again: necessary?) */ snmp_free_pdu(resp); } else { MPC_error(sptr, __FUNCTION__, "cannot create pdu"); } /* error */ return (FALSE); }
char *session_query(struct snmp_session *ss, struct snmp_pdu *pdu) { struct snmp_pdu *response; struct variable_list *vars; int status; char buf[SPRINT_MAX_LEN]; char *rbuffer=NULL; /* Send the Request */ status = snmp_synch_response(ss, pdu, &response); /* Process the response */ if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR ) { /* Success: Print the results */ /* for (vars=response->variables; vars; vars=vars->next_variable) { */ vars=response->variables; if (vars!=NULL && vars->type <ASN_LONG_LEN) { if (vars->type == ASN_INTEGER) { sprintf(buf,"%ld",*vars->val.integer); rbuffer=malloc(sizeof(char)*(strlen(buf)+1)); memset(rbuffer,'\0',strlen(buf)+1); strncpy(rbuffer,buf,strlen(buf)); } else { snprint_variable(buf, sizeof (buf), vars->name, vars->name_length, vars); rbuffer=malloc(sizeof(char)*(strlen(buf)+1)); memset(rbuffer,'\0',strlen(buf)+1); strncpy(rbuffer,buf,strlen(buf)); } } else rbuffer = NULL; } 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 ", ss); } /* Clean up */ if (response) snmp_free_pdu(response); // printf("Result : %s\n",rbuffer); return rbuffer; }
QtNetSNMP::SNMPPDU *QtNetSNMP::QSNMPCore::sendPDU(SNMPSession *session, SNMPPDU *pdu) throw(QSNMPException) { SNMPPDU *response; int status; status = snmp_synch_response(session, pdu, &response); if(status == STAT_SUCCESS) if(response -> errstat == SNMP_ERR_NOERROR) return response; else throw QSNMPException("QSNMPCore :: Send PDU :: Responde PDU has errors"); else if(status == STAT_TIMEOUT) throw QSNMPException("QSNMPCore :: Send PDU :: Timeout. No response from agent"); else throw QSNMPException("QSNMPCore :: Send PDU :: SNMP Session error"); }
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; }
char *session_set( struct snmp_session *ss,const char *name, const char *value ) { struct snmp_pdu *pdu, *response; oid anOID[MAX_OID_LEN]; size_t anOID_len = MAX_OID_LEN; struct variable_list *vars; int status; char buf[SPRINT_MAX_LEN]; char *rbuffer=NULL; /* create PDU for request */ pdu = snmp_pdu_create(SNMP_MSG_SET); get_node(name, anOID, &anOID_len); snmp_add_var(pdu, anOID, anOID_len,'i',value); // snmp_add_null_var(pdu, anOID, anOID_len); /* Send the Request */ status = snmp_synch_response(ss, pdu, &response); /* Process the response */ if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR) { /* Success: Print the results */ /* for (vars=response->variables; vars; vars=vars->next_variable) { */ vars=response->variables; if (vars!=NULL) { snprint_variable(buf, sizeof (buf), vars->name, vars->name_length, vars); rbuffer=malloc(sizeof(char)*(strlen(buf)+1)); memset(rbuffer,'\0',strlen(buf)+1); strncpy(rbuffer,buf,strlen(buf)); } else rbuffer = NULL; } 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("snmpset", ss); } /* Clean up */ /* if (pdu) snmp_free_pdu(pdu); */ if (response) snmp_free_pdu(response); return rbuffer; }
static int snmp_get_nextif(struct snmp_session *ss, int index) { oid ifindex[] = { 1, 3, 6, 1, 2, 1, 2, 2, 1, 1, 0 }; int len = sizeof(ifindex) / sizeof(oid); struct snmp_pdu *pdu; struct snmp_pdu *response = NULL; struct variable_list *vars; int status; if (index >= 0) ifindex[len - 1] = index; if ((pdu = snmp_pdu_create(SNMP_MSG_GETNEXT)) == NULL) { ifstat_error("snmp_pdu_create: %s", snmp_api_errstring(snmp_errno)); return -1; } snmp_add_null_var(pdu, ifindex, (index < 0) ? len - 1 : len); if ((status = snmp_synch_response(ss, pdu, &response)) != STAT_SUCCESS || response->errstat != SNMP_ERR_NOERROR || response->variables == NULL) { if (status == STAT_SUCCESS) ifstat_error("snmp: Error: %s", snmp_errstring(response->errstat)); else ifstat_error("snmpgetnext(interfaces.ifTable.ifEntry.ifIndex...): %s", snmp_sess_errstring(ss)); if (response != NULL) snmp_free_pdu(response); return -1; } for(vars = response->variables; vars; vars = vars->next_variable) { /* check that the variable is under the base oid */ if (vars->name_length != len) continue; if (memcmp(ifindex, vars->name, sizeof(ifindex) - sizeof(oid)) != 0) continue; index = vars->name[vars->name_length - 1]; snmp_free_pdu(response); return index; } snmp_free_pdu(response); return -1; }
// a higher abstraction function takes oid string and SNMP methods and call void snmpcommand(char* oid,int cmd) { SOCK_STARTUP; ss = snmp_open(&session); /* establish the session */ if (!ss) { snmp_sess_perror("ack", &session); SOCK_CLEANUP; exit(1); } pdu = snmp_pdu_create(cmd); if ( cmd == SNMP_MSG_GETBULK) { // for bulkget only pdu->non_repeaters = 0; pdu->max_repetitions = 50; } anOID_len = MAX_OID_LEN; get_node(oid, anOID, &anOID_len); snmp_add_null_var(pdu, anOID, anOID_len);// all OID should be paired with null for out going req status = snmp_synch_response(ss, pdu, &response); // sent req }
static void snmp_get_and_print(netsnmp_session * ss, oid * theoid, size_t theoid_len) { netsnmp_pdu *pdu, *response; netsnmp_variable_list *vars; int status; pdu = snmp_pdu_create(SNMP_MSG_GET); snmp_add_null_var(pdu, theoid, theoid_len); status = snmp_synch_response(ss, pdu, &response); if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR) { for (vars = response->variables; vars; vars = vars->next_variable) { numprinted++; print_variable(vars->name, vars->name_length, vars); } } if (response) { snmp_free_pdu(response); } }
QVariant QSnmp::get(const QString &oid) { struct snmp_pdu *pdu; struct snmp_pdu *response; // oid anOID[MAX_OID_LEN]; u_long anOID[MAX_OID_LEN]; size_t anOID_len = MAX_OID_LEN; int status; struct variable_list *vars; pdu = snmp_pdu_create(SNMP_MSG_GET); read_objid(oid.toUtf8().constData(), anOID, &anOID_len); snmp_add_null_var(pdu, anOID, anOID_len); status = snmp_synch_response(ss, pdu, &response); if (status != STAT_SUCCESS || response->errstat != SNMP_ERR_NOERROR) { qDebug("QSnmp: failed to get an oid"); if (response) snmp_free_pdu(response); return QVariant(); } for(vars = response->variables; vars; vars = vars->next_variable) { if (vars->type == ASN_OCTET_STR) { // string QByteArray res(vars->val_len, '\0'); memcpy(res.data(), vars->val.string, vars->val_len); snmp_free_pdu(response); return res; } if (vars->type == ASN_INTEGER) { int res = (int)*vars->val.integer; snmp_free_pdu(response); return res; } print_variable(vars->name, vars->name_length, vars); qDebug("QSnmp: unknown var type %d", vars->type); } snmp_free_pdu(response); return QVariant(); }
bool Session::getVariable(const QString &oid, QVariant &retvar, uint32_t &status) { QMutexLocker ml(&m_mutex); struct snmp_pdu *response = NULL; if ( ! m_session ) { status = 0xFFFFFFFF; return false; } // Buffer requestd for net-snmp library u_long anOID[MAX_OID_LEN]; size_t anOID_len = MAX_OID_LEN; // Prepare the PDU for a GET command struct snmp_pdu *pdu = snmp_pdu_create(SNMP_MSG_GET); get_node(oid.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; } retvar = snmpvarToVariant(response->variables); snmp_free_pdu(response); return true; }
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); }
/* * send_trap_to_sess: sends a trap to a session but assumes that the * pdu is constructed correctly for the session type. */ void send_trap_to_sess(netsnmp_session * sess, netsnmp_pdu *template_pdu) { netsnmp_pdu *pdu; netsnmp_pdu *response; int result; if (!sess || !template_pdu) return; DEBUGMSGTL(("trap", "sending trap type=%d, version=%d\n", template_pdu->command, sess->version)); #ifndef DISABLE_SNMPV1 if (sess->version == SNMP_VERSION_1 && (template_pdu->command != SNMP_MSG_TRAP)) return; /* Skip v1 sinks for v2 only traps */ #endif template_pdu->version = sess->version; pdu = snmp_clone_pdu(template_pdu); pdu->sessid = sess->sessid; /* AgentX only ? */ if ( template_pdu->command == SNMP_MSG_INFORM #ifdef USING_AGENTX_PROTOCOL_MODULE || template_pdu->command == AGENTX_MSG_NOTIFY #endif ) { result = snmp_synch_response(sess, pdu, &response); result = !result; /* XXX - different return code :-( */ } else result = snmp_send(sess, pdu); if (result == 0) { snmp_sess_perror("snmpd: send_trap", sess); /* snmp_free_pdu(pdu); */ } else { snmp_increment_statistic(STAT_SNMPOUTTRAPS); snmp_increment_statistic(STAT_SNMPOUTPKTS); } }
int ifstatus(int type, oid inst) { struct snmp_pdu *pdu, *resp; oid tmp_oid[] = { 1,3,6,1,2,1,2,2,1,0,0 }; int stat, tmp; tmp_oid[9]=type; tmp_oid[10]=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((int)resp->variables->val.integer[0] > 2) tmp=0; else tmp=(int)resp->variables->val.integer[0]; if(resp) snmp_free_pdu(resp); return tmp; }