Forum Discussion

Dim's avatar
8 years ago

Monitor the size of a folder

I had a discussion with one of the support Engineers and like to raise this as a feature request. I would like to monitor a folder size on a windows and set up an alert if the utilization reaches "x" amount.
The only possible way right now in LM  is to monitor the folder using a "UNC path"monitor but it's not actually presenting the size of the folder. Is there any chance this feature request can be enabled in a future LM release?

  • This would be a fairly simple embedded Groovy datasource.  This is a good starting point for calculating directory size:
    http://mrhaki.blogspot.com/2013/01/groovy-goodness-calculating-directory.html
    My recommendation is to define a custom property with the target path and use Groovy to extract that at the script initialization, for example:

    def hostname = hostProps.get("system.hostname");
    def filepath = hostProps.get("dirsizecheck.filepath");
    

    You can then use "dirsizecheck.filepath" as your "Applies To" to automatically run this DS for any hosts that have that defined.  Not appropriate if you want multiple instances, though -- that would require more thought.  Perhaps a comma or semicolon separated list in that property -- still learning the ropes on best practices.

     

  • Yeah the data source we built used the same Get-ChildItem method with the recurse modifier. Worked great when we were looking at small number of folders. Attached the xml if anyone may be looking to do something similar. AD script gets all sub folders one tier down and passes them as wild values. Collection script uses the Get-ChildItem -recurse method to report the size in MB. Again this works great for monitoring a directory with a limited number of sub-directories, but has issues when monitoring a directory with a ton of sub-directories.

    <?xml version="1.0" encoding="UTF-8" ?>
    <feed version="1.0" hasPendingRequests="false" >
    <company></company>
    <status>200</status>
    <errmsg>OK</errmsg>
    <interval>0</interval>
    <entry type="predatasource">
    <version>1478878984</version>
    <name>FolderSizeTest</name>
    <displayedas>FolderSizeTest</displayedas>
    <description></description>
    <collector>script</collector>
    <hasMultiInstances>true</hasMultiInstances>
    <schedule>600</schedule>
    <appliesTo>false()</appliesTo>
    <wildcardauto>true</wildcardauto>
    <wildcardpersist>true</wildcardpersist>
    <wildcardlinuxscript>ad_script</wildcardlinuxscript>
    <wildcardlinuxcmdline>type=&#34;powerShell&#34; </wildcardlinuxcmdline>
    <wildcardwinscript>ad_script</wildcardwinscript>
    <wildcardwincmdline>type=&#34;powerShell&#34; </wildcardwincmdline>
    <wildcardgroovyscript># Pass credentials and hostname as arguments
    $hostname = &#34;##HOSTNAME##&#34;
    $user = &#34;##wmi.USER##&#34;
    $pass = &#34;##wmi.PASS##&#34;

    # Change the password into a secure string to be used in the credentials
    $remotepass= ConvertTo-SecureString -String $pass -AsPlainText -Force

    # Build the credential
    $remotecredential= new-object -typename System.Management.Automation.PSCredential -argumentlist $user,$remotepass

    # Invoke command to run command remotely on host
    Invoke-Command -ComputerName $hostname -ScriptBlock {

    #Folder to get sub-directories hardcoded here (C:\) but could also be set to reference a system property
    $colItems = (Get-ChildItem C:\ | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object)
    foreach ($i in $colItems){Write-Host &#34;$($i.FullName)##$($i.Name)&#34;}

    } -credential $remotecredential</wildcardgroovyscript>
    <wildcardschedule>1440</wildcardschedule>
    <wildcarddisable>false</wildcarddisable>
    <agdmethod>none</agdmethod>
    <agdparams></agdparams>
    <group></group>
    <tags></tags>
    <technology></technology>
    <adlist><![CDATA[{"agdmethod":"none","method":"ad_script","agdparams":"","id":0,"filters":[],"params":{"type":"powerShell","groovyscript":"# Pass credentials and hostname as arguments\n$hostname = \"##HOSTNAME##\"\n$user = \"##wmi.USER##\"\n$pass = \"##wmi.PASS##\"\n \n# Change the password into a secure string to be used in the credentials\n$remotepass= ConvertTo-SecureString -String $pass -AsPlainText -Force\n \n# Build the credential \n$remotecredential= new-object -typename System.Management.Automation.PSCredential -argumentlist $user,$remotepass\n\n# Invoke command to run command remotely on host\nInvoke-Command -ComputerName $hostname -ScriptBlock {\n\n#Folder to get sub-directories hardcoded here (C:\\) but could also be set to reference a system property\n$colItems = (Get-ChildItem C:\\ | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object)\nforeach ($i in $colItems){Write-Host \"$($i.FullName)##$($i.Name)\"}\n\n} -credential $remotecredential"}}]]></adlist>
    <schemaVersion>1</schemaVersion>
    <dataSourceType>1</dataSourceType>
    <attributes>
    <attribute>
    <name>scripttype</name>
    <value>powerShell</value>
    <comment></comment>
    </attribute>
    <attribute>
    <name>scriptgroovy</name>
    <value># Pass credentials and hostname as arguments
    $hostname = &#34;##HOSTNAME##&#34;
    $user = &#34;##wmi.USER##&#34;
    $pass = &#34;##wmi.PASS##&#34;
    $wildvalue = &#34;##WILDVALUE##&#34;

    # Change the password into a secure string to be used in the credentials
    $remotepass= ConvertTo-SecureString -String $pass -AsPlainText -Force

    # Build the credential
    $remotecredential= new-object -typename System.Management.Automation.PSCredential -argumentlist $user,$remotepass

    Invoke-Command -ComputerName $hostname -ScriptBlock {

    param ($wild)
    $colItems = (Get-ChildItem $wild -recurse | Measure-Object -property length -sum)
    &#34;{0:f2}&#34; -f ($colItems.sum / 1MB)

    } -credential $remotecredential -ArgumentList $wildvalue</value>
    <comment></comment>
    </attribute>
    <attribute>
    <name>windowsscript</name>
    <value>Collect_Veeam_V_Folder5.ps1</value>
    <comment></comment>
    </attribute>
    <attribute>
    <name>linuxscript</name>
    <value></value>
    <comment></comment>
    </attribute>
    <attribute>
    <name>windowscmdline</name>
    <value>##ps.hostname## ##ps.user## ##ps.pass## ##wildvalue##</value>
    <comment></comment>
    </attribute>
    <attribute>
    <name>linuxcmdline</name>
    <value></value>
    <comment></comment>
    </attribute>
    </attributes>
    <datapoints>
    <datapoint>
    <name>size</name>
    <dataType>7</dataType>
    <type>2</type>
    <postprocessormethod>none</postprocessormethod>
    <postprocessorparam></postprocessorparam>
    <usevalue>output</usevalue>
    <alertexpr></alertexpr>
    <alertmissing>1</alertmissing>
    <alertsubject></alertsubject>
    <alertbody></alertbody>
    <description>size in MB of folder</description>
    <maxvalue></maxvalue>
    <minvalue></minvalue>
    <userparam1></userparam1>
    <userparam2></userparam2>
    <userparam3></userparam3>
    <iscomposite>false</iscomposite>
    <rpn></rpn>
    <alertTransitionIval>0</alertTransitionIval>
    <alertClearTransitionIval>0</alertClearTransitionIval>
    </datapoint>
    </datapoints>
    <graphs>
    <graph>
    <name>Size</name>
    <title>Size</title>
    <verticallabel>TB</verticallabel>
    <rigid>false</rigid>
    <maxvalue>NaN</maxvalue>
    <minvalue>0.0</minvalue>
    <displayprio>1</displayprio>
    <timescale>1day</timescale>
    <base1024>false</base1024>
    <graphdatapoints>
    <graphdatapoint>
    <name>size</name>
    <datapointname>size</datapointname>
    <cf>1</cf>
    </graphdatapoint>
    </graphdatapoints>
    <graphvirtualdatapoints>
    <graphvirtualdatapoint>
    <name>sizeTB</name>
    <rpn>(size/1024/1024)</rpn>
    </graphvirtualdatapoint>
    </graphvirtualdatapoints>
    <graphdatas>
    <graphdata>
    <type>2</type>
    <legend>Used</legend>
    <color>blue</color>
    <datapointname>sizeTB</datapointname>
    <isvirtualdatapoint>true</isvirtualdatapoint>
    </graphdata>
    </graphdatas>
    </graph>
    </graphs>
    <overviewgraphs>
    <overviewgraph>
    <name>Overview</name>
    <title>Overview</title>
    <verticallabel>TB</verticallabel>
    <rigid>false</rigid>
    <maxvalue>NaN</maxvalue>
    <minvalue>0.0</minvalue>
    <displayprio>1</displayprio>
    <timescale>1day</timescale>
    <base1024>false</base1024>
    <aggregated>false</aggregated>
    <datapoints>
    <overviewgraphdatapoint>
    <name>size</name>
    <datapointname>size</datapointname>
    <cf>1</cf>
    <aggregateMethod>SUM</aggregateMethod>
    </overviewgraphdatapoint>
    </datapoints>
    <virtualdatapoints>
    <overviewgraphvirtualdatapoint>
    <name>sizeTB</name>
    <rpn>size/1024/1024</rpn>
    </overviewgraphvirtualdatapoint>
    </virtualdatapoints>
    <lines>
    <overviewgraphline>
    <type>1</type>
    <legend>##INSTANCE##</legend>
    <datapointname>sizeTB</datapointname>
    <isvirtualdatapoint>true</isvirtualdatapoint>
    <color>blue</color>
    </overviewgraphline>
    </lines>
    </overviewgraph>
    </overviewgraphs>
    <scripts>
    </scripts>
    </entry>
    </feed>
     
  • I'd like to see this too. We have a datasource which uses a recursive PowerShell script to do this currently, which works great when the are only a handful of child directories, but we've seen issues running the same script on say a fileserver with hundreds of chid directories where it was pegging mem and cpu usage. The grovvy script above is also recursive so I wonder if it may have the same issues. Worth a look though.

  • @mnagel Thanks , I got a working datasource to check the folder size now. @SchuylerCthere's a beta datasource for this in the repository now (search for folder size). this is the powershell script :

     (Get-ChildItem "C:\Users\Administrator\AppData\roaming" -recurse | Measure-Object -property length -sum).sum

    Make sure you include "recurse" if you trying to get properties of Administrator folders. The result should be raw output (folder size in bytes) and then I created a complex datapoint to convert bytes into GB (Size/1024/1024/1024)