Forum Discussion

Idan's avatar
8 years ago

Monitor File System - extend the built in UNC monitor

Hi,

I developed a small PowerShell script that monitors the number of files in a folder and returns the total number of files in the root folder, the number of files in all the subfolders, the number of folders in the root of the folder and number of folders in all the subfolders.

In additional, you can filter by file extension.

Because it's powershell, you can easily add filter a specific folder and more.

Remember, PowerShell script run on the collector and not on the server even if in the apply to you select a server.

So or install the collector on the server you want to monitor, or use UNC like \\

Also remember to allow remote PowerShell

https://www.logicmonitor.com/support/datasources/powershell-support/using-powershell-scripts-in-logicmonitor/

The active discovery script is:

$folderspath = '##FolderToMonitor##';
$SearchSExt = 'zip'
$separator = ";"
$option = [System.StringSplitOptions]::RemoveEmptyEntries
$array = $folderspath.Split($separator, $option)
$stringcount = $folderspath.Split($separator,2, $option).Count
$folder_id=0

foreach ($folder in $array) {
  $folder_id++

  $separatorext = "@"
  $option = [System.StringSplitOptions]::RemoveEmptyEntries
  $arrayext = $folder.Split($separatorext, $option)
  $folderpath =  $arrayext[0]
  $SearchSExt = $arrayext[1]
  
  If ($SearchSExt -eq '*' -or $SearchSExt -eq $null)
    {

    # write out the instance data
     Write-Host "OPFID$folder_id##$folderpath##$folderpath";
    }
    else
    {
         Write-Host "OPFID$folder_id##$folderpath##$folderpath filter ext $SearchSExt";
    }
}

 

The collector script below and it get parameter as like that

\\MyServer\MyShare\Test1@*;\\MyServer\MyShare\Test2@xml;\\MyServer\MyShare\Test3@zip;\\MyServer\MyShare\Test4@*

This will apply to $folderspath = '##FolderToMonitor##'; if you will use the device properties

So you can specify a list of folders separated by ";" and you can specify the file ext. filter by @

 

$folderspath = '##FolderToMonitor##';
$separator = ";"
$option = [System.StringSplitOptions]::RemoveEmptyEntries
$array = $folderspath.Split($separator, $option)
$stringcount = $folderspath.Split($separator,2, $option).Count
$folder_id=0

foreach ($folder in $array) {
  $folder_id++

  $separatorext = "@"
  $option = [System.StringSplitOptions]::RemoveEmptyEntries
  $arrayext = $folder.Split($separatorext, $option)
  $folderpath =  $arrayext[0]
  $SearchSExt = $arrayext[1]
  
If ($SearchSExt -eq '*' -or $SearchSExt -eq $null)
    {
        
        $TotalFiles=Get-ChildItem $folderpath -Recurse -File | Measure-Object | %{$_.Count}
        $TotalFolders= Get-ChildItem $folderpath -Recurse -Directory | Measure-Object | %{$_.Count}
        $TotalFilesInRoot=Get-ChildItem $folderpath -File | Measure-Object | %{$_.Count}
        $TotalFoldersInRoot=Get-ChildItem $folderpath -Directory  | Measure-Object | %{$_.Count}

    }
    else 
    {

              
        $TotalFiles=Get-ChildItem $folderpath -recurse -File -filter *.$SearchSExt | Measure-Object | %{$_.Count}
        $TotalFolders= Get-ChildItem $folderpath -Recurse -Directory | Measure-Object | %{$_.Count}
        $TotalFilesInRoot=Get-ChildItem $folderpath -File -filter *.$SearchSExt | Measure-Object | %{$_.Count}
        $TotalFoldersInRoot=Get-ChildItem $folderpath -Directory  | Measure-Object | %{$_.Count}

    }

Write-Host   "OPFID$folder_id.TotalFiles="$TotalFiles;
Write-Host   "OPFID$folder_id.TotalFolders="$TotalFolders;
Write-Host   "OPFID$folder_id.TotalFilesInRoot="$TotalFilesInRoot;
Write-Host   "OPFID$folder_id.TotalFoldersInRoot="$TotalFoldersInRoot;

}

 

10 Replies

Replies have been turned off for this discussion
  • I have been having issues with Monitoring just a folder, I took your powerscript and added in the folder size info and then changed it to GB/TB. Published the Source to the Logic Monitor Data Source: YL7PL2 . Thanks for the script

  • I was trying to convert it to list all the subfolders in the subfolder instead of just OPFID1. I can get it to work in Powershell but not in LM tells me that it found now instances. 

     

  • Not not what you are trying to do, if you will post the script I can try to look into that.

    Just send me also what you are trying to do as I'm not sure I understand it.

    Make sure you have the right script in the auto discover too.

    You can run debugging in th eLM

  • On 6/5/2018 at 9:09 AM, Mcooner said:

    I have been having issues with Monitoring just a folder, I took your powerscript and added in the folder size info and then changed it to GB/TB. Published the Source to the Logic Monitor Data Source: YL7PL2 . Thanks for the script

    Any idea when this will be out of 'security review' ?

  • On 6/5/2018 at 9:09 AM, Mcooner said:

    I have been having issues with Monitoring just a folder, I took your powerscript and added in the folder size info and then changed it to GB/TB. Published the Source to the Logic Monitor Data Source: YL7PL2 . Thanks for the script

    Any idea when this will be out of 'security review' ?

  • On 4/5/2017 at 8:56 PM, Idan said:

    The collector script below and it get parameter as like that

    \\MyServer\MyShare\Test1@*;\\MyServer\MyShare\Test2@xml;\\MyServer\MyShare\Test3@zip;\\MyServer\MyShare\Test4@*

    This will apply to $folderspath = '##FolderToMonitor##'; if you will use the device properties

    So you can specify a list of folders separated by ";" and you can specify the file ext. filter by @

     

     

    How can I configure additional paths with spaces in the folder names?

  • Hi,

    I didn't check it,but this is a powershell, so maybe search how to send a parameter in PowerShell with the space.

    Try to modify the $folderpath and try to change it to '$folderpath' 

    You can try to run some basic powershell commands before deploy it in LogicMonitor

     

     

  • Tried a number of normal escape characters typically used for PowerShell - no dice.  Thanks