Add Set-NumericNegative Tool; Seperate Directory check into provider, change directory check from date to relative (20d)

This commit is contained in:
Crited 2019-10-29 09:01:38 +01:00
parent a0b5efba19
commit cfb6d9f5a0
3 changed files with 89 additions and 34 deletions

View file

@ -0,0 +1,11 @@
function Set-NumericNegative()
{
param(
$Value
);
$Value = $Value * -1;
return $Value;
}

View file

@ -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;

View file

@ -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;
}