From 6b8576c0cc68db5b34d0081733f3925d7de58b2c Mon Sep 17 00:00:00 2001 From: Alexander Stoll Date: Thu, 31 Oct 2019 11:16:33 +0100 Subject: [PATCH 1/3] Add Creation Filter & seperated into multiple functions (modular enhancement) --- .../directory/Icinga_Provider_Directory.psm1 | 162 +++++++++++++++--- 1 file changed, 140 insertions(+), 22 deletions(-) diff --git a/lib/provider/directory/Icinga_Provider_Directory.psm1 b/lib/provider/directory/Icinga_Provider_Directory.psm1 index 3332b56..f33af95 100644 --- a/lib/provider/directory/Icinga_Provider_Directory.psm1 +++ b/lib/provider/directory/Icinga_Provider_Directory.psm1 @@ -6,44 +6,58 @@ function Get-IcingaDirectoryAll() [string]$Path, [array]$FileNames, [bool]$Recurse, - [string]$YoungerThan, - [string]$OlderThan, + [string]$ChangeTimeEqual, + [string]$ChangeYoungerThan, + [string]$ChangeOlderThan, + [string]$CreationTimeEqual, + [string]$CreationOlderThan, + [string]$CreationYoungerThan, [string]$FileSizeGreaterThan, [string]$FileSizeSmallerThan ); if ($Recurse -eq $TRUE) { - $DirectoryData = Get-ChildItem -Recurse -Path $Path -Include $FileNames; + $DirectoryData = Get-IcingaDirectoryRecurse -Path $Path -FileNames $FileNames; } else { - $DirectoryData = Get-ChildItem -Path $Path -Include $FileNames; - } - - if ([string]::IsNullOrEmpty($OlderThan) -eq $FALSE -And [string]::IsNullOrEmpty($YoungerThan) -eq $FALSE) { - $OlderThan = Set-NumericNegative (ConvertTo-Seconds $OlderThan); - $DirectoryData = ($DirectoryData | Where-Object {$_.LastWriteTime -lt (Get-Date).AddSeconds($OlderThan)}) - $YoungerThan = Set-NumericNegative (ConvertTo-Seconds $YoungerThan); - $DirectoryData = ($DirectoryData | Where-Object {$_.LastWriteTime -gt (Get-Date).AddSeconds($YoungerThan)}) - } elseif ([string]::IsNullOrEmpty($OlderThan) -eq $FALSE) { - $OlderThan = Set-NumericNegative (ConvertTo-Seconds $OlderThan); - $DirectoryData = ($DirectoryData | Where-Object {$_.LastWriteTime -lt (Get-Date).AddSeconds($OlderThan)}) - } elseif ([string]::IsNullOrEmpty($YoungerThan) -eq $FALSE) { - $YoungerThan = Set-NumericNegative (ConvertTo-Seconds $YoungerThan); - $DirectoryData = ($DirectoryData | Where-Object {$_.LastWriteTime -gt ((Get-Date).AddSeconds($YoungerThan))}) + $DirectoryData = Get-IcingaDirectory -Path $Path -FileNames $FileNames; } + if ([string]::IsNullOrEmpty($ChangeTimeEqual) -eq $FALSE) { + $DirectoryData = Get-IcingaDirectoryChangeTimeEqual -ChangeTimeEqual $ChangeTimeEqual -DirectoryData $DirectoryData; + } + + if ([string]::IsNullOrEmpty($CreationTimeEqual) -eq $FALSE) { + $DirectoryData = Get-IcingaDirectoryCreationTimeEqual -CreationTimeEqual $CreationTimeEqual -DirectoryData $DirectoryData; + } + + If ([string]::IsNullOrEmpty($ChangeTimeEqual) -eq $TRUE -Or [string]::IsNullOrEmpty($CreationTimeEqual) -eq $TRUE) { + if ([string]::IsNullOrEmpty($ChangeOlderThan) -eq $FALSE) { + $DirectoryData = Get-IcingaDirectoryChangeOlderThan -ChangeOlderThan $ChangeOlderThan -DirectoryData $DirectoryData; + } + if ([string]::IsNullOrEmpty($ChangeYoungerThan) -eq $FALSE) { + $DirectoryData = Get-IcingaDirectoryChangeYoungerThan -ChangeYoungerThan $ChangeYoungerThan -DirectoryData $DirectoryData; + } + if ([string]::IsNullOrEmpty($CreationOlderThan) -eq $FALSE) { + $DirectoryData = Get-IcingaDirectoryCreationOlderThan -CreationOlderThan $CreationOlderThan -DirectoryData $DirectoryData; + } + if ([string]::IsNullOrEmpty($CreationYoungerThan) -eq $FALSE) { + $DirectoryData = Get-IcingaDirectoryCreationYoungerThan -CreationYoungerThan $CreationYoungerThan -DirectoryData $DirectoryData; + } + } if ([string]::IsNullOrEmpty($FileSizeGreaterThan) -eq $FALSE) { - $FileSizeGreaterThanValue = (Convert-Bytes $FileSizeGreaterThan -Unit B).value - $DirectoryData = ($DirectoryData | Where-Object {$_.Length -gt $FileSizeGreaterThanValue}) + $DirectoryData = (Get-IcingaDirectorySizeGreaterThan -FileSizeGreaterThan $FileSizeGreaterThan -DirectoryData $DirectoryData); } - if ([string]::IsNullOrEmpty($FileSizeSmallerThan) -eq $FALSE) { - $FileSizeSmallerThanValue = (Convert-Bytes $FileSizeSmallerThan -Unit B).value - $DirectoryData = ($DirectoryData | Where-Object {$_.Length -gt $FileSizeSmallerThanValue}) + $DirectoryData = (Get-IcingaDirectorySizeSmallerThan -FileSizeSmallerThan $FileSizeSmallerThan -DirectoryData $DirectoryData); } return $DirectoryData; } + + +# RECURSE + function Get-IcingaDirectory() { param( @@ -65,5 +79,109 @@ function Get-IcingaDirectoryRecurse() $DirectoryData = Get-ChildItem -Recurse -Include $FileNames -Path $Path; + return $DirectoryData; +} + +# FILE SIZE + +function Get-IcingaDirectorySizeGreaterThan() +{ + param( + [string]$FileSizeGreaterThan, + $DirectoryData + ); + $FileSizeGreaterThanValue = (Convert-Bytes $FileSizeGreaterThan -Unit B).value + $DirectoryData = ($DirectoryData | Where-Object {$_.Length -gt $FileSizeGreaterThanValue}) + + return $DirectoryData; +} + +function Get-IcingaDirectorySizeSmallerThan() +{ + param( + [string]$FileSizeSmallerThan, + $DirectoryData + ); + $FileSizeSmallerThanValue = (Convert-Bytes $FileSizeSmallerThan -Unit B).value + $DirectoryData = ($DirectoryData | Where-Object {$_.Length -gt $FileSizeSmallerThanValue}) + + return $DirectoryData; +} + +# TIME BASED CHANGE + +function Get-IcingaDirectoryChangeOlderThan() +{ + param ( + [string]$ChangeOlderThan, + $DirectoryData + ) + $ChangeOlderThan = Set-NumericNegative (ConvertTo-Seconds $ChangeOlderThan); + $DirectoryData = ($DirectoryData | Where-Object {$_.LastWriteTime -lt (Get-Date).AddSeconds($ChangeOlderThan)}) + + return $DirectoryData; +} + +function Get-IcingaDirectoryChangeYoungerThan() +{ + param ( + [string]$ChangeYoungerThan, + $DirectoryData + ) + $ChangeYoungerThan = Set-NumericNegative (ConvertTo-Seconds $ChangeYoungerThan); + $DirectoryData = ($DirectoryData | Where-Object {$_.LastWriteTime -gt (Get-Date).AddSeconds($ChangeYoungerThan)}) + + return $DirectoryData; +} + +function Get-IcingaDirectoryChangeTimeEqual() +{ + param ( + [string]$ChangeTimeEqual, + $DirectoryData + ) + $ChangeTimeEqual = Set-NumericNegative (ConvertTo-Seconds $ChangeTimeEqual); + $ChangeTimeEqual = (Get-Date).AddSeconds($ChangeTimeEqual); + $DirectoryData = ($DirectoryData | Where-Object {$_.LastWriteTime.Day -eq $ChangeTimeEqual.Day -And $_.LastWriteTime.Month -eq $ChangeTimeEqual.Month -And $_.LastWriteTime.Year -eq $ChangeTimeEqual.Year}) + + return $DirectoryData; +} + +# TIME BASED CREATION + +function Get-IcingaDirectoryCreationYoungerThan() +{ + param ( + [string]$CreationYoungerThan, + $DirectoryData + ) + $CreationYoungerThan = Set-NumericNegative (ConvertTo-Seconds $CreationYoungerThan); + $DirectoryData = ($DirectoryData | Where-Object {$_.CreationTime -gt (Get-Date).AddSeconds($CreationYoungerThan)}) + + return $DirectoryData; +} + +function Get-IcingaDirectoryCreationOlderThan() +{ + param ( + [string]$CreationOlderThan, + $DirectoryData + ) + $CreationOlderThan = Set-NumericNegative (ConvertTo-Seconds $CreationOlderThan); + $DirectoryData = ($DirectoryData | Where-Object {$_.CreationTime -lt (Get-Date).AddSeconds($CreationOlderThan)}) + + return $DirectoryData; +} + +function Get-IcingaDirectoryCreationTimeEqual() +{ + param ( + [string]$CreationTimeEqual, + $DirectoryData + ) + $CreationTimeEqual = Set-NumericNegative (ConvertTo-Seconds $CreationTimeEqual); + $CreationTimeEqual = (Get-Date).AddSeconds($CreationTimeEqual); + $DirectoryData = ($DirectoryData | Where-Object {$_.CreationTime.Day -eq $CreationTimeEqual.Day -And $_.CreationTime.Month -eq $CreationTimeEqual.Month -And $_.CreationTime.Year -eq $CreationTimeEqual.Year}) + return $DirectoryData; } \ No newline at end of file From 32cd85cdef3e4aa8e86bd460a942e66d7e67fbb3 Mon Sep 17 00:00:00 2001 From: Alexander Stoll Date: Thu, 31 Oct 2019 11:17:12 +0100 Subject: [PATCH 2/3] Changes to directory check accordingly, based on provider, Creation time added. --- lib/plugins/Invoke-IcingaCheckDirectory.psm1 | 45 +++++++++++++++----- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/lib/plugins/Invoke-IcingaCheckDirectory.psm1 b/lib/plugins/Invoke-IcingaCheckDirectory.psm1 index 76d1092..9e2be91 100644 --- a/lib/plugins/Invoke-IcingaCheckDirectory.psm1 +++ b/lib/plugins/Invoke-IcingaCheckDirectory.psm1 @@ -19,7 +19,7 @@ Import-IcingaLib provider\directory; [WARNING]: Check package "C:\Users\Icinga\Downloads" is [WARNING] (Match All) \_ [WARNING]: C:\Users\Icinga\Downloads is 24 .EXAMPLE - PS>Invoke-IcingaCheckDirectory -Path "C:\Users\Icinga\Downloads" -Warning 20 -Critical 30 -Verbosity 3 -YoungerThen 20d -OlderThen 10d + PS>Invoke-IcingaCheckDirectory -Path "C:\Users\Icinga\Downloads" -Warning 20 -Critical 30 -Verbosity 3 -ChangeYoungerThen 20d -ChangeOlderThen 10d [OK]: Check package "C:\Users\Icinga\Downloads" is [OK] (Match All) \_ [OK]: C:\Users\Icinga\Downloads is 1 .EXAMPLE @@ -39,14 +39,34 @@ Import-IcingaLib provider\directory; e.g '*.txt', '*.sql' # Fiends all files ending with .txt and .sql .PARAMETER Recurse A switch, which can be set to filter through directories recursively. -.PARAMETER YoungerThen +.PARAMETER ChangeYoungerThen String that expects input format like "20d", which translates to 20 days. Allowed units: ms, s, m, h, d, w, M, y. - Thereby all files younger then 20 days are considered within the check -.PARAMETER OlderThen + Thereby all files which have a change date younger then 20 days are considered within the check. +.PARAMETER ChangeOlderThen String that expects input format like "20d", which translates to 20 days. Allowed units: ms, s, m, h, d, w, M, y. - Thereby all files older then 20 days are considered within the check + Thereby all files which have a change date older then 20 days are considered within the check. +.PARAMETER CreationYoungerThen + String that expects input format like "20d", which translates to 20 days. Allowed units: ms, s, m, h, d, w, M, y. + + Thereby all files which have a creation date younger then 20 days are considered within the check. +.PARAMETER CreationOlderThen + String that expects input format like "20d", which translates to 20 days. Allowed units: ms, s, m, h, d, w, M, y. + + Thereby all files which have a creation date older then 20 days are considered within the check. + +.PARAMETER ChangeTimeEqual + String that expects input format like "20d", which translates to 20 days. Allowed units: ms, s, m, h, d, w, M, y. + + Thereby all files which have been changed 20 days ago are considered within the check. + +.PARAMETER CreationTimeEqual + String that expects input format like "20d", which translates to 20 days. Allowed units: ms, s, m, h, d, w, M, y. + + Thereby all files which have been created 20 days ago are considered within the check. + + .INPUTS System.String .OUTPUTS @@ -64,8 +84,12 @@ function Invoke-IcingaCheckDirectory() [switch]$Recurse, $Critical = $null, $Warning = $null, - [string]$YoungerThan, - [string]$OlderThan, + [string]$ChangeTimeEqual, + [string]$ChangeYoungerThan, + [string]$ChangeOlderThan, + [string]$CreationTimeEqual, + [string]$CreationOlderThan, + [string]$CreationYoungerThan, [string]$FileSizeGreaterThan, [string]$FileSizeSmallerThan, [ValidateSet(0, 1, 2, 3)] @@ -73,9 +97,10 @@ function Invoke-IcingaCheckDirectory() [switch]$NoPerfData ); - $DirectoryData = Get-IcingaDirectoryAll -Path $Path -FileNames $FileNames ` - -Recurse $Recurse -YoungerThan $YoungerThan -OlderThan $OlderThan ` - -FileSizeGreaterThan $FileSizeGreaterThan -FileSizeSmallerThan $FileSizeSmallerThan; + $DirectoryData = Get-IcingaDirectoryAll -Path $Path -FileNames $FileNames -Recurse $Recurse ` + -ChangeYoungerThan $ChangeYoungerThan -ChangeOlderThan $ChangeOlderThan ` + -CreationYoungerThan $CreationYoungerThan -CreationOlderThan $CreationOlderThan ` + -CreationTimeEqual $CreationTimeEqual -ChangeTimeEqual $ChangeTimeEqual; $DirectoryCheck = New-IcingaCheck -Name 'File Count' -Value $DirectoryData.Count; $DirectoryCheck.WarnOutOfRange( From c5d902c1d3d086ecfd821f0a51e3f2924f652f72 Mon Sep 17 00:00:00 2001 From: Alexander Stoll Date: Thu, 31 Oct 2019 13:43:09 +0100 Subject: [PATCH 3/3] Remove unnecessary code from services and add new performance counter --- lib/plugins/Invoke-IcingaCheckService.psm1 | 49 +++++++++++++++------- 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/lib/plugins/Invoke-IcingaCheckService.psm1 b/lib/plugins/Invoke-IcingaCheckService.psm1 index 16049fb..3afa6c1 100644 --- a/lib/plugins/Invoke-IcingaCheckService.psm1 +++ b/lib/plugins/Invoke-IcingaCheckService.psm1 @@ -37,12 +37,14 @@ function Invoke-IcingaCheckService() [ValidateSet('Stopped', 'StartPending', 'StopPending', 'Running', 'ContinuePending', 'PausePending', 'Paused')] [string]$Status = 'Running', [ValidateSet(0, 1, 2, 3)] - [int]$Verbosity = 0 + [int]$Verbosity = 0, + [switch]$NoPerfData ); - $ServicesPackage = New-IcingaCheckPackage -Name 'Services' -OperatorAnd -Verbose $Verbosity; + $ServicesPackage = New-IcingaCheckPackage -Name 'Services' -OperatorAnd -Verbose $Verbosity; + $ServicesCountPackage = New-IcingaCheckPackage -Name 'Count Services' -OperatorAnd -Verbose $Verbosity -Hidden; - if ($Service.Count -ne 1) { + [int]$StoppedCount,[int]$StartPendingCount,[int]$StopPendingCount,[int]$RunningCount,[int]$ContinuePendingCount,[int]$PausePendingCount,[int]$PausedCount,[int]$ServicesCounted = 0 foreach ($services in $Service) { $IcingaCheck = $null; @@ -50,22 +52,41 @@ function Invoke-IcingaCheckService() $ServiceName = Get-IcingaServiceCheckName -ServiceInput $services -Service $FoundService; $ConvertedStatus = ConvertTo-ServiceStatusCode -Status $Status; $StatusRaw = $FoundService.Values.configuration.Status.raw; - + $IcingaCheck = New-IcingaCheck -Name $ServiceName -Value $StatusRaw -ObjectExists $FoundService -Translation $ProviderEnums.ServiceStatusName; $IcingaCheck.CritIfNotMatch($ConvertedStatus) | Out-Null; $ServicesPackage.AddCheck($IcingaCheck) + + switch($StatusRaw) { + {1 -contains $_} { $StoppedCount++; $ServicesCounted++} + {2 -contains $_} { $StartPendingCount++; $ServicesCounted++} + {3 -contains $_} { $StopPendingCount++; $ServicesCounted++} + {4 -contains $_} { $RunningCount++; $ServicesCounted++} + {5 -contains $_} { $ContinuePendingCount++; $ServicesCounted++} + {6 -contains $_} { $PausePendingCount++; $ServicesCounted++} + {7 -contains $_} { $PausedCount++; $ServicesCounted++} + } } - } else { + $IcingaStopped = New-IcingaCheck -Name 'stopped services' -Value $StoppedCount; + $IcingaStartPending = New-IcingaCheck -Name 'pending started services' -Value $StartPendingCount; + $IcingaStopPending = New-IcingaCheck -Name 'pending stopped services' -Value $StopPendingCount; + $IcingaRunning = New-IcingaCheck -Name 'running services' -Value $RunningCount; + $IcingaContinuePending = New-IcingaCheck -Name 'pending continued services' -Value $ContinuePendingCount; + $IcingaPausePending = New-IcingaCheck -Name 'pending paused services' -Value $PausePendingCount; + $IcingaPaused = New-IcingaCheck -Name 'paused services' -Value $PausePendingCount; + + $IcingaCount = New-IcingaCheck -Name 'service count' -Value $ServicesCounted; - $FoundService = Get-IcingaServices -Service $Service; - $ServiceName = Get-IcingaServiceCheckName -ServiceInput $Service -Service $FoundService; - $IntStatus = ConvertTo-ServiceStatusCode -Status $Status; - $StatusRaw = $FoundService.Values.configuration.Status.raw; + $ServicesCountPackage.AddCheck($IcingaStopped) + $ServicesCountPackage.AddCheck($IcingaStartPending) + $ServicesCountPackage.AddCheck($IcingaStopPending) + $ServicesCountPackage.AddCheck($IcingaRunning) + $ServicesCountPackage.AddCheck($IcingaContinuePending) + $ServicesCountPackage.AddCheck($IcingaPausePending) + $ServicesCountPackage.AddCheck($IcingaPaused) - $IcingaCheck = New-IcingaCheck -Name $ServiceName -Value $StatusRaw -ObjectExists $FoundService -Translation $ProviderEnums.ServiceStatusName; - $IcingaCheck.CritIfNotMatch($IntStatus) | Out-Null; - $ServicesPackage.AddCheck($IcingaCheck); + $ServicesCountPackage.AddCheck($IcingaCount) + $ServicesPackage.AddCheck($ServicesCountPackage) - } - return (New-IcingaCheckResult -Name 'Services' -Check $ServicesPackage -NoPerfData $TRUE -Compile); + return (New-IcingaCheckResult -Name 'Services' -Check $ServicesPackage -NoPerfData $NoPerfData -Compile); }