<# Version: 0.2 Date: Aug 28, 2020 Author: ivan.stetka@live.com Summary: This is a Nagios NCPA plugin to check the expiration dates in txt file on Windows machine (based on plugin check_expiry_win.pl v0.7) -N -fileName # full path to text file # example 'C:\Data\expiry\sra.txt' -w -warn # warning threshold of time before expiration in days -c -crit # critical threshold of time before expiration in days -e -expiry # expiration time in days -l -label # label On host side copy plugin to folder 'C:\Program Files (x86)\Nagios\NCPA\plugins\' Nagios example: ./check_ncpa.py -H '' -t '' -M 'plugins/check_expiry_win.ps1' -a "-N 'C:\Data\expiry\sra.txt' -w 20 -c 10 -e 90 -l access" Text in file example: 2020-07-20;Ivan 2020-08-20;Ondra (where the first value is the start date of validity and second value is the name of item) #> param ( [parameter(Mandatory = $true)] [Alias("N")] [string] $fileName, [parameter(Mandatory = $true)] [Alias("w")] [int] $warn, [parameter(Mandatory = $true)] [Alias("c")] [int] $crit, [parameter(Mandatory = $true)] [Alias("e")] [int] $expiry, [parameter(Mandatory = $true)] [Alias("l")] [string] $label ) $fileName = invoke-expression $fileName $STATES = @{ OK = 0; WARNING = 1; CRITICAL= 2; UNKNOWN = 3 } $NAMEEXPIRY = @{} $valuesCrit = @() $valuesWarn = @() $countCrit = 0 $countWarn = 0 $diffExpiry = 0 if ($warn -lt $crit) { Write-Output "The critical threshold '$crit' cannot be greater than warning threshold '$warn'." Exit($STATES.UNKNOWN) } $actualDate = get-date -UFormat '%Y-%m-%d' if (Test-Path -Path $fileName -PathType Leaf) { foreach($line in Get-Content $fileName) { if($line -match '(\d{4}-\d{2}-\d{2});([\w\-\(\)\& ]+)'){ $textDate = $matches[1] $textName = $matches[2] $diffDate = New-TimeSpan -Start $textDate -end $actualDate $diffDays = $diffDate.Days $diffExpiry = $expiry - $diffDays if ($diffExpiry -le $crit) { $countCrit++ $valuesCrit += $textName } elseif ($diffExpiry -le $warn) { $countWarn++ $valuesWarn += $textName } $NAMEEXPIRY.Add($textName, $diffExpiry) } else { Write-Output "CRITICAL: No match text. The file is empty or the text format is incorrect." Exit($STATES.CRITICAL) } } } else { Write-Output "CRITICAL: File not found.`nFile: $fileName" Exit($STATES.CRITICAL) } if ($countCrit -ge 1) { $valueCritText = $valuesCrit -join ', ' Write-Output "CRITICAL: $label will expire in critical threshold or expired: $valueCritText." } if ($countWarn -ge 1) { $valueWarnText = $valuesWarn -join ', ' Write-Output "WARNING: $label will expire in warning threshold: $valueWarnText." } if ($countCrit -eq 0 -AND $countWarn -eq 0) { Write-Output "OK: No expiring $label" } Write-Output "*** Expiry period is $expiry days ***" $NAMEEXPIRY.GetEnumerator() | Sort-Object -Property Value | ForEach-Object{ if ($_.value -eq 0){ $msgNameExpiry = $_.key + "'s $label expired today!!!" Write-Output $msgNameExpiry } elseif ($_.value -lt 0){ $valueAbs = [Math]::Abs($_.value) $msgNameExpiry = $_.key + "'s $label expired before $valueAbs days!!!" Write-Output $msgNameExpiry } else { $msgNameExpiry = $_.key + "'s $label will expire in " + $_.value + " days." Write-Output $msgNameExpiry } } if ($countCrit -ge 1) { Exit($STATES.CRITICAL) } elseif ($countCrit -ge 0 -AND $countWarn -ge 1 ) { Exit($STATES.WARNING) } else { Exit($STATES.OK) } Write-Output "CRITICAL: unexpected plugin error." Exit($STATES.CRITICAL)