int CSVBaseSNMP::GetBulkRequest(MonitorResult &ResultList) { WriteLog("\n\n*****************************"); WriteLog("GetBulkRequest!"); static const int BULK_BUFF = 10; int nResult = 0; char chPrvOID[MAX_BUFF_LEN] = {0}; bool bEnd = false; Pdu pdu; // construct a Pdu object Vb vb; // construct a Vb object vb.set_oid( oid); // set the Oid portion of the Vb pdu += vb; // add the vb to the Pdu SnmpTarget *target; if(version ==version3) {//If SNMP Version Is 3 nResult = InitUTarget(); //Init UTarget pdu.set_security_level( m_lSecurityLevel); //Set the Security Level portion of Pdu pdu.set_context_name (m_szContextName); //Set the Context Name portion of Pdu pdu.set_context_engine_id(m_szContextEngineID); //Set the Context Engine ID portion of Pdu target = &m_Utarget; //Set SNMP Target } else { target = &m_Ctarget; //Set SNMP Target } try { if(m_pSnmp) { int nIndex = 0, i = 0; while (( nResult = m_pSnmp->get_bulk(pdu, *target, 0, BULK_BUFF)) == SNMP_CLASS_SUCCESS) { if(bEnd) break; for (i = 0; i < pdu.get_vb_count(); i ++) { pdu.get_vb( vb, i); if (pdu.get_type() == REPORT_MSG) { Oid tmp; vb.get_oid(tmp); return -5; } // look for var bind exception, applies to v2 only if ( vb.get_syntax() != sNMP_SYNTAX_ENDOFMIBVIEW) { string szOID = vb.get_printable_oid(); WriteLog(szOID.c_str()); int nPosition = static_cast<int>(szOID.find(m_szStartID)); if(nPosition < 0) { bEnd = true; break; } if(static_cast<int>(strlen(chPrvOID)) > 0) {//如果上次OID不为空 if(strcmp(vb.get_printable_oid(), chPrvOID) == 0) {//比较OID名称是否相同,相同则退出循环 bEnd = true; break; } } //结果赋值 if(static_cast<int>(strlen(vb.get_printable_oid())) < MAX_BUFF_LEN) strcpy(chPrvOID, vb.get_printable_oid()); SNMP_Monitor_Result result; result.m_szOID = vb.get_printable_oid(); if(strcmp(result.m_szOID.substr(0,19).c_str(),"1.3.6.1.2.1.2.2.1.2")==0) { char str[100]; vb.get_value(str); result.m_szValue=str; WriteLog(str); } else result.m_szValue = vb.get_printable_value(); //nPosition = static_cast<int>(result.m_szOID.find(m_szStartID.c_str()) + m_szStartID.length()); //if(nPosition > 0) //{ result.m_szIndex = result.m_szOID.substr(m_szStartID.length() + 1); result.m_szOID = result.m_szOID.substr(0, m_szStartID.length()); //PrintDebugString("index is " + result.m_szIndex); //} WriteLog(result.m_szIndex.c_str()); WriteLog(result.m_szValue.c_str()); ResultList[nIndex] = result; nIndex ++; } else { m_szErrorMsg = "End of MIB Reached"; return -4; } } // last vb becomes seed of next rquest pdu.set_vblist(&vb, 1); } } } catch(...) { DWORD dwError = GetLastError(); char szMsg[512] = {0}; int nlen = sprintf(szMsg, "Error Number is %08X --*GetBulkRequest*---", dwError); DumpLog("snmpmonitor-bulk.log", szMsg, nlen); } if(nResult == SNMP_ERROR_NO_SUCH_NAME) { nResult = 0; } return nResult; }
int wpdu::convert_vb_to_smival( Vb &tempvb, SmiVALUE *smival ) { smival->syntax = tempvb.get_syntax(); switch ( smival->syntax ) { case sNMP_SYNTAX_NULL: break; // case sNMP_SYNTAX_INT32: case sNMP_SYNTAX_INT: { SnmpInt32 tmp; tempvb.get_value(tmp); smival->value.sNumber = tmp; } break; // case sNMP_SYNTAX_UINT32: case sNMP_SYNTAX_GAUGE32: case sNMP_SYNTAX_CNTR32: case sNMP_SYNTAX_TIMETICKS: { SnmpUInt32 tmp; tempvb.get_value(tmp); smival->value.uNumber = tmp; } break; // case Counter64 case sNMP_SYNTAX_CNTR64: { Counter64 c64; tempvb.get_value(c64); smival->value.hNumber.hipart = c64.high(); smival->value.hNumber.lopart = c64.low(); } break; // OID syntax case sNMP_SYNTAX_OID: { Oid tmpoid; tmpoid.oidval(); tempvb.get_value(tmpoid); SmiLPOID smi = tmpoid.oidval(); smival->value.oid.len = tmpoid.length(); ACE_NEW_RETURN(smival->value.oid.ptr, SmiUINT32 [smival->value.oid.len], 1); ACE_OS::memcpy(smival->value.oid.ptr, smi->ptr, smival->value.oid.len *sizeof(SmiUINT32)); } break; case sNMP_SYNTAX_BITS: case sNMP_SYNTAX_OCTETS: case sNMP_SYNTAX_IPADDR: { OctetStr os; tempvb.get_value(os); smival->value.string.ptr = 0; smival->value.string.len = os.length(); if ( smival->value.string.len > 0 ) { ACE_NEW_RETURN(smival->value.string.ptr, SmiBYTE [smival->value.string.len], 1); if ( smival->value.string.ptr ) { for (int i=0; i<(int) smival->value.string.len ; i++) smival->value.string.ptr[i] = os[i]; } else { smival->syntax = sNMP_SYNTAX_NULL; // invalidate the smival return SNMP_CLASS_RESOURCE_UNAVAIL; } } } break; default: ACE_DEBUG((LM_DEBUG, "wpdu::convert_vb_to_smival did not convert vb\n")); // ACE_ASSERT(0); } // switch return 0; }
//------------[ convert SNMP++ VB to WinSNMP smiVALUE ]---------------- int convertVbToSmival( const Vb &tempvb, SmiVALUE *smival ) { smival->syntax = tempvb.get_syntax(); switch ( smival->syntax ) { // case sNMP_SYNTAX_NULL case sNMP_SYNTAX_NULL: case sNMP_SYNTAX_NOSUCHOBJECT: case sNMP_SYNTAX_NOSUCHINSTANCE: case sNMP_SYNTAX_ENDOFMIBVIEW: break; // case sNMP_SYNTAX_INT32: case sNMP_SYNTAX_INT: tempvb.get_value(smival->value.sNumber); break; // case sNMP_SYNTAX_UINT32: case sNMP_SYNTAX_GAUGE32: case sNMP_SYNTAX_CNTR32: case sNMP_SYNTAX_TIMETICKS: // case sNMP_SYNTAX_UINT32: tempvb.get_value(smival->value.uNumber); break; // case Counter64 case sNMP_SYNTAX_CNTR64: { Counter64 c64; tempvb.get_value(c64); smival->value.hNumber.hipart = c64.high(); smival->value.hNumber.lopart = c64.low(); } break; case sNMP_SYNTAX_BITS: case sNMP_SYNTAX_OCTETS: case sNMP_SYNTAX_OPAQUE: case sNMP_SYNTAX_IPADDR: { OctetStr os; tempvb.get_value(os); smival->value.string.ptr = NULL; smival->value.string.len = os.len(); if ( smival->value.string.len > 0 ) { smival->value.string.ptr = (SmiLPBYTE) new unsigned char [smival->value.string.len]; if ( smival->value.string.ptr ) { for (int i=0; i<(int) smival->value.string.len ; i++) smival->value.string.ptr[i] = os[i]; } else { smival->syntax = sNMP_SYNTAX_NULL; // invalidate the smival return SNMP_CLASS_RESOURCE_UNAVAIL; } } } break; case sNMP_SYNTAX_OID: { Oid oid; tempvb.get_value(oid); smival->value.oid.ptr = NULL; smival->value.oid.len = oid.len(); if ( smival->value.oid.len > 0 ) { smival->value.oid.ptr = (SmiLPUINT32) new unsigned long [ smival->value.oid.len]; if ( smival->value.oid.ptr ) { for (int i=0; i<(int)smival->value.oid.len ; i++) smival->value.oid.ptr[i] = oid[i]; } else { smival->syntax = sNMP_SYNTAX_NULL; // invalidate the smival return SNMP_CLASS_RESOURCE_UNAVAIL; } } } break; default: return SNMP_CLASS_INTERNAL_ERROR; } return SNMP_CLASS_SUCCESS; }