Contact Info

Crumbtrail

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

Directory.vbs - Directory checking 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 Directory 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 Directory check

Directory.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 = CheckDirectorySize( "\\localhost\admin$", "", 300 )
' WScript.Echo "Return value: [" & bResult & "]"
' WScript.Echo "SYSDATA: [" & SYSDATA & "]"
' WScript.Echo "SYSEXPLANATION: [" & SYSEXPLANATION & "]"
' ////////////////////////////////////////////////////////////////////////////////////////

Function CheckDirectorySize( strPath, strCredentials, strLimitMB )
' Description: 
'     Check the directory size on a (remote) computer. Use network monitor service credentials to access the (remote) computer
' Parameters:
'     1) strPath As String - UNC formatted directory path
'     2) strCredentials As String - Specify an empty string to use Metwork 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:
'     3) strLimitMB As String - Maximum allowed directory size (MB)
' Usage:
'     CheckDirectorySize( "<\\Server\Share\Path>", "<Empty String | Server>", Max_MB )
' Sample:
'     CheckDirectorySize( "\\localhost\admin$", "", 300  )

    Dim nSizeMB, nDiffMB

    CheckDirectorySize         = retvalUnknown ' Unless indicated otherwise
    SYSDATA                    = ""             ' Initally empty; will contain the size of the directory (in MBs)
    SYSEXPLANATION             = ""             ' Set initial value

    If( strCredentials <> "" ) Then
        If( Not login( strCredentials, SYSEXPLANATION ) ) Then
            Exit Function
        End If
    End If

    nSizeMB                    = getFolderSizeMB( strPath )
    If( nSizeMB < 0 ) Then
        SYSEXPLANATION         = "Unable to retrieve directory size. Make sure that the directory exists and that Network Monitor can access it."
        CheckDirectorySize     = retvalUnknown 
    Else

        nDiffMB                    = strLimitMB - nSizeMB
        SYSDATA                    = nSizeMB
        SYSEXPLANATION             = "Directory size=[" & nSizeMB & " MB], maximum allowed size=[" & strLimitMB & " MB]"
        If( nDiffMB >= 0 ) Then
            CheckDirectorySize = True
        Else
            CheckDirectorySize = False
        End If
    End If

    If( strCredentials <> "" ) Then
        logout( strCredentials )
    End If

End Function


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

Function CheckDirectoryChange( strPath, strCredentials )
' Description: 
'     Check the directory for a change. 
'     If the size of the directory changes with 1KB or more, the directory has changed.
' Parameters:
'     1) strPath As String - UNC formatted directory path
'     2) strCredentials As String - Specify an empty string to use Metwork 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:
' Usage:
'     CheckDirectoryChange( "<\\Server\Share\Path>", "<Empty String | Server>" )
' Sample:
'     CheckDirectoryChange( "\\localhost\admin$", "" )
    
    Dim strPrevSizeKB, strSizeKB

    CheckDirectoryChange         = retvalUnknown ' Unless indicated otherwise
    SYSDATA                      = ""            ' Initally empty; will contain the size of the directory (in KBs)
    SYSEXPLANATION               = ""            ' Set initial value

    If( strCredentials <> "" ) Then
        If( Not login( strCredentials, SYSEXPLANATION ) ) Then
            Exit Function
        End If
    End If

    strSizeKB                    = "" & getFolderSizeKB( strPath )
    If( strSizeKB = "-1" ) Then
       CheckDirectoryChange      = retvalUnknown  
       SYSDATA                   = ""
       SYSEXPLANATION            = "Directory does not exist"
       Exit Function
    End If    

    strPrevSizeKB                = getCacheValue( extractPlainFile( strPath ) )
    setCacheValue extractPlainFile( strPath ), strSizeKB
    If( strPrevSizeKB = "" ) Then
       CheckDirectoryChange      = True  
       SYSDATA                   = ""
       SYSEXPLANATION            = "Directory was not monitored before"
       Exit Function
    End If

    If( strPrevSizeKB <> strSizeKB ) Then
       CheckDirectoryChange      = False  
       SYSDATA                   = strSizeKB
       SYSEXPLANATION            = "Directory has changed since last check"
    Else
       CheckDirectoryChange      = True  
       SYSDATA                   = strSizeKB
       SYSEXPLANATION            = "Directory has not changed since last check"
    End If

    If( strCredentials <> "" ) Then
        logout( strCredentials )
    End If

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 login( strCredentials, strSysExplanation )
' Login function for non-WMI based checks. 

    Dim objCredentials, objRemoteServer
    Dim strUsername, strPassword

    login                 = False

    Set objCredentials    = CreateObject( "ActiveXperts.NMServerCredentials" )
    Set objRemoteServer   = CreateObject( "ActiveXperts.RemoteServer" )

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

    If( strUsername = "" ) Then
        login             = 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

    objRemoteServer.Connect strCredentials, strUsername, strPassword
    If( objRemoteServer.LastError <> 0 ) Then
        login             = False
        strSysExplanation = "Login failed"
        Exit Function   
    End If

    login                 = True

End Function

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

Function logout( strCredentials )
' Logout function for non-WMI based checks. 

    Dim objRemoteServer

    logout                = False

    Set objRemoteServer   = CreateObject( "ActiveXperts.RemoteServer" )

    objRemoteServer.Disconnect strCredentials
    If( objRemoteServer.LastError <> 0 ) Then
        logout            = False
        Exit Function   
    End If

    logout                = True

End Function

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

Function getFileSizeKB( strPath )

' Description: 
'     Get the size of a file on a (remote) computer, in KB. 
'     Use network monitor service credentials to access the (remote) computer
' Parameters:
'     1) strPath - UNC formatted file path

On Error Resume Next

    Dim objFSO, objFile

    getFileSizeKB              = -1

    Set objFSO                 = CreateObject("Scripting.FileSystemObject")
    Set objFile                = objFSO.GetFile( strPath )
    If( Err.Number <> 0 ) Then
        getFileSizeKB          = -1
        Exit Function
    End If

    getFileSizeKB              = FormatNumber( objFile.Size / 1024, 0, 0, 0, 0 )

End Function

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

Function getFolderSizeKB( strPath )

' Description: 
'     Get the size of a folderon a (remote) computer, in KB. 
'     Use network monitor service credentials to access the (remote) computer
' Parameters:
'     1) strPath - UNC formatted folder

On Error Resume Next

    Dim objFSO, objFolder

    getFolderSizeKB            = -1

    Set objFSO                 = CreateObject("Scripting.FileSystemObject")
    Set objFolder              = objFSO.GetFolder( strPath )
    If( Err.Number <> 0 ) Then
        getFolderSizeKB        = -1
        Exit Function
    End If

    getFolderSizeKB            = FormatNumber( objFolder.Size / ( 1024 ), 0, 0, 0, 0 )

End Function

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

Function getFolderSizeMB( strPath )

' Description: 
'     Get the size of a folderon a (remote) computer, in MB. 
'     Use network monitor service credentials to access the (remote) computer
' Parameters:
'     1) strPath - UNC formatted folder

On Error Resume Next

    Dim objFSO, objFolder

    getFolderSizeMB            = -1

    Set objFSO                 = CreateObject("Scripting.FileSystemObject")
    Set objFolder              = objFSO.GetFolder( strPath )
    If( Err.Number <> 0 ) Then
        getFolderSizeMB        = -1
        Exit Function
    End If

    getFolderSizeMB            = FormatNumber( objFolder.Size / ( 1024 * 1024 ), 0, 0, 0, 0 )

End Function

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

Function getFileModDateString( strFile )

On Error Resume Next

    Dim fso, fsoFile, dtModDate

    getFileModDateString   = "" 

    Set fso                = CreateObject("Scripting.FileSystemObject")
    Set fsoFile            = fso.GetFile( strFile )
    dtModDate              = fsoFile.DateLastModified

    getFileModDateString   = "" & dtModDate  ' converts datetime format to string

End Function

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

Function extractPlainFile( strFilepath )

On Error Resume Next

    Dim arr, i

    arr = Split( strFilepath, "\" )
    extractPlainFile = arr( UBound( arr ) )

End Function

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

Function getCacheValue( strEntry )

On Error Resume Next

   Dim WshShell

   getCacheValue = ""

   Set WshShell  = CreateObject("WScript.Shell")
   getCacheValue = WshShell.RegRead("HKLM\Software\ActiveXperts\Network Monitor\Server\VBScript_Cache\" & strEntry )

End Function

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

Sub setCacheValue( strEntry, strData )

   Dim WshShell
   Set WshShell = CreateObject("WScript.Shell")
   WshShell.RegWrite "HKLM\Software\ActiveXperts\Network Monitor\Server\VBScript_Cache\" & strEntry, strData, "REG_SZ"

End Sub

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

Function cntFiles( strFolder, bIncludeDirInCount )
  Dim objFolder, objSubFolders, objFso, o, n

On Error Resume Next

  cntFiles = -1

  Set objFso = Createobject( "Scripting.FileSystemObject" ) 
  Set objFolder = objFso.GetFolder(strFolder)
  If( Err.Number <> 0 ) Then
     Exit Function
  End If
  n = objFolder.files.count
  Set objSubFolders = objFolder.SubFolders 
  For Each o In objSubFolders
     n = n + cntFiles( o, bIncludeDirInCount )
     If( bIncludeDirInCount ) Then
       n = n + 1
     End If 
  Next

  Set objSubFolders = Nothing
  Set objFolder     = Nothing

  cntFiles = n
End Function