Page 1 of 1
Monitor windows oldest file age in a directory
Posted: Fri Jul 12, 2019 6:05 am
by jenstar13
Hi,
Is there a way to monitor the age of the oldest file in a windows directory and alert if it is older than 24 hours?
We are using NCPA for our windows machine, and I can't install python or other scripting languages
if it's a script, it has to be self contained that can be dropped in the ncpa plugin directory
thank you in advance
Jen
Re: Monitor windows oldest file age in a directory
Posted: Fri Jul 12, 2019 9:22 am
by lmiltchev
You could probably use a powershell script like the one below:
Code: Select all
$FileDate = (Get-Date -Format g)
$path = "C:\Path\To\Your\Directory"
Get-ChildItem -Path $path -Recurse | ForEach-Object {
if ($_.LastWriteTime -lt $FileDate -and -not $_.PSIsContainer) {
$FileDate = $_.LastWriteTime
$OldFile = $_.FullName
}
}
Write-Host 'The oldest file on the system is: ' $OldFile $FileDate
Place the script in the NCPA plugins directory, and call it via check_ncpa.py from the Nagios XI box.
Example:
Code: Select all
/usr/local/nagios/libexec/check_ncpa.py -H x.x.x.x -t 'mytoken' -M 'plugins/check_oldest_file.ps1' -q ''
The oldest file on the system is: D:\TEMP\check_esxi_hardware.py 7/10/2019 1:41:08 PM
Re: Monitor windows oldest file age in a directory
Posted: Mon Jul 15, 2019 6:51 am
by jenstar13
thank you
I have to admit that I know nothing about powershell , me being a linux admin
I've added the script and it runs just fine, and i can call it from nagios
but how do i turn it into an alert if a file is older than one day? if there are no files or the files in the directory are less than one day old, then it's ok
I'll be googling but if you have a solution, can you share
thank you in advance
Jen
Re: Monitor windows oldest file age in a directory
Posted: Mon Jul 15, 2019 10:35 am
by lmiltchev
You can try something like this:
Code: Select all
$FileDate = (Get-Date -Format g)
$path = "C:\Path\To\Your\Directory"
$OneDayOld = (Get-Date).AddDays(-1)
Get-ChildItem -Path $path -Recurse | ForEach-Object {
if ($_.LastWriteTime -lt $FileDate -and -not $_.PSIsContainer) {
$FileDate = $_.LastWriteTime
$OldFile = $_.FullName
}
}
if ($FileDate -lt $OneDayOld) {
Write-Host 'WARNING: The oldest file on the system is: ' $OldFile $FileDate
exit 1
} else {
Write-Host 'OK: The oldest file on the system is: ' $OldFile $FileDate
exit 0
}
exit 0
If the file is newer than a day, you will get an "OK" message (exit code 0). If the file is older than a day, you will get a "WARNING" (exit code 1).
Re: Monitor windows oldest file age in a directory
Posted: Mon Jul 15, 2019 10:56 am
by jenstar13
thank you very much, i was lost in googleland trying to figure it out
Re: Monitor windows oldest file age in a directory
Posted: Mon Jul 15, 2019 11:07 am
by lmiltchev
You are welcome! Let us know if it is OK to close this topic now and mark it as resolved. Thank you!
Re: Monitor windows oldest file age in a directory
Posted: Mon Jul 15, 2019 11:14 am
by jenstar13
yes , please, you can close it, and thank you again