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

📄 snmpeldl.cpp

📁 windows的snmp api源码
💻 CPP
📖 第 1 页 / 共 5 页
字号:

		} // end switch()

		WriteTrace(0x0a,"SNMPEventLogDllMain: Exiting SNMPEventLogDllMain routine with TRUE\n");
	return(TRUE);
}
}


extern "C" {
BOOL
APIENTRY
SnmpExtensionInit(
		IN		DWORD							dwTimeZeroReference,
		OUT 	HANDLE							*hPollForTrapEvent,
		OUT 	AsnObjectIdentifier 	*supportedView
		)

/*++

Routine Description:

		SnmpExtensionInit is the extension dll initialization routine.

		This routine will create the event used to notify the manager agent that an event
		has occurred and that a trap should be generated. The TimeZeroReference will be
		saved and will be used by the trap generation routine to insert the time reference
		into the generated trap.

		The registry will be queried to determine which event logs will be used for tracking.
		These event log names are validated to insure that they are real log names. Event logs
		are opened and their handles are saved for event log processing.

		An event is created to notify the log processing thread of DLL termination. Then the
		log processing thread is spawned to handle all further event processing.

		The registry is then read to get the value for the supported view for this extension
		agent DLL. The registry layout for this routine is as follows:

		Registry
				Machine
						SOFTWARE
								Microsoft
										SNMP_EVENTS
												EventLog
														Parameters
																TraceFileName		(REG_SZ)
																TraceLevel			(REG_DWORD)
																BaseEnterpriseOID	(REG_SZ)
																SupportedView		(REG_SZ)
																TrimMessage         (REG_DWORD)
																MaxTrapSize 		(REG_DWORD)
																TrimFlag			(REG_DWORD)
																ThresholdEnabled	(REG_DWORD)
																ThresholdFlag		(REG_DWORD)
																ThresholdCount		(REG_DWORD)
																ThresholdTime		(REG_DWORD)

Arguments:

		dwTimeZeroReference 	-		Specifies a time-zero reference for the extension agent.

		hPollForTrapEvent		-		Pointer to an event handle for an event that will be asserted
														when the SnmpExtensionTrap entry point should be polled by the
														manager agent.

		supportedView			-		Points to an AsnObjectIdentifier specifying the MIB sub-tree
														supported by this extension agent. Read from the registry.

Return Value:

		TRUE	-		If initialization or termination was successful.

		FALSE	-		If initialization or termination was unsuccessful.

--*/

{
		LONG	lastError;						// for GetLastError()
		DWORD	dwThreadID; 					// for CreateThread()

	WriteTrace(0x0a,"SnmpExtensionInit: Entering extension agent SnmpExtensionInit routine\n");

		if ( !Read_Registry_Parameters() )
		{
				WriteTrace(0x14,"SnmpExtensionInit: Error during registry initialization processing\n");
				WriteLog(SNMPELEA_REGISTRY_INIT_ERROR);

				WriteTrace(0x14,"SnmpExtensionInit: SNMP Event Log Extension Agent DLL abnormal initialization\n");
				WriteLog(SNMPELEA_ABNORMAL_INITIALIZATION);

				CloseStopAll(); 								// close event handle

				if (fRegOk)
				{
						CloseRegNotify();						// close registry change event handle
						CloseRegParmKey();						// close registry key
				}
				WriteTrace(0x0a,"SnmpExtensionInit: Exiting extension agent SnmpExtensionInit routine with FALSE\n");
				return(FALSE);									// exit init routine
		}


	WriteTrace(0x0a,"SnmpExtensionInit: Creating event for manager agent trap event notification\n");

	if ( (hEventNotify = CreateEvent(
		(LPSECURITY_ATTRIBUTES) NULL,
		FALSE,
		FALSE,
		(LPTSTR) NULL)) == NULL)
	{
		lastError = GetLastError(); // save error status
		WriteTrace(0x14,"SnmpExtensionInit: Error creating EventNotify event; code %lu\n",
			lastError);
		WriteLog(SNMPELEA_ERROR_CREATING_EVENT_NOTIFY_EVENT, lastError);

				CloseStopAll(); 								// close event handle

				if (fRegOk)
				{
						CloseRegNotify();						// close registry change event handle
						CloseRegParmKey();						// close registry key
				}

		WriteTrace(0x14,"SnmpExtensionInit: SNMP Event Log Extension Agent DLL abnormal initialization\n");
		WriteLog(SNMPELEA_ABNORMAL_INITIALIZATION); 	 // log error message
				WriteTrace(0x0a,"SnmpExtensionInit: Exiting extension agent SnmpExtensionInit routine with FALSE\n");
		return(FALSE);
	}

	WriteTrace(0x00,"SnmpExtensionInit: Manager agent trap event notification handle is %08X\n",
				hEventNotify);

	WriteTrace(0x0a,"SnmpExtensionInit: Creating thread for event log processing routine\n");

	if ( (hServThrd = CreateThread(
		(LPSECURITY_ATTRIBUTES) NULL,					// security attributes
		0,																				// initial thread stack size
		(LPTHREAD_START_ROUTINE) SnmpEvLogProc, // starting address of thread
		0,																				// no arguments
		0,																				// creation flags
		&dwThreadID) ) == NULL )								// returned thread id
	{
		lastError = GetLastError(); 					// save error status
		WriteTrace(0x14,"SnmpExtensionInit: Error creating event log processing thread; code %lu\n",
			lastError);
		WriteLog(SNMPELEA_ERROR_CREATING_LOG_THREAD, lastError);	// log error message

				CloseStopAll(); 								// close event handle
				CloseEventNotify(); 							// close notify event handle

				if (fRegOk)
				{
						CloseRegNotify();						// close registry change event handle
						CloseRegParmKey();						// close registry key
				}

		WriteTrace(0x14,"SnmpExtensionInit: SNMP Event Log Extension Agent DLL abnormal initialization\n");
		WriteLog(SNMPELEA_ABNORMAL_INITIALIZATION); 	 // log error message
				WriteTrace(0x0a,"SnmpExtensionInit: Exiting extension agent SnmpExtensionInit routine with FALSE\n");
		return(FALSE);
	}

	WriteTrace(0x00,"SnmpExtensionInit: Handle to event log processing routine thread is %08X\n",
				hServThrd);

		dwTimeZero = dwTimeZeroReference;						// save time zero reference
		*hPollForTrapEvent = hEventNotify;						// return handle to event

		if (!StrToOid(szSupView, supportedView))
		{
				WriteTrace(0x14,"SnmpExtensionInit: Unable to convert supported view string to OID\n");
				WriteLog(SNMPELEA_SUPVIEW_CONVERT_ERROR);

				CloseStopAll(); 								// close event handle
				CloseEventNotify(); 							// close notify event handle

				if (fRegOk)
				{
						CloseRegNotify();						// close registry change event handle
						CloseRegParmKey();						// close registry key
				}

				WriteTrace(0x14,"SnmpExtensionInit: SNMP Event Log Extension Agent DLL abnormal initialization\n");
		WriteLog(SNMPELEA_ABNORMAL_INITIALIZATION); 	 // log error message
				WriteTrace(0x0a,"SnmpExtensionInit: Exiting extension agent SnmpExtensionInit routine with FALSE\n");
				return(FALSE);
		}

		WriteTrace(0x0a,"SnmpExtensionInit: Exiting extension agent SnmpExtensionInit routine with TRUE\n");
	return(TRUE);
}
}

extern "C" {
VOID
APIENTRY
SnmpExtensionClose()
{
    DWORD   lastError;      // to save GetLastError() return code
    DWORD   dwThreadID;
    DWORD   dwThirtySeconds = 30000;
    DWORD   dwWaitResult;
    BOOL    Itworked;

    WriteTrace(0x0a,"SnmpExtensionClose: Entering extension agent SnmpExtensionClose routine.\n");
    if ( !SetEvent(hStopAll) )
    {
	    lastError = GetLastError(); // save error status
	    WriteTrace(0x14,"SNMPEventLogDllMain: Error setting dll termination event %08X in process detach; code %lu\n",
		    hStopAll, lastError);
	    WriteLog(SNMPELEA_ERROR_SET_AGENT_STOP_EVENT, HandleToUlong(hStopAll), lastError);  // log error message
    }
    else
    {
	    WriteTrace(0x0a,"SNMPEventLogDllMain: Shutdown event %08X is now complete\n",
							    hStopAll);
    }

    if (hServThrd)
    {
        WriteTrace(0x0a,"SNMPEventLogDllMain: Waiting for event log processing thread %08X to terminate\n", hServThrd);
        WriteTrace(0x0a,"SNMPEventLogDllMain: Checking for thread exit code value\n");
		Itworked = GetExitCodeThread(hServThrd, &dwThreadID);
		WriteTrace(0x0a,"SNMPEventLogDllMain: Thread exit code value is %lu\n",dwThreadID);

        if (!Itworked || (dwThreadID == STILL_ACTIVE))
        {
            if (!Itworked)
		    {
		        lastError = GetLastError();
		        WriteTrace(0x14,"SNMPEventLogDllMain: GetExitCodeThread returned FALSE, reason code %lu\n",
				        lastError);
		        WriteLog(SNMPELEA_GET_EXIT_CODE_THREAD_FAILED, lastError);
		    }
		    else
		    {
			    WriteTrace(0x0a,"SNMPEventLogDllMain: Thread exit code indicates still active. Will wait...\n");
            }

		    // wait for the child to end
		    WriteTrace(0x0a,"SNMPEventLogDllMain: About to wait...\n");
		    dwWaitResult = WaitForSingleObject(hServThrd, dwThirtySeconds);
		    WriteTrace(0x0a,"SNMPEventLogDllMain: Finished wait...\n");

            switch (dwWaitResult)
            {
            case MAXDWORD :
                lastError = GetLastError(); // save error status
                WriteTrace(0x14,"SNMPEventLogDllMain: Error on WaitForSingleObject/log processing thread %08X; code %lu\n",
		                hServThrd, lastError);
                WriteLog(SNMPELEA_ERROR_WAIT_LOG_THREAD_STOP,
		                HandleToUlong(hServThrd), lastError);	 // log error message
                break;
            case 0 :
			    WriteTrace(0x0a,"SNMPEventLogDllMain: Event log processing thread %08X has terminated!\n",hServThrd);
                break;
            case WAIT_TIMEOUT :
                WriteTrace(0x14,"SNMPEventLogDllMain: Event log processing thread %08X has not terminated within 30 seconds; terminating thread\n",
		                hServThrd);
                WriteLog(SNMPELEA_LOG_THREAD_STOP_WAIT_30,
		                HandleToUlong(hServThrd)); 	// log error message
                KillLog();										// kill the log processing thread
                break;
            default :
                WriteTrace(0x14,"SNMPEventLogDllMain: Unknown result from WaitForSingleObject waiting on log processing thread %08X termination is %lu\n",
		                hServThrd, dwWaitResult );
                WriteLog(SNMPELEA_WAIT_LOG_STOP_UNKNOWN_RETURN,
		                HandleToUlong(hServThrd), dwWaitResult);  // log error message
            }
        }

        WriteTrace(0x0a,"SNMPEventLogDllMain: Checking for thread exit code again\n");
        Itworked = GetExitCodeThread(hServThrd, &dwThreadID);
        WriteTrace(0x0a,"SNMPEventLogDllMain: Thread exit code value is %lu\n",dwThreadID);

        WriteTrace(0x0a,"SNMPEventLogDllMain: Closing handle to log processing thread %08X\n",
		        hServThrd);
        if ( !CloseHandle(hServThrd) )
        {
		        lastError = GetLastError(); // save error status
		        WriteTrace(0x14,"SNMPEventLogDllMain: Error closing handle for log processing thread %08X; code %lu\n",
				        hServThrd, lastError);
		        WriteLog(SNMPELEA_ERROR_CLOSING_STOP_LOG_THREAD_HANDLE,
				        HandleToUlong(hServThrd), lastError); // log error message
        }
    }

    CloseStopAll();                     // close event handle
    CloseEventNotify(); 	            // close event handle
    if (fRegOk)
    {
        CloseRegNotify();	            // close event handle
        CloseRegParmKey();	            // close registry key
    }
    CloseLogs();			            // close all open log files
    CloseSourceHandles();

    WriteLog(SNMPELEA_STOPPED);

    DeregisterEventSource(hWriteEvent); // no longer a need for logging
    WriteTrace(0x14,"SNMPEventLogDllMain: SNMPELEA Event Log Extension Agent DLL has terminated\n");
}
}


BOOL
BuildThresholdTrap(
	IN	VOID
	)

/*++

Routine Description:

		This routine will build the threshold trap.


Arguments:

		None


Return Value:

		TRUE if created varbind, FALSE if an error occurred.

--*/

{
		TCHAR	szBuf[MAX_PATH+1];		// for OID conversion
		UINT	i;										// counter

		WriteTrace(0x0a,"BuildThresholdTrap: Building static variable bindings for threshold trap\n");
		WriteTrace(0x00,"BuildThresholdTrap: &thresholdVarBind is at %08X\n", &thresholdVarBind);
		WriteTrace(0x00,"BuildThresholdTrap: thresholdVarBind is %08X\n", thresholdVarBind);

		WriteTrace(0x00,"BuildThresholdTrap: BaseEnterpriseOID value read is %s\n", szBaseOID);

		if ( !StrToOid((char *) &szBaseOID, &thresholdOID) )
		{
				WriteTrace(0x14,"BuildThresholdTrap: Unable to convert OID from BaseEnterpriseOID\n");
				WriteLog(SNMPELEA_CANT_CONVERT_ENTERPRISE_OID);
				return(FALSE);
		}

		strcpy(szBuf, szBaseOID);								// copy base string
		strcat(szBuf, TEXT(".1.0"));					// tack on for varbind OID

		thresholdVarBind.list = (RFC1157VarBind *) SNMP_malloc(sizeof(RFC1157VarBind)); // allocate storage for varbind

		if (thresholdVarBind.list == NULL)
		{
				WriteTrace(0x14,"BuildThresholdTrap: Unable to allocate storage for varbind\n");
				WriteLog(SNMPELEA_ERROR_ALLOC_VAR_BIND);
				return(FALSE);
		}

		WriteTrace(0x00,"BuildThresholdTrap: Storage allocated for varbind entry at address at %08X\n",
				thresholdVarBind.list);

		thresholdVarBind.len = 1;				// set # of varbinds

		WriteTrace(0x00,"BuildThresholdTrap: Number of varbinds present set to %lu\n",
				thresholdVarBind.len);

		TCHAR * tempthreshmsg = (TCHAR *) SNMP_malloc(strlen(lpszThreshold) + 1);
		strcpy(tempthreshmsg, lpszThreshold);

		thresholdVarBind.list[0].value.asnValue.string.length = strlen(tempthreshmsg);	// get string length
		thresholdVarBind.list[0].value.asnValue.string.stream = (PUCHAR) tempthreshmsg; // point to string
		thresholdVarBind.list[0].value.asnValue.string.dynamic = TRUE;	// indicate not dynamically allocated
		thresholdVarBind.list[0].value.asnType = ASN_RFC1213_DISPSTRING;		// indicate type of object

		if ( !StrToOid((char *) &szBuf, &thresholdVarBind.list[0].name) )
		{
				WriteTrace(0x14,"BuildThresholdTrap: Unable to convert OID from BaseEnterpriseOID\n");
				WriteLog(SNMPELEA_CANT_CONVER

⌨️ 快捷键说明

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