This article applies as of PRTG 22
Monitoring files using placeholders
The File sensor can monitor a specific file on a disk if you provide the exact path and file name in the sensor settings. This sensor does not support placeholders to provide the name of the target file. Because log files often have the date of creation in their name, the File sensor might not be perfectly suitable for your scenario.
To monitor log files with a date in their name, for example, you can use a custom sensor with a PowerShell script that allows placeholders for the files that you want to monitor. We provide such a sample script below. You can just use the script and adjust the parameters to your scenario. The script also gives you an idea about how to use placeholders for monitoring files in other scenarios.
For this purpose, you need an EXE/Script Advanced sensor. This sensor can execute the sample script and shows the following data:
- Number of files that match the pattern of the given file name with placeholders
- Age of the oldest and newest file matching the name pattern
- Size of the newest file matching the name pattern
- Total size of files matching the name pattern
See below for the sample script and how to use it.
How to use the PowerShell script
- Open a text editor.
- Copy the source code from below and paste it into the editor.
- Adjust the default values for $path and $include to your scenario. The script as provided below looks for text files in the \temp folder of the target system. You can also control script execution with the Parameters setting of the sensor.
- Save the file with the extension .ps1, for example, logfileStats.ps1.
- Copy this file to the \Custom Sensors\EXEXML subfolder of the PRTG data directory on the probe system from which you want to monitor your files.
- Add an EXE/Script Advanced sensor to the target device.
- Select logfileStats.ps1 from the list of scripts.
- Provide the Parameters that you want to use to execute the script. For the sample script, these are the path to the target files and the file name pattern include. For example, to monitor log files of an Apache web server that have the date right before the file extension, you can provide the following parameter that overrides the defaults:
-path \\<unc-path to share>\apache-log\*\ -include Webserver.log-*
- Adjust all other sensor settings to your needs.
- Click Create to add the sensor. The sensor shows the retrieved data in the channels Files matching pattern, Oldest File, Newest File, Newest File Size, and Size (see the bullet points above for detailed descriptions).
Note: We do not provide official support for custom sensors. Of course, you are free to adjust the script according to your needs.
Script
Param(
[string]$path="C:\temp",
[string]$include= "*.txt"
)
$result = ""
$size = 0
# Get the list of all files
$files = Get-ChildItem -Path $path -include $include -Recurse | Sort-Object LastWriteTime
$count = $files.count
if ($count -gt 0)
{
$newestfilesize = $files[-1].length
$oldestfile = ("{0:N2}" -f (New-Timespan -end (Get-Date) -start ($files)[0].LastWriteTime).TotalDays).Replace(',','.')
$newestfile = ("{0:N2}" -f (New-Timespan -end (Get-Date) -start ($files)[-1].LastWriteTime).TotalDays).Replace(',','.')
foreach ($file in $files) {
$size += $file.length
}
$result += @"
<result>
<value>$count</value>
<unit>count</unit>
<channel>Number of $($include) Files</channel>
</result>
<result>
<value>$oldestfile</value>
<unit>custom</unit>
<CustomUnit>Days</CustomUnit>
<float>1</float>
<channel>Oldest File Age</channel>
</result>
<result>
<value>$newestfile</value>
<unit>custom</unit>
<CustomUnit>Days</CustomUnit>
<float>1</float>
<channel>Newest File Age</channel>
</result>
<result>
<value>$newestfilesize</value>
<unit>BytesFile</unit>
<volumeSize>Byte</volumeSize>
<float>0</float>
<channel>Newest File Size</channel>
</result>
<result>
<value>$size</value>
<unit>BytesFile</unit>
<volumeSize>Byte</volumeSize>
<channel>Total File Size</channel>
</result>
<text>
OK
</text>
"@
} else {
$result += @"
<error>1</error>
<text>
No file found matching the filter-parameter "$include" at path "$path"
</text>
"@
}
write-host @"
<?xml version=1.0 encoding=UTF-8 ?>
<prtg>
$result
</prtg>
"@
Add comment