⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bmssnmpagent.cpp

📁 利用Windows自带的api采用snmp协议读取网络设备运行状态。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "StdAfx.h"
#include "BmsSnmpAgent.h"

#pragma comment(lib,"mgmtapi")
#pragma comment(lib,"snmpapi")
BmsSnmpAgent::BmsSnmpAgent(void):m_isIni(false),m_commStr("public"),m_ip("127.0.0.1"),m_isConn(false),m_lastErMsg(""),m_lpMgrSession(NULL)
{
		
}

BmsSnmpAgent::~BmsSnmpAgent(void)
{
	this->disConnectSnmpServer();
}

bool BmsSnmpAgent::iniAgent( const std::string IpAd ,const std::string commStr )
{
	//TODO 加锁

	//TODO check ip 
	this->m_ip = IpAd;
	this->m_commStr = commStr;
	this->m_isIni = true;
	return true;
}

bool BmsSnmpAgent::connectSnmpServer()
{
	//TODO 加锁
	IceUtil::Mutex::Lock sync_lmxist(m_mutex);
	if(!this->m_isIni){
		m_lastErMsg = "conn::not ini";
		return false;
	}
	CString ip = m_ip.c_str();
	CString commStr = m_commStr.c_str();		 
	//m_lpMgrSession = SnmpMgrOpen(ip.GetBuffer(0),commStr.GetBuffer(0),1000,3);
	m_lpMgrSession = SnmpMgrOpen(ip.GetBuffer(0),commStr.GetBuffer(0),1000,3);
	if(m_lpMgrSession == NULL)
	{
		return false;
	}
	this->m_isConn = true;
	return true;
}

bool BmsSnmpAgent::reConnectSnmpServer()
{
	//TODO 加锁
	
	//复位
	this->m_isConn = false;
	//停服务	
	if(disConnectSnmpServer()){
		if(this->connectSnmpServer())
			return true;
	}
	return false;
	
}

void BmsSnmpAgent::getLastErMsg( ::std::string &Msg )
{
	Msg = m_lastErMsg;	
}


bool BmsSnmpAgent::walkNode( const std::string &Oid,BmsSnmpNodeList& NodeList )
{  AfxMessageBox("xx");
	char *szOID = (char*)malloc(sizeof(char)*255);
	char *asciiStr, *tmpStr;
	char *szListEntry;

	SnmpVarBindList snmpVarList;
	AsnObjectIdentifier asnOid, asnOidTemp;

	AsnInteger	errorStatus=0;	// Error type that is returned if encountered
	AsnInteger	errorIndex=0;		// Works with variable above

	snmpVarList.list = NULL;
	snmpVarList.len = 0;

	CString _oid = Oid.c_str();
	if(!SnmpMgrStrToOid(_oid.GetBuffer(0), &asnOid))
	{
		m_lastErMsg = "getNodeValue Oid is valid";
		return false;
	}

	snmpVarList.len++;

	snmpVarList.list = (SnmpVarBind *)SNMP_realloc(snmpVarList.list, sizeof(SnmpVarBind) *snmpVarList.len); 

	// Assigning OID to variable bindings list
	SnmpUtilOidCpy(&snmpVarList.list[0].name,&asnOid);
	snmpVarList.list[0].value.asnType = ASN_NULL;

	for(;;)
	{
		// one by one get the variables
		if(!SnmpMgrRequest(m_lpMgrSession,SNMP_PDU_GETNEXT, &snmpVarList, &errorStatus, &errorIndex))
		{
			asciiStr = (char*)malloc(sizeof(char)*255);
			PrintStatusError(errorStatus,tmpStr);
			sprintf(asciiStr,"Snmp Request Failed\nErrorStatus: %s  ErrorIndex: %d",tmpStr,errorIndex);
			AfxMessageBox("1");
			AfxMessageBox(asciiStr);
			free(asciiStr);
			free(tmpStr);
			free(szOID);


			SnmpUtilVarBindListFree(&snmpVarList);
			SnmpUtilOidFree(&asnOid);
			return false;			
		}
		if(errorStatus == SNMP_ERRORSTATUS_NOSUCHNAME||SnmpUtilOidNCmp(&snmpVarList.list[0].name,&asnOid, asnOid.idLength))
			break;

		if(errorStatus > 0)
		{
			asciiStr = (char*)malloc(sizeof(char)*255);
			PrintStatusError(errorStatus,tmpStr);
			sprintf(asciiStr,"ErrorStatus: %s  ErrorIndex: %d",tmpStr,errorIndex);
			AfxMessageBox("2");
			AfxMessageBox(asciiStr);
			free(asciiStr);
			free(tmpStr);
			free(szOID);
			break;
		}
		else
		{
			char *szOidString = NULL;
			if(snmpVarList.list[0].name.idLength)
			{
				szOidString = (char *) SnmpUtilMemAlloc(snmpVarList.list[0].name.idLength * 5 );
				if(szOidString)
				{
					UINT i;
					char szBuf[17];
					strcpy(szOidString,".");
					for(i = 0; i < snmpVarList.list[0].name.idLength; i++)
					{
						lstrcat(szOidString, itoa(snmpVarList.list[0].name.ids[i], szBuf, 10 ) );
						if(i < snmpVarList.list[0].name.idLength-1)
							lstrcat(szOidString, ".");
					}
				}
			}
			//SnmpMgrOidToStr(&snmpVarList.list[0].name, &szOidString);

			// i had problems in SnmpMgrOidToStr so I created this custome func to convert to string
			asciiStr = SNMP_AnyToStr(&snmpVarList.list[0].value);
			if(!asciiStr)
			{
				asciiStr = (char*)SnmpUtilMemAlloc(5);
				strcpy(asciiStr,"");
			}

			szListEntry = (char*)malloc(sizeof(char)*(strlen(asciiStr)+strlen(szOidString)+5));

			strcpy(szListEntry,szOidString);
			strcat(szListEntry," : ");
			strcat(szListEntry,asciiStr);

			AfxMessageBox("3");
			AfxMessageBox(szListEntry);

			SnmpUtilMemFree(asciiStr);
			SNMP_free(szOidString);
			free(szListEntry);
		}
		// Prepare for the next iteration.  Make sure returned oid is
		// preserved and the returned value is freed.

		SnmpUtilOidCpy(&asnOidTemp, &snmpVarList.list[0].name);

		SnmpUtilVarBindFree(&snmpVarList.list[0]);

		SnmpUtilOidCpy(&snmpVarList.list[0].name, &asnOidTemp);
		snmpVarList.list[0].value.asnType = ASN_NULL;

		SnmpUtilOidFree(&asnOidTemp);
	}
	SnmpUtilVarBindListFree(&snmpVarList);
	SnmpUtilOidFree(&asnOid);
	free(szOID);
	return true;
}


bool BmsSnmpAgent::walkNode(const std::string &Oid,SnmpTable*TableList)
{
	TableList->clear();
	char *szOID = (char*)malloc(sizeof(char)*255);
	char *asciiStr, *tmpStr;
	char *szListEntry;

	SnmpVarBindList snmpVarList;
	AsnObjectIdentifier asnOid, asnOidTemp;

	AsnInteger	errorStatus=0;	// Error type that is returned if encountered
	AsnInteger	errorIndex=0;		// Works with variable above

	snmpVarList.list = NULL;
	snmpVarList.len = 0;

	CString _oid = Oid.c_str();
	if(!SnmpMgrStrToOid(_oid.GetBuffer(0), &asnOid))
	{
		m_lastErMsg = "getNodeValue Oid is valid";
		return false;
	}

	snmpVarList.len++;

	snmpVarList.list = (SnmpVarBind *)SNMP_realloc(snmpVarList.list, sizeof(SnmpVarBind) *snmpVarList.len); 

	// Assigning OID to variable bindings list
	SnmpUtilOidCpy(&snmpVarList.list[0].name,&asnOid);
	snmpVarList.list[0].value.asnType = ASN_NULL;

	for(;;)
	{
		// one by one get the variables
		if(!SnmpMgrRequest(m_lpMgrSession,SNMP_PDU_GETNEXT, &snmpVarList, &errorStatus, &errorIndex))
		{
			asciiStr = (char*)malloc(sizeof(char)*255);
			PrintStatusError(errorStatus,tmpStr);
			sprintf(asciiStr,"Snmp Request Failed\nErrorStatus: %s  ErrorIndex: %d",tmpStr,errorIndex);
			free(asciiStr);
			free(tmpStr);
			free(szOID);


			SnmpUtilVarBindListFree(&snmpVarList);
			SnmpUtilOidFree(&asnOid);
			return false;			
		}
		if(errorStatus == SNMP_ERRORSTATUS_NOSUCHNAME||SnmpUtilOidNCmp(&snmpVarList.list[0].name,&asnOid, asnOid.idLength))
			break;

		if(errorStatus > 0)
		{
			asciiStr = (char*)malloc(sizeof(char)*255);
			PrintStatusError(errorStatus,tmpStr);
			sprintf(asciiStr,"ErrorStatus: %s  ErrorIndex: %d",tmpStr,errorIndex);
			free(asciiStr);
			free(tmpStr);
			free(szOID);
			break;
		}
		else
		{
			char *szOidString = NULL;
			if(snmpVarList.list[0].name.idLength)
			{
				szOidString = (char *) SnmpUtilMemAlloc(snmpVarList.list[0].name.idLength * 5 );
				if(szOidString)
				{
					UINT i;
					char szBuf[17];
					strcpy(szOidString,".");
					for(i = 0; i < snmpVarList.list[0].name.idLength; i++)
					{
						lstrcat(szOidString, itoa(snmpVarList.list[0].name.ids[i], szBuf, 10 ) );
						if(i < snmpVarList.list[0].name.idLength-1)
							lstrcat(szOidString, ".");
					}
				}
			}
			//SnmpMgrOidToStr(&snmpVarList.list[0].name, &szOidString);

			// i had problems in SnmpMgrOidToStr so I created this custome func to convert to string
			asciiStr = SNMP_AnyToStr(&snmpVarList.list[0].value);
			if(!asciiStr)
			{
				asciiStr = (char*)SnmpUtilMemAlloc(5);
				strcpy(asciiStr,"");
			}

			szListEntry = (char*)malloc(sizeof(char)*(strlen(asciiStr)+strlen(szOidString)+5));

			strcpy(szListEntry,szOidString);
			strcat(szListEntry," : ");
			strcat(szListEntry,asciiStr);

			SnmpTableFiled tabInfo;
			tabInfo.setOid(szOidString);
			tabInfo.setValues(asciiStr);
			TableList->push_back(tabInfo),
			//TableList->insert(TableList->end(),tabInfo);
// 			SnmpTableItem Item=TableList->end();
// 			(--Item)->setOid(szOidString);
// 			(--Item)->setValues(asciiStr);

			SnmpUtilMemFree(asciiStr);
			SNMP_free(szOidString);
			free(szListEntry);
		}
		// Prepare for the next iteration.  Make sure returned oid is
		// preserved and the returned value is freed.

		SnmpUtilOidCpy(&asnOidTemp, &snmpVarList.list[0].name);

		SnmpUtilVarBindFree(&snmpVarList.list[0]);

		SnmpUtilOidCpy(&snmpVarList.list[0].name, &asnOidTemp);
		snmpVarList.list[0].value.asnType = ASN_NULL;

		SnmpUtilOidFree(&asnOidTemp);
	}
	SnmpUtilVarBindListFree(&snmpVarList);
	SnmpUtilOidFree(&asnOid);
	free(szOID);
	return true;
}

bool BmsSnmpAgent::disConnectSnmpServer()
{
	//TODO 加锁

	//复位
	this->m_isConn = false;
	//停服务
	if(m_lpMgrSession!=NULL){
		if(SnmpMgrClose(m_lpMgrSession)){
			return true;
		}
	}
	return false;

}

bool BmsSnmpAgent::getNodeValue( const std::string &Oid,std::string &OidValues)
{
	//check ini
	if(!this->m_isIni||!this->m_isConn)
			return false;
	//
	AsnObjectIdentifier asnOid;
	CString _oid = Oid.c_str();
	if(!SnmpMgrStrToOid(_oid.GetBuffer(0), &asnOid))
	{
		m_lastErMsg = "getNodeValue Oid is valid";
		return false;
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -