Zdrojové kódy pro vývojáře.
Přeskočit odkazy pro navigaci Top 10 přispěvatelů
UživatelČlánky
codeshare45
sochor1
stoupa1
tomas.oplt15
Článek: Jak odeslat SNMP Trap (Simple network monitor protocol)
Špatný Super
Autor:
Vytvořeno:
Popularita:

BSTR MyMessage;
	ZeroMemory(charInfoString,sizeof("MyMessage"));
	::WideCharToMultiByte(CP_ACP, 0, MyMessage  , -1, charInfoString, 1024, NULL, NULL);
	
	// SNMP startup
	lStat = SnmpStartup (&lStat, &lStat, &lStat, &lStat, &lStat);
	if (lStat != SNMPAPI_SUCCESS)
	{
		switch ( lStat )
		{
			case SNMPAPI_ALLOC_ERROR:        
				throw _T("SNMPAPI_ALLOC_ERROR");
				break;
			case SNMPAPI_NOT_INITIALIZED:
				throw _T("SNMPAPI_NOT_INITIALIZED");					
				break;
			case SNMPAPI_OTHER_ERROR:	     
				throw _T("SNMPAPI_OTHER_ERROR");					
				break;
		}			
	}
	bSnmpStartup = TRUE;
	
	hSession = SnmpCreateSession (NULL, 0, cbFunc, NULL);
	if (hSession == SNMPAPI_FAILURE)
		throw _T("SnmpCreateSession :: SNMPAPI_FAILURE"); 
	bSnmpCreateSession = TRUE;

	lStat = SnmpSetTranslateMode (SNMPAPI_UNTRANSLATED_V1);
	if (lStat == SNMPAPI_FAILURE)
		throw _T("SnmpSetTranslateMode :: SNMPAPI_FAILURE");

	HSNMP_ENTITY hDst = SnmpStrToEntity (hSession, m_DestinationAgentIP);
	if (hDst == SNMPAPI_FAILURE)
		throw _T("SnmpStrToEntity :: SNMPAPI_FAILURE");

	HSNMP_CONTEXT hContext = SnmpStrToContext (hSession, &cComunityName);
	if (hContext == SNMPAPI_FAILURE)
		throw _T("SnmpStrToContext :: SNMPAPI_FAILURE");

	HSNMP_VBL hVbl = SnmpCreateVbl (hSession, NULL, NULL);
	if (hVbl == SNMPAPI_FAILURE)
		throw _T("SnmpCreateVbl :: SNMPAPI_FAILURE");

	// SysUpTime VALUE
	smiVALUE  valSysUpTime;
	smiUINT32 sysUpTime[] = {1,3,6,1,2,1,1,3,0};					          // DEFAULT array values for SNMP TRAP SysUpTime
	smiOID dSysUpTimeName = {sizeof(sysUpTime)/sizeof(smiUINT32), sysUpTime}; // number of elements in sysUpTime array
	valSysUpTime.syntax = SNMP_SYNTAX_TIMETICKS;
	valSysUpTime.value.uNumber = GetTickCount() / 10;
	SnmpSetVb (hVbl, 0, &dSysUpTimeName, &valSysUpTime);
	
	// OID VALUE 
	smiVALUE  valTrap;
	smiOID dTrapName = {sizeof(SNMPTrapOid)/sizeof(smiUINT32), SNMPTrapOid};     // number of elements in snmpTrapOid array
	valTrap.syntax = SNMP_SYNTAX_OID;
	valTrap.value.oid.len = sizeof(SNMPTrapValue) / sizeof(smiUINT32);      // number of elements in trapValue array
	SNMPTrapValue[TRAPLEN-1] = MessagePriority;							    // Last value is message priority (Normal, critical)
	valTrap.value.oid.ptr = SNMPTrapValue;
	SnmpSetVb (hVbl, 0, &dTrapName, &valTrap);
	
	//Octet VALUE
	smiOID oidOcted   = {sizeof(SNMPOctetValue)/sizeof(smiUINT32), SNMPOctetValue};
	smiVALUE valOCTET;
	valOCTET.syntax = SNMP_SYNTAX_OCTETS;
	valOCTET.value.string.len = lstrlen(MyMessage);
	valOCTET.value.string.ptr = (unsigned char *)charInfoString;
	lStat = SnmpSetVb (hVbl, 0, &oidOcted, &valOCTET); 
	
	// Create new PDU
	HSNMP_PDU hPdu = SnmpCreatePdu (hSession, SNMP_PDU_TRAP, counter, 0, 0, hVbl);
	if (hPdu == SNMPAPI_FAILURE)
		throw _T("SnmpCreatePdu :: SNMPAPI_FAILURE");


	// send TRAP
	lStat = SnmpSendMsg (hSession, NULL, hDst, hContext, hPdu);


	if (lStat != SNMPAPI_SUCCESS)
	{	// Get SNMP error 
		int lErr = SnmpGetLastError (hSession);
		// Store SNMP error code

		switch ( lErr )
		{
			case SNMPAPI_FAILURE:            
				throw _T("SNMPAPI_FAILURE");			  
				break;
			case SNMPAPI_NOT_INITIALIZED:    
				throw _T("SNMPAPI_NOT_INITIALIZED");	  
				break;
			case SNMPAPI_ALLOC_ERROR:	     
				throw _T("SNMPAPI_ALLOC_ERROR");		  
				break;
			case SNMPAPI_SESSION_INVALID:    
				throw _T("SNMPAPI_SESSION_INVALID");	  
				break;
			case SNMPAPI_ENTITY_INVALID:     
				throw _T("SNMPAPI_ENTITY_INVALID");	  
				break;
			case SNMPAPI_CONTEXT_INVALID:    
				throw _T("SNMPAPI_CONTEXT_INVALID");	  
				break;
			case SNMPAPI_PDU_INVALID:        
				throw _T("SNMPAPI_PDU_INVALID");       
				break;
			case SNMPAPI_OPERATION_INVALID:  
				throw _T("SNMPAPI_OPERATION_INVALID"); 
				break;
			case SNMPAPI_TL_NOT_INITIALIZED: 
				throw _T("SNMPAPI_TL_NOT_INITIALIZED");
				break;
			case SNMPAPI_TL_NOT_SUPPORTED:   
				throw _T("SNMPAPI_TL_NOT_SUPPORTED");  
				break;
			case SNMPAPI_TL_NOT_AVAILABLE:   
				throw _T("SNMPAPI_TL_NOT_AVAILABLE");  
				break;
			case SNMPAPI_TL_RESOURCE_ERROR:  
				throw _T("SNMPAPI_TL_RESOURCE_ERROR"); 
				break;
			case SNMPAPI_TL_SRC_INVALID:     
				throw _T("SNMPAPI_TL_SRC_INVALID");    
				break;
			case SNMPAPI_TL_INVALID_PARAM:   
				throw _T("SNMPAPI_TL_INVALID_PARAM");  
				break;
			case SNMPAPI_TL_PDU_TOO_BIG:     
				throw _T("SNMPAPI_TL_PDU_TOO_BIG");    
				break;
			case SNMPAPI_TL_OTHER:           
				throw _T("SNMPAPI_TL_OTHER");		  
				break;
			case SNMPAPI_OTHER_ERROR:        
				throw _T("SNMPAPI_OTHER_ERROR");		  
				break;
		}
	}
}

// SNMP Cleanup...
if(bSnmpCreateSession)
	SnmpClose (hSession);
if(bSnmpStartup)
	SnmpCleanup ();
  Na stránku 
screen  Nový příspěvek
Název  Uživatel  Datum 
Poslední návštěva: 20:50:43, 22. září 2017 První  Předchozí  0 Záznamů  Další  Poslední  

Autor článku
Jméno
Pracovní pozice
Informace
Foto

   

Počet návštěvníků:58
 
  Kontakt