vmware.vbs - vbscript script by ActiveXperts Software
vmware.vbs checks a counter on a VMware host or a VMware Virtual Machine.
Use vmware.vbs directly from ActiveXperts Network Monitor; in the Manager's 'Monitor' menu, select 'New Check (Script)' and select vmware.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.
vmware.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 Dim objConstants ' Constants - return values Const retvalUnknown = 1 ' ActiveXperts Network Monitor functions should always return True (-1, Success), False (0, Error) or retvalUnknown (1, Uncertain) Set objConstants = CreateObject( "AxNetwork.NwConstants" ) ' // 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 = Check_CPU_Usage_Percent( "esx01", "", "<90" ) ' WScript.Echo "Return value: [" & bResult & "]" ' WScript.Echo "SYSDATA: [" & SYSDATA & "]" ' WScript.Echo "SYSEXPLANATION: [" & SYSEXPLANATION & "]" Function Check_CPU_Usage_Percent( strHost, strVM, strCondition ) ' Description: ' Checks CPU usage on a VMware host or virtual machine. ' Parameters: ' 1) strHost As String - Hostname or IP address of the ESXi VMware Server ' 2) strVM As String - The Virtual Machine you want to monitor; use empty string to monitor the VMware host ' 3) strCondition As String - The condition, formatted as the operator (<, >, <>, =, <=, >=) followed by a number, e.g.: "< 95" ' Usage: ' Check_CPU_Usage_Percent( "<Hostname | IP>", "<VM>", "<Condition>" ) ' Sample: ' Check_CPU_Usage_Percent( "esx01", "", "<90" ) Check_CPU_Usage_Percent = checkVMware( strHost, strVM, objConstants.nwVMWARE_CPU_USAGE, "", strCondition ) End Function ' ////////////////////////////////////////////////////////////////////////////// Function Check_Memory_Usage_Percent( strHost, strVM, strCondition ) ' Description: ' Checks Memory usage on a VMware host or virtual machine. ' Parameters: ' 1) strHost As String - Hostname or IP address of the ESXi VMware Server ' 2) strVM As String - The Virtual Machine you want to monitor; use empty string to monitor the VMware host ' 3) strCondition As String - The condition, formatted as the operator (<, >, <>, =, <=, >=) followed by a number, e.g.: "< 95" ' Usage: ' Check_Memory_Usage_Percent( "<Hostname | IP>", "<VM>", "<Condition>" ) ' Sample: ' Check_Memory_Usage_Percent( "esx01", "", "<90" ) Check_Memory_Usage_Percent = checkVMware( strHost, strVM, objConstants.nwVMWARE_MEMORY_USAGE, "", strCondition ) End Function ' ////////////////////////////////////////////////////////////////////////////// Function Check_Memory_Available_MB( strHost, strVM, strCondition ) ' Description: ' Checks Memory Availabe in MB on a VMware host or virtual machine. ' Parameters: ' 1) strHost As String - Hostname or IP address of the ESXi VMware Server ' 2) strVM As String - The Virtual Machine you want to monitor; use empty string to monitor the VMware host ' 3) strCondition As String - The condition, formatted as the operator (<, >, <>, =, <=, >=) followed by a number, e.g.: "< 95" ' Usage: ' Check_Memory_Available_MB( "<Hostname | IP>", "<VM>", "<Condition>" ) ' Sample: ' Check_Memory_Available_MB( "esx01", "", ">200" ) Check_Memory_Available_MB = checkVMware( strHost, strVM, objConstants.nwVMWARE_MEMORY_AVAILABLE, "", strCondition ) End Function ' ////////////////////////////////////////////////////////////////////////////// Function Check_Memory_Used_MB( strHost, strVM, strCondition ) ' Description: ' Checks Memory Used in MB on a VMware host or virtual machine. ' Parameters: ' 1) strHost As String - Hostname or IP address of the ESXi VMware Server ' 2) strVM As String - The Virtual Machine you want to monitor; use empty string to monitor the VMware host ' 3) strCondition As String - The condition, formatted as the operator (<, >, <>, =, <=, >=) followed by a number, e.g.: "<2000" ' Usage: ' Check_Memory_Usage_Percent( "<Hostname | IP>", "<VM>", "<Condition>" ) ' Sample: ' Check_Memory_Usage_Percent( "esx01", "", "<20000" ) Check_Memory_Used_MB = checkVMware( strHost, strVM, objConstants.nwVMWARE_MEMORY_USED, "", strCondition) End Function ' ////////////////////////////////////////////////////////////////////////////// Function Check_Network_Packets_Received_Per_Second( strHost, strVM, strContext, strCondition ) ' Description: ' Checks Network Packets Received Per Second on a VMware host or virtual machine. ' Parameters: ' 1) strHost As String - Hostname or IP address of the ESXi VMware Server ' 2) strVM As String - The Virtual Machine you want to monitor; use empty string to monitor the VMware host ' 3) strContext as String - The context of the counter, indicating the NIC card (zero-based index, as a string), e.g.: "0", or "1", etc. ' 4) strCondition As String - The condition, formatted as the operator (<, >, <>, =, <=, >=) followed by a number, e.g.: "<100000" ' Usage: ' Check_Network_Packets_Received_Per_Second( "<Hostname | IP>", "<VM>", "<Context>", "<Condition>" ) ' Sample: ' Check_Network_Packets_Received_Per_Second( "esx01", "", "0", "<100000" ) Check_Network_Packets_Received_Per_Second = checkVMware( strHost, strVM, objConstants.nwVMWARE_NETWORK_PACKETSRX, strContext, strCondition ) End Function ' ////////////////////////////////////////////////////////////////////////////// Function Check_Network_Packets_Transmitted_Per_Second( strHost, strVM, strContext, strCondition ) ' Description: ' Checks Network Packets Transmitted Per Second on a VMware host or virtual machine. ' Parameters: ' 1) strHost As String - Hostname or IP address of the ESXi VMware Server ' 2) strVM As String - The Virtual Machine you want to monitor; use empty string to monitor the VMware host ' 3) strContext as String - The context of the counter, indicating the NIC card (zero-based index, as a string), e.g.: "0", or "1", etc. ' 4) strCondition As String - The condition, formatted as the operator (<, >, <>, =, <=, >=) followed by a number, e.g.: "< 95" ' Usage: ' Check_Network_Packets_Transmitted_Per_Second( "<Hostname | IP>", "<VM>", "<Context>", "<Condition>" ) ' Sample: ' Check_Network_Packets_Transmitted_Per_Second( "esx01", "", "0", "<100000" ) Check_Network_Packets_Transmitted_Per_Second = checkVMware( strHost, strVM, objConstants.nwVMWARE_NETWORK_PACKETSTX, strContext, strCondition ) End Function ' ////////////////////////////////////////////////////////////////////////////// Function Check_Network_Receiving_Rate_KBS(strHost, strVM, strContext, strCondition ) ' Description: ' Checks Network Receiving Rate in KB per Second on a VMware host or virtual machine. ' Parameters: ' 1) strHost As String - Hostname or IP address of the ESXi VMware Server ' 2) strVM As String - The Virtual Machine you want to monitor; use empty string to monitor the VMware host ' 3) strContext as String - The context of the counter, indicating the NIC card (zero-based index, as a string), e.g.: "0", or "1", etc. ' 4) strCondition As String - The condition, formatted as the operator (<, >, <>, =, <=, >=) followed by a number, e.g.: "< 95" ' Usage: ' Check_Network_Receiving_Rate_KBS( "<Hostname | IP>", "<VM>", "<Context>", "<Condition>" ) ' Sample: ' Check_Network_Receiving_Rate_KBS( "esx01", "", "0", "<250" ) Check_Network_Receiving_Rate_KBS = checkVMware( strHost, strVM, objConstants.nwVMWARE_NETWORK_RATERX, strContext, strCondition ) End Function ' ////////////////////////////////////////////////////////////////////////////// Function Check_Network_Transmitting_Rate_KBS( strHost, strVM, strContext, strCondition ) ' Description: ' Checks Network Transmitting Rate in KB per Second on a VMware host or virtual machine. ' Parameters: ' 1) strHost As String - Hostname or IP address of the ESXi VMware Server ' 2) strVM As String - The Virtual Machine you want to monitor; use empty string to monitor the VMware host ' 3) strContext as String - The context of the counter, indicating the NIC card (zero-based index, as a string), e.g.: "0", or "1", etc. ' 4) strCondition As String - The condition, formatted as the operator (<, >, <>, =, <=, >=) followed by a number, e.g.: "< 95" ' Usage: ' Check_Network_Transmitting_Rate_KBS( "<Hostname | IP>", "<VM>", "<Context>", "<Condition>" ) ' Sample: ' Check_Network_Transmitting_Rate_KBS( "esx01", "", "0", "<250" ) Check_Network_Transmitting_Rate_KBS = checkVMware( strHost, strVM, objConstants.nwVMWARE_NETWORK_RATETX, strContext, strCondition ) End Function ' ////////////////////////////////////////////////////////////////////////////// Function Check_Disk_Usage_Percent( strHost, strVM, strContext, strCondition ) ' Description: ' Checks Disk Usage in Percentage on a VMware host or virtual machine. ' Parameters: ' 1) strHost As String - Hostname or IP address of the ESXi VMware Server ' 2) strVM As String - The Virtual Machine you want to monitor; use empty string to monitor the VMware host ' 3) strContext as String - The context of the counter, indicating the Disk (zero-based index, as a string), e.g.: "0", or "1", etc. ' 4) strCondition As String - The condition, formatted as the operator (<, >, <>, =, <=, >=) followed by a number, e.g.: "< 95" ' Usage: ' Check_Disk_Usage_Percent( "<Hostname | IP>", "<VM>", "<Context>", "<Condition>" ) ' Sample: ' Check_Disk_Usage_Percent( "esx01", "", "0", "<95" ) Check_Disk_Usage_Percent = checkVMware( strHost, strVM, objConstants.nwVMWARE_DISK_USAGE, strContext, strCondition ) End Function ' ////////////////////////////////////////////////////////////////////////////// Function Check_Disk_Space_Available_MB(strHost, strVM, strContext, strCondition ) ' Description: ' Checks Disk Space Available in MB on a VMware host or virtual machine. ' Parameters: ' 1) strHost As String - Hostname or IP address of the ESXi VMware Server ' 2) strVM As String - The Virtual Machine you want to monitor; use empty string to monitor the VMware host ' 3) strContext as String - The context of the counter, indicating the Disk (zero-based index, as a string), e.g.: "0", or "1", etc. ' 4) strCondition As String - The condition, formatted as the operator (<, >, <>, =, <=, >=) followed by a number, e.g.: "< 95" ' Usage: ' Check_Disk_Space_Available_MB( "<Hostname | IP>", "<VM>", "<Context>", "<Condition>" ) ' Sample: ' Check_Disk_Space_Available_MB( "esx01", "", "0", ">10000" ) Check_Disk_Space_Available_MB = checkVMware( strHost, strVM, objConstants.nwVMWARE_DISK_AVAILABLE, strContext, strCondition ) End function ' ////////////////////////////////////////////////////////////////////////////// Function Check_Disk_Space_Used_MB( strHost, strVM, strContext, strCondition ) ' Description: ' Checks Disk Space Used in MB on a VMware host or virtual machine. ' Parameters: ' 1) strHost As String - Hostname or IP address of the ESXi VMware Server ' 2) strVM As String - The Virtual Machine you want to monitor; use empty string to monitor the VMware host ' 3) strContext as String - The context of the counter, indicating the Disk (zero-based index, as a string), e.g.: "0", or "1", etc. ' 4) strCondition As String - The condition, formatted as the operator (<, >, <>, =, <=, >=) followed by a number, e.g.: "< 95" ' Usage: ' Check_Disk_Space_Used_MB( "<Hostname | IP>", "<VM>", "<Context>", "<Condition>" ) ' Sample: ' Check_Disk_Space_Used_MB( "esx01", "", "0", "<500000" ) Check_Disk_Space_Used_MB = checkVMware( strHost, strVM, objConstants.nwVMWARE_DISK_USED, strContext, strCondition ) End Function ' ////////////////////////////////////////////////////////////////////////////// Function Check_VM_Powered_ON( strHost, strVM, strCondition ) ' Description: ' Checks if VirtualMachine is PoweredON. ' This check only works on a Virtual Machine, not on the Host ' Use one of the following integers in the nValue parameter: ' 0: Off; 1: On; 2: Suspended ' Parameters: ' 1) strHost As String - Hostname or IP address of the ESXi VMware Server ' 2) strVM As String - The Virtual Machine you want to monitor; use empty string to monitor the VMware host ' 3) strCondition As String - The condition, formatted as the operator (<, >, <>, =, <=, >=) followed by a number, e.g.: "=1" ' Usage: ' Check_VM_Powered_ON( "<Hostname | IP>", "<VM>", "<Condition>" ) ' Sample: ' Check_VM_Powered_ON( "esx01", "dell15", "=1" ) Check_VM_Powered_ON = checkVMware( strHost, strVM, objConstants.nwVMWARE_MACHINE_STATE, "", strCondition ) End Function ' ////////////////////////////////////////////////////////////////////////////// Function Check_VM_Running( strHost, strVM, strCondition ) ' Description: ' Checks if VirtualMachine is Running. ' This check only works on a Virtual Machine, not on the Host ' Use one of the following integers in the nValue parameter: ' 0: Not Running; 1: Resetting; 2: Running; 3: Shutting Down; 4: Standby; 5: Unknown ' Parameters: ' 1) strHost As String - Hostname or IP address of the ESXi VMware Server ' 2) strVM As String - The Virtual Machine you want to monitor ' 3) strCondition As String - The condition, formatted as the operator (<, >, <>, =, <=, >=) followed by a number, e.g.: "=1" ' Usage: ' Check_VM_Running( "<Hostname | IP>", "<VM>", "<Condition>" ) ' Sample: ' Check_VM_Running( "esx01", "dell15", "=1" ) Check_VM_Running = checkVMware( strHost, strVM, objConstants.nwVMWARE_POWER_STATE, "", strCondition ) End Function ' ////////////////////////////////////////////////////////////////////////////// Function Check_VM_Guest_Tools_Installed(strHost, strVM, strCondition ) ' Description: ' Checks if Quest Tools are Installed/Running is Running. ' This check only works on a Virtual Machine, not on the Host ' Use one of the following integers in the nValue parameter: ' 0: Not Running ' 1: Running ' Parameters: ' 1) strHost As String - Hostname or IP address of the ESXi VMware Server ' 2) strVM As String - The Virtual Machine you want to monitor ' 3) strCondition As String - The condition, formatted as the operator (<, >, <>, =, <=, >=) followed by a number, e.g.: "=1" ' Usage: ' Check_VM_Guest_Tools_Installed( "<Hostname | IP>", "<VM>", "<Condition>" ) ' Sample: ' Check_VM_Guest_Tools_Installed( "esx01", "dell15", "=1" ) Check_VM_Guest_Tools_Installed = checkVMware( strHost, strVM, objConstants.nwVMWARE_GUESTTOOLS, "", strCondition ) 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 checkVMware(strHost, strVM, nCounterID, strContext, strCondition ) Dim objVMware, objServerCredentials, nCurrValue, strEval, strResultKeywork checkVMware = retvalUnknown SYSDATA = "" SYSEXPLANATION = "" Set objVMware = CreateObject( "AxNetwork.VMWare" ) Set objServerCredentials = CreateObject( "ActiveXperts.NMVMwareCredentials" ) objVMware.Server = strHost objVMware.ServerAccount = objServerCredentials.GetLogin( strHost ) objVMware.ServerPassword = objServerCredentials.GetPassword( strHost ) ' Get Credentials of VMware Server If( objVMware.ServerAccount = "" ) Then SYSEXPLANATION = "No VMware credentials defined for [" & strHost & "]. In the Manager application, create a 'new Monitoring Check', Pick the 'VMware ESXi' check and add the credentials." Exit Function End If ' Initialize VMware object objVMware.Initialize If( objVMware.LastError <> 0 ) Then SYSEXPLANATION = "VMware Initialize failed, error [" & objVMware.LastError & "]: " & objVMware.GetErrorDescription( objVMware.LastError ) Exit Function End If ' Connect to VMware Server objVMware.Connect If ( objVMware.LastError <> 0 ) Then SYSEXPLANATION = "VMware Connect failed, error [" & objVMware.LastError & "]: " & objVMware.GetErrorDescription( objVMware.LastError ) objVMware.Disconnect objVMware.Shutdown Exit Function End If ' Get counter value nCurrValue = objVMware.GetPerfData( strVM, nCounterID, strContext ) If ( objVMware.LastError <> 0 ) Then SYSEXPLANATION = "VMware GetPerfData failed, error [" & objVMware.LastError & "]: " & objVMware.GetErrorDescription( objVMware.LastError ) objVMware.Disconnect objVMware.Shutdown Exit Function End If strEval = nCurrValue & strCondition If( Not Eval( strEval ) ) Then checkVMware = False strResultKeywork = "failed" Else checkVMware = True strResultKeywork = "OK" End If SYSDATA = nCurrValue SYSEXPLANATION = "Counter [" & objVMware.GetCounterDescription( nCounterID ) & "], Condition [" & strCondition & "] " & strResultKeywork & ", Current Value=[" & nCurrValue & "]" objVMware.Disconnect objVMware.Shutdown End Function