Improved Check-Command Config generation

* Switch arguments will now use boolean data types in Director
* Fixed an issue for Check Commands with integrated help, as arguments were not rendered properly
* Fixed an issue on datafield ID lookup and assigning
* Fixed an issue with assigning default values
This commit is contained in:
Lord Hepipud 2019-09-16 15:33:57 +02:00
parent 516b915472
commit fae69a3e69

View file

@ -82,34 +82,6 @@ function Get-IcingaCheckCommandConfig()
$Basket.Add('DataList', @{});
$Basket.Add('Command', @{});
# "NoPerfData" gets added to all checks build and exported no matter what, so we add it from the start
if ($Basket.DataList.ContainsKey('PowerShell NoPerfData') -eq $FALSE) {
# DataList Content for NoPerfData
$Basket.DataList.Add(
'PowerShell NoPerfData', @{
'list_name' = 'PowerShell NoPerfData';
'owner' = $env:username;
'originalId' = '1';
'entries' = @(
@{
'entry_name' = '0';
'entry_value' = "yes";
'format' = 'string';
'allowed_roles' = $NULL;
},
@{
'entry_name' = '1';
'entry_value' = "no";
'format' = 'string';
'allowed_roles' = $NULL;
}
);
}
);
}
# "Verbose" gets added to all Checks build and exported no matter what, so we add it from the start
if ($Basket.DataList.ContainsKey('PowerShell Verbose') -eq $FALSE) {
$Basket.DataList.Add(
@ -150,28 +122,26 @@ function Get-IcingaCheckCommandConfig()
# Loop through ${CheckName}, to get information on every command specified/all commands.
foreach ($check in $CheckName) {
[int]$FieldNumeration = 0;
# Get necessary syntax-information and more through cmdlet "Get-Help"
$Data = (Get-Help $check)
# Add command Structure
$Basket.Command.Add(
$Data.Syntax.syntaxItem.Name, @{
$Data.Name, @{
'arguments'= @{
# Gets set for every command as default
'-C' = @{
'value' = [string]::Format('Use-Icinga; {0}', $Data.Syntax.syntaxItem.Name);
'value' = [string]::Format('Use-Icinga; {0}', $Data.Name);
'order' = '0';
}
}
'command' = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe";
'disabled' = $FALSE;
'fields' = @{};
'fields' = @();
'imports' = @();
'is_string' = $NULL;
'methods_execute' = 'PluginCheck';
'object_name' = $Data.Syntax.syntaxItem.Name;
'object_name' = $Data.Name;
'object_type' = 'object';
'timeout' = '180';
'vars' = @{};
@ -180,7 +150,7 @@ function Get-IcingaCheckCommandConfig()
);
# Loop through parameters of a given command
foreach ($parameter in $Data.Syntax.syntaxItem.parameter) {
foreach ($parameter in $Data.parameters.parameter) {
# Filter for Parameter 'core', because its set by default
if ($parameter.name -ne 'core') {
@ -196,7 +166,7 @@ function Get-IcingaCheckCommandConfig()
# Add arguments to a given command
if ($parameter.type.name -eq 'switch') {
$Basket.Command[$Data.Syntax.syntaxItem.Name].arguments.Add(
$Basket.Command[$Data.Name].arguments.Add(
[string]::Format('-{0}', $parameter.Name), @{
'set_if' = $IcingaCustomVariable;
'set_if_format' = 'string';
@ -204,11 +174,11 @@ function Get-IcingaCheckCommandConfig()
}
);
$Basket.Command[$Data.Syntax.syntaxItem.Name].vars.Add($parameter.Name, "0");
$Basket.Command[$Data.Name].vars.Add($parameter.Name, $FALSE);
# Conditional whether type of parameter is array
} elseif ($parameter.type.name -eq 'array') {
$Basket.Command[$Data.Syntax.syntaxItem.Name].arguments.Add(
$Basket.Command[$Data.Name].arguments.Add(
[string]::Format('-{0}', $parameter.Name), @{
'value' = @{
'type' = 'Function';
@ -225,7 +195,7 @@ function Get-IcingaCheckCommandConfig()
);
} else {
# Default to Object
$Basket.Command[$Data.Syntax.syntaxItem.Name].arguments.Add(
$Basket.Command[$Data.Name].arguments.Add(
[string]::Format('-{0}', $parameter.Name), @{
'value' = $IcingaCustomVariable;
'order' = $Order;
@ -233,9 +203,9 @@ function Get-IcingaCheckCommandConfig()
);
if ($parameter.name -ne 'Verbose') {
$Basket.Command[$Data.Syntax.syntaxItem.Name].vars.Add($parameter.Name, '$$null');
$Basket.Command[$Data.Name].vars.Add($parameter.Name, '$$null');
} else {
$Basket.Command[$Data.Syntax.syntaxItem.Name].vars.Add($parameter.Name, "0");
$Basket.Command[$Data.Name].vars.Add($parameter.Name, "");
}
}
@ -251,17 +221,7 @@ function Get-IcingaCheckCommandConfig()
$DataListName = [string]::Format('PowerShell {0}', $parameter.Name)
if ($parameter.type.name -eq 'switch') {
$IcingaDataType='Datalist';
if ($Basket.DataList.ContainsKey($DataListName) -eq $FALSE) {
$Basket.DataList.Add(
$DataListName, @{
'list_name' = $DataListName;
'owner' = $env:username;
'originalId' = '50'; #Gehört noch geändert
'entries' = @{};
}
);
}
$IcingaDataType='Boolean';
} elseif ($parameter.type.name -eq 'Object') {
if ($parameter.Name -eq 'Verbose') {
$IcingaDataType='Datalist'
@ -279,16 +239,11 @@ function Get-IcingaCheckCommandConfig()
$Basket.Datafield.Add(
'0', @{
'varname' = 'PowerShell_switch_NoPerfData';
'caption' = 'NoPerfData';
'caption' = 'Ignore Performance Data';
'description' = 'Specifies if the plugin will return performance data output or not';
'datatype' = 'Icinga\\Module\\Director\\DataType\\DataTypeDatalist';
'datatype' = 'Icinga\Module\Director\DataType\DataTypeBoolean';
'format' = $NULL;
'originalId' = '0';
'settings' = @{
'datalist' = 'PowerShell NoPerfData';
'datatype' = 'string';
'behavior' = 'strict';
}
}
);
}
@ -296,10 +251,10 @@ function Get-IcingaCheckCommandConfig()
if($Basket.Datafield.ContainsKey('1') -eq $FALSE){
$Basket.Datafield.Add(
'1', @{
'varname' = 'PowerShell_switch_NoPerfData';
'varname' = 'PowerShell_Object_Verbose';
'caption' = 'Verbose';
'description' = 'Specifies if the plugin will return performance data output or not';
'datatype' = 'Icinga\\Module\\Director\\DataType\\DataTypeString';
'datatype' = 'Icinga\Module\Director\DataType\DataTypeString';
'format' = $NULL;
'originalId' = '1';
'settings' = @{
@ -313,8 +268,8 @@ function Get-IcingaCheckCommandConfig()
$IcingaDataType = [string]::Format('Icinga\Module\Director\DataType\DataType{0}', $IcingaDataType)
if ($Basket.Datafield.Values.varname -eq $IcingaCustomVariable) {
} else {
if ($Basket.Datafield.Values.varname -ne $IcingaCustomVariable) {
$Basket.Datafield.Add(
[string]$FieldID, @{
'varname' = $IcingaCustomVariable;
@ -326,7 +281,7 @@ function Get-IcingaCheckCommandConfig()
}
);
if ($parameter.type.name -eq 'switch' -or $parameter.Name -eq 'Verbose') {
if ($parameter.Name -eq 'Verbose') {
$Basket.Datafield[[string]$FieldID].Add(
'settings', @{
'behavior' = 'strict';
@ -353,20 +308,20 @@ function Get-IcingaCheckCommandConfig()
}
}
# Increment FieldNumeation, so unique fields for a given command are added.
# Increment FieldNumeration, so unique fields for a given command are added.
[int]$FieldNumeration = [int]$FieldNumeration + 1;
}
# Check whether or not noperfdata and verbose is set and add it if necessary
if ($Basket.Command[$Data.Syntax.syntaxItem.Name].arguments.ContainsKey('-Verbose') -eq $FALSE) {
$Basket.Command[$Data.Syntax.syntaxItem.Name].arguments.Add(
if ($Basket.Command[$Data.Name].arguments.ContainsKey('-Verbose') -eq $FALSE) {
$Basket.Command[$Data.Name].arguments.Add(
'-Verbose', @{
'value' = '$PowerShell_Object_Verbose$';
'order' = '99';
}
);
$Basket.Command[$Data.Syntax.syntaxItem.Name].vars.Add(
$Basket.Command[$Data.Name].vars.Add(
'PowerShell_Object_Verbose', "0"
);
@ -390,34 +345,15 @@ function Get-IcingaCheckCommandConfig()
}
}
if ($Basket.Command[$Data.Syntax.syntaxItem.Name].arguments.ContainsKey('-NoPerfData') -eq $FALSE) {
$Basket.Command[$Data.Syntax.syntaxItem.Name].arguments.Add(
if ($Basket.Command[$Data.Name].arguments.ContainsKey('-NoPerfData') -eq $FALSE) {
$Basket.Command[$Data.Name].arguments.Add(
'-NoPerfData', @{
'set_if' = '$PowerShell_switch_NoPerfData$';
'set_if_format' = 'string';
'order' = '99';
}
);
$Basket.Command[$Data.Syntax.syntaxItem.Name].vars.Add('PowerShell_switch_NoPerfData', "0");
if ($Basket.Datafield.Values.varname -eq $IcingaCustomVariable) {
} else {
$Basket.Datafield.Add(
[string]$FieldID, @{
'varname' = 'PowerShell_switch_NoPerfData';
'caption' = 'Perf Data';
'description' = 'Specifies if the plugin will return performance data output or not';
'datatype' = 'Icinga\Module\Director\DataType\DataTypeDatalist';
'format' = $NULL;
'originalId' = [string]$FieldID;
'settings' = @{
'behavior' = 'strict';
'data_type' = 'string';
'datalist' = 'PowerShell NoPerfData'
}
}
);
}
$Basket.Command[$Data.Name].vars.Add('PowerShell_switch_NoPerfData', $FALSE);
}
}
@ -426,38 +362,25 @@ function Get-IcingaCheckCommandConfig()
$Data = (Get-Help $check)
foreach ($parameter in $Data.Syntax.syntaxItem.parameter){
foreach ($parameter in $Data.parameters.parameter){
$IcingaCustomVariable = [string]::Format('PowerShell_{0}_{1}', $parameter.type.name, $parameter.Name);
# Hashtable for Matching Command.Name.Fields to DataFields (and there given IDs)
[hashtable]$TranslationDataField = @{}
# Looping through IDs of existing DataFields
foreach ($DataFieldID in $Basket.Datafield.Keys)
{
# Ignore Default-Set Deatafield "NoPerfData"
if ($TranslationDataField.Contains('PowerShell_switch_NoPerfData') -eq $TRUE){
}else{
$TranslationDataField.Add($Basket.Datafield.$DataFieldID.varname, $DataFieldID);
}
# Todo: Should we improve this? Actually the handling would be identical, we just need to assign
# the proper field for this
if ($IcingaCustomVariable -eq 'PowerShell_Int32_Verbose') {
$IcingaCustomVariable = 'PowerShell_Object_Verbose';
}
foreach($key in $TranslationDataField.Keys)
{
if ([string]$IcingaCustomVariable -eq [string]$key) {
$MatchedDataFieldID = $TranslationDataField[$IcingaCustomVariable];
} else {}
}
$Basket.Command[$Data.Syntax.syntaxItem.Name].fields.Add(
[string]$FieldNumeration, @{
'datafield_id' = [int]$MatchedDataFieldID;
foreach ($DataFieldID in $Basket.Datafield.Keys) {
[string]$varname = $Basket.Datafield[$DataFieldID].varname;
if ([string]$varname -eq [string]$IcingaCustomVariable) {
$Basket.Command[$Data.Name].fields += @{
'datafield_id' = [int]$DataFieldID;
'is_required' = $Required;
'var_filter' = $NULL;
};
}
}
);
[int]$FieldNumeration = [int]$FieldNumeration + 1;
}
}