<# Version: 0.1 Date: Aug 2, 2022 Author: ivan.stetka@live.com Summary: This is a Nagios NCPA plugin for check BIP log file -N -logName # full path of log file # [example: 'D:\Program Files\Qognify\GatewaysHost\Logs\SensorGatewayHost.log'] -M -minutesAgo # the number of minutes to look back at the log # [example: 30] -w -warn # warning errors threshold -c -crit # critical errors threshold On host side copy plugin to folder 'C:\Program Files (x86)\Nagios\NCPA\plugins\' Nagios example: ./check_ncpa.py -H '' -t '' -M 'plugins/check_log_bip.ps1' -q "args='D:\\Program Files\\Qognify\\GatewaysHost\\Logs\\SensorGatewayHost.log'',args=30,args=1,args=2" #> param ( [parameter(Mandatory = $true)] [Alias("N")] [string] $logName, [parameter(Mandatory = $true)] [Alias("M")] [int] $minutesAgo, [parameter(Mandatory = $true)] [Alias("w")] [int] $warn, [parameter(Mandatory = $true)] [Alias("c")] [int] $crit ) $STATES = @{ OK = 0; WARNING = 1; CRITICAL= 2; UNKNOWN = 3 } function perf { Write-Output ("| Errors=$errorCount;$warn;$crit;") } $logName = invoke-expression $logName $startTime = (Get-Date).AddMinutes(-$minutesAgo) $dateFormat = 'yyyy-MM-dd HH:mm:ss,fff' $culture = [cultureinfo]::CurrentCulture $systemDate = Get-Date $todayDate = $systemDate.ToString('yyyy-MM-dd') if (Test-Path -Path $logName -PathType Leaf) { $logContent = (Get-Content $logName | Select-Object -Last 1000 | Where-Object { If ($_ -match $todayDate ) { ([datetime]::ParseExact(([string]$_).Substring(0, 23), $dateFormat, $culture)) -gt $startTime } } -EA SilentlyContinue) $errors = $logContent | Select-String -Pattern 'ERROR' -CaseSensitive -SimpleMatch $errorCount = $($errors.Count) } else { Write-Output "CRITICAL: Log file not found.`nExpected $logName" Exit($STATES.CRITICAL) } $additText = "in the last $minutesAgo minutes, the log contains $($logContent.Count) lines and $errorCount errors." if ($errorCount -ge $crit) { Write-Output "CRITICAL: $additText" Write-Output ($errors -join "`n") perf Exit($STATES.CRITICAL) } elseif ($errorCount -ge $warn) { Write-Output "WARNING: $additText" Write-Output ($errors -join "`n") perf Exit($STATES.WARNING) } elseif ($errorCount -lt $warn) { Write-Output "OK: $additText" perf Exit($STATES.OK) } else { Write-Output "CRITICAL: unexpected plugin error, $additText" perf Exit($STATES.CRITICAL) }