mirror of
https://github.com/Icinga/icinga-powershell-framework.git
synced 2025-12-21 07:10:15 -05:00
Add Set-NumericNegative Tool; Seperate Directory check into provider, change directory check from date to relative (20d)
This commit is contained in:
parent
717417dd8a
commit
72b7749dc2
3 changed files with 89 additions and 34 deletions
11
lib/core/tools/Set-NumericNegative.psm1
Normal file
11
lib/core/tools/Set-NumericNegative.psm1
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
|
||||||
|
function Set-NumericNegative()
|
||||||
|
{
|
||||||
|
param(
|
||||||
|
$Value
|
||||||
|
);
|
||||||
|
|
||||||
|
$Value = $Value * -1;
|
||||||
|
|
||||||
|
return $Value;
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
Import-IcingaLib provider\bios;
|
Import-IcingaLib provider\directory;
|
||||||
|
|
||||||
<#
|
<#
|
||||||
.SYNOPSIS
|
.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.
|
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.
|
Based on the thresholds set the status will change between 'OK', 'WARNING' or 'CRITICAL'. The function will return one of these given codes.
|
||||||
.EXAMPLE
|
.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]: Check package "C:\Users\Icinga\Downloads" is [OK] (Match All)
|
||||||
\_ [OK]: C:\Users\Icinga\Downloads is 19
|
\_ [OK]: C:\Users\Icinga\Downloads is 19
|
||||||
.EXAMPLE
|
.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]: Check package "C:\Users\Icinga\Downloads" is [WARNING] (Match All)
|
||||||
\_ [WARNING]: C:\Users\Icinga\Downloads is 24
|
\_ [WARNING]: C:\Users\Icinga\Downloads is 24
|
||||||
.EXAMPLE
|
.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]: Check package "C:\Users\Icinga\Downloads" is [OK] (Match All)
|
||||||
\_ [OK]: C:\Users\Icinga\Downloads is 1
|
\_ [OK]: C:\Users\Icinga\Downloads is 1
|
||||||
.EXAMPLE
|
.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]: Check package "C:\Users\Icinga\Downloads" is [OK] (Match All)
|
||||||
\_ [OK]: C:\Users\Icinga\Downloads is 4
|
\_ [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.
|
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.
|
Used to specify a Critical threshold. In this case an integer value.
|
||||||
.PARAMETER Path
|
.PARAMETER Path
|
||||||
Used to specify a path.
|
Used to specify a path.
|
||||||
|
|
@ -36,13 +36,17 @@ Import-IcingaLib provider\bios;
|
||||||
.PARAMETER FileNames
|
.PARAMETER FileNames
|
||||||
Used to specify an array of filenames or expressions to match against.
|
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
|
.PARAMETER Recurse
|
||||||
A switch, which can be set to filter through directories recursively.
|
A switch, which can be set to filter through directories recursively.
|
||||||
.PARAMETER YoungerThen
|
.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
|
.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
|
.INPUTS
|
||||||
System.String
|
System.String
|
||||||
.OUTPUTS
|
.OUTPUTS
|
||||||
|
|
@ -58,38 +62,21 @@ function Invoke-IcingaCheckDirectory()
|
||||||
[string]$Path,
|
[string]$Path,
|
||||||
[array]$FileNames,
|
[array]$FileNames,
|
||||||
[switch]$Recurse,
|
[switch]$Recurse,
|
||||||
[int]$IcingaCheckDirectory_Int_Critical,
|
[int]$Critical,
|
||||||
[int]$IcingaCheckDirectory_Int_Warning,
|
[int]$Warning,
|
||||||
[string]$YoungerThen,
|
[string]$YoungerThen,
|
||||||
[string]$OlderThen,
|
[string]$OlderThen,
|
||||||
[int]$Verbose
|
[int]$Verbose
|
||||||
);
|
);
|
||||||
|
|
||||||
if ($Recurse -eq $TRUE) {
|
$DirectoryData = Get-IcingaDirectoryAll -Path $Path -FileNames $FileNames `
|
||||||
$FileCount = (Get-ChildItem -Include $FileNames -Recurse -Path $Path);
|
-Recurse $Recurse -YoungerThen $YoungerThen -OlderThen $OlderThen;
|
||||||
} else {
|
$DirectoryCheck = New-IcingaCheck -Name $Path -Value $DirectoryData.Count -NoPerfData;
|
||||||
$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;
|
|
||||||
|
|
||||||
$DirectoryCheck.WarnOutOfRange(
|
$DirectoryCheck.WarnOutOfRange(
|
||||||
($IcingaCheckDirectory_Int_Warning)
|
($Warning)
|
||||||
).CritOutOfRange(
|
).CritOutOfRange(
|
||||||
($IcingaCheckDirectory_Int_Critical)
|
($Critical)
|
||||||
) | Out-Null;
|
) | Out-Null;
|
||||||
|
|
||||||
$DirectoryPackage = New-IcingaCheckPackage -Name $Path -OperatorAnd -Checks $DirectoryCheck -Verbose $Verbose;
|
$DirectoryPackage = New-IcingaCheckPackage -Name $Path -OperatorAnd -Checks $DirectoryCheck -Verbose $Verbose;
|
||||||
|
|
|
||||||
57
lib/provider/directory/Icinga_Provider_Directory.psm1
Normal file
57
lib/provider/directory/Icinga_Provider_Directory.psm1
Normal 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;
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue