Monitor windows oldest file age in a directory

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
jenstar13
Posts: 174
Joined: Wed May 11, 2016 7:48 am

Monitor windows oldest file age in a directory

Post 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
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: Monitor windows oldest file age in a directory

Post 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
Be sure to check out our Knowledgebase for helpful articles and solutions!
jenstar13
Posts: 174
Joined: Wed May 11, 2016 7:48 am

Re: Monitor windows oldest file age in a directory

Post 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
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: Monitor windows oldest file age in a directory

Post 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).
Be sure to check out our Knowledgebase for helpful articles and solutions!
jenstar13
Posts: 174
Joined: Wed May 11, 2016 7:48 am

Re: Monitor windows oldest file age in a directory

Post by jenstar13 »

thank you very much, i was lost in googleland trying to figure it out
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: Monitor windows oldest file age in a directory

Post by lmiltchev »

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!
jenstar13
Posts: 174
Joined: Wed May 11, 2016 7:48 am

Re: Monitor windows oldest file age in a directory

Post by jenstar13 »

yes , please, you can close it, and thank you again
Locked