Contact Info

Crumbtrail

ActiveXperts.com » Administration » VBScript » Network Monitor » Disk Drives

Disks.vbs - Check disk functions to monitor disks

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 Disk 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 Disk Drives check

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

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


Function CheckDiskDrives( strComputer, strCredentials )

' Description: 
'     This function checks all disks on a computer specified by strComputer.
' Parameters:
'     1) strComputer As String  - Hostname or IP address of the computer you want to monitor
'     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)
' Usage:
'     CheckDiskDrives( "<Hostname | IP>", "<Empty String | Server>" )
' Sample:
'     CheckDiskDrives( "localhost", "" )

    Dim objWMIService

    CheckDiskDrives      = retvalUnknown  ' Default return value
    SYSDATA              = ""             ' Not used in this function
    SYSEXPLANATION       = ""             ' Set initial value

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

    CheckDiskDrives      = checkDiskDrivesWMI( objWMIService, strComputer, SYSEXPLANATION )

End Function

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


Function CheckFloppyDrives( strComputer, strCredentials )

' Description: 
'     This function checks all floppy disk drives on a computer specified by strComputer.
' Parameters:
'     1) strComputer As String  - Hostname or IP address of the computer you want to monitor
'     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)
' Usage:
'     CheckFloppyDrives( "<Hostname | IP>", "<Empty String | Server>" )
' Sample:
'     CheckFloppyDrives( "localhost", "" )

    Dim objWMIService

    CheckFloppyDrives        = retvalUnknown  ' Default return value
    SYSDATA                  = ""             ' Not used in this function
    SYSEXPLANATION           = ""             ' Set initial value

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

    CheckFloppyDrives        = checkFloppyDrivesWMI( objWMIService, strComputer, 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 checkDiskDrivesWMI( objWMIService, strComputer, BYREF strSysExplanation )

    Dim colDisks, objDisk
    Dim strDisks

    checkDiskDrivesWMI            = retvalUnknown  ' Default return value

On Error Resume Next

    Set colDisks = objWMIService.ExecQuery( "Select * from Win32_DiskDrive" )
    If( Err.Number <> 0 ) Then
        strSysData         = ""
        strSysExplanation  = "Unable to query WMI on computer [" & strComputer & "]"
        Exit Function
    End If
    If( colDisks.Count <= 0  ) Then
        strSysData         = ""
        strSysExplanation  = "No disks on computer [" & strComputer & "]"
        Exit Function
    End If

On Error Goto 0

    For Each objDisk In colDisks
        If( Err.Number <> 0 ) Then
	    checkDiskDrivesWMI    = False
	    strSysExplanation     = "Unable to list disks on computer [" & strComputer & "]"
	    Exit Function
        End If
	If( objDisk.Status <> "OK" ) Then
            checkDiskDrivesWMI    = False
            strSysExplanation     = "Status of Disk [" & objDisk.Name & "] is: [" & objDisk.Status & "]"
            Exit Function
        Else
            If( strDisks <> "" ) Then
               strDisks = strDisks & ","
            End If
            strDisks = strDisks	& "[" & objDisk.Name & "]"
        End If
    Next

    checkDiskDrivesWMI            = True
    strSysExplanation             = "All disks are OK; disks checked=" & strDisks
End Function

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

Function checkFloppyDrivesWMI( objWMIService, strComputer, BYREF strSysExplanation )

On Error Resume Next

    Dim colDrives, objDrive

    checkFloppyDrivesWMI         = retvalUnknown  ' Default return value

On Error Resume Next

    Set colDrives = objWMIService.ExecQuery( "Select * from Win32_FloppyDrive" )
    If( Err.Number <> 0 ) Then
        strSysData         = ""
        strSysExplanation  = "Unable to query WMI on computer [" & strComputer & "]"
        Exit Function
    End If
    If( colDrives.Count <= 0  ) Then
        strSysData         = ""
        strSysExplanation  = "No floppy drives on computer [" & strComputer & "]"
        Exit Function
    End If

On Error Goto 0

    For Each objDrive In colDrives
        If( Err.Number <> 0 ) Then
            checkFloppyDrivesWMI = retvalUnknown
	    strSysExplanation    = "Unable to list floppy drives on computer [" & strComputer & "]"
	    Exit Function
        End If
        If( objDrive.Status <> "OK" ) Then
            checkFloppyDrivesWMI = False
            strSysExplanation    = "Floppy drive [" & objDrive.Name & "] is: [" & objDrive.Status & "]"
            Exit Function			    
        End If
    Next

    checkFloppyDrivesWMI         = True
    strSysExplanation            = "All floppy drives are OK"
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