📄 eventcmt.cpp
字号:
//
// Returns:
//
// none
//
//
//============================================================================
void EventConfigModifier::StartSNMP()
{
// Check that /NOSTOPSTART was NOT specified and there
// were no errors in stopping the SNMP service earlier
// ===================================================
if (!SNMPStopStart)
{
return;
}
// Get a handle to the service manager so we can get the SNMP service
// ==================================================================
SC_HANDLE services;
if (Machine.GetLength())
{
services = OpenSCManager(Machine, //the remote machine
"ServicesActive", //active services
GENERIC_EXECUTE); //want to interrogate, start and stop a service
}
else
{
services = OpenSCManager(NULL, //NULL for local machine
"ServicesActive", //active services
GENERIC_EXECUTE); //want to interrogate, start and stop a service
}
if (!services) //didn't connect to the service control manager, log it
{
DWORD its_broke = GetLastError();
CString sstr;
sstr += NL;
sstr += NL;
CString msg;
msg.LoadString(IDS_MSG37);
sstr += msg;
sstr += NL;
msg.Empty();
msg.LoadString(IDS_MSG38);
sstr += msg;
sstr += NL;
sstr += NL;
WriteToLog(&sstr);
if (SNMPModified) //Only log raise an error if we need a restart for changes.
{
StatusMif(IDS_STARTSTOPSNMP, FALSE);
SetError(EVCMT_SNMP_STOPSTART);
}
return;
}
// Get a handle to the SNMP service
// ================================
SC_HANDLE snmp_service = OpenService(services, "SNMP", SERVICE_CONTROL_INTERROGATE |
SERVICE_START | SERVICE_STOP);
if (!snmp_service) //failed to get the handle, log it
{
DWORD its_broke = GetLastError();
CloseServiceHandle(services);
CString sstr;
sstr += NL;
sstr += NL;
CString msg;
msg.LoadString(IDS_MSG37);
sstr += msg;
sstr += NL;
msg.Empty();
msg.LoadString(IDS_MSG38);
sstr += msg;
sstr += NL;
sstr += NL;
WriteToLog(&sstr);
if (SNMPModified) //Only log raise an error if we need a restart for changes
{
StatusMif(IDS_STARTSTOPSNMP, FALSE);
SetError(EVCMT_SNMP_STOPSTART);
}
return;
}
BOOL flag_error = FALSE;
// Try to start the SNMP service
// =============================
if (!StartService(snmp_service, 0, NULL))
{
DWORD its_broke = GetLastError();
if (its_broke == ERROR_SERVICE_ALREADY_RUNNING)
{
if (!SNMPModified) //we didn't stop the service so don't report an error or try again.
{
CloseServiceHandle(snmp_service);
CloseServiceHandle(services);
return;
}
Sleep(10000); //ten second wait for it to stop
if (!StartService(snmp_service, 0, NULL)) //one last try.
{
flag_error = TRUE; //we made changes and can't stop/start snmp, error.
}
}
else //it didn't start and it's not running, raise an error
{
flag_error = TRUE;
}
}
if (!flag_error) // no error, log it.
{
CString sstr;
sstr += NL;
sstr += NL;
CString msg;
msg.LoadString(IDS_MSG52);
sstr += msg;
sstr += NL;
sstr += NL;
WriteToLog(&sstr);
}
else //there was an error, log it
{
CString sstr;
sstr += NL;
sstr += NL;
CString msg;
msg.LoadString(IDS_MSG39);
sstr += msg;
sstr += NL;
msg.Empty();
msg.LoadString(IDS_MSG40);
sstr += msg;
sstr += NL;
sstr += NL;
WriteToLog(&sstr);
StatusMif(IDS_STARTSNMP, FALSE);
SetError(EVCMT_SNMP_START);
}
CloseServiceHandle(snmp_service);
CloseServiceHandle(services);
}
//============================================================================
// EventConfigModifier::WriteSNMPRegistry
//
// This private method is called to write the new image of the trap destination
// configuration. First the current config is deleted from the registry and
// then the new configuration (old config cached and modified in memory) is
// written into the registry.
//
//
// Parameters:
//
// none
//
//
// Returns:
//
// BOOL TRUE if this function is successful, FALSE otherwise.
// NOTE: There may have been problems but this function
// will still return TRUE if they were not serious.
//
//
//============================================================================
BOOL EventConfigModifier::WriteSNMPRegistry()
{
HKEY hkeyOpen;
CString keyName;
keyName.LoadString(IDS_TRAP_CONF);
// Open the registry at the point were the trap dests are kept
// ===========================================================
LONG Result = RegOpenKeyEx(hkey_machine, keyName,
0, KEY_ALL_ACCESS, &hkeyOpen);
if (Result != ERROR_SUCCESS)
{
CString sstr;
sstr += NL;
sstr += NL;
CString amsg;
amsg.LoadString(IDS_MSG45);
sstr += amsg;
sstr += NL;
sstr += NL;
WriteToLog(&sstr);
StatusMif(IDS_REGWRITE, FALSE);
SetError(EVCMT_REG_FAIL);
return FALSE;
}
// Delete all trap destinations
// ============================
if(!DeleteSNMPRegistry(&hkeyOpen))
{
CString sstr;
sstr += NL;
sstr += NL;
CString msg;
msg.LoadString(IDS_MSG41);
sstr += msg;
sstr += NL;
msg.Empty();
msg.LoadString(IDS_MSG42);
sstr += msg;
sstr += NL;
sstr += NL;
WriteToLog(&sstr);
StatusMif(IDS_REGWRITE, FALSE);
SetError(EVCMT_REG_FAIL);
return FALSE;
}
CString sstr;
sstr += NL;
sstr += NL;
CString amsg;
amsg.LoadString(IDS_MSG44);
sstr += amsg;
sstr += NL;
sstr += NL;
WriteToLog(&sstr);
// Now loop through our image of the trap destinations. For
// each community name create a new registry key and for each
// destination address under that community name create a value
// ============================================================
CommListItem * Comm = (CommListItem *)CommNames.Pop();
while (Comm)
{
HKEY hkey;
DWORD createtype;
CString * commkey = Comm->GetString();
Result = RegCreateKeyEx(hkeyOpen, *commkey, 0,
NULL, REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS, NULL, &hkey, &createtype);
if (Result != ERROR_SUCCESS) //failed to create this key, log it and continue
{
CString sstr;
sstr += NL;
CString msg;
msg.LoadString(IDS_MSG46);
sstr += msg;
sstr += *commkey;
sstr += NL;
msg.Empty();
msg.LoadString(IDS_MSG47);
sstr += msg;
sstr += NL;
sstr += NL;
WriteToLog(&sstr);
delete Comm;
CommListItem * Comm = (CommListItem *)CommNames.Pop();
StatusMif(IDS_REGWRITE, FALSE);
SetError(EVCMT_REG_FAIL);
continue;
}
// Add the addresses for this community name
// =========================================
ListItem * addr = Comm->addresses.Pop();
int x = 1;
while (addr)
{
char str[34];
_ultoa(x, str, 10);
CMyString * addrstr = (CMyString *)addr->GetString();
LPTSTR val = addrstr->GetBuffer(1);
DWORD valsz = addrstr->GetBufferSize();
Result = RegSetValueEx(hkey, str, 0, REG_SZ,
(const unsigned char*) val, valsz);
if (Result != ERROR_SUCCESS) //failed to create this value, log it and continue.
{
CString sstr;
sstr += NL;
CString msg;
msg.LoadString(IDS_MSG48);
sstr += msg;
sstr += val;
sstr += NL;
msg.Empty();
msg.LoadString(IDS_MSG49);
sstr += msg;
sstr += *commkey;
sstr += NL;
sstr += NL;
WriteToLog(&sstr);
StatusMif(IDS_REGWRITE, FALSE);
SetError(EVCMT_REG_FAIL);
}
addrstr->ReleaseBuffer();
x++;
delete addr;
addr = Comm->addresses.Pop();
}
RegCloseKey(hkey);
delete Comm;
Comm = (CommListItem *)CommNames.Pop();
}
// Finished writing to the (trap destination's part of the) registry
// =================================================================
RegCloseKey(hkeyOpen);
CString astr;
astr += NL;
astr += NL;
CString done;
done.LoadString(IDS_MSG43);
astr += done;
astr += NL;
astr += NL;
WriteToLog(&astr);
return TRUE;
}
//============================================================================
// EventConfigModifier::DeleteSNMPRegistry
//
// This private method is called to delete the current SNMP trap destinations
// from the registry. The only parameter is the starting point in the registry.
//
// Note: This method can be used to delete any section of the registry
// given a "root" key to start from. All keys below this "root"
// will be deleted. This function is recursive.
//
//
// Parameters:
//
// none
//
//
// Returns:
//
// BOOL TRUE if this function is a complete success, FALSE if any
// part of the trap dest config remains in the registry.
//
//
//============================================================================
BOOL EventConfigModifier::DeleteSNMPRegistry(HKEY * hkey)
{
HKEY hkeyOpen;
char Buffer[1024 + 1];
DWORD dwLength;
int i = 0;
LONG Result;
// Enumerate all keys below the "root" (the parameter)
// and delete them by recursively calling this method.
// ===================================================
while (TRUE)
{
// Get a key below the root
// ========================
dwLength = 1024 + 1;
Result = RegEnumKeyEx(*hkey, i, Buffer, &dwLength, NULL,
NULL, NULL, NULL);
if (Result != ERROR_SUCCESS)
break;
if (dwLength > 0)
{
Result = RegOpenKeyEx(*hkey, Buffer, 0,
KEY_ALL_ACCESS, &hkeyOpen);
if (Result != ERROR_SUCCESS)
break;
// Delete any the keys below this one,
// if there are none then delete this key
// ======================================
if (!DeleteSNMPRegistry(&hkeyOpen))
{
RegCloseKey(hkeyOpen);
return FALSE;
}
else
{
RegCloseKey(hkeyOpen);
Result = RegDeleteKey(*hkey, Buffer);
}
}
}
// Did we find a normal end condition?
// ===================================
if (Result == ERROR_NO_MORE_ITEMS)
return TRUE;
return FALSE;
}
//============================================================================
// EventConfigModifier::ReadSNMPRegistry
//
// This private method is called to read the registry and construct an image
// of the trap destination configuration so they may be manipulated in memory.
//
//
// Parameters:
//
// none
//
//
// Returns:
//
// BOOL
//
//
//============================================================================
BOOL EventConfigModifier::ReadSNMPRegistry()
{
HKEY hkeyOpen;
CString keyName;
keyName.LoadString(IDS_TRAP_CONF);
// Get the root key of all the community names
// ===========================================
LONG Result = RegOpenKeyEx(hkey_machine, keyName,
0, KEY_READ, &hkeyOpen);
if (Result != ERROR_SUCCESS)
return FALSE;
char Buffer[1024 + 1];
DWORD dwLength;
int i = 0;
BOOL NoError = TRUE;
// Now enumerate this key and get all the community names
// For each community name construct a list of addresses.
// ======================================================
while (NoError)
{
dwLength = 1024 + 1;
Result = RegEnumKeyEx(hkeyOpen, i, Buffer, &dwLength, NULL,
NULL, NULL, NULL);
if (Result != ERROR_SUCCESS)
break;
if (dwLength > 0)
{
CString * str = new CString(Buffer);
CommListItem * item = new CommListItem(str);
CommNames.Add(item); //There can't be duplicates (it's a regkey)
NoError = item->GetAddresses(&hkeyOpen);
}
i++;
}
RegCloseKey(hkeyOpen);
// Did we find a normal end condition?
// ===================================
if ((Result == ERROR_NO_MORE_ITEMS) && NoError)
return TRUE;
return FALSE;
}
//============================================================================
// EventConfigModifier::ProcessCommandQ
//
// This private method is called to process the list of translation requests.
//
// Note: If the compile time flag EVENTCMT_VALIDATE_ID is TRUE then the
// eventIds are validated on the machine that the tool is running on.
// If this is re
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -