Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Adds array thresholds and date time support #303

Merged
merged 1 commit into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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