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
Monitor windows oldest file age in a directory
Re: Monitor windows oldest file age in a directory
You could probably use a powershell script like the one below:
Place the script in the NCPA plugins directory, and call it via check_ncpa.py from the Nagios XI box.
Example:
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 $FileDateExample:
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 PMBe sure to check out our Knowledgebase for helpful articles and solutions!
Re: Monitor windows oldest file age in a directory
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
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
You can try something like this:
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).
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 0Be sure to check out our Knowledgebase for helpful articles and solutions!
Re: Monitor windows oldest file age in a directory
thank you very much, i was lost in googleland trying to figure it out
Re: Monitor windows oldest file age in a directory
You are welcome! Let us know if it is OK to close this topic now and mark it as resolved. Thank you!
Be sure to check out our Knowledgebase for helpful articles and solutions!
Re: Monitor windows oldest file age in a directory
yes , please, you can close it, and thank you again