Contact Info

Crumbtrail

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

Memory.vbs - Monitor memory usage on a Windows operating system

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 Memory monitoring 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 Memory check

Memory.vbs script source code

' ///////////////////////////////////////////////////////////////////////////////
' // ActiveXperts Network Monitor  - VBScript based checks
' // (c) 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/
' // 
' ///////////////////////////////////////////////////////////////////////////////
'  

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 =  CheckFreePhysicalMemory( "localhost", "", 100 )
' WScript.Echo "Return value: [" & bResult & "]"
' WScript.Echo "SYSDATA: [" & SYSDATA & "]"
' WScript.Echo "SYSEXPLANATION: [" & SYSEXPLANATION & "]"' ////////////////////////////////////////////////////////////////////////////////////////


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

Function CheckFreePhysicalMemory( strComputer, strCredentials, nMinMB )

' Description: 
'     Check free memory (MB) on the 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) nMinMB As Number - Minimum required free memory (in MB)
' Usage:
'     CheckFreePhysicalMemory( "<Hostname | IP>", "<Empty String | Server>", Min_MB )
' Sample:
'     CheckFreePhysicalMemory( "localhost", "", 100 )

    Dim objWMIService

    CheckFreePhysicalMemory      = retvalUnknown  ' Default return value
    SYSDATA          = ""             ' Initally empty; will contain the number of free MBs
    SYSEXPLANATION   = ""             ' Set initial value

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

    CheckFreePhysicalMemory      = checkFreePhysicalMemoryWMI( objWMIService, strComputer, nMinMB, SYSDATA, SYSEXPLANATION )

End Function

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

Function CheckPagesPerSecond( strComputer, strCredentials, nMaxPages )

' Description: 
'     Check pages per second on the 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) nMaxPages As Number - Maximum pages per second allowed
' Usage:
'     CheckPagesPerSecond( "<Hostname | IP>", "<Empty String | Server>", Max_Pages )
' Sample:
'     CheckPagesPerSecond( "localhost", "", 5 )

    Dim objWMIService

    CheckPagesPerSecond = retvalUnknown  ' Default return value
    SYSDATA             = ""             ' Initally empty; will contain the number of free MBs
    SYSEXPLANATION      = ""             ' Set initial value

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

    CheckPagesPerSecond = checkPagesPerSecondWMI( objWMIService, strComputer, nMaxPages, SYSDATA, SYSEXPLANATION )

End Function


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

Function CheckCommittedMemory( strComputer, strCredentials, nMaxCommittedMB )

' Description: 
'     Check free memory (MB) on the 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) nMaxCommittedMB  As Number - Maximum allowed committed memory (in MB)
' Usage:
'     CheckCommittedMemory( "<Hostname | IP>", "<Empty String | Server>", Max_MB )
' Sample:
'     CheckCommittedMemory( "localhost", "", 800 )

    Dim objWMIService

    CheckCommittedMemory  = retvalUnknown  ' Default return value
    SYSDATA               = ""             ' Initally empty; will contain the number of free MBs
    SYSEXPLANATION        = ""             ' Set initial value

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

    CheckCommittedMemory      = checkCommittedMemoryWMI( objWMIService, strComputer, nMaxCommittedMB, 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 checkFreePhysicalMemoryWMI( objWMIService, strComputer, nMinMB, BYREF strSysData, BYREF strSysExplanation )

    Dim colItems, objOS, nFreeMemMB, nDiffMB

    checkFreePhysicalMemoryWMI             = retvalUnknown  ' Default return value

On Error Resume Next

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

On Error Goto 0

    For Each objOS In colItems

        If( Err.Number <> 0 ) Then
            checkFreePhysicalMemoryWMI     = retvalUnknown
            strSysData         = ""
            strSysExplanation  = "Unable to queue memory information." 
            Exit Function 
        End If

        nFreeMemMB             = FormatNumber( objOS.FreePhysicalMemory / 1024, 0, 0, 0, 0 )
        strSysData             = nFreeMemMB
        strSysExplanation      = "Free physical memory=[" & nFreeMemMB & " MB], minimum required=[" & nMinMB & " MB]"
        nDiffMB = nFreeMemMB - nMinMB 
        If( nDiffMB > 0 ) Then 
             checkFreePhysicalMemoryWMI    = True
             Exit Function
        Else
            checkFreePhysicalMemoryWMI     = False
            Exit Function
        End If
    Next

    checkFreePhysicalMemoryWMI = retvalUnknown
    strSysExplanation          = "Unable to find WMI memory class on computer [" & strComputer & "]"

End Function

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

Function checkPagesPerSecondWMI( objWMIService, strComputer, nMaxPages, BYREF strSysData, BYREF strSysExplanation )

    Dim colItems, i, nPages, nDiffPages

    checkPagesPerSecondWMI    = retvalUnknown  ' Default return value

On Error Resume Next

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

On Error Goto 0

    For Each i In colItems: Exit For: Next    'Hack to recover object from collection.

        If( Err.Number <> 0 ) Then
            checkPagesPerSecondWMI = retvalUnknown
            strSysData             = ""
            strSysExplanation      = "Unable to queue memory information." 
            Exit Function 
        End If

        nPages                     = i.PagesPerSec
        strSysData                 = nPages
        strSysExplanation          = "Pages per second=[" & nPages & "], maximum allowed=[" & nMaxPages & "]"
        nDiffPages                 = nMaxPages - nPages
        If( nDiffPages > 0 ) Then 
             checkPagesPerSecondWMI    = True
             Exit Function
        Else
            checkPagesPerSecondWMI     = False
            Exit Function
        End If

End Function

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

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

    Dim colItems, i, nCommittedMB, nDiffMB

    checkCommittedMemoryWMI    = retvalUnknown  ' Default return value

On Error Resume Next

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

On Error Goto 0

    For Each i In colItems: Exit For: Next    'Hack to recover object from collection.

        If( Err.Number <> 0 ) Then
            checkCommittedMemoryWMI    = retvalUnknown
            strSysData         = ""
            strSysExplanation  = "Unable to queue memory information." 
            Exit Function 
        End If

        nCommittedMB           = FormatNumber( i.CommittedBytes / ( 1024 * 1024 ), 0, 0, 0, 0 )
        strSysData             = nCommittedMB
        strSysExplanation      = "Committed memory=[" & nCommittedMB & " MB], maximum allowed=[" & nMaxMB & " MB]"
        nDiffMB = nMaxMB - nCommittedMB
        If( nDiffMB > 0 ) Then 
             checkCommittedMemoryWMI    = True
             Exit Function
        Else
            checkCommittedMemoryWMI     = False
            Exit Function
        End If

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