/* * 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; }
QtNetSNMP::QMIBTree *QtNetSNMP::QSNMPManager::getMIBFile(const QString& fileName) throw(QSNMPException) { if(!QFile(fileName).exists()) throw QSNMPException("QSNMPManager :: Get MIB File :: File not found"); return _core -> getMIBTree(read_mib(fileName.toStdString().c_str())); }
void init_mib(char *file) { if (Mib != NULL) return; if (file != NULL) Mib = read_mib(file); }
int load_mib(char *path, int MibType) { if (Mib != NULL) return(0); if (MibType == OLD_CMU_SNMP_MIB) Mib = read_mib(path); else if (MibType == SNMPV2_MIB) Mib = read_mib_v2(path); if (Mib == NULL) return(0); set_functions(Mib); return(1); }
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); }
int init_mib(void) { char *file;//, *getenv(); if (Mib != NULL) return (0); /* First, try the new parser if the variable exists. */ file = getenv("MIBFILE_v2"); if (file != NULL) Mib = read_mib_v2(file); /* Then our overrides */ if (Mib == NULL) if ((file = getenv("MIBFILE")) != NULL) Mib = read_mib(file); /* Then the default mibfiles */ if (Mib == NULL) Mib = read_mib_v2("mib-v2.txt"); if (Mib == NULL) Mib = read_mib_v2("/etc/mib-v2.txt"); #ifndef WIN32 #define MIBDIR "." if (Mib == NULL) { char path[MAXPATHLEN]; sprintf(path, WIDE("%s/mib-v2.txt"), MIBDIR); Mib = read_mib_v2(path); } #endif /* WIN32 */ /* And finally the old faithful files. */ if (Mib == NULL) Mib = read_mib("mib.txt"); if (Mib == NULL) Mib = read_mib("/etc/mib.txt"); #ifndef WIN32 if (Mib == NULL) { char path[MAXPATHLEN]; sprintf(path, WIDE("%s/mib.txt"), MIBDIR); Mib = read_mib(path); } #endif /* WIN32 */ #ifdef WIN32 if (Mib == NULL) { /* Fetch the name from the registry */ long ret; HKEY hKey; DWORD Type; #define MAX_VALUE_NAME 128 #define KEY "SOFTWARE\\Carnegie Mellon\\Network Group\\SNMP Library" TCHAR ValueName[MAX_VALUE_NAME]; DWORD dwcValueName = MAX_VALUE_NAME; ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _TEXT(KEY), 0, KEY_READ, &hKey); if (ret == ERROR_SUCCESS) { /* Found this registry entry. */ ret = RegQueryValueEx(hKey, _TEXT("MIB Location"), NULL, &Type, (LPBYTE)&ValueName, &dwcValueName); if (ret == ERROR_SUCCESS) { #ifdef UNICODE _tprintf(_TEXT("Found '%s'\n"), ValueName); { char lpszBuf[MAX_VALUE_NAME]; if (WideCharToMultiByte(CP_ACP, 0, ValueName, WC_SEPCHARS, // -1 lpszBuf, dwcValueName, NULL, NULL)) { Mib = read_mib_v2(lpszBuf); } } #else /* UNICODE */ Mib = read_mib_v2(ValueName); #endif /* UNICODE */ } else { /* Unable to read key */ LPVOID lpMsgBuf; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, ret, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (TCHAR *)&lpMsgBuf, 0, NULL); #ifdef STDERR_OUTPUT fprintf(stderr, WIDE("Reg Read Error: %s\n"), (LPTSTR)lpMsgBuf); #endif } /* And close the registry */ RegCloseKey(hKey); } else { /* Unable to open key */ LPVOID lpMsgBuf; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, ret, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (TCHAR *)&lpMsgBuf, 0, NULL); #ifdef STDERR_OUTPUT fprintf(stderr, WIDE("Reg Open Error: %s\n"), (LPTSTR)lpMsgBuf); #endif } } #endif /* WIN32 */ if (Mib == NULL) { #ifdef STDERR_OUTPUT fprintf(stderr, WIDE("Couldn't find mib file\n")); #endif return(0); } set_functions(Mib); return(1); }