Contact Info

Crumbtrail

ActiveXperts.com » Network Monitor » Scripts » Custom Script

memory.vbs - vbscript script by ActiveXperts Software

memory.vbs checks free memory (MB) on a defined host.

Use memory.vbs directly from ActiveXperts Network Monitor; in the Manager's 'Monitor' menu, select 'New Check (Script)' and select memory.vbs. Configure the required parameter, or press 'Load a working sample'.

In ActiveXperts Network Monitor, Administrators can use three different scripting languages: Powershell, VBScript and SSH.


memory.vbs script code

' ///////////////////////////////////////////////////////////////////////////////
' // ActiveXperts Network Monitor  - VBScript based checks
' // For more information about ActiveXperts Network Monitor and VBScript, visit
' // http://www.activexperts.com/support/network-monitor/online/vbscript/
' ///////////////////////////////////////////////////////////////////////////////

Option Explicit

' Declaration of global variables
Dim   SYSDATA, SYSEXPLANATION   ' SYSDATA is displayed in the 'Data' column in the Manager; SYSEXPLANATION in the 'LastResponse' column

' Constants - return values
Const retvalUnknown = 1         ' ActiveXperts Network Monitor functions should always return True (-1, Success), False (0, Error) or retvalUnknown (1, Uncertain)

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


Function CheckFreePhysicalMemory( strHost, strAltCredentials, nMinMB )
' Description: 
'     Check free memory (MB) on the computer specified by strHost
' Parameters:
'     1) strHost As String - Hostname or IP address of the computer you want to check
'     2) strAltCredentials 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 strAltLogin, strAltPassword
  Dim objWMIService

  CheckFreePhysicalMemory = retvalUnknown  ' Default return value, and will be shown as a yellow (uncertain) icon in the Manager
  SYSDATA          = ""             ' SYSDATA displayed in the 'Data' column in the Manager          
  SYSEXPLANATION   = ""             ' SYSEXPLANATION displayed in the 'LastResponse' column in the Manager
  
  strAltLogin      = ""
  strAltPassword   = ""

  ' If alternate credentials are specified, retrieve the alternate login and password from the ActiveXperts global settings
  If( strAltCredentials <> "" ) Then	
    If( Not getCredentials( strHost, strAltCredentials, strAltLogin, strAltPassword, SYSEXPLANATION )) Then
      Exit Function
    End If
  End If  

  ' WMI Connect
  If( Not wmiConnect( strHost, strAltLogin, strAltPassword, objWMIService, SYSEXPLANATION ) ) Then
    Exit Function
  End If

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

End Function


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

Function CheckPagesPerSecond( strHost, strAltCredentials, nMaxPages )
' Description: 
'     Check pages per second on the computer specified by strHost
' Parameters:
'     1) strHost As String - Hostname or IP address of the computer you want to check
'     2) strAltCredentials 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 strAltLogin, strAltPassword
  Dim objWMIService

  CheckPagesPerSecond = retvalUnknown  ' Default return value, and will be shown as a yellow (uncertain) icon in the Manager
  SYSDATA         = ""             ' SYSDATA displayed in the 'Data' column in the Manager          
  SYSEXPLANATION  = ""             ' SYSEXPLANATION displayed in the 'LastResponse' column in the Manager
  
  strAltLogin     = ""
  strAltPassword  = ""
  
  ' If alternate credentials are specified, retrieve the alternate login and password from the ActiveXperts global settings
  If( strAltCredentials <> "" ) Then	
    If( Not getCredentials( strHost, strAltCredentials, strAltLogin, strAltPassword, SYSEXPLANATION )) Then
      Exit Function
    End If
  End If  

  ' WMI Connect
  If( Not wmiConnect( strHost, strAltLogin, strAltPassword, objWMIService, SYSEXPLANATION ) ) Then
    Exit Function
  End If

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

End Function


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

Function CheckCommittedMemory( strHost, strAltCredentials, nMaxCommittedMB )
' Description: 
'     Check free memory (MB) on the computer specified by strHost
' Parameters:
'     1) strHost As String - Hostname or IP address of the computer you want to check
'     2) strAltCredentials 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 strAltLogin, strAltPassword
  Dim objWMIService

  CheckCommittedMemory = retvalUnknown  ' Default return value, and will be shown as a yellow (uncertain) icon in the Manager
  SYSDATA         = ""             ' SYSDATA displayed in the 'Data' column in the Manager          
  SYSEXPLANATION  = ""             ' SYSEXPLANATION displayed in the 'LastResponse' column in the Manager
  
  strAltLogin     = ""
  strAltPassword  = ""

  ' If alternate credentials are specified, retrieve the alternate login and password from the ActiveXperts global settings
  If( strAltCredentials <> "" ) Then	
    If( Not getCredentials( strHost, strAltCredentials, strAltLogin, strAltPassword, SYSEXPLANATION )) Then
      Exit Function
    End If
  End If  

  ' WMI Connect
  If( Not wmiConnect( strHost, strAltLogin, strAltPassword, objWMIService, SYSEXPLANATION ) ) Then
    Exit Function
  End If

   CheckCommittedMemory = checkCommittedMemoryWMI( objWMIService, strHost, nMaxCommittedMB, SYSDATA, SYSEXPLANATION )

End Function



' //////////////////////////////////////////////////////////////////////////////
' // --- Private Functions section ---
' // Private functions names should start with a lower case character, so they 
' // will not be listed in the Network Monitor's function browser.
' //////////////////////////////////////////////////////////////////////////////

Function checkFreePhysicalMemoryWMI( objWMIService, strHost, 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 class on computer [" & strHost & "]"
    Exit Function
  End If
  If( colItems.Count <= 0  ) Then
    strSysData         = ""
    strSysExplanation  = "Win32_OperatingSystem class does not exist on computer [" & strHost & "]"
    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 [" & strHost & "]"

End Function

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

Function checkPagesPerSecondWMI( objWMIService, strHost, 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 class on computer [" & strHost & "]"
    Exit Function
  End If
  If( colItems.Count <= 0  ) Then
    strSysData         = ""
    strSysExplanation  = "Win32_PerfFormattedData_PerfOS_Memory class does not exist on computer [" & strHost & "]"
    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, strHost, 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 class on computer [" & strHost & "]"
  Exit Function
  End If
  If( colItems.Count <= 0  ) Then
    strSysData         = ""
    strSysExplanation  = "Win32_PerfFormattedData_PerfOS_Memory class does not exist on computer [" & strHost & "]"
    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 getCredentials( strHost, strAltCredentials, BYREF strAltLogin, BYREF strAltPassword, BYREF strSysExplanation )	

  Dim objNMServerCredentials
  
  strAltLogin = ""
  strAltPassword = ""
  strSysExplanation = ""
  
  getCredentials  = False    
  
  If( strAltCredentials = "" ) Then
    ' No alternate credentials specified, so login and password are empty and service credentials will be used
    getCredentials = True
    Exit Function
  End If
  
  Set objNMServerCredentials = CreateObject( "ActiveXperts.NMServerCredentials" )

  strAltLogin           = objNMServerCredentials.GetLogin( strAltCredentials )
  strAltPassword        = objNMServerCredentials.GetPassword( strAltCredentials )

  If( strAltLogin = "" ) Then
    getCredentials      = False
    strSysExplanation = "No alternate credentials defined for [" & strAltCredentials & "]. In the Manager application, select 'Options' from the 'Tools' menu and select the 'Server Credentials' tab to enter alternate credentials"
    Exit Function
  End If   

  getCredentials = True 

End Function

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

Function wmiConnect( strHost, strAltLogin, strAltPassword, BYREF objWMIService, BYREF strSysExplanation )	

  Dim objSWbemLocator, colItems
  Dim bConnectResult

  wmiConnect         = False
  Set objWMIService  = Nothing
      
  If( strAltLogin = "" ) Then	
    ' Connect to remote host on same domain using same security context
On Error Resume Next    
    Set objWMIService     = GetObject( "winmgmts:{impersonationLevel=Impersonate}!\\" & strHost &"\root\cimv2" )
    If( Err.Number <> 0 ) Then
      bConnectResult = False
    Else
      bConnectResult = True
    End If
On Error Goto 0    
    
  Else
    ' Connect to remote host using different security context and/or different domain 
On Error Resume Next        
    Set objSWbemLocator   = CreateObject( "WbemScripting.SWbemLocator" )
    Set objWMIService     = objSWbemLocator.ConnectServer( strHost, "root\cimv2", strAltLogin, strAltPassword )

    If( Err.Number <> 0 ) Then
      bConnectResult = False
    Else
      bConnectResult = True
    End If

    objWMIService.Security_.ImpersonationLevel = 3
On Error Goto 0    
    
  End If

  If( Not bConnectResult ) Then
    Set objWMIService  = Nothing
    wmiConnect         = False
    strSysExplanation  = "Unable to connect to [" & strHost & "]. Possible reasons: no WMI installed on the remote server, firewall blocking WMI calls, login failure, or remote server down"
    Exit Function
  End If    
  
  wmiConnect = True 
End Function