From b68ed5440e5ad712b7d787dfb985fb2703652a53 Mon Sep 17 00:00:00 2001 From: Alexander Stoll Date: Tue, 29 Oct 2019 09:01:38 +0100 Subject: [PATCH] Add Set-NumericNegative Tool; Seperate Directory check into provider, change directory check from date to relative (20d) --- lib/core/tools/Set-NumericNegative.psm1 | 11 ++++ lib/plugins/Invoke-IcingaCheckDirectory.psm1 | 55 +++++++----------- .../directory/Icinga_Provider_Directory.psm1 | 57 +++++++++++++++++++ 3 files changed, 89 insertions(+), 34 deletions(-) create mode 100644 lib/core/tools/Set-NumericNegative.psm1 create mode 100644 lib/provider/directory/Icinga_Provider_Directory.psm1 diff --git a/lib/core/tools/Set-NumericNegative.psm1 b/lib/core/tools/Set-NumericNegative.psm1 new file mode 100644 index 0000000..394d72a --- /dev/null +++ b/lib/core/tools/Set-NumericNegative.psm1 @@ -0,0 +1,11 @@ + +function Set-NumericNegative() +{ + param( + $Value + ); + + $Value = $Value * -1; + + return $Value; +} \ No newline at end of file diff --git a/lib/plugins/Invoke-IcingaCheckDirectory.psm1 b/lib/plugins/Invoke-IcingaCheckDirectory.psm1 index bfcc104..7b0957e 100644 --- a/lib/plugins/Invoke-IcingaCheckDirectory.psm1 +++ b/lib/plugins/Invoke-IcingaCheckDirectory.psm1 @@ -1,4 +1,4 @@ -Import-IcingaLib provider\bios; +Import-IcingaLib provider\directory; <# .SYNOPSIS @@ -11,24 +11,24 @@ Import-IcingaLib provider\bios; This module is intended to be used to check how many files and directories are within are specified path. Based on the thresholds set the status will change between 'OK', 'WARNING' or 'CRITICAL'. The function will return one of these given codes. .EXAMPLE - PS>Invoke-IcingaCheckDirectory -Path "C:\Users\Icinga\Downloads" -InvokeIcingaCheck_Int_Warning 20 -InvokeIcingaCheck_Int_Critical 30 -Verbose 3 + PS>Invoke-IcingaCheckDirectory -Path "C:\Users\Icinga\Downloads" -Warning 20 -Critical 30 -Verbose 3 [OK]: Check package "C:\Users\Icinga\Downloads" is [OK] (Match All) \_ [OK]: C:\Users\Icinga\Downloads is 19 .EXAMPLE - PS>Invoke-IcingaCheckDirectory -Path "C:\Users\Icinga\Downloads" -InvokeIcingaCheck_Int_Warning 20 -InvokeIcingaCheck_Int_Critical 30 -Verbose 3 + PS>Invoke-IcingaCheckDirectory -Path "C:\Users\Icinga\Downloads" -Warning 20 -Critical 30 -Verbose 3 [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" -InvokeIcingaCheck_Int_Warning 20 -InvokeIcingaCheck_Int_Critical 30 -Verbose 3 -YoungerThen 08.10.2018 -OlderThen 10.12.2018 + PS>Invoke-IcingaCheckDirectory -Path "C:\Users\Icinga\Downloads" -Warning 20 -Critical 30 -Verbose 3 -YoungerThen 20d -OlderThen 10d [OK]: Check package "C:\Users\Icinga\Downloads" is [OK] (Match All) \_ [OK]: C:\Users\Icinga\Downloads is 1 .EXAMPLE - PS>Invoke-IcingaCheckDirectory -Path "C:\Users\Icinga\Downloads" -FileNames "*.txt","*.sql" -InvokeIcingaCheck_Int_Warning 20 -InvokeIcingaCheck_Int_Critical 30 -Verbose 3 + PS>Invoke-IcingaCheckDirectory -Path "C:\Users\Icinga\Downloads" -FileNames "*.txt","*.sql" -Warning 20 -Critical 30 -Verbose 3 [OK]: Check package "C:\Users\Icinga\Downloads" is [OK] (Match All) \_ [OK]: C:\Users\Icinga\Downloads is 4 -.PARAMETER IcingaCheckDirectory_Int_Warning +.PARAMETER Warning Used to specify a Warning threshold. In this case an integer value. -.PARAMETER IcingaCheckDirectory_Int_Critical +.PARAMETER Critical Used to specify a Critical threshold. In this case an integer value. .PARAMETER Path Used to specify a path. @@ -36,13 +36,17 @@ Import-IcingaLib provider\bios; .PARAMETER FileNames Used to specify an array of filenames or expressions to match against. - e.g '*.txt','.sql' # Fiends all files ending with .txt and .sql + 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 - String that expects input format "MM.dd.yyyy". Used to only filter for files younger then the specified date. + 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 - String that expects input format "MM.dd.yyyy". Used to only filter for files older then the specified date. + 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 .INPUTS System.String .OUTPUTS @@ -58,38 +62,21 @@ function Invoke-IcingaCheckDirectory() [string]$Path, [array]$FileNames, [switch]$Recurse, - [int]$IcingaCheckDirectory_Int_Critical, - [int]$IcingaCheckDirectory_Int_Warning, + [int]$Critical, + [int]$Warning, [string]$YoungerThen, [string]$OlderThen, [int]$Verbose ); - if ($Recurse -eq $TRUE) { - $FileCount = (Get-ChildItem -Include $FileNames -Recurse -Path $Path); - } else { - $FileCount = (Get-ChildItem -Include $FileNames -Path $Path); - } - - if ($OlderThen -ne "" -And $YoungerThen -ne "") { - $OlderThen=[datetime]::ParseExact($OlderThen,'MM.dd.yyyy',$null) - $YoungerThen=[datetime]::ParseExact($YoungerThen,'MM.dd.yyyy',$null) - $FileCount = ($FileCount | Where-Object {$_.LastWriteTime -lt ($OlderThen)}) #| Where-Object {$_LastWriteTime -gt ($YoungerThen)} - $FileCount = ($FileCount | Where-Object {$_.LastWriteTime -gt ($YoungerThen)}) - } elseif ($OlderThen -ne "") { - $OlderThen=[datetime]::ParseExact($OlderThen,'MM.dd.yyyy',$null) - $FileCount = ($FileCount | Where-Object {$_.LastWriteTime -lt ($OlderThen)}) - } elseif ($YoungerThen -ne "") { - $YoungerThen=[datetime]::ParseExact($YoungerThen,'MM.dd.yyyy',$null) - $FileCount = ($FileCount | Where-Object {$_.LastWriteTime -gt ($YoungerThen)}) - } - - $DirectoryCheck = New-IcingaCheck -Name $Path -Value $FileCount.Count -NoPerfData; + $DirectoryData = Get-IcingaDirectoryAll -Path $Path -FileNames $FileNames ` + -Recurse $Recurse -YoungerThen $YoungerThen -OlderThen $OlderThen; + $DirectoryCheck = New-IcingaCheck -Name $Path -Value $DirectoryData.Count -NoPerfData; $DirectoryCheck.WarnOutOfRange( - ($IcingaCheckDirectory_Int_Warning) + ($Warning) ).CritOutOfRange( - ($IcingaCheckDirectory_Int_Critical) + ($Critical) ) | Out-Null; $DirectoryPackage = New-IcingaCheckPackage -Name $Path -OperatorAnd -Checks $DirectoryCheck -Verbose $Verbose; diff --git a/lib/provider/directory/Icinga_Provider_Directory.psm1 b/lib/provider/directory/Icinga_Provider_Directory.psm1 new file mode 100644 index 0000000..efcfc31 --- /dev/null +++ b/lib/provider/directory/Icinga_Provider_Directory.psm1 @@ -0,0 +1,57 @@ +Import-IcingaLib core\tools; + +function Get-IcingaDirectoryAll() +{ + param( + [string]$Path, + [array]$FileNames, + [bool]$Recurse, + [string]$YoungerThen, + [string]$OlderThen, + ); + + if ($Recurse -eq $TRUE) { + $DirectoryData = Get-ChildItem -Recurse -Path $Path -Include $FileNames; + } else { + $DirectoryData = Get-ChildItem -Path $Path -Include $FileNames; + } + + if ([string]::IsNullOrEmpty($OlderThen) -eq $FALSE -And [string]::IsNullOrEmpty($YoungerThen) -eq $FALSE) { + $OlderThen = Set-NumericNegative (ConvertTo-Seconds $OlderThen); + $DirectoryData = ($DirectoryData | Where-Object {$_.LastWriteTime -lt (Get-Date).AddSeconds($OlderThen)}) + $YoungerThen = Set-NumericNegative (ConvertTo-Seconds $YoungerThen); + $DirectoryData = ($DirectoryData | Where-Object {$_.LastWriteTime -gt (Get-Date).AddSeconds($YoungerThen)}) + } elseif ([string]::IsNullOrEmpty($OlderThen) -eq $FALSE) { + $OlderThen = Set-NumericNegative (ConvertTo-Seconds $OlderThen); + $DirectoryData = ($DirectoryData | Where-Object {$_.LastWriteTime -lt (Get-Date).AddSeconds($OlderThen)}) + } elseif ([string]::IsNullOrEmpty($YoungerThen) -eq $FALSE) { + $YoungerThen = Set-NumericNegative (ConvertTo-Seconds $YoungerThen); + $DirectoryData = ($DirectoryData | Where-Object {$_.LastWriteTime -gt ((Get-Date).AddSeconds($YoungerThen))}) + } + + return $DirectoryData; +} + +function Get-IcingaDirectory() +{ + param( + [string]$Path, + [array]$FileNames + ); + + $DirectoryData = Get-ChildItem -Include $FileNames -Path $Path; + + return $DirectoryData; +} + +function Get-IcingaDirectoryRecurse() +{ + param( + [string]$Path, + [array]$FileNames + ); + + $DirectoryData = Get-ChildItem -Recurse -Include $FileNames -Path $Path; + + return $DirectoryData; +} \ No newline at end of file