PRTG Custom Sensor: Alarm for Large File

Requirement

Need to check for any file larger then let’s say 60 Mb and save its name somewhere, and raise alarm on PRTG.

Tools/Environment

Powershell, PRTG, Windows Server 2012 R2.

Powershell Script

Following script will look for any file larger then 60 Mb, of file type wav, in F:\Dir\SubDir\SubDirL2\SubDirL3\ and will save it to F:\Dir\res.txt and keep it on appending it with the results.

##Mention the path to search the files
$path = “F:\Dir\SubDir\SubDirL2\SubDirL3\”
##Find out the files greater than equal to below mentioned size
$size = 60MB
##Limit the number of rows in text file mentioning file names
$limit = 5
##Find out the specific extension file
$Extension = “*.wav”
##script to find out the files based on the above input
$largeSizefiles = get-ChildItem -path $path -recurse -ErrorAction “SilentlyContinue” -include $Extension | ? { $_.GetType().Name -eq “FileInfo” } | where-Object {$_.Length -gt $size} | sort-Object -property length -Descending | Select-Object Name, @{Name=”SizeInMB”;Expression={$_.Length / 1MB}},@{Name=”Path”;Expression={$_.directory}} -first $limit
IF ($largeSizefiles -eq $NULL)

{
$res = “NORMAL”
$date = Get-Date
Out-File -FilePath F:\Dir\res.txt -InputObject $date -Encoding Default
Out-File -FilePath F:\Dir\res.txt -InputObject $res -Encoding Default -Append
}
ELSE
{
$res = “ERROR”
$date = Get-Date -UFormat “%Y%m%d%H%M%S”
Out-File -FilePath F:\Dir\res.txt -InputObject $date -Encoding Default
Out-File -FilePath F:\Dir\res.txt -InputObject $res -Encoding Default -Append
$largeSizefiles | Out-File -FilePath F:\Dir\res.txt -Encoding Default -Append
}

Run Script Periodically

Using Windows Task Scheduler, the script can be run periodically every 5 minutes. Create new task with following settings:

2016-01-28 16_41_52-10.150.10.160 - Remote Desktop Connection

2016-01-28 16_43_49-10.150.10.160 - Remote Desktop Connection

2016-01-28 16_44_25-10.150.10.160 - Remote Desktop Connection.png

 

PRTG Sensor Settings

  • Go to corresponding probe and the machine where you set up the previous Powershell Script.
  • Go to device list of that machine and add new sensor “File Sensor” with following settings:

2016-01-28 16_52_59-60MB+ Recorded File _ Sensor Details _ PRTG

2016-01-28 16_55_12-60MB+ Recorded File _ Sensor Details _ PRTG2016-01-28 16_56_33-60MB+ Recorded File _ Sensor Details _ PRTG.png

Change Scanning interval to your desired time.

Results:

2016-01-28 16_58_39-60MB+ Recorded File _ Sensor Details _ PRTG

The error will be displayed whenever there’s wav file larger then 60 Mb in the location mentioned earlier. You can also look for any word other then “Error” and raise the alarm.

Advertisements