<# Version: 0.2 Date: Sep 17, 2020 Author: ivan.stetka@live.com Summary: This is a Nagios NCPA plugin for check windows file/folder age, including checking the number of items (files/folders) corresponding to a certain age in the folder and checking the total number of items (files/folders) in the folder. -N -fileFolderName # full path to file/folder # example folder: 'C:\Program Files (x86)\CA\DSM\logs\' # example file: 'C:\Program Files (x86)\CA\DSM\logs\TRC_AMAGENT_1.log' -c -crit # critical threshold of file/folder age in the corresponding unit # example: 12 (you can also use floating numbers like 12.1) -u -unit # unit of critical threshold: # M - minutes # H - hours # D - days -O -okCount # the required number of items (files/folders) in the folder corresponding to the critical threshold [default: 1] -T -totalCount # the total required number of items (files/folders) in the folder [default: 0] # if the value is 0, then the function totalCount is not used On host side copy plugin to folder 'C:\Program Files (x86)\Nagios\NCPA\plugins\' Nagios example: ./check_ncpa.py -H '' -t '' -M 'plugins/check_filefolder_age.ps1' -a "-N 'C:\Program Files (x86)\CA\DSM\logs\' -c 5 -u D -O 2 -T 20" ./check_ncpa.py -H '' -t '' -M 'plugins/check_filefolder_age.ps1' -a "-N 'C:\Program Files (x86)\CA\DSM\logs\log.txt' -c 5 -u D" check_command check_ncpa_Ma!'plugins/check_filefolder_age.ps1'!"-N 'C:\\Program Files (x86)\\CA\\DSM\\logs\\' -c 5 -u D -O 2 -T 20" #> param ( [parameter(Mandatory = $true)] [Alias("N")] [string] $fileFolderName, [parameter(Mandatory = $true)] [Alias("c")] [float] $crit, [parameter(Mandatory = $true)] [Alias("u")] [string] $unit, [parameter(Mandatory = $false)] [Alias("O")] [int] $okCount, [parameter(Mandatory = $false)] [Alias("T")] [int] $totalCount ) $fileFolderName = invoke-expression $fileFolderName if (-not($okCount)) {$okCount = 1} if (-not($totalCount)) {$totalCount = 0} $STATES = @{ OK = 0; WARNING = 1; CRITICAL= 2; UNKNOWN = 3 } $UNITS = @{ M = 'minutes'; H = 'hours'; D = 'days' } $fileFolderCount = 0 if ($UNITS.$unit) { $unitText = ($UNITS.$unit) if ($crit -eq 1) { $unitText = $unitText -replace '.{1}$' } } else { Write-Output "Incorrect format of unit '$unit'. Accepted units are only M, H or D." Exit($STATES.UNKNOWN) } if ($crit -le 0 -or $okCount -le 0) { print "Incorrect crit or okCount threshold (must be greater than 0)" Exit($STATES.UNKNOWN) } $fileFolderCount = (Get-ChildItem $fileFolderName -EA SilentlyContinue).Count if (Test-Path -Path $fileFolderName -PathType Container) { $fileFolderType = "Folder" } elseif (Test-Path -Path $fileFolderName -PathType Leaf){ $fileFolderType = "File" if ($okCount -ne 1) { Write-Output "Incorrect okCount threshold $okCount for one specific file, please set okCount to 1." Exit($STATES.UNKNOWN) } elseif ($totalCount -ne 0) { Write-Output "The totalCount function is not applicable for one specific file, please set totalCount to 0." Exit($STATES.UNKNOWN) } } else { Write-Output "CRITICAL: File or folder does not exist. `nItem: $fileFolderName" Exit($STATES.CRITICAL) } if ($fileFolderCount -eq 0 ){ Write-Output "CRITICAL: Folder is empty. `nFolder: '$fileFolderName'" Exit($STATES.CRITICAL) } elseif ($totalCount -gt 0 -and $fileFolderCount -ne $totalCount){ Write-Output "CRITICAL: Folder $fileFolderName contains a total of $fileFolderCount items, but $totalCount files are required." Exit($STATES.CRITICAL) } $unitCommand = "Add" + ($UNITS.$unit) $fileCountOk = ((Get-ChildItem $fileFolderName | Where-Object {$_.LastWriteTime -gt (Get-Date).$unitCommand(-$crit)}) | Sort-Object LastWriteTime -Descending).count $fileFolderTime = ((Get-ChildItem $fileFolderName) | Sort-Object LastWriteTime -Descending | Select-Object -First 1).LastWriteTime.ToString('yyyy-MM-dd HH:mm:ss') $baseText = $fileFolderType + " LastWriteTime: $fileFolderTime." $folderText1 = $baseText + " The number of items in the folder younger than $crit $unitText is $fileCountOk (required: $okCount)" $folderText2 = "`nThe total number of items in the folder $fileFolderName is $fileFolderCount." $fileText1 = $baseText + "`nFile $fileFolderName is" $fileText2 = "than of the required $crit $unitText" if ($fileCountOk -ge $okCount){ if ($fileFolderType -eq "Folder") { $files = (((Get-ChildItem $fileFolderName | Where-Object{$_.LastWriteTime -gt (Get-Date).$unitCommand(-$crit)}) | Sort-Object LastWriteTime -Descending) | Select-Object -First $fileCountOk) | Format-List -property Name, LastWriteTime, Attributes Write-Output "OK: $folderText1. $folderText2" Write-Output @files } else { Write-Output "OK: $fileText1 younger $fileText2." } Exit($STATES.OK) } if ($fileFolderType -eq "Folder") { Write-Output "CRITICAL: $folderText1! $folderText2" } else { Write-Output "CRITICAL: $fileText1 older $fileText2!" } Exit($STATES.CRITICAL)