Contact Info

Crumbtrail

ActiveXperts.com » Administration » VBScript » Network Monitor » Process

Process.vbs - Monitor processes and process memory usage using ActiveXperts Network Monitor

ActiveXperts Network Monitor ships with a powerful set of pre-defined checks. Each individual check has a static number of configuration items. To monitor other items, or to combine monitoring items, you can make use of custom VBScript checks.

Most of the built-in checks have a VBScript equivalent, implemented as a Function in a VBScript (.vbs) file. Out-of-the-box, each VBScript function monitors the same items as the built-in check. Feel free to modify a function. The VBScript check can be customized by editing the VBScript function.

To add a new VBScript-based Process check, do the following:

To customize the above monitoring check, click on the 'Edit button' next to the 'File selection box'. Notepad will be launched. You can now make changes to the VBScript function(s).

Screenshot of a VBScript Process check

Printer.vbs script source code

' ///////////////////////////////////////////////////////////////////////////////
' // ActiveXperts Network Monitor  - VBScript based checks
' //  ActiveXperts Software B.V.
' //
' // For more information about ActiveXperts Network Monitor and VBScript, please
' // visit the online ActiveXperts Network Monitor VBScript Guidelines at:
' //    https://www.activexperts.com/support/network-monitor/online/vbscript/
' // 
' ///////////////////////////////////////////////////////////////////////////////
'  CheckProcess( "localhost", "", "explorer.exe" )z

Option Explicit
Const  retvalUnknown = 1
Dim    SYSDATA, SYSEXPLANATION  ' Used by Network Monitor, don't change the names


' ///////////////////////////////////////////////////////////////////////////////

' // To test a function outside Network Monitor (e.g. using CSCRIPT from the
' // command line), remove the comment character (') in the following 5 lines:
' Dim bResult
' bResult =  CheckProcess( "localhost", "", "explorer.exe" )
' WScript.Echo "Return value: [" & bResult & "]"
' WScript.Echo "SYSDATA: [" & SYSDATA & "]"
' WScript.Echo "SYSEXPLANATION: [" & SYSEXPLANATION & "]"

' //////////////////////////////////////////////////////////////////////////////


' //////////////////////////////////////////////////////////////////////////////

Function CheckProcess( strComputer, strCredentials, strProcess )

' Description: 
'     Checks if a process, specified by strProcess, is running on the machine specified by strComputer. 
' Parameters:
'     1) strComputer As String - Hostname or IP address of the computer you want to check
'     2) strCredentials As String - Specify an empty string to use Network Monitor service credentials.
'         To use alternate credentials, enter a server that is defined in Server Credentials table.
'         (To define Server Credentials, choose Tools->Options->Server Credentials)
'     3) strProcess As String - Name of the process
' Usage:
'     CheckProcess( "", "", "" )
' Sample:
'     CheckProcess( "localhost", "", "explorer.exe" )

    Dim objWMIService

    CheckProcess      = retvalUnknown  ' Default return value
    SYSDATA           = ""             ' Will not be used by this function
    SYSEXPLANATION    = ""             ' Set initial value

    If( Not getWMIObject( strComputer, strCredentials, objWMIService, SYSEXPLANATION ) ) Then
        Exit Function
    End If

    CheckProcess      = checkProcessWMI( objWMIService, strComputer, strProcess, SYSEXPLANATION )

End Function


' //////////////////////////////////////////////////////////////////////////////


Function CheckProcessMemory( strComputer, strCredentials, strProcessName, nMaxMB )

' Description: 
'     This function checks if a process, specified by strProcess, is consuming less than nMaxMB of memory on computer specified by strComputer. 
' Parameters:
'     1) strComputer As String - Hostname or IP address of the computer you want to check
'     2) strCredentials As String - Specify an empty string to use Network Monitor service credentials.
'         To use alternate credentials, enter a server that is defined in Server Credentials table.
'         (To define Server Credentials, choose Tools->Options->Server Credentials)
'     3) strProcessName As String - Name of the process
'     4) nMaxMB As String - Maximum allowed memory usage (MB)
' Usage:
'     CheckProcessMemory( "", "", "", Max_MB )
' Sample:
'     CheckProcessMemory( "localhost", "", "axsnmsvc.exe", 30 )

    Dim objWMIService

    CheckProcessMemory      = retvalUnknown  ' Default return value
    SYSDATA                 = ""             ' Will store the number of MBs used by the process
    SYSEXPLANATION          = ""             ' Set initial value

    If( Not getWMIObject( strComputer, strCredentials, objWMIService, SYSEXPLANATION ) ) Then
        Exit Function
    End If

    CheckProcessMemory      = checkProcessMemoryWMI( objWMIService, strComputer, strProcessName, nMaxMB, SYSDATA, SYSEXPLANATION )

End Function ' //////////////////////////////////////////////////////////////////////////////


Function CheckAnyProcessMemory( strComputer, strCredentials, nMaxMB )

' Description: 
'     This function checks if any process, is consuming less than nMaxMB of memory on computer specified by strComputer. 
' Parameters:
'     1) strComputer As String - Hostname or IP address of the computer you want to check
'     2) strCredentials As String - Specify an empty string to use Network Monitor service credentials.
'         To use alternate credentials, enter a server that is defined in Server Credentials table.
'         (To define Server Credentials, choose Tools->Options->Server Credentials)
'     3) nMaxMB As String - Maximum allowed memory usage (MB)
' Usage:
'     CheckAnyProcessMemory( "", "", Max_MB )
' Sample:
'     CheckAnyProcessMemory( "localhost", "", 100 )

    Dim objWMIService

    CheckAnyProcessMemory      = retvalUnknown  ' Default return value
    SYSDATA                 = ""             ' Will store the number of MBs used by the process
    SYSEXPLANATION          = ""             ' Set initial value

    If( Not getWMIObject( strComputer, strCredentials, objWMIService, SYSEXPLANATION ) ) Then
        Exit Function
    End If

    CheckAnyProcessMemory   = checkAnyProcessMemoryWMI( objWMIService, strComputer, nMaxMB, SYSDATA, SYSEXPLANATION )

End Function 

' //////////////////////////////////////////////////////////////////////////////

Function CheckProcessCpu( strComputer, strCredentials, strProcess, numMaxCPU )

' Description: 
'     This function checks if a process doesn't consume more than the maximum specified CPU usage (%) 
' Parameters:
'     1) strComputer As String - Hostname or IP address of the computer you want to check
'     2) strCredentials As String - Specify an empty string to use Network Monitor service credentials.
'         To use alternate credentials, enter a server that is defined in Server Credentials table.
'         (To define Server Credentials, choose Tools->Options->Server Credentials)
'     3) strProcess As String - Name of the process
'     4) numMaxCPU As Number - Maximum CPU usage (in %)
' Usage:
'     CheckProcessCpu( "", "", "", Max_CPU)
' Sample:
'     CheckProcessCpu( "localhost", "", "explorer.exe", 10 )

    Dim objWMIService

    CheckProcessCpu         = retvalUnknown  ' Default return value
    SYSDATA                 = ""             ' Will store the CPU (pct) used by the process
    SYSEXPLANATION          = ""             ' Set initial value

    If( Not getWMIObject( strComputer, strCredentials, objWMIService, SYSEXPLANATION ) ) Then
        Exit Function
    End If

    CheckProcessCpu         = checkProcessCpuWMI( objWMIService, strComputer, strProcess, numMaxCPU, SYSDATA, SYSEXPLANATION )

End Function

' //////////////////////////////////////////////////////////////////////////////

Function CheckNumProcesses( strComputer, strCredentials, strProcess, numMinInstances, numMaxInstances )

' Description: 
'     This function counts the number processes - indicated by strProcess -on the computer specified by strComputer.
'     If this count is less than numMinInstances or greater than numMaxInstances, an eror is generated. 
' Parameters:
'     1) strComputer As String - Hostname or IP address of the computer you want to check
'     2) strCredentials As String - Specify an empty string to use Network Monitor service credentials.
'         To use alternate credentials, enter a server that is defined in Server Credentials table.
'         (To define Server Credentials, choose Tools->Options->Server Credentials)
'     3) strProcess As String - Name of the process
'     4) numMinInstances - minimum number of instances
'     5) numMaxInstances - maximum number of instances
' Usage:
'     CheckNumProcesses( "", "", "", Min_Instances, Max_Instances )
' Sample:
'     CheckNumProcesses( "localhost", "", "svchost.exe", 1, 5 )

    Dim objWMIService

    CheckNumProcesses      = retvalUnknown  ' Default return value
    SYSDATA                = ""             ' Will store number of processes
    SYSEXPLANATION         = ""             ' Set initial value

    If( Not getWMIObject( strComputer, strCredentials, objWMIService, SYSEXPLANATION ) ) Then
        Exit Function
    End If

    CheckNumProcesses      = checkNumProcessesWMI( objWMIService, strComputer, strProcess, numMinInstances, numMaxInstances, SYSDATA, SYSEXPLANATION )

End Function




' //////////////////////////////////////////////////////////////////////////////
' //
' // Private Functions
' //   NOTE: Private functions are used by the above functions, and will not
' //         be called directly by the ActiveXperts Network Monitor Service.
' //         Private function names start with a lower case character and will
' //         not be listed in the Network Monitor's function browser.
' //
' //////////////////////////////////////////////////////////////////////////////

Function checkProcessWMI( objWMIService, strComputer, strProcess, BYREF strSysExplanation )

    Dim objProcess, collProcesses

    checkProcessWMI            = retvalUnknown  ' Default return value

On Error Resume Next

    set collProcesses          = objWMIService.ExecQuery( "select * from Win32_Process" ) 
    If( Err.Number <> 0 ) Then
        strSysData         = ""
        strSysExplanation  = "Unable to query WMI on computer [" & strComputer & "]"
        Exit Function
    End If
    If( collProcesses.Count <= 0  ) Then
        strSysData         = ""
        strSysExplanation  = "Win32_Process class does not exist on computer [" & strComputer & "]"
        Exit Function
    End If

On Error Goto 0

    For Each objProcess in collProcesses
        If( Err.Number <> 0 ) Then
            checkProcessWMI    = retvalUnknown
            strSysExplanation  = "Unable to list processes on computer [" & strComputer & "]"
            Exit Function 
        End If
	If UCase( objProcess.Name ) = UCase( strProcess ) Then
            checkProcessWMI    = True
    	    strSysExplanation  = "Process [" & strProcess & "] is running on computer [" & strComputer & "]"
            Exit Function
        End If
    Next

    checkProcessWMI            = False
    strSysExplanation          = "Process [" & strProcess & "] is not running on computer [" & strComputer & "]"

End Function

' //////////////////////////////////////////////////////////////////////////////

Function checkProcessMemoryWMI( objWMIService, strComputer, strProcessName, nMaxMB, BYREF strSysData, BYREF strSysExplanation )

    Dim objProcess, colProcesses, nDiff

    checkProcessMemoryWMI          = retvalUnknown  ' Default return value

On Error Resume Next

    Set colProcesses = objWMIService.ExecQuery( "Select * from Win32_Process" )
    If( Err.Number <> 0 ) Then
        strSysData         = ""
        strSysExplanation  = "Unable to query WMI on computer [" & strComputer & "]"
        Exit Function
    End If
    If( colProcesses.Count <= 0  ) Then
        strSysData         = ""
        strSysExplanation  = "Win32_Process class does not exist on computer [" & strComputer & "]"
        Exit Function
    End If

On Error Goto 0

    For Each objProcess in colProcesses
        If( Err.Number <> 0 ) Then
            checkProcessMemoryWMI  = retvalUnknown
            strSysExplanation      = "Unable to list processes on computer [" & strComputer & "]"
            Exit Function 
        End If

        If UCase( objProcess.Name )= UCase( strProcessName ) Then
          nDiff                    = nMaxMB -  CInt( objProcess.WorkingSetSize / ( 1024 * 1024 ) )
          strSysData               = CInt( objProcess.WorkingSetSize / ( 1024 * 1024 ) )
          strSysExplanation        = "Memory usage=[" & CInt( objProcess.WorkingSetSize / ( 1024 * 1024 ) ) & "MB], maximum allowed=[" & nMaxMB & " MB]"
          If nDiff >= 0 then
             checkProcessMemoryWMI = True
          Else
             checkProcessMemoryWMI = False
          End if

          Exit Function 
        End If
    Next

    checkProcessMemoryWMI          = retvalUnknown
    strSysExplanation              = "Process [" & strProcessName & "] is not running on computer [" & strComputer & "]"

End Function 
' //////////////////////////////////////////////////////////////////////////////

Function checkAnyProcessMemoryWMI( objWMIService, strComputer, nMaxMB, BYREF strSysData, BYREF strSysExplanation )

    Dim objProcess, colProcesses, nDiff

    checkAnyProcessMemoryWMI          = retvalUnknown  ' Default return value

On Error Resume Next

    Set colProcesses = objWMIService.ExecQuery( "Select * from Win32_Process" )
    If( Err.Number <> 0 ) Then
        strSysData         = ""
        strSysExplanation  = "Unable to query WMI on computer [" & strComputer & "]"
        Exit Function
    End If
    If( colProcesses.Count <= 0  ) Then
        strSysData         = ""
        strSysExplanation  = "Win32_Process class does not exist on computer [" & strComputer & "]"
        Exit Function
    End If

On Error Goto 0

    For Each objProcess in colProcesses
        If( Err.Number <> 0 ) Then
            checkAnyProcessMemoryWMI = retvalUnknown
            strSysExplanation        = "Unable to list processes on computer [" & strComputer & "]"
            Exit Function 
        End If

        nDiff                        = nMaxMB -  CInt( objProcess.WorkingSetSize / ( 1024 * 1024 ) )
        If nDiff < 0 then
            strSysData               = CInt( objProcess.WorkingSetSize / ( 1024 * 1024 ) )
            strSysExplanation        = "Memory usage of process [" & objProcess.Name & "] is [" & CInt( objProcess.WorkingSetSize / ( 1024 * 1024 ) ) & "MB], maximum allowed=[" & nMaxMB & " MB]"
            checkAnyProcessMemoryWMI = False
            Exit Function 
        End If
    Next

    strSysData               = ""
    strSysExplanation        = "No process uses more than [" & nMaxMB & " MB]"
    checkAnyProcessMemoryWMI = True

End Function 


' //////////////////////////////////////////////////////////////////////////////

Function checkProcessCpuWMI( objWMIService, strComputer, strProcess, numMaxCPU, BYREF strSysData, BYREF strSysExplanation )

On Error Resume Next

    Dim N1, D1, N2, D2, numCPU
    Dim objProcess, objVbUtils, objInstance1, objInstance2
    Dim strQuery, strProcessEx, strObjectPath

    checkProcessCpuWMI     = retvalUnknown  ' Default return value

    strProcessEx           = ReplaceEx( UCase( strProcess ), ".EXE", "" ) ' Cutt off ".exe" is necessary
    strObjectPath          = "Win32_PerfRawData_PerfProc_Process.Name=" & chr(34) & strProcessEx & chr(34)

    Set objVbUtils         = CreateObject( "ActiveXperts.VbUtilities" )
    Set objInstance1       = objWMIService.get( strObjectPath )

    N1                     = objInstance1.PercentProcessorTime
    D1                     = objInstance1.TimeStamp_Sys100NS
    If( N1 = 0 OR D1 = 0 ) Then
        checkProcessCpuWMI = retvalUnknown
        strSysExplanation  = "Process [" & strProcess & "] not running on computer [" & strComputer & "]"
        Exit Function
    End If

    objVbUtils.Sleep( 1000 )

    Set objInstance2       = objWMIService.get( strObjectPath )
    N2                     = objInstance2.PercentProcessorTime
    D2                     = objInstance2.TimeStamp_Sys100NS
    If( N1 = 0 OR D1 = 0 ) Then
        checkProcessCpuWMI = retvalUnknown
        strSysExplanation     = "Process [" & strProcess & "] not running on computer [" & strComputer & "]"
        Exit Function
    End If

    If( ( D2 - D1 ) = 0 ) Then
        checkProcessCpuWMI = True
        strSysData         = "0"
        strSysExplanation  = "CPU usage of process [" & strProcess & "] = [0%]"
        Exit Function
    End If

    numCPU                 = ((N2 - N1) / (D2 - D1)) * 100
    strSysData             = CInt( ((N2 - N1) / (D2 - D1)) * 100 )
    strSysExplanation      = "CPU usage of process [" & strProcess & "] = [" & CInt( ((N2 - N1) / (D2 - D1)) * 100 ) & "%]" 
    If( numCPU > numMaxCPU ) Then 
        checkProcessCpuWMI = False
    Else 
        checkProcessCpuWMI = True
    End If

End Function

' //////////////////////////////////////////////////////////////////////////////

Function checkNumProcessesWMI( objWMIService, strComputer, strProcess, numMinInstances, numMaxInstances, BYREF strSysData, BYREF strSysExplanation )

    Dim objProcess, collProcesses, numProcesses

    checkNumProcessesWMI         = retvalUnknown  ' Default return value
    numProcesses                 = 0

On Error Resume Next

    set collProcesses = objWMIService.ExecQuery( "Select * from Win32_Process" ) 
    If( Err.Number <> 0 ) Then
        strSysData         = ""
        strSysExplanation  = "Unable to query WMI on computer [" & strComputer & "]"
        Exit Function
    End If
    If( collProcesses.Count <= 0  ) Then
        strSysData         = ""
        strSysExplanation  = "Win32_Process class does not exist on computer [" & strComputer & "]"
        Exit Function
    End If

On Error Goto 0

    For Each objProcess in collProcesses
        If( Err.Number <> 0 ) Then
            checkNumProcessesWMI = retvalUnknown
            strSysExplanation    = "Unable to list processes on computer [" & strComputer & "]"
            Exit Function 
        End If
	If UCase( objProcess.Name ) = UCase( strProcess ) Then
            numProcesses = numProcesses + 1
        End If
    Next

    strSysData                  = numProcesses
    strSysExplanation           = numProcesses & " number of instances of process [" & strProcess & "] running on computer [" & strComputer & "]"

    If( numProcesses < numMinInstances OR numProcesses > numMaxInstances ) Then
        checkNumProcessesWMI    = False
        Exit Function
    End If

    checkNumProcessesWMI        = True

End Function

' //////////////////////////////////////////////////////////////////////////////

Function getWMIObject( strComputer, strCredentials, BYREF objWMIService, BYREF strSysExplanation )	

On Error Resume Next

    Dim objNMServerCredentials, objSWbemLocator, colItems
    Dim strUsername, strPassword

    getWMIObject              = False

    Set objWMIService         = Nothing
    
    If( strCredentials = "" ) Then	
        ' Connect to remote host on same domain using same security context
        Set objWMIService     = GetObject( "winmgmts:{impersonationLevel=Impersonate}!\\" & strComputer &"\root\cimv2" )
    Else
        Set objNMServerCredentials = CreateObject( "ActiveXperts.NMServerCredentials" )

        strUsername           = objNMServerCredentials.GetLogin( strCredentials )
        strPassword           = objNMServerCredentials.GetPassword( strCredentials )

        If( strUsername = "" ) Then
            getWMIObject      = False
            strSysExplanation = "No alternate credentials defined for [" & strCredentials & "]. In the Manager application, select 'Options' from the 'Tools' menu and select the 'Server Credentials' tab to enter alternate credentials"
            Exit Function
        End If
	
        ' Connect to remote host using different security context and/or different domain 
        Set objSWbemLocator   = CreateObject( "WbemScripting.SWbemLocator" )
        Set objWMIService     = objSWbemLocator.ConnectServer( strComputer, "root\cimv2", strUsername, strPassword )

        If( Err.Number <> 0 ) Then
            objWMIService     = Nothing
            getWMIObject      = False
            strSysExplanation = "Unable to access [" & strComputer & "]. Possible reasons: WMI not running on the remote server, Windows firewall is blocking WMI calls, insufficient rights, or remote server down"
            Exit Function
        End If

        objWMIService.Security_.ImpersonationLevel = 3

    End If
	
    If( Err.Number <> 0 ) Then
        objWMIService         = Nothing
        getWMIObject          = False
        strSysExplanation     = "Unable to access '" & strComputer & "'. Possible reasons: no WMI installed on the remote server, no rights to access remote WMI service, or remote server down"
        Exit Function
    End If    

    getWMIObject              = True 

End Function