'*******************************************************************************************
'*
'* Function:
'* ---------
'* $File: ForefrontProcessChecker.vbs $
'* $Revision: 4 $
'*
'* Purpose:
'* --------
'* Monitors Scan Job Processes by counting them to make sure the correct number are present
'*
'* Parameters:
'* -----------
'* Process
'* TextLog
'*
'* Parameter definitions:
'* ----------------------
'* 1. Process => defines the name of the Scan Job process that is being monitored
'* 2. TextLog => defines whether this scripts logs entries to a text file under the local
'* Forefront installation folder. Possible Values are true ; false
'*
'*******************************************************************************************
If IsNull(ForefrontInstallPath) Then
WriteMOMEvent "TASK ERROR: Unable To Retrieve Forefront for Exchange Installation Path", 1, INSTALL_PATH_FAIL
ScriptContext.Quit
End If
If TextLogParameter = "true" then
CreateLogFolder(ForefrontInstallPath)
End If
WriteLog "NULL"
WriteLog "Beginning Execution of ScanProcessMonitor Script to check status of " & ProcessParameter
Select Case ProcessParameter
Case "FSCTransportScanner.exe"
ExchService = TRANSPORT_SERV
Case "FSCRealtimeScanner.exe"
ExchService = EXCH_STORE
End Select
ForefrontService=FOREFRONT_CONTROLLER
WriteLog "Waiting to check Forefront Service: " & ForefrontService & " and Exchange Server: " & ExchService
If CheckServiceRunning (TextLogParameter, ForefrontService, ExchService) Then
WriteLog "Waiting to check Scan Job Hook"
If CheckScanJobHook(TextLogParameter, ProcessParameter) Then
WriteLog "Waiting to check Process Number"
If ExpectedNumProcesses (ProcessParameter, InstalledProduct, NumSG) <= ActualNumProcesses (ProcessParameter) Then
WriteLog "Process Count OK"
Else
Select Case ProcessParameter
Case "FSCTransportScanner.exe"
WriteMOMEvent "MOM detected an FSCTransportScanner.exe process may have crashed due to an exception", 1, INTERNET_COUNT_FAIL
Case "FSCRealtimeScanner.exe"
WriteMOMEvent "MOM detected an FSCRealtimeScanner process may have crashed due to an exception", 1, REALTIME_COUNT_FAIL
End Select
WriteLog "Process Count Incorrect - Possible Process Failure"
End If
End If
End If
WriteLog "Finished Execution of ScanProcessMonitor Script"
'***********************************************************************
'*
'* Function: ExpectedNumProcesses()
'*
'* Purpose: Determines expected number of process instances
'*
'***********************************************************************
Function ExpectedNumProcesses (ProcessParameter, InstalledProduct, NumSG)
On Error Resume Next
Err.Clear
If ProcessParameter = "FSCTransportScanner.exe" Then
ExpectedNumProcesses = RetrieveRegValue (REG_KEY & InstalledProduct,"InternetProcessCount", 2)
WriteLog "InternetProcessCount RegKey=" & ExpectedNumProcesses
Else
ExpectedNumProcesses = RetrieveRegValue (REG_KEY & InstalledProduct,"RealtimeProcessCount", 2)
WriteLog "RealtimeProcessCount RegKey = " & ExpectedNumProcesses
End If
WriteLog "Expected Number of " & ProcessParameter & " Processes = " & ExpectedNumProcesses
End Function
'***********************************************************************
'*
'* Function: ActualNumProcesses()
'*
'* Purpose: Determines actual number of process instances
'*
'***********************************************************************
Function ActualNumProcesses (ProcessParameter)
On Error Resume Next
Err.Clear
i = 0
Set refWMI = GetObject("winMgmts:")
strQuery = "SELECT * FROM Win32_Process WHERE Name='" & ProcessParameter & "'"
Set colProcesses = refWMI.ExecQuery(strQuery)
For Each refItem In ColProcesses
i = i + 1
Next
Set refWMI = Nothing
Set colProcesses = Nothing
ActualNumProcesses = i
WriteLog "Actual Number of " & ProcessParameter & " Processes = " & ActualNumProcesses
End Function
'***********************************************************************
'*
'* Function: CheckScanJobHook()
'*
'* Purpose: Checks corresponding process is running
'*
'***********************************************************************
Function CheckScanJobHook(TextLogParameter, ProcessParameter)
On Error Resume Next
Err.Clear
EnableValue = RetrieveRegValue (REG_KEY & InstalledProduct,"ForefrontEnabled", 2) '
If ProcessParameter = "FSCTransportScanner.exe" Then
If EnableValue = 2 or EnableValue = 3 Then
CheckScanJobHook = True
Else
CheckScanJobHook = False
End If
End If
If ProcessParameter = "FSCRealtimeScanner.exe" Then
If EnableValue = 1 or EnableValue = 3 Then
CheckScanJobHook = True
Else
CheckScanJobHook = False
End If
End If
If CheckScanJobHook Then
WriteLog "Scan Interface Active...Process Check Proceeding"
Else
WriteLog "Scan Interface Inactive...Process Check Aborting"
End If
End Function
'***********************************************************************
'*
'* Function: CheckServiceRunning()
'*
'* Purpose: Checks corresponding process is running
'*
'***********************************************************************
Function CheckServiceRunning (TextLogParameter, strService1, strService2)
Dim refWMI
Dim refService
Dim refService2
Dim refSecurity
Dim colListOfServices
Dim colListOfServices2
On Error Resume Next
Err.Clear
WriteLog "Try to create WMI object."
Set refWMI = GetObject("winMgmts:")
If (IsNull(refWMI) or Err<>0 ) Then
WriteLog "Unable to create WMI object."
CheckServiceRunning=False
Else
WriteLog "WMI object Create Successful."
strQuery="Select * from Win32_Service where Name='"+strService1+"'"
Set refService= null
Err.Clear
Set colListOfServices =refWMI.ExecQuery(strQuery)
If (Err <> 0 or IsNull(colListOfServices))Then
WriteLog "ERROR: Could not connect to Service: " & strService1 &" Process Check Aborting"
Set colListOfServices=Nothing
Set colListOfServices=null
CheckServiceRunning=False
Else
WriteLog "Successful connect to Service: " & strService1
For each refService in colListOfServices
WriteLog "Check status of Service: " & refService.Name
If refService.State = "Running" Then
WriteLog strService1 & " Service running...Process Check Proceeding"
If IsNull(strService2) Then
WriteLog strService1 & " Service running...Process Check Proceeding"
CheckServiceRunning = True
Else
strQuery="Select * from Win32_Service where Name='"+strService2+"'"
Set refService= null
Err.Clear
Set colListOfServices2 =refWMI.ExecQuery(strQuery)
If (Err <> 0 or IsNull(refService)) Then
WriteLog "ERROR: Could not connect to Service: " & strService2 + ".... Process Check Aborting"
CheckServiceRunning=False
Set colListOfServices2=Nothing
set colListOfServices2=null
Else
For each refService2 in colListOfServices2
WriteLog "Successful connect to Service: " & refService2.Name
If refService2.State = "Running" Then
WriteLog strService1 & " and " & strService2 & " Service running...Process Check Proceeding"
CheckServiceRunning = True
Else
WriteLog strService2 & " Service not running...Process Check Aborting"
CheckServiceRunning = False
End If
Next
End If
End If
Else
WriteLog strService1 & " Service not running...Process Check Aborting"
CheckServiceRunning = False
End If
Next
End If
End If
Set refService=Nothing
Set refWMI=Nothing
End Function
Dim objFSO
set objFSO = CreateObject ("Scripting.FileSystemObject")
If objFSO.FolderExists(ForefrontInstallPath & "\MOMLogs") = 0 then
objFSO.CreateFolder(ForefrontInstallPath & "\MOMLogs")
End If
Set objFSO = Nothing
End Function
'***********************************************************************
'*
'* Function: WriteLog()
'*
'* Purpose: Writes script activity and errors to a log file
'*
'***********************************************************************
Function WriteLog (strLogText)
On Error Resume Next
Err.Clear
Dim objfs
Dim objf
Dim strTimeStamp
If TextLogParameter = "true" Then
On Error Resume Next
Err.Clear
Set objfs = CreateObject("Scripting.FileSystemObject")
Set objf = objfs.OpentextFile(ForefrontInstallPath & "\MOMLogs\" & "Tasks.log", 8, False)
' If log file doesn't exist - create it
If Err.Number <> 0 Then
Set objf = objfs.CreatetextFile(ForefrontInstallPath & "\MOMLogs\" & "Tasks.log", False)
Err.Clear
End If
If strLogText = "NULL" then
objf.WriteLine("")
Else
strTimeStamp = Date & " " & Time & " "
objf.WriteLine(strTimeStamp & strLogText)
End If
End If
Set objfs = Nothing
Set objf = Nothing
End Function
'********************************************************************
'*
'* Function: RetrieveRegValue
'*
'* Purpose: Retrieves String Value from Registry
'*
'********************************************************************
Function RetrieveRegValue (Key, strValueName, intValueType)
' intValueType -> 1 = String Value
' -> 2 = DWORD Value
const HKEY_LOCAL_MACHINE = &H80000002
Dim strServerName
Dim objReg
Dim strRegValue
On Error Resume Next
Err.Clear
Set objReg=GetObject("winmgmts:\root\default:StdRegProv")
If Err.Number <> 0 Then
Err.Clear
strRegValue = NULL
Else
Select Case intValueType
Case 1
strErr = objReg.GetStringValue (HKEY_LOCAL_MACHINE, Key, strValueName, strRegValue)
Case 2
strErr = objReg.GetDWORDValue (HKEY_LOCAL_MACHINE, Key, strValueName, strRegValue)
End Select
' if reading the registry fails via wmi return error
If strErr <> 0 then
strRegValue = NULL
End If
End If