/** * \file mibquery.c * * Extracts requested data from MIB II. * * Makes query for specified data to MIB II using SNMP protocol. * * \author Tomas Jirak, Vladimir Sochor * \date 2003 */ #include "snmp.h" #include "snmp_impl.h" #include "snmp_client.h" #include "mib.h" #include "mibquery.h" #include #include #include #include #include extern int val_count; extern char *tab_values[]; extern struct snmp_session *p_session; /** * Prints error message on the standard output. */ void error_msg(int errCode, char *errMsg) { printf("ERROR: %s\n", errMsg); printf("error code %d: %s\n", errCode, strerror(errCode)); return; } /** * Odesle dotaz SNMP agentovi bezicim na stroji 'host' na obsah objektu 'mibTable'. * Pri uspechu ulozi ziskana data do pole 'tab_values' a vrati 1, jinak vrati 0. */ int mib_query(char *host, char *mibTable) { struct snmp_pdu *pdu, *response; struct variable_list *vars; char obj_out[MAX_OID_LEN]; char val_out[BUF_SIZE]; char buff[BUF_SIZE]; oid name[MAX_NAME_LEN]; int name_len; oid root[MAX_NAME_LEN]; int root_len; int i; int status, ret; int continue_flag = 1; root_len = MAX_NAME_LEN; if (!read_objid(mibTable, &root[0], &root_len)) error_msg(errno, "Invalid object identifier"); name_len = root_len; bcopy((char *) &root[0], (char *) &name[0], root_len*sizeof(oid)); while (continue_flag) { pdu = snmp_pdu_create(GETNEXT_REQ_MSG); snmp_add_null_var(pdu, name, name_len); status = snmp_synch_response(p_session, pdu, &response); ret = 1; switch (status) { /* status == STAT_SUCCESS */ case STAT_SUCCESS: if (response->errstat == SNMP_ERR_NOERROR) { if (response->command == REPORT_MSG) { printf("Operation wasn't succesfull -- error report.\n"); for (vars = response->variables; vars; vars = vars->next_variable) { sprint_objid(obj_out, vars->name, vars->name_length); sprint_variable(val_out, vars->name, vars->name_length, vars); } } vars = response->variables; if (vars->name_length < root_len || bcmp(&root[0], vars->name, root_len*sizeof(oid))) { continue_flag = 0; break; } if (vars->type != SNMP_ENDOFMIBVIEW) { if (response->command == REPORT_MSG) printf("This is an error report\n"); sprint_objid(obj_out, vars->name, vars->name_length); sprint_variable(buff, vars->name, vars->name_length, vars); strcpy(val_out, (char *) &buff[strlen(obj_out)]); for (i = 0; i < strlen(val_out); i++) if (val_out[i] == '\n') val_out[i] = '\0'; tab_values[val_count] = (char *) malloc(BUF_SIZE); strcpy(tab_values[val_count], val_out); val_count++; bcopy((char *) vars->name, (char *) &name[0], (vars->name_length)*sizeof(oid)); name_len = vars->name_length; } } else { /* response->errstat != SNMP_ERR_NOERROR */ continue_flag = 0; if (response->errstat == SNMP_ERR_NOSUCHNAME) { strcpy(obj_out, "End of MIB."); ret = 0; } else { strcpy(obj_out, "Error in packet."); if (response->errstat == SNMP_ERR_NOSUCHNAME) { strcpy(val_out, "The request for object identifier failed."); ret = 0; } } } break; /* status == STAT_TIMEOUT */ case STAT_TIMEOUT: error_msg(errno, "No response from host."); continue_flag = 0; ret = 0; break; /* status == STAT_ERROR */ case STAT_ERROR: error_msg(errno, "An unknown error occurred."); continue_flag = 0; ret = 0; break; default: continue_flag = 0; break; } } //while if (response) snmp_free_pdu(response); return(ret); } /** * Prints results - data from 'at' table - on standard output. */ void print_table(int count) { int i; int i_max = count/3; int i_max2 = 2*i_max; printf("------------------------------------------------------------------\n"); printf(" atIfIndex | atPhysAddress | atNetAddress\n"); printf("------------------------------------------------------------------\n\n"); for (i = 0; i < i_max; i++) printf(" %s | %s | %s\n", tab_values[i], tab_values[i+i_max], tab_values[i+i_max2]); printf("------------------------------------------------------------------\n\n"); printf("Totally %d rows.\n\n", i_max); }