Jean Douter
Jean Douter

Reputation: 1

[SOLVED ]PRTG Monitor: Monitoring age of files at level Main Folder/Subfolders/Folder/Files

Yo, hello, I need bit of a help. Getting frustrated more and more by the time.

I need to monitor folder at server. That folder at server contains several subfolders. And from those subfolders I need to monitor one called "Send" and if there are some files more than one hour I need to trigger senzor down.

Using this powershell script:

$rootFolder = "D:\AS2\data"
$subfolders = Get-ChildItem -Path $rootFolder -Directory
$maxFileAge = (Get-Date).AddMinutes(-60)
$exitCode = 0
foreach ($subfolder in $subfolders) {
    $sendFolder = Join-Path -Path $subfolder.FullName -ChildPath "Send"
    $oldFiles = Get-ChildItem -Path $sendFolder -File | Where-Object { $_.LastWriteTime -lt $maxFileAge }
    if ($oldFiles.Count -gt 0) {
        $exitCode = 1
        Write-Host "V podsložce $($sendFolder) byly nalezeny soubory starší než 60 minut."
    }
}
exit $exitCode

Basically, if there are any files older than 60 minutes, giving exitCode to 1, but when I use this Custom Sensor using EXE/Script at Probe Device it didn't work. I tried EXE/Script Advanced also.

Running this senzor always ended with:

Response not well-formed: "( )" (code: PE132)

Senzor setting below: enter image description here

r/prtg - Monitoring age of files at level Main Folder/Subfolders/Folder/Files

Adding Overview also. enter image description here

r/prtg - Monitoring age of files at level Main Folder/Subfolders/Folder/Files

I can't get it work :'(. Went through several topics na knowledge base at Paessler, but still can't find solution for it.

Please, can you help me with script, senzor settings or whatever what will work as I need to? Or use some another way to solve this lookup to folder, subfolders and folder at subfolder for files.

Update: I tried to change powershell script as I found new informations about giving correct respons code, but it ended up with "Response not well-formed: "( )" (code: PE132)". Script below. Please, does anyone has any ideas?

$rootFolder = "D:\AS2\data"
$subfolders = Get-ChildItem -Path $rootFolder -Directory
$maxFileAge = (Get-Date).AddMinutes(-60)
$exitCode = 0
$errorDetected = $false
foreach ($subfolder in $subfolders) {
    $sendFolder = Join-Path -Path $subfolder.FullName -ChildPath "Send"
    $oldFiles = Get-ChildItem -Path $sendFolder -File | Where-Object { $_.LastWriteTime -lt $maxFileAge }
    if ($oldFiles.Count -gt 0 -and !$errorDetected) { $exitCode = 1 $errorDetected = $true }
    if ($oldFiles.Count -eq 0) { $exitCode = 0 $errorDetected = $false }}
if ($errorCode -eq 0) {write-output "0:OK"}
if ($errorCode -eq 1) {write-output "1:Down"}
exit

I almost tried everything what I read, tried, debugged. I'm out of any idea right now.

Upvotes: 0

Views: 390

Answers (1)

Jean Douter
Jean Douter

Reputation: 1

Well, looks like I write wrong the end of script for results. Instead of using "$exitCode" I used "$errorCode" which ended up to error. Will be tested it for a few times to confirm it is okay.

Allright, that's the final script that worked as requested.

$rootFolder = "D:\AS2\data"
$subfolders = Get-ChildItem -Path $rootFolder -Directory
$maxFileAge = (Get-Date).AddMinutes(-60)
$exitCode = 0
$errorDetected = $false
foreach ($subfolder in $subfolders) {
    $sendFolder = Join-Path -Path $subfolder.FullName -ChildPath "Send"
    $oldFiles = Get-ChildItem -Path $sendFolder -File | Where-Object { $_.LastWriteTime -lt (Get-Date).AddMinutes(-60) }
    if ($oldFiles.Count -gt 0 -and !$errorDetected) {
        $exitCode = 1
        $errorDetected = $true
    }
}
if ($exitCode -eq 0) {write-output "0:OK"}
if ($exitCode -eq 1) {write-output "1:Down"}
exit

Upvotes: 0

Related Questions