Skip to content

Commit

Permalink
Merge pull request #303 from Icinga:feature/array_thresholds_date_time
Browse files Browse the repository at this point in the history
Feature: Adds array thresholds and date time support

Adds support to parse arrays to Icinga Check thresholds functions like `WarnOutOfRange` and adds two new functions `WarnDateTime` and `CritDateTime`, for easier comparing of time stamps.

Required for new `Invoke-IcingaCheckScheduledTask` features requested on [Plugin Feature Reqest #208](Icinga/icinga-powershell-plugins#208)
  • Loading branch information
LordHepipud authored Aug 13, 2021
2 parents 76c4aac + 3afbce0 commit b5661d7
Show file tree
Hide file tree
Showing 5 changed files with 316 additions and 1 deletion.
3 changes: 2 additions & 1 deletion doc/31-Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ documentation before upgrading to a new release.

Released closed milestones can be found on [GitHub](https://github.com/Icinga/icinga-powershell-framework/milestones?state=closed).

## 1.6.0 (pending)
## 1.6.0 (2021-09-07)

[Issue and PRs](https://github.com/Icinga/icinga-powershell-framework/milestone/15?closed=1)

Expand All @@ -19,6 +19,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
### Enhancements

* [#301](https://github.com/Icinga/icinga-powershell-framework/pull/301) Improves error handling to no longer print passwords in case `String` is used for `SecureString` arguments
* [#303](https://github.com/Icinga/icinga-powershell-framework/pull/303) Adds support to parse arrays to Icinga Check thresholds functions like `WarnOutOfRange` and adds two new functions `WarnDateTime` and `CritDateTime`, for easier comparing of time stamps.
* [#305](https://github.com/Icinga/icinga-powershell-framework/pull/305) Adds a new Cmdlet to test if functions with `Add-Type` are already present inside the current scope of the shell
* [#306](https://github.com/Icinga/icinga-powershell-framework/pull/306) Adds new Cmdlet `Exit-IcingaThrowCritical` to throw critical exit with a custom message, either by force or by using string filtering and adds storing of plugin exit codes internally
* [#314](https://github.com/Icinga/icinga-powershell-framework/pull/314) Adds support to configure on which address TCP sockets are created on, defaults to `loopback` interface
Expand Down
14 changes: 14 additions & 0 deletions lib/core/tools/Convert-IcingaPluginThresholds.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ function Convert-IcingaPluginThresholds()
[array]$Content = @();

if ($Threshold.Contains(':')) {
# If we have more than one ':' inside our string, lets check if this is a date time value
# In case it is convert it properly to a FileTime we can work with later on
if ([Regex]::Matches($Threshold, ":").Count -gt 1) {
try {
$DateTimeValue = [DateTime]::ParseExact($Threshold, 'yyyy\/MM\/dd HH:mm:ss', $null);
$RetValue.Value = $DateTimeValue.ToFileTime();
$RetValue.Unit = 's';
} catch {
$RetValue.Value = $Threshold;
}

return $RetValue;
}

$Content = $Threshold.Split(':');
} else {
$Content += $Threshold;
Expand Down
42 changes: 42 additions & 0 deletions lib/icinga/plugin/Compare-IcingaPluginThresholds.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function Compare-IcingaPluginThresholds()
$BaseValue = $null,
[switch]$Matches = $FALSE,
[switch]$NotMatches = $FALSE,
[switch]$DateTime = $FALSE,
[string]$Unit = '',
$ThresholdCache = $null,
[string]$CheckName = '',
Expand Down Expand Up @@ -178,6 +179,47 @@ function Compare-IcingaPluginThresholds()
(ConvertTo-IcingaPluginOutputTranslation -Translation $Translation -Value (Convert-IcingaPluginValueToString -Unit $IcingaThresholds.Unit -Value $ThresholdValue -OriginalUnit $IcingaThresholds.OriginalUnit))
);
}
} elseif ($DateTime) {
# Checks if the InputValue Is Inside our time value

try {
$DateTimeValue = 0;
[decimal]$TimeThreshold = 0;
$CurrentDate = $global:Icinga.CurrentDate;
$IcingaThresholds.Unit = '';

if ([string]::IsNullOrEmpty($InputValue) -eq $FALSE) {
$DateTimeValue = [DateTime]::FromFileTime($InputValue);
$IcingaThresholds.Value = $DateTimeValue.ToString('yyyy\/MM\/dd HH:mm:ss');
}

if ([string]::IsNullOrEmpty($ThresholdValue) -eq $FALSE) {
$TimeThreshold = (ConvertTo-Seconds -Value $Threshold);
$CurrentDate = $CurrentDate.AddSeconds($TimeThreshold);
$IcingaThresholds.IcingaThreshold = $CurrentDate.ToFileTimeUtc();
}

if ([string]::IsNullOrEmpty($ThresholdValue) -eq $FALSE -And ($DateTimeValue -eq 0 -Or $DateTimeValue -lt $CurrentDate)) {
$IcingaThresholds.InRange = $FALSE;
$IcingaThresholds.Message = 'is lower than';
$IcingaThresholds.Range = [string]::Format(
'{0} ({1}{2})',
((Get-Date).ToString('yyyy\/MM\/dd HH:mm:ss')),
( $( if ($TimeThreshold -ge 0) { '+'; } else { ''; } )),
$Threshold
);
}
} catch {
$IcingaThresholds.ErrorMessage = [string]::Format(
'Invalid date time specified. Your InputValue "{0}" seems not be a valid date time or your provided Threshold "{1}" cannot be converted to seconds. Exception: {2}',
$InputValue,
$ThresholdValue,
$_.Exception.Message
);
$IcingaThresholds.HasError = $TRUE;

return $IcingaThresholds;
}
} elseif ($IsBetween) {
if ($InputValue -gt $Minium -And $InputValue -lt $Maximum) {
$IcingaThresholds.InRange = $FALSE;
Expand Down
Loading

0 comments on commit b5661d7

Please sign in to comment.