void display_vars( struct snmp_pdu *response) { #ifdef DEBUG int c = 0; struct variable_list *vars; fprintf(stderr, "********************************************************\n"); for(vars = response->variables; vars; vars = vars->next_variable) { c++; fprintf(stderr, "**** oid count %d ******\n", c); if (CHECK_END(vars->type)) { fprint_variable(stderr, vars->name, vars->name_length, vars); /* print_variable(vars->name, vars->name_length, vars); */ } else dbg("display_vars(): No idea."); } fprintf(stderr, "********************************************************\n"); #endif }
/** * snmp_set2: Gets a single value indicated by the objectid * using snmp. * @handle: a handle to the snmp session needed to make an * snmp transaction. * @objid: string containing the OID entry. * @value: the value received from snmp will be put in this union. * * In the case of multiple values being returned, the type in 'value' will be * ASN_NULL (0x05). Nothing else in 'value' will be filled in. * Use snmp_get_all for doing gets that return multiple values. * * Return value: Returns 0 if successful, -1 if there was an error. **/ SaErrorT snmp_set2(struct snmp_session *ss, oid *objid, size_t objid_len, struct snmp_value *value) { struct snmp_pdu *pdu; struct snmp_pdu *response; struct variable_list *vars; void *dataptr = NULL; int status = 0; SaErrorT rtncode = SA_OK; /* Default - All is OK */ /* * Create the PDU for the data for our request. */ pdu = snmp_pdu_create(SNMP_MSG_SET); switch (value->type) { case ASN_INTEGER: case ASN_UNSIGNED: case ASN_COUNTER: dataptr = &value->integer; break; case ASN_OCTET_STR: dataptr = value->string; break; default: rtncode = SA_ERR_HPI_INVALID_PARAMS; dbg("datatype %c not yet supported by snmp_set2()", value->type); break; } if (rtncode == SA_OK) { /* * Set the data to send out */ /* Old code - snmp_add_var(pdu, objid, objid_len, datatype, dataptr); */ //int retcode = snmp_add_var(pdu, objid, objid_len, datatype, dataptr); snmp_pdu_add_variable(pdu, objid, objid_len, value->type, dataptr, value->str_len); /* * Send the Request out. */ status = snmp_synch_response(ss, pdu, &response); /* * Process the response. */ if (status == STAT_SUCCESS) { vars = response->variables; if (response->errstat == SNMP_ERR_NOERROR) { /* display data */ #ifdef DEBUG fprintf(stderr, "*** snmp_set2 ******************************************\n"); if (CHECK_END(response->variables->type)) { fprint_variable(stderr, response->variables->name, response->variables->name_length, response->variables); } else fprintf(stderr, "snmp_set2(): No idea.\n"); fprintf(stderr, "********************************************************\n"); #endif if (!(CHECK_END(response->variables->type)) ) { /* This is one of the exception condition */ rtncode = SA_ERR_HPI_NOT_PRESENT; dbg("snmp exception %d \n",vars->type); } } else { dbg("snmp_set2: Error in packet, Reason: %s", snmp_errstring(response->errstat)); rtncode = errstat2hpi(response->errstat); } } else { snmp_sess_perror("snmpset", ss); rtncode = snmpstat2hpi(status); } /* Clean up: free the response */ if (response) snmp_free_pdu(response); } return rtncode; }
/** * snmp_get2: Gets a single value indicated by the objectid * using snmp. * @handle: a handle to the snmp session needed to make an * snmp transaction. * @objid: string containing the OID entry. * @value: the value received from snmp will be put in this union. * * In the case of multiple values being returned, the type in 'value' will be * ASN_NULL (0x05). Nothing else in 'value' will be filled in. * Use snmp_get_all for doing gets that return multiple values. * * Return value: Returns 0 if successful, <0 if there was an error. **/ SaErrorT snmp_get2(struct snmp_session *ss, oid *objid, size_t objid_len, struct snmp_value *value) { struct snmp_pdu *pdu; struct snmp_pdu *response; struct variable_list *vars; SaErrorT returncode = SA_OK; int i; int status; /* * Create the PDU for the data for our request. */ pdu = snmp_pdu_create(SNMP_MSG_GET); snmp_add_null_var(pdu, objid, objid_len); /* * Send the Request out. */ status = snmp_synch_response(ss, pdu, &response); /* * Process the response. */ if (status == STAT_SUCCESS) { if (response->errstat == SNMP_ERR_NOERROR) { vars = response->variables; value->type = vars->type; if (vars->next_variable != NULL) { /* If there are more values, set return type to null. */ value->type = ASN_NULL; } else if ( !(CHECK_END(vars->type)) ) { /* This is one of the exception condition */ returncode = SA_ERR_HPI_NOT_PRESENT; dbg("snmp exception %d \n",vars->type); } else if ( (vars->type == ASN_INTEGER) || (vars->type == ASN_COUNTER) || (vars->type == ASN_UNSIGNED) ) { value->integer = *(vars->val.integer); } else { value->str_len = vars->val_len; if (value->str_len >= MAX_ASN_STR_LEN) value->str_len = MAX_ASN_STR_LEN; else value->string[value->str_len] = '\0'; memcpy(value->string, vars->val.string, value->str_len); } /* display data */ #ifdef DEBUG if (CHECK_END(vars->type)) { fprintf(stderr, "*** snmp_get2 ******************************************\n"); fprint_variable(stderr, vars->name, vars->name_length, vars); fprintf(stderr, "********************************************************\n"); } else { dbg("snmp_get2(): No idea.Unknown Type: %X", vars->type); fprint_variable(stderr, vars->name, vars->name_length, vars); } #endif } else { dbg("Error, Reason: %s", snmp_errstring(response->errstat)); fprintf(stderr, "objid: "); for(i = 0; i<objid_len; i++ ) fprintf(stderr, "%d.", (int)objid[i]); fprintf(stderr, "\n"); returncode = errstat2hpi(response->errstat); } } else { snmp_sess_perror("snmpget", ss); returncode = snmpstat2hpi(status); } /* Clean up: free the response */ sc_free_pdu(&response); return (returncode); }
void get_table_entries(netsnmp_session * ss) { int running = 1; netsnmp_pdu *pdu, *response; netsnmp_variable_list *vars; int count; int status; int i; int col; char *buf = NULL; size_t out_len = 0, buf_len = 0; char *cp; char *name_p = NULL; char **dp; int have_current_index; /* * TODO: * 1) Deal with multiple index fields * 2) Deal with variable length index fields * 3) optimize to remove a sparse column from get-requests */ while (running && ((max_width && !column_width) || (entries < max_getbulk))) { /* * create PDU for GETNEXT request and add object name to request */ pdu = snmp_pdu_create(SNMP_MSG_GETNEXT); for (i = 1; i <= fields; i++) { name[rootlen] = column[i - 1].subid; snmp_add_null_var(pdu, name, name_length); } /* * do the request */ status = snmp_synch_response(ss, pdu, &response); if (status == STAT_SUCCESS) { if (response->errstat == SNMP_ERR_NOERROR) { /* * check resulting variables */ vars = response->variables; entries++; if (entries >= allocated) { if (allocated == 0) { allocated = 10; data = (char **) malloc(allocated * fields * sizeof(char *)); memset(data, 0, allocated * fields * sizeof(char *)); if (show_index) indices = (char **) malloc(allocated * sizeof(char *)); } else { allocated += 10; data = (char **) realloc(data, allocated * fields * sizeof(char *)); memset(data + entries * fields, 0, (allocated - entries) * fields * sizeof(char *)); if (show_index) indices = (char **) realloc(indices, allocated * sizeof(char *)); } } dp = data + (entries - 1) * fields; col = -1; end_of_table = 1; /* assume end of table */ have_current_index = 0; name_length = rootlen + 1; for (vars = response->variables; vars; vars = vars->next_variable) { col++; name[rootlen] = column[col].subid; if ((vars->name_length < name_length) || (vars->name[rootlen] != column[col].subid) || memcmp(name, vars->name, name_length * sizeof(oid)) != 0 || vars->type == SNMP_ENDOFMIBVIEW) { /* * not part of this subtree */ if (localdebug) { fprint_variable(stderr, vars->name, vars->name_length, vars); fprintf(stderr, " => ignored\n"); } continue; } /* * save index off */ if (!have_current_index) { end_of_table = 0; have_current_index = 1; name_length = vars->name_length; memcpy(name, vars->name, name_length * sizeof(oid)); out_len = 0; if (!sprint_realloc_objid ((u_char **)&buf, &buf_len, &out_len, 1, vars->name, vars->name_length)) { break; } i = vars->name_length - rootlen + 1; if (localdebug || show_index) { if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_EXTENDED_INDEX)) { name_p = strchr(buf, '['); } else { switch (netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT)) { case NETSNMP_OID_OUTPUT_MODULE: case 0: name_p = strchr(buf, ':'); break; case NETSNMP_OID_OUTPUT_SUFFIX: name_p = buf; break; case NETSNMP_OID_OUTPUT_FULL: case NETSNMP_OID_OUTPUT_NUMERIC: case NETSNMP_OID_OUTPUT_UCD: name_p = buf + strlen(table_name)+1; name_p = strchr(name_p, '.')+1; break; default: fprintf(stderr, "Unrecognized -O option: %d\n", netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT)); exit(1); } name_p = strchr(name_p, '.') + 1; } } if (localdebug) { printf("Name: %s Index: %s\n", buf, name_p); } if (show_index) { indices[entries - 1] = strdup(name_p); i = strlen(name_p); if (i > index_width) index_width = i; } } if (localdebug && buf) { printf("%s => taken\n", buf); } out_len = 0; sprint_realloc_value((u_char **)&buf, &buf_len, &out_len, 1, vars->name, vars->name_length, vars); for (cp = buf; *cp; cp++) { if (*cp == '\n') { *cp = ' '; } } dp[col] = buf; i = out_len; buf = NULL; buf_len = 0; if (i > column[col].width) { column[col].width = i; } } if (end_of_table) { --entries; /* * not part of this subtree */ if (localdebug) { printf("End of table: %s\n", buf ? (char *) buf : "[NIL]"); } running = 0; continue; } } else { /* * error in response, print it */ running = 0; if (response->errstat == SNMP_ERR_NOSUCHNAME) { printf("End of MIB\n"); end_of_table = 1; } else { fprintf(stderr, "Error in packet.\nReason: %s\n", snmp_errstring(response->errstat)); if (response->errindex != 0) { fprintf(stderr, "Failed object: "); for (count = 1, vars = response->variables; vars && count != response->errindex; vars = vars->next_variable, count++) /*EMPTY*/; if (vars) { fprint_objid(stderr, vars->name, vars->name_length); } fprintf(stderr, "\n"); } exitval = 2; } } } else if (status == STAT_TIMEOUT) { fprintf(stderr, "Timeout: No Response from %s\n", ss->peername); running = 0; exitval = 1; } else { /* status == STAT_ERROR */ snmp_sess_perror("snmptable", ss); running = 0; exitval = 1; } if (response) snmp_free_pdu(response); } }