From fc165c79afbc1d94cde0dd0c051e9ebe4477f9ff Mon Sep 17 00:00:00 2001 From: Crited Date: Wed, 11 Sep 2019 15:08:30 +0200 Subject: [PATCH 01/13] Pulled from testing, build basic IcingaCheckCommandBasket-Function --- .../Invoke-IcingaCheckCommandBasket.psm1 | 311 ++++++++++++++++++ 1 file changed, 311 insertions(+) create mode 100644 lib/plugins/Invoke-IcingaCheckCommandBasket.psm1 diff --git a/lib/plugins/Invoke-IcingaCheckCommandBasket.psm1 b/lib/plugins/Invoke-IcingaCheckCommandBasket.psm1 new file mode 100644 index 0000000..7c3121e --- /dev/null +++ b/lib/plugins/Invoke-IcingaCheckCommandBasket.psm1 @@ -0,0 +1,311 @@ +function Invoke-IcingaCheckCommandBasket() +{ + param( + $CheckName + ); + + [hashtable]$AllChecks = @{}; + + if ($NULL -eq $CheckName) { + $CheckName = (Get-Command Invoke-IcingaCheck*).Name + } + + foreach ($check in $CheckName) { + + [hashtable]$Basket = @{}; + + [int]$FieldID = 0; + + $Data = (Get-Help $check) + + $Basket.Add( + 'Command', @{ + $Data.Syntax.syntaxItem.Name = @{ + 'arguments'= @{ + '-C' = @{ + 'value' = [string]::Format('Use-Icinga; {0}', $Data.Syntax.syntaxItem.Name); + 'order' = '0'; + } + } + 'command' = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"; #Gehört noch geändert + 'disabled' = $FALSE; + 'fields' = @{}; + 'imports' = @(); + 'is_string' = $NULL; + 'methods_excute' = 'PluginCheck'; + 'object_name' = $Data.Syntax.syntaxItem.Name; + 'object_type' = 'object'; + 'timeout' = '180'; + 'vars' = @{}; + 'zone' = $NULL; + } + } + ) + + $Basket.Add('Datafield', @{}); + $Basket.Add('DataList', @{}); + + foreach ($parameter in $Data.Syntax.syntaxItem.parameter) { + if ($parameter.name -ne 'core') { + # Is Numeric Check on position to determine the order value + If (Test-Numeric($parameter.position) -eq $TRUE) { + [string]$Order = [int]$parameter.position + 1 + } else { + [string]$Order = 99 + } + + $IcingaCustomVariable = [string]::Format('$PowerShell_{0}$', $parameter.Name); + + # Conditional whether type of parameter is switch + if ($parameter.type.name -eq 'switch') { + $Basket.Command[$Data.Syntax.syntaxItem.Name].arguments.Add( + [string]::Format('-{0}', $parameter.Name), @{ + 'set_if' = $IcingaCustomVariable; + 'set_if_format' = 'string'; + 'order' = $Order; + } + ); + $Basket.Command[$Data.Syntax.syntaxItem.Name].vars.Add( + $parameter.Name, "0" + ); + # Condotional whether type of parameter is array + } elseif ($parameter.type.name -eq 'array') { + $Basket.Command[$Data.Syntax.syntaxItem.Name].arguments.Add( + [string]::Format('-{0}', $parameter.Name), @{ + 'value' = [string]::Format('(Split-IcingaCheckCommandArgs {0})', $IcingaCustomVariable); + 'order' = $Order; + } + ); + } else { + # Default to Object + $Basket.Command[$Data.Syntax.syntaxItem.Name].arguments.Add( + [string]::Format('-{0}', $parameter.Name), @{ + 'value' = $IcingaCustomVariable; + 'order' = $Order; + } + ); + if ($parameter.name -ne 'Verbose') { + $Basket.Command[$Data.Syntax.syntaxItem.Name].vars.Add($parameter.Name, '$$null'); + } else { + $Basket.Command[$Data.Syntax.syntaxItem.Name].vars.Add($parameter.Name, "0"); + } + } + # Fields + # Required? + if ($parameter.required -eq $TRUE) { + $Required = 'y'; + } else { + $Required = 'n'; + } + $Basket.Command[$Data.Syntax.syntaxItem.Name].fields.Add( + [string]$FieldID, @{ + 'datafield_id' = [int]$FieldID; + 'is_required' = $Required; + 'var_filter' = $NULL; + } + ); + + $IcingaCustomVariable = [string]::Format('PowerShell_{0}', $parameter.Name); + + $DataListName = [string]::Format('PowerShell {0}', $parameter.Name) + + if ($parameter.type.name -eq 'switch') { + $IcingaDataType='Datalist'; + $Basket.DataList.Add( + $DataListName, @{ + 'list_name' = $DataListName; + 'owner' = $env:username; + 'originalId' = '50'; #Gehört noch geändert + 'entries' = @{}; + } + ); + + } elseif ($parameter.type.name -eq 'Object') { + if ($parameter.Name -eq 'Verbose') { + $IcingaDataType='Datalist' + } + $IcingaDataType='String'; + } elseif ($parameter.type.name -eq 'Array') { + $IcingaDataType='Array'; + } else { + $IcingaDataType='String'; + } + $IcingaDataType = [string]::Format('Icinga\Module\Director\DataType\DataType{0}', $IcingaDataType) + + $Basket.Datafield.Add( + [string]$FieldID, @{ + 'varname' = $IcingaCustomVariable; + 'caption' = $parameter.Name; + 'description' = $NULL; + 'datatype' = $IcingaDataType; + 'format' = $NULL; + 'originalId' = [string]$FieldID; + } + ); + + if ($parameter.type.name -eq 'switch' -or $parameter.Name -eq 'Verbose') { + $Basket.Datafield[[string]$FieldID].Add( + 'settings', @{ + 'behavior' = 'strict'; + 'datatype' = 'string'; + 'datalist' = $DataListName; + } + ); + } elseif ($parameter.type.name -eq 'Object') { + $Basket.Datafield[[string]$FieldID].Add( + 'settings', @{ + 'visbility' = 'visible'; + } + ); + } else { + $Basket.Datafield[[string]$FieldID].Add( + 'settings', @{} + ); + } + [int]$FieldID = $FieldID + 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) { + [int]$FieldID = $FieldID + 1; + $Basket.Command[$Data.Syntax.syntaxItem.Name].arguments.Add( + '-Verbose', @{ + 'value' = '$PowerShell_Verbose$'; + 'order' = 'o'; + } + ); + $Basket.Command[$Data.Syntax.syntaxItem.Name].vars.Add( + 'PowerShell_Verbose', "0" + ); + $Basket.Datafield.Add( + [string]$FieldID, @{ + 'varname' = 'PowerShell_Verbose'; + 'caption' = 'Verbose'; + 'description' = $NULL; + 'datatype' = 'Icinga\Module\Director\DataType\DataTypeDatalist'; + 'format' = $NULL; + 'originalId' = [string]$FieldID; + 'settings' = @{ + 'behavior' = 'strict'; + 'data_type' = 'string'; + 'datalist' = 'PowerShell Verbose' + } + } + ); + } + + + if ($Basket.Command[$Data.Syntax.syntaxItem.Name].arguments.ContainsKey('-NoPerfData') -eq $FALSE) { + [int]$FieldID = $FieldID + 1; + $Basket.Command[$Data.Syntax.syntaxItem.Name].arguments.Add( + '-NoPerfData', @{ + 'set_if' = '$PowerShell_NoPerfData$'; + 'set_if_format' = 'string'; + 'order' = '99'; + } + ); + $Basket.Command[$Data.Syntax.syntaxItem.Name].vars.Add( + 'PowerShell_NoPerfData', "0" + ); + $Basket.Datafield.Add( + [string]$FieldID, @{ + 'varname' = 'PowerShell_NoPerfData'; + 'caption' = 'Perf Data'; + 'description' = $NULL; + 'datatype' = 'Icinga\Module\Director\DataType\DataTypeDatalist'; + 'format' = $NULL; + 'originalId' = [string]$FieldID; + 'settings' = @{ + 'behavior' = 'strict'; + 'data_type' = 'string'; + 'datalist' = 'PowerShell NoPerfData' + } + } + ); + } + # DataList Entries (Default for NoPerfData) + if ($Basket.DataList.ContainsKey('PowerShell NoPerfData') -eq $FALSE) { + $Basket.DataList.Add( + 'PowerShell NoPerfData', @{ + 'list_name' = 'PowerShell NoPerfData'; + 'owner' = $env:username; + 'originalId' = '1'; #Gehört noch geändert + 'entries' = @{}; + } + ); + } + $Basket.DataList["PowerShell NoPerfData"].entries.Add( + '0', @{ + 'entry_name' = '0'; + 'entry_value:' = "yes"; + 'format' = 'string'; + 'allowed_roles' = $NULL; + } + ); + $Basket.DataList["PowerShell NoPerfData"].entries.Add( + '1', @{ + 'entry_name' = '1'; + 'entry_value:' = "no"; + 'format' = 'string'; + 'allowed_roles' = $NULL; + } + ); + + # DataList Entries (Default for Verbose) + if ($Basket.DataList.ContainsKey('PowerShell Verbose') -eq $FALSE) { + $Basket.DataList.Add( + 'PowerShell Verbose', @{ + 'list_name' = 'PowerShell Verbose'; + 'owner' = $env:username; + 'originalId' = '50'; #Gehört noch geändert + 'entries' = @{}; + } + ); + } + $Basket.DataList["PowerShell Verbose"].entries.Add( + '0', @{ + 'entry_name' = '0'; + 'entry_value:' = "Show Default"; + 'format' = 'string'; + 'allowed_roles' = $NULL; + } + ); + $Basket.DataList["PowerShell Verbose"].entries.Add( + '1', @{ + 'entry_name' = '1'; + 'entry_value:' = "Show Operator"; + 'format' = 'string'; + 'allowed_roles' = $NULL; + } + ); + $Basket.DataList["PowerShell Verbose"].entries.Add( + '2', @{ + 'entry_name' = '2'; + 'entry_value:' = "Show Problems"; + 'format' = 'string'; + 'allowed_roles' = $NULL; + } + ) + $Basket.DataList["PowerShell Verbose"].entries.Add( + '3', @{ + 'entry_name' = '3'; + 'entry_value:' = "Show All"; + 'format' = 'string'; + 'allowed_roles' = $NULL; + } + ); + $AllChecks.Add($check, $Basket); + } + +Write-Host $CheckName; +Write-Host $CheckName.Count; + + if ([string]$CheckName.Count -eq '1') { + $output=ConvertTo-Json -D 100 $Basket > Check.json; + } else { + $output=ConvertTo-Json -D 100 $AllChecks > Check.json; + } + + return $output; +} \ No newline at end of file From 428f98d32de0506963e1c381c1401db62dcf6023 Mon Sep 17 00:00:00 2001 From: Crited Date: Wed, 11 Sep 2019 16:07:52 +0200 Subject: [PATCH 02/13] Update CheckBasket --- .../Invoke-IcingaCheckCommandBasket.psm1 | 207 ++++++++++-------- 1 file changed, 114 insertions(+), 93 deletions(-) diff --git a/lib/plugins/Invoke-IcingaCheckCommandBasket.psm1 b/lib/plugins/Invoke-IcingaCheckCommandBasket.psm1 index 7c3121e..4398190 100644 --- a/lib/plugins/Invoke-IcingaCheckCommandBasket.psm1 +++ b/lib/plugins/Invoke-IcingaCheckCommandBasket.psm1 @@ -10,17 +10,96 @@ function Invoke-IcingaCheckCommandBasket() $CheckName = (Get-Command Invoke-IcingaCheck*).Name } + [int]$FieldID = 0; + [hashtable]$Basket = @{}; + $Basket.Add('Datafield', @{}); + $Basket.Add('DataList', @{}); + $Basket.Add('Command', @{}); + + # DataList Entries (Default for NoPerfData) + if ($Basket.DataList.ContainsKey('PowerShell NoPerfData') -eq $FALSE) { + $Basket.DataList.Add( + 'PowerShell NoPerfData', @{ + 'list_name' = 'PowerShell NoPerfData'; + 'owner' = $env:username; + 'originalId' = '1'; #Gehört noch geändert + 'entries' = @{}; + } + ); + } + + $Basket.DataList["PowerShell NoPerfData"].entries.Add( + '0', @{ + 'entry_name' = '0'; + 'entry_value:' = "yes"; + 'format' = 'string'; + 'allowed_roles' = $NULL; + } + ); + $Basket.DataList["PowerShell NoPerfData"].entries.Add( + '1', @{ + 'entry_name' = '1'; + 'entry_value:' = "no"; + 'format' = 'string'; + 'allowed_roles' = $NULL; + } + ); + + # DataList Entries (Default for Verbose) + if ($Basket.DataList.ContainsKey('PowerShell Verbose') -eq $FALSE) { + $Basket.DataList.Add( + 'PowerShell Verbose', @{ + 'list_name' = 'PowerShell Verbose'; + 'owner' = $env:username; + 'originalId' = '50'; #Gehört noch geändert + 'entries' = @{}; + } + ); + } + $Basket.DataList["PowerShell Verbose"].entries.Add( + '0', @{ + 'entry_name' = '0'; + 'entry_value:' = "Show Default"; + 'format' = 'string'; + 'allowed_roles' = $NULL; + } + ); + $Basket.DataList["PowerShell Verbose"].entries.Add( + '1', @{ + 'entry_name' = '1'; + 'entry_value:' = "Show Operator"; + 'format' = 'string'; + 'allowed_roles' = $NULL; + } + ); + $Basket.DataList["PowerShell Verbose"].entries.Add( + '2', @{ + 'entry_name' = '2'; + 'entry_value:' = "Show Problems"; + 'format' = 'string'; + 'allowed_roles' = $NULL; + } + ) + $Basket.DataList["PowerShell Verbose"].entries.Add( + '3', @{ + 'entry_name' = '3'; + 'entry_value:' = "Show All"; + 'format' = 'string'; + 'allowed_roles' = $NULL; + } + ); + + foreach ($check in $CheckName) { - [hashtable]$Basket = @{}; +# [hashtable]$Basket = @{}; - [int]$FieldID = 0; +# [int]$FieldID = 0; $Data = (Get-Help $check) - $Basket.Add( - 'Command', @{ - $Data.Syntax.syntaxItem.Name = @{ + $Basket.Command.Add( + $Data.Syntax.syntaxItem.Name, @{ 'arguments'= @{ '-C' = @{ 'value' = [string]::Format('Use-Icinga; {0}', $Data.Syntax.syntaxItem.Name); @@ -39,11 +118,10 @@ function Invoke-IcingaCheckCommandBasket() 'vars' = @{}; 'zone' = $NULL; } - } ) - $Basket.Add('Datafield', @{}); - $Basket.Add('DataList', @{}); +# $Basket.Add('Datafield', @{}); +# $Basket.Add('DataList', @{}); foreach ($parameter in $Data.Syntax.syntaxItem.parameter) { if ($parameter.name -ne 'core') { @@ -54,7 +132,7 @@ function Invoke-IcingaCheckCommandBasket() [string]$Order = 99 } - $IcingaCustomVariable = [string]::Format('$PowerShell_{0}$', $parameter.Name); + $IcingaCustomVariable = [string]::Format('$PowerShell_{0}_{1}$', $parameter.type.name, $parameter.Name); # Conditional whether type of parameter is switch if ($parameter.type.name -eq 'switch') { @@ -105,12 +183,13 @@ function Invoke-IcingaCheckCommandBasket() } ); - $IcingaCustomVariable = [string]::Format('PowerShell_{0}', $parameter.Name); + $IcingaCustomVariable = [string]::Format('PowerShell_{0}_{1}', $parameter.type.name, $parameter.Name); $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; @@ -119,7 +198,7 @@ function Invoke-IcingaCheckCommandBasket() 'entries' = @{}; } ); - + } } elseif ($parameter.type.name -eq 'Object') { if ($parameter.Name -eq 'Verbose') { $IcingaDataType='Datalist' @@ -132,6 +211,11 @@ function Invoke-IcingaCheckCommandBasket() } $IcingaDataType = [string]::Format('Icinga\Module\Director\DataType\DataType{0}', $IcingaDataType) + if ($Basket.Datafield.Values.varname -eq $IcingaCustomVariable) + { + } + else { + [int]$FieldID = $FieldID + 1; $Basket.Datafield.Add( [string]$FieldID, @{ 'varname' = $IcingaCustomVariable; @@ -143,6 +227,7 @@ function Invoke-IcingaCheckCommandBasket() } ); + if ($parameter.type.name -eq 'switch' -or $parameter.Name -eq 'Verbose') { $Basket.Datafield[[string]$FieldID].Add( 'settings', @{ @@ -162,6 +247,7 @@ function Invoke-IcingaCheckCommandBasket() 'settings', @{} ); } + } [int]$FieldID = $FieldID + 1; } } @@ -171,16 +257,18 @@ function Invoke-IcingaCheckCommandBasket() [int]$FieldID = $FieldID + 1; $Basket.Command[$Data.Syntax.syntaxItem.Name].arguments.Add( '-Verbose', @{ - 'value' = '$PowerShell_Verbose$'; - 'order' = 'o'; + 'value' = '$PowerShell_Object_Verbose$'; + 'order' = '99'; } ); $Basket.Command[$Data.Syntax.syntaxItem.Name].vars.Add( - 'PowerShell_Verbose', "0" + 'PowerShell_Object_Verbose', "0" ); + if ($Basket.Datafield.Values.varname -eq $IcingaCustomVariable) { + } else { $Basket.Datafield.Add( [string]$FieldID, @{ - 'varname' = 'PowerShell_Verbose'; + 'varname' = 'PowerShell_Object_Verbose'; 'caption' = 'Verbose'; 'description' = $NULL; 'datatype' = 'Icinga\Module\Director\DataType\DataTypeDatalist'; @@ -193,6 +281,7 @@ function Invoke-IcingaCheckCommandBasket() } } ); + } } @@ -200,17 +289,19 @@ function Invoke-IcingaCheckCommandBasket() [int]$FieldID = $FieldID + 1; $Basket.Command[$Data.Syntax.syntaxItem.Name].arguments.Add( '-NoPerfData', @{ - 'set_if' = '$PowerShell_NoPerfData$'; + 'set_if' = '$PowerShell_switch_NoPerfData$'; 'set_if_format' = 'string'; 'order' = '99'; } ); $Basket.Command[$Data.Syntax.syntaxItem.Name].vars.Add( - 'PowerShell_NoPerfData', "0" + 'PowerShell_switch_NoPerfData', "0" ); + if ($Basket.Datafield.Values.varname -eq $IcingaCustomVariable) { + } else { $Basket.Datafield.Add( [string]$FieldID, @{ - 'varname' = 'PowerShell_NoPerfData'; + 'varname' = 'PowerShell_switch_NoPerfData'; 'caption' = 'Perf Data'; 'description' = $NULL; 'datatype' = 'Icinga\Module\Director\DataType\DataTypeDatalist'; @@ -223,89 +314,19 @@ function Invoke-IcingaCheckCommandBasket() } } ); - } - # DataList Entries (Default for NoPerfData) - if ($Basket.DataList.ContainsKey('PowerShell NoPerfData') -eq $FALSE) { - $Basket.DataList.Add( - 'PowerShell NoPerfData', @{ - 'list_name' = 'PowerShell NoPerfData'; - 'owner' = $env:username; - 'originalId' = '1'; #Gehört noch geändert - 'entries' = @{}; - } - ); - } - $Basket.DataList["PowerShell NoPerfData"].entries.Add( - '0', @{ - 'entry_name' = '0'; - 'entry_value:' = "yes"; - 'format' = 'string'; - 'allowed_roles' = $NULL; - } - ); - $Basket.DataList["PowerShell NoPerfData"].entries.Add( - '1', @{ - 'entry_name' = '1'; - 'entry_value:' = "no"; - 'format' = 'string'; - 'allowed_roles' = $NULL; - } - ); - - # DataList Entries (Default for Verbose) - if ($Basket.DataList.ContainsKey('PowerShell Verbose') -eq $FALSE) { - $Basket.DataList.Add( - 'PowerShell Verbose', @{ - 'list_name' = 'PowerShell Verbose'; - 'owner' = $env:username; - 'originalId' = '50'; #Gehört noch geändert - 'entries' = @{}; - } - ); } - $Basket.DataList["PowerShell Verbose"].entries.Add( - '0', @{ - 'entry_name' = '0'; - 'entry_value:' = "Show Default"; - 'format' = 'string'; - 'allowed_roles' = $NULL; - } - ); - $Basket.DataList["PowerShell Verbose"].entries.Add( - '1', @{ - 'entry_name' = '1'; - 'entry_value:' = "Show Operator"; - 'format' = 'string'; - 'allowed_roles' = $NULL; - } - ); - $Basket.DataList["PowerShell Verbose"].entries.Add( - '2', @{ - 'entry_name' = '2'; - 'entry_value:' = "Show Problems"; - 'format' = 'string'; - 'allowed_roles' = $NULL; - } - ) - $Basket.DataList["PowerShell Verbose"].entries.Add( - '3', @{ - 'entry_name' = '3'; - 'entry_value:' = "Show All"; - 'format' = 'string'; - 'allowed_roles' = $NULL; - } - ); + } $AllChecks.Add($check, $Basket); } Write-Host $CheckName; Write-Host $CheckName.Count; - if ([string]$CheckName.Count -eq '1') { +# if ([string]$CheckName.Count -eq '1') { $output=ConvertTo-Json -D 100 $Basket > Check.json; - } else { - $output=ConvertTo-Json -D 100 $AllChecks > Check.json; - } +# } else { +# $output=ConvertTo-Json -D 100 $AllChecks > Check.json; +# } return $output; } \ No newline at end of file From 92bc0cc2d14af16c4db9e3ee8ddbd523d33aac9d Mon Sep 17 00:00:00 2001 From: Crited Date: Thu, 12 Sep 2019 09:10:55 +0200 Subject: [PATCH 03/13] Changed DataLists to Array; Added Synopsis, Description, Examples, Parameter, Inputs, Outputs, Link in Comment-Based-Help; Fixed 'self-export'; Changed codestyling; Deleted unnecessary code; Deleted unnecessary comments and added new one as deemed necessary --- .../Invoke-IcingaCheckCommandBasket.psm1 | 368 ++++++++++-------- 1 file changed, 211 insertions(+), 157 deletions(-) diff --git a/lib/plugins/Invoke-IcingaCheckCommandBasket.psm1 b/lib/plugins/Invoke-IcingaCheckCommandBasket.psm1 index 4398190..744b1a2 100644 --- a/lib/plugins/Invoke-IcingaCheckCommandBasket.psm1 +++ b/lib/plugins/Invoke-IcingaCheckCommandBasket.psm1 @@ -1,112 +1,155 @@ +<# +.SYNOPSIS + Exports command as JSON for icinga director + +.DESCRIPTION + Invoke-IcingaCheckCommandBasket returns a JSON-file of one or all 'Invoke-IcingaCheck'-Commands, which can be imported via Icinga-Director + When no single command is specified all commands will be exported, and vice versa. + + More Information on https://github.com/LordHepipud/icinga-module-windows + +.EXAMPLE + PS>Invoke-IcingaCheckCommandBasket + The following commands have been exported: + - 'Invoke-IcingaCheckBiosSerial' + - 'Invoke-IcingaCheckCommandBasket' + - 'Invoke-IcingaCheckCPU' + - 'Invoke-IcingaCheckProcessCount' + - 'Invoke-IcingaCheckService' + - 'Invoke-IcingaCheckUpdates' + - 'Invoke-IcingaCheckUptime' + - 'Invoke-IcingaCheckUsedPartitionSpace' + - 'Invoke-IcingaCheckUsers' + JSON export created to 'C:\Program Files\WindowsPowerShell\Modules\icinga-module-windows\Checks.json' + +.EXAMPLE + PS>Invoke-IcingaCheckCommandBasket Invoke-IcingaCheckCPU + The following commands have been exported: + - 'Invoke-IcingaCheckCPU' + JSON export created to 'C:\Program Files\WindowsPowerShell\Modules\icinga-module-windows\Invoke-IcingaCheckCPU.json' +.PARAMETER CheckName + Used to specify a single command which should be exported. + Has to be a single string. + .INPUTS + System.String + + .OUTPUTS + System.String + System.Object + + .LINK + https://github.com/LordHepipud/icinga-module-windows + + .NOTES +#> + function Invoke-IcingaCheckCommandBasket() { param( $CheckName ); - [hashtable]$AllChecks = @{}; - + # Check whether all Checks will be exported or just the single one specified if ($NULL -eq $CheckName) { $CheckName = (Get-Command Invoke-IcingaCheck*).Name } + # Variable definition and initialization [int]$FieldID = 0; [hashtable]$Basket = @{}; + + # Define basic hashtable structure by adding fields: "Datafield", "DataList", "Command" $Basket.Add('Datafield', @{}); $Basket.Add('DataList', @{}); $Basket.Add('Command', @{}); - # DataList Entries (Default for NoPerfData) - if ($Basket.DataList.ContainsKey('PowerShell NoPerfData') -eq $FALSE) { - $Basket.DataList.Add( - 'PowerShell NoPerfData', @{ - 'list_name' = 'PowerShell NoPerfData'; - 'owner' = $env:username; - 'originalId' = '1'; #Gehört noch geändert - 'entries' = @{}; - } - ); - } - $Basket.DataList["PowerShell NoPerfData"].entries.Add( - '0', @{ - 'entry_name' = '0'; - 'entry_value:' = "yes"; - 'format' = 'string'; - 'allowed_roles' = $NULL; - } - ); - $Basket.DataList["PowerShell NoPerfData"].entries.Add( - '1', @{ - 'entry_name' = '1'; - 'entry_value:' = "no"; - 'format' = 'string'; - 'allowed_roles' = $NULL; - } - ); + # "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 Entries (Default for Verbose) - if ($Basket.DataList.ContainsKey('PowerShell Verbose') -eq $FALSE) { - $Basket.DataList.Add( - 'PowerShell Verbose', @{ - 'list_name' = 'PowerShell Verbose'; - 'owner' = $env:username; - 'originalId' = '50'; #Gehört noch geändert - 'entries' = @{}; - } + # DataList Content for NoPerfData + $Basket.DataList.Add( + 'PowerShell NoPerfData', @{ + 'list_name' = 'PowerShell NoPerfData'; + 'owner' = $env:username; + 'originalId' = '1'; #Gehört noch geändert + 'entries' = @( + @{ + 'entry_name' = '0'; + 'entry_value:' = "yes"; + 'format' = 'string'; + 'allowed_roles' = $NULL; + }, + @{ + 'entry_name' = '1'; + 'entry_value:' = "no"; + 'format' = 'string'; + 'allowed_roles' = $NULL; + } ); } - $Basket.DataList["PowerShell Verbose"].entries.Add( - '0', @{ + ); + } + # "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( + 'PowerShell Verbose', @{ + 'list_name' = 'PowerShell Verbose'; + 'owner' = $env:username; + 'originalId' = '50'; + 'entries' = @( + @{ 'entry_name' = '0'; 'entry_value:' = "Show Default"; 'format' = 'string'; 'allowed_roles' = $NULL; - } - ); - $Basket.DataList["PowerShell Verbose"].entries.Add( - '1', @{ + }, + @{ 'entry_name' = '1'; 'entry_value:' = "Show Operator"; 'format' = 'string'; 'allowed_roles' = $NULL; - } - ); - $Basket.DataList["PowerShell Verbose"].entries.Add( - '2', @{ + }, + @{ 'entry_name' = '2'; 'entry_value:' = "Show Problems"; 'format' = 'string'; 'allowed_roles' = $NULL; - } - ) - $Basket.DataList["PowerShell Verbose"].entries.Add( - '3', @{ + }, + @{ 'entry_name' = '3'; 'entry_value:' = "Show All"; 'format' = 'string'; 'allowed_roles' = $NULL; } ); - + } + ); + } + <# + Loop through either: + $CheckName = (Get-Command Invoke-IcingaCheck*).Name + or one of $CheckName = 'Invoke-IcingaCheckCommand' + #> foreach ($check in $CheckName) { + if ($check -eq 'Invoke-IcingaCheckCommandBasket') { + } else { -# [hashtable]$Basket = @{}; - -# [int]$FieldID = 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, @{ 'arguments'= @{ + # Gets set for every command as Default '-C' = @{ 'value' = [string]::Format('Use-Icinga; {0}', $Data.Syntax.syntaxItem.Name); 'order' = '0'; } } - 'command' = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"; #Gehört noch geändert + 'command' = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"; 'disabled' = $FALSE; 'fields' = @{}; 'imports' = @(); @@ -120,21 +163,21 @@ function Invoke-IcingaCheckCommandBasket() } ) -# $Basket.Add('Datafield', @{}); -# $Basket.Add('DataList', @{}); - + # Loop through Parameter of a given command foreach ($parameter in $Data.Syntax.syntaxItem.parameter) { + # Filter for Parameter 'core', because its set by default if ($parameter.name -ne 'core') { - # Is Numeric Check on position to determine the order value - If (Test-Numeric($parameter.position) -eq $TRUE) { - [string]$Order = [int]$parameter.position + 1 - } else { - [string]$Order = 99 - } - $IcingaCustomVariable = [string]::Format('$PowerShell_{0}_{1}$', $parameter.type.name, $parameter.Name); + # Is Numeric Check on position to determine the order value + If (Test-Numeric($parameter.position) -eq $TRUE) { + [string]$Order = [int]$parameter.position + 1 + } else { + [string]$Order = 99 + } - # Conditional whether type of parameter is switch + $IcingaCustomVariable = [string]::Format('$PowerShell_{0}_{1}$', $parameter.type.name, $parameter.Name); + + #Adding arguments to a given command if ($parameter.type.name -eq 'switch') { $Basket.Command[$Data.Syntax.syntaxItem.Name].arguments.Add( [string]::Format('-{0}', $parameter.Name), @{ @@ -155,26 +198,31 @@ function Invoke-IcingaCheckCommandBasket() } ); } else { - # Default to Object + # Default to Object $Basket.Command[$Data.Syntax.syntaxItem.Name].arguments.Add( [string]::Format('-{0}', $parameter.Name), @{ 'value' = $IcingaCustomVariable; 'order' = $Order; } ); + if ($parameter.name -ne 'Verbose') { $Basket.Command[$Data.Syntax.syntaxItem.Name].vars.Add($parameter.Name, '$$null'); } else { $Basket.Command[$Data.Syntax.syntaxItem.Name].vars.Add($parameter.Name, "0"); } } - # Fields - # Required? - if ($parameter.required -eq $TRUE) { + + # Fields + + # Determine wether a parameter is required based on given syntax-information + if ($parameter.required -eq $TRUE) { $Required = 'y'; - } else { + } else { $Required = 'n'; - } + } + + [int]$FieldID = $FieldID + 1; $Basket.Command[$Data.Syntax.syntaxItem.Name].fields.Add( [string]$FieldID, @{ 'datafield_id' = [int]$FieldID; @@ -190,14 +238,14 @@ function Invoke-IcingaCheckCommandBasket() 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' = @{}; - } - ); + $Basket.DataList.Add( + $DataListName, @{ + 'list_name' = $DataListName; + 'owner' = $env:username; + 'originalId' = '50'; #Gehört noch geändert + 'entries' = @{}; + } + ); } } elseif ($parameter.type.name -eq 'Object') { if ($parameter.Name -eq 'Verbose') { @@ -209,84 +257,79 @@ function Invoke-IcingaCheckCommandBasket() } else { $IcingaDataType='String'; } + $IcingaDataType = [string]::Format('Icinga\Module\Director\DataType\DataType{0}', $IcingaDataType) - if ($Basket.Datafield.Values.varname -eq $IcingaCustomVariable) - { - } - else { - [int]$FieldID = $FieldID + 1; - $Basket.Datafield.Add( - [string]$FieldID, @{ - 'varname' = $IcingaCustomVariable; - 'caption' = $parameter.Name; - 'description' = $NULL; - 'datatype' = $IcingaDataType; - 'format' = $NULL; - 'originalId' = [string]$FieldID; - } - ); - - - if ($parameter.type.name -eq 'switch' -or $parameter.Name -eq 'Verbose') { - $Basket.Datafield[[string]$FieldID].Add( - 'settings', @{ - 'behavior' = 'strict'; - 'datatype' = 'string'; - 'datalist' = $DataListName; - } - ); - } elseif ($parameter.type.name -eq 'Object') { - $Basket.Datafield[[string]$FieldID].Add( - 'settings', @{ - 'visbility' = 'visible'; - } - ); + if ($Basket.Datafield.Values.varname -eq $IcingaCustomVariable) { } else { - $Basket.Datafield[[string]$FieldID].Add( - 'settings', @{} + $Basket.Datafield.Add( + [string]$FieldID, @{ + 'varname' = $IcingaCustomVariable; + 'caption' = $parameter.Name; + 'description' = $NULL; + 'datatype' = $IcingaDataType; + 'format' = $NULL; + 'originalId' = [string]$FieldID; + } ); + + if ($parameter.type.name -eq 'switch' -or $parameter.Name -eq 'Verbose') { + $Basket.Datafield[[string]$FieldID].Add( + 'settings', @{ + 'behavior' = 'strict'; + 'datatype' = 'string'; + 'datalist' = $DataListName; + } + ); + } elseif ($parameter.type.name -eq 'Object') { + $Basket.Datafield[[string]$FieldID].Add( + 'settings', @{ + 'visbility' = 'visible'; + } + ); + } else { + $Basket.Datafield[[string]$FieldID].Add( + 'settings', @{} + ); + } } } - [int]$FieldID = $FieldID + 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) { - [int]$FieldID = $FieldID + 1; $Basket.Command[$Data.Syntax.syntaxItem.Name].arguments.Add( '-Verbose', @{ 'value' = '$PowerShell_Object_Verbose$'; 'order' = '99'; } ); + $Basket.Command[$Data.Syntax.syntaxItem.Name].vars.Add( 'PowerShell_Object_Verbose', "0" ); + if ($Basket.Datafield.Values.varname -eq $IcingaCustomVariable) { } else { - $Basket.Datafield.Add( - [string]$FieldID, @{ - 'varname' = 'PowerShell_Object_Verbose'; - 'caption' = 'Verbose'; - 'description' = $NULL; - 'datatype' = 'Icinga\Module\Director\DataType\DataTypeDatalist'; - 'format' = $NULL; - 'originalId' = [string]$FieldID; - 'settings' = @{ - 'behavior' = 'strict'; - 'data_type' = 'string'; - 'datalist' = 'PowerShell Verbose' + $Basket.Datafield.Add( + [string]$FieldID, @{ + 'varname' = 'PowerShell_Object_Verbose'; + 'caption' = 'Verbose'; + 'description' = $NULL; + 'datatype' = 'Icinga\Module\Director\DataType\DataTypeDatalist'; + 'format' = $NULL; + 'originalId' = [string]$FieldID; + 'settings' = @{ + 'behavior' = 'strict'; + 'data_type' = 'string'; + 'datalist' = 'PowerShell Verbose' + } } - } - ); + ); } } - if ($Basket.Command[$Data.Syntax.syntaxItem.Name].arguments.ContainsKey('-NoPerfData') -eq $FALSE) { - [int]$FieldID = $FieldID + 1; $Basket.Command[$Data.Syntax.syntaxItem.Name].arguments.Add( '-NoPerfData', @{ 'set_if' = '$PowerShell_switch_NoPerfData$'; @@ -297,36 +340,47 @@ function Invoke-IcingaCheckCommandBasket() $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' = $NULL; - 'datatype' = 'Icinga\Module\Director\DataType\DataTypeDatalist'; - 'format' = $NULL; - 'originalId' = [string]$FieldID; - 'settings' = @{ - 'behavior' = 'strict'; - 'data_type' = 'string'; - 'datalist' = 'PowerShell NoPerfData' + $Basket.Datafield.Add( + [string]$FieldID, @{ + 'varname' = 'PowerShell_switch_NoPerfData'; + 'caption' = 'Perf Data'; + 'description' = $NULL; + 'datatype' = 'Icinga\Module\Director\DataType\DataTypeDatalist'; + 'format' = $NULL; + 'originalId' = [string]$FieldID; + 'settings' = @{ + 'behavior' = 'strict'; + 'data_type' = 'string'; + 'datalist' = 'PowerShell NoPerfData' + } } - } - ); + ); } } - $AllChecks.Add($check, $Basket); + } } -Write-Host $CheckName; -Write-Host $CheckName.Count; + if ($CheckName.Count -eq 1) { + $FileName = "${CheckName}.json"; + } else { + $FileName = "Checks.json"; + } -# if ([string]$CheckName.Count -eq '1') { - $output=ConvertTo-Json -D 100 $Basket > Check.json; -# } else { -# $output=ConvertTo-Json -D 100 $AllChecks > Check.json; -# } + $output=ConvertTo-Json -D 100 $Basket > "$FileName"; + + $FilePath = (Get-Location).Path + + # Output-Text + Write-Host "The following commands have been exported:" + foreach ($check in $CheckName) { + if ($check -ne "Invoke-IcingaCheckCommandBasket") { + Write-Host "- '$check'" + } + } + Write-Host "JSON export created in '${FilePath}\${FileName}'" return $output; } \ No newline at end of file From 36093f302a593a00fd6256f89c692501550b253c Mon Sep 17 00:00:00 2001 From: Crited Date: Fri, 13 Sep 2019 08:18:31 +0200 Subject: [PATCH 04/13] Probably fixed ID issues --- .../Invoke-IcingaCheckCommandBasket.psm1 | 62 +++++++++++++++---- 1 file changed, 51 insertions(+), 11 deletions(-) diff --git a/lib/plugins/Invoke-IcingaCheckCommandBasket.psm1 b/lib/plugins/Invoke-IcingaCheckCommandBasket.psm1 index 744b1a2..cb2818a 100644 --- a/lib/plugins/Invoke-IcingaCheckCommandBasket.psm1 +++ b/lib/plugins/Invoke-IcingaCheckCommandBasket.psm1 @@ -56,6 +56,7 @@ function Invoke-IcingaCheckCommandBasket() # Variable definition and initialization [int]$FieldID = 0; +# [int]$FieldNumeration = 0; [hashtable]$Basket = @{}; # Define basic hashtable structure by adding fields: "Datafield", "DataList", "Command" @@ -96,7 +97,7 @@ function Invoke-IcingaCheckCommandBasket() 'PowerShell Verbose', @{ 'list_name' = 'PowerShell Verbose'; 'owner' = $env:username; - 'originalId' = '50'; + 'originalId' = '2'; 'entries' = @( @{ 'entry_name' = '0'; @@ -133,6 +134,7 @@ function Invoke-IcingaCheckCommandBasket() or one of $CheckName = 'Invoke-IcingaCheckCommand' #> foreach ($check in $CheckName) { + [int]$FieldNumeration = 0; if ($check -eq 'Invoke-IcingaCheckCommandBasket') { } else { @@ -222,15 +224,6 @@ function Invoke-IcingaCheckCommandBasket() $Required = 'n'; } - [int]$FieldID = $FieldID + 1; - $Basket.Command[$Data.Syntax.syntaxItem.Name].fields.Add( - [string]$FieldID, @{ - 'datafield_id' = [int]$FieldID; - 'is_required' = $Required; - 'var_filter' = $NULL; - } - ); - $IcingaCustomVariable = [string]::Format('PowerShell_{0}_{1}', $parameter.type.name, $parameter.Name); $DataListName = [string]::Format('PowerShell {0}', $parameter.Name) @@ -289,11 +282,16 @@ function Invoke-IcingaCheckCommandBasket() ); } else { $Basket.Datafield[[string]$FieldID].Add( - 'settings', @{} + 'settings', @{ + 'visbility' = 'visible'; + } ); } + [int]$FieldID = [int]$FieldID + 1; } } + +[int]$FieldNumeration = [int]$FieldNumeration + 1; } # Check whether or not noperfdata and verbose is set and add it if necessary @@ -362,6 +360,48 @@ function Invoke-IcingaCheckCommandBasket() } } } + foreach ($check in $CheckName) { + [int]$FieldNumeration = 0; + if ($check -eq 'Invoke-IcingaCheckCommandBasket') { + } else { + + $Data = (Get-Help $check) + + foreach ($parameter in $Data.Syntax.syntaxItem.parameter){ + $IcingaCustomVariable = [string]::Format('PowerShell_{0}_{1}', $parameter.type.name, $parameter.Name); + + + [hashtable]$translationdatafield = @{} + foreach ($DID in $Basket.Datafield.Keys) + { + + $translationdatafield.Add($Basket.Datafield.$DID.varname, $DID); + } + +# $translationdatafield.Add() + foreach($key in $translationdatafield.Keys) + { + if ([string]$IcingaCustomVariable -eq [string]$key) + { + $otherID = $translationdatafield[$IcingaCustomVariable]; + } else {} + } + # Get Necessary Syntax-Information and more through cmdlet "Get-Help" + Write-Host $Data.Syntax.syntaxItem.Name + Write-Host $Parameter.Name + # [int]$FieldID = [int]$FieldID + 1; + $Basket.Command[$Data.Syntax.syntaxItem.Name].fields.Add( + [string]$FieldNumeration, @{ + 'datafield_id' = [int]$otherID; + 'is_required' = $Required; + 'var_filter' = $NULL; + } + ); + + [int]$FieldNumeration = [int]$FieldNumeration + 1; + } + } +} if ($CheckName.Count -eq 1) { $FileName = "${CheckName}.json"; From 61c23e49b9de9652e4f30414fe405b1fe54b23e1 Mon Sep 17 00:00:00 2001 From: Crited Date: Fri, 13 Sep 2019 09:22:27 +0200 Subject: [PATCH 05/13] Presumably finished Invoke-IcingaCheckCommandBasket Module --- .../Invoke-IcingaCheckCommandBasket.psm1 | 79 ++++++++++++++++++- 1 file changed, 75 insertions(+), 4 deletions(-) diff --git a/lib/plugins/Invoke-IcingaCheckCommandBasket.psm1 b/lib/plugins/Invoke-IcingaCheckCommandBasket.psm1 index cb2818a..70b0cae 100644 --- a/lib/plugins/Invoke-IcingaCheckCommandBasket.psm1 +++ b/lib/plugins/Invoke-IcingaCheckCommandBasket.psm1 @@ -8,6 +8,10 @@ More Information on https://github.com/LordHepipud/icinga-module-windows +.FUNCTIONALITY + This module is intended to be used to export one or all PowerShell-Modules with the namespace 'Invoke-IcingaCheck'. + The JSON-Export, which will be egenerated through this module is structured like an Icinga-Director-JSON-Export, so it can be imported via the Icinga-Director the same way. + .EXAMPLE PS>Invoke-IcingaCheckCommandBasket The following commands have been exported: @@ -32,6 +36,8 @@ Has to be a single string. .INPUTS System.String + Oder: + None. You cannot pipe objects to Add-Extension. .OUTPUTS System.String @@ -55,7 +61,7 @@ function Invoke-IcingaCheckCommandBasket() } # Variable definition and initialization - [int]$FieldID = 0; + [int]$FieldID = 3; # [int]$FieldNumeration = 0; [hashtable]$Basket = @{}; @@ -251,6 +257,56 @@ function Invoke-IcingaCheckCommandBasket() $IcingaDataType='String'; } + if($Basket.Datafield.ContainsKey('0') -eq $FALSE){ + $Basket.Datafield.Add( + '0', @{ + 'varname' = 'PowerShell_switch_NoPerfData'; + 'caption' = 'NoPerfData'; + 'description' = $NULL; + 'datatype' = 'Icinga\\Module\\Director\\DataType\\DataTypeDatalist'; + 'format' = $NULL; + 'originalId' = '0'; + 'settings' = @{ + 'datalist' = 'PowerShell NoPerfData'; + 'datatype' = 'string'; + 'behavior' = 'strict'; + } + } + ) + } + + if($Basket.Datafield.ContainsKey('1') -eq $FALSE){ + $Basket.Datafield.Add( + '1', @{ + 'varname' = 'PowerShell_switch_NoPerfData'; + 'caption' = 'Verbose'; + 'description' = $NULL; + 'datatype' = 'Icinga\\Module\\Director\\DataType\\DataTypeString'; + 'format' = $NULL; + 'originalId' = '1'; + 'settings' = @{ + 'datalist' = 'PowerShell Verbose'; + 'datatype' = 'string'; + 'behavior' = 'strict'; + } + } + ) + } + + if($Basket.Datafield.ContainsKey('2') -eq $FALSE){ + $Basket.Datafield.Add( + '2', @{ + 'varname' = 'Basket_Check_Variable'; + 'caption' = 'Basket_Check'; + 'description' = $NULL; + 'datatype' = 'Icinga\\Module\\Director\\DataType\\DataTypeArray'; + 'format' = $NULL; + 'originalId' = '2'; + 'settings' = @{}; + } + ) + } + $IcingaDataType = [string]::Format('Icinga\Module\Director\DataType\DataType{0}', $IcingaDataType) if ($Basket.Datafield.Values.varname -eq $IcingaCustomVariable) { @@ -361,7 +417,7 @@ function Invoke-IcingaCheckCommandBasket() } } foreach ($check in $CheckName) { - [int]$FieldNumeration = 0; + [int]$FieldNumeration = 1; if ($check -eq 'Invoke-IcingaCheckCommandBasket') { } else { @@ -370,12 +426,26 @@ function Invoke-IcingaCheckCommandBasket() foreach ($parameter in $Data.Syntax.syntaxItem.parameter){ $IcingaCustomVariable = [string]::Format('PowerShell_{0}_{1}', $parameter.type.name, $parameter.Name); + if ($Basket.Command[$Data.Syntax.syntaxItem.Name].fields.ContainsKey('0') -eq $FALSE){ + $Basket.Command[$Data.Syntax.syntaxItem.Name].fields.Add( + '0', @{ + 'datafield_id' = '2'; + 'is_required' = 'n'; + 'var_filter' = $NULL; + } + ); + } + + [hashtable]$translationdatafield = @{} foreach ($DID in $Basket.Datafield.Keys) { + if ($translationdatafield.Contains('PowerShell_switch_NoPerfData') -eq $TRUE){ + }else{ $translationdatafield.Add($Basket.Datafield.$DID.varname, $DID); + } } # $translationdatafield.Add() @@ -387,9 +457,10 @@ function Invoke-IcingaCheckCommandBasket() } else {} } # Get Necessary Syntax-Information and more through cmdlet "Get-Help" - Write-Host $Data.Syntax.syntaxItem.Name - Write-Host $Parameter.Name +# Write-Host $Data.Syntax.syntaxItem.Name +# Write-Host $Parameter.Name # [int]$FieldID = [int]$FieldID + 1; + $Basket.Command[$Data.Syntax.syntaxItem.Name].fields.Add( [string]$FieldNumeration, @{ 'datafield_id' = [int]$otherID; From e33b593ab053dd47440e7a056b52fd00f8ed87c8 Mon Sep 17 00:00:00 2001 From: Crited Date: Fri, 13 Sep 2019 11:44:37 +0200 Subject: [PATCH 06/13] Renamed Module; Code-Styling; Added Timestamp; Rewrote Commentbased Help; This is the supposedly last version that works (has worked) --- lib/plugins/Get-IcingaCheckCommandConfig.psm1 | 492 +++++++++++++++++ .../Invoke-IcingaCheckCommandBasket.psm1 | 497 ------------------ 2 files changed, 492 insertions(+), 497 deletions(-) create mode 100644 lib/plugins/Get-IcingaCheckCommandConfig.psm1 delete mode 100644 lib/plugins/Invoke-IcingaCheckCommandBasket.psm1 diff --git a/lib/plugins/Get-IcingaCheckCommandConfig.psm1 b/lib/plugins/Get-IcingaCheckCommandConfig.psm1 new file mode 100644 index 0000000..3b1f42a --- /dev/null +++ b/lib/plugins/Get-IcingaCheckCommandConfig.psm1 @@ -0,0 +1,492 @@ +<# +.SYNOPSIS + Exports command as JSON for icinga director + +.DESCRIPTION + Get-IcingaCheckCommandConfig returns a JSON-file of one or all 'Invoke-IcingaCheck'-Commands, which can be imported via Icinga-Director + When no single command is specified all commands will be exported, and vice versa. + + More Information on https://github.com/LordHepipud/icinga-module-windows + +.FUNCTIONALITY + This module is intended to be used to export one or all PowerShell-Modules with the namespace 'Invoke-IcingaCheck'. + The JSON-Export, which will be egenerated through this module is structured like an Icinga-Director-JSON-Export, so it can be imported via the Icinga-Director the same way. + +.EXAMPLE + PS>Get-IcingaCheckCommandConfig + Check Command JSON for the following commands: + - 'Invoke-IcingaCheckBiosSerial' + - 'Invoke-IcingaCheckCPU' + - 'Invoke-IcingaCheckProcessCount' + - 'Invoke-IcingaCheckService' + - 'Invoke-IcingaCheckUpdates' + - 'Invoke-IcingaCheckUptime' + - 'Invoke-IcingaCheckUsedPartitionSpace' + - 'Invoke-IcingaCheckUsers' +############################################################ + + +.EXAMPLE + Get-IcingaCheckCommandConfig -OutFile 'C:\Users\icinga\config-exports' + The following commands have been exported: + - 'Invoke-IcingaCheckBiosSerial' + - 'Invoke-IcingaCheckCPU' + - 'Invoke-IcingaCheckProcessCount' + - 'Invoke-IcingaCheckService' + - 'Invoke-IcingaCheckUpdates' + - 'Invoke-IcingaCheckUptime' + - 'Invoke-IcingaCheckUsedPartitionSpace' + - 'Invoke-IcingaCheckUsers' + JSON export created in 'C:\Users\icinga\config-exports\PowerShell_CheckCommands_09-13-2019-10-55-1989.json' + +.EXAMPLE + Get-IcingaCheckCommandConfig Invoke-IcingaCheckBiosSerial, Invoke-IcingaCheckCPU -OutFile 'C:\Users\icinga\config-exports' + The following commands have been exported: + - 'Invoke-IcingaCheckBiosSerial' + - 'Invoke-IcingaCheckCPU' + JSON export created in 'C:\Users\icinga\config-exports\PowerShell_CheckCommands_09-13-2019-10-58-5342.json' + +.PARAMETER CheckName + Used to specify an array of commands which should be exported. + Seperated with ',' + + .INPUTS + System.Array + + .OUTPUTS + System.String + + .LINK + https://github.com/LordHepipud/icinga-module-windows + + .NOTES +#> + +function Get-IcingaCheckCommandConfig() +{ + param( + [Parameter(ValueFromPipeline)] + [array]$CheckName, + [string]$OutFile + ); + + # Check whether all Checks will be exported or just the ones specified + if ([string]::IsNullOrEmpty($CheckName) -eq $true) { + $CheckName = (Get-Command Invoke-IcingaCheck*).Name + } + + [int]$FieldID = 2; # Starts at '2', because '0' and '1' are reserved for 'Verbose' and 'NoPerfData' + [hashtable]$Basket = @{}; + + # Define basic hashtable structure by adding fields: "Datafield", "DataList", "Command" + $Basket.Add('Datafield', @{}); + $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( + 'PowerShell Verbose', @{ + 'list_name' = 'PowerShell Verbose'; + 'owner' = $env:username; + 'originalId' = '2'; + 'entries' = @( + @{ + 'entry_name' = '0'; + 'entry_value:' = "Show Default"; + 'format' = 'string'; + 'allowed_roles' = $NULL; + }, + @{ + 'entry_name' = '1'; + 'entry_value:' = "Show Operator"; + 'format' = 'string'; + 'allowed_roles' = $NULL; + }, + @{ + 'entry_name' = '2'; + 'entry_value:' = "Show Problems"; + 'format' = 'string'; + 'allowed_roles' = $NULL; + }, + @{ + 'entry_name' = '3'; + 'entry_value:' = "Show All"; + 'format' = 'string'; + 'allowed_roles' = $NULL; + } + ); + } + ); + } + + # 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, @{ + 'arguments'= @{ + # Gets set for every command as default + '-C' = @{ + 'value' = [string]::Format('Use-Icinga; {0}', $Data.Syntax.syntaxItem.Name); + 'order' = '0'; + } + } + 'command' = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"; + 'disabled' = $FALSE; + 'fields' = @{}; + 'imports' = @(); + 'is_string' = $NULL; + 'methods_excute' = 'PluginCheck'; + 'object_name' = $Data.Syntax.syntaxItem.Name; + 'object_type' = 'object'; + 'timeout' = '180'; + 'vars' = @{}; + 'zone' = $NULL; + } + ); + + # Loop through parameters of a given command + foreach ($parameter in $Data.Syntax.syntaxItem.parameter) { + + # Filter for Parameter 'core', because its set by default + if ($parameter.name -ne 'core') { + + # IsNumeric-Check on position to determine the order-value + If (Test-Numeric($parameter.position) -eq $TRUE) { + [string]$Order = [int]$parameter.position + 1; + } else { + [string]$Order = 99 + } + + $IcingaCustomVariable = [string]::Format('$PowerShell_{0}_{1}$', $parameter.type.name, $parameter.Name); + + # Add arguments to a given command + if ($parameter.type.name -eq 'switch') { + $Basket.Command[$Data.Syntax.syntaxItem.Name].arguments.Add( + [string]::Format('-{0}', $parameter.Name), @{ + 'set_if' = $IcingaCustomVariable; + 'set_if_format' = 'string'; + 'order' = $Order; + } + ); + + $Basket.Command[$Data.Syntax.syntaxItem.Name].vars.Add($parameter.Name, "0"); + + # Conditional whether type of parameter is array + } elseif ($parameter.type.name -eq 'array') { + $Basket.Command[$Data.Syntax.syntaxItem.Name].arguments.Add( + [string]::Format('-{0}', $parameter.Name), @{ + 'value' = [string]::Format('(Split-IcingaCheckCommandArgs {0})', $IcingaCustomVariable); + 'order' = $Order; + } + ); + } else { + # Default to Object + $Basket.Command[$Data.Syntax.syntaxItem.Name].arguments.Add( + [string]::Format('-{0}', $parameter.Name), @{ + 'value' = $IcingaCustomVariable; + 'order' = $Order; + } + ); + + if ($parameter.name -ne 'Verbose') { + $Basket.Command[$Data.Syntax.syntaxItem.Name].vars.Add($parameter.Name, '$$null'); + } else { + $Basket.Command[$Data.Syntax.syntaxItem.Name].vars.Add($parameter.Name, "0"); + } + } + + # Determine wether a parameter is required based on given syntax-information + if ($parameter.required -eq $TRUE) { + $Required = 'y'; + } else { + $Required = 'n'; + } + + $IcingaCustomVariable = [string]::Format('PowerShell_{0}_{1}', $parameter.type.name, $parameter.Name); + + $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' = @{}; + } + ); + } + } elseif ($parameter.type.name -eq 'Object') { + if ($parameter.Name -eq 'Verbose') { + $IcingaDataType='Datalist' + } + + $IcingaDataType='String'; + + } elseif ($parameter.type.name -eq 'Array') { + $IcingaDataType='Array'; + } else { + $IcingaDataType='String'; + } + + if($Basket.Datafield.ContainsKey('0') -eq $FALSE){ + $Basket.Datafield.Add( + '0', @{ + 'varname' = 'PowerShell_switch_NoPerfData'; + 'caption' = 'NoPerfData'; + 'description' = $NULL; + 'datatype' = 'Icinga\\Module\\Director\\DataType\\DataTypeDatalist'; + 'format' = $NULL; + 'originalId' = '0'; + 'settings' = @{ + 'datalist' = 'PowerShell NoPerfData'; + 'datatype' = 'string'; + 'behavior' = 'strict'; + } + } + ); + } + + if($Basket.Datafield.ContainsKey('1') -eq $FALSE){ + $Basket.Datafield.Add( + '1', @{ + 'varname' = 'PowerShell_switch_NoPerfData'; + 'caption' = 'Verbose'; + 'description' = $NULL; + 'datatype' = 'Icinga\\Module\\Director\\DataType\\DataTypeString'; + 'format' = $NULL; + 'originalId' = '1'; + 'settings' = @{ + 'datalist' = 'PowerShell Verbose'; + 'datatype' = 'string'; + 'behavior' = 'strict'; + } + } + ); + } + + $IcingaDataType = [string]::Format('Icinga\Module\Director\DataType\DataType{0}', $IcingaDataType) + + if ($Basket.Datafield.Values.varname -eq $IcingaCustomVariable) { + } else { + $Basket.Datafield.Add( + [string]$FieldID, @{ + 'varname' = $IcingaCustomVariable; + 'caption' = $parameter.Name; + 'description' = $NULL; + 'datatype' = $IcingaDataType; + 'format' = $NULL; + 'originalId' = [string]$FieldID; + } + ); + + if ($parameter.type.name -eq 'switch' -or $parameter.Name -eq 'Verbose') { + $Basket.Datafield[[string]$FieldID].Add( + 'settings', @{ + 'behavior' = 'strict'; + 'datatype' = 'string'; + 'datalist' = $DataListName; + } + ); + } elseif ($parameter.type.name -eq 'Object') { + $Basket.Datafield[[string]$FieldID].Add( + 'settings', @{ + 'visbility' = 'visible'; + } + ); + } else { + $Basket.Datafield[[string]$FieldID].Add( + 'settings', @{ + 'visbility' = 'visible'; + } + ); + } + + # Increment FieldID, so unique datafields are added. + [int]$FieldID = [int]$FieldID + 1; + } + } + + # Increment FieldNumeation, 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( + '-Verbose', @{ + 'value' = '$PowerShell_Object_Verbose$'; + 'order' = '99'; + } + ); + + $Basket.Command[$Data.Syntax.syntaxItem.Name].vars.Add( + 'PowerShell_Object_Verbose', "0" + ); + + if ($Basket.Datafield.Values.varname -eq $IcingaCustomVariable) { + } else { + $Basket.Datafield.Add( + [string]$FieldID, @{ + 'varname' = 'PowerShell_Object_Verbose'; + 'caption' = 'Verbose'; + 'description' = $NULL; + 'datatype' = 'Icinga\Module\Director\DataType\DataTypeDatalist'; + 'format' = $NULL; + 'originalId' = [string]$FieldID; + 'settings' = @{ + 'behavior' = 'strict'; + 'data_type' = 'string'; + 'datalist' = 'PowerShell Verbose' + } + } + ); + } + } + + if ($Basket.Command[$Data.Syntax.syntaxItem.Name].arguments.ContainsKey('-NoPerfData') -eq $FALSE) { + $Basket.Command[$Data.Syntax.syntaxItem.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' = $NULL; + 'datatype' = 'Icinga\Module\Director\DataType\DataTypeDatalist'; + 'format' = $NULL; + 'originalId' = [string]$FieldID; + 'settings' = @{ + 'behavior' = 'strict'; + 'data_type' = 'string'; + 'datalist' = 'PowerShell NoPerfData' + } + } + ); + } + } + } + + foreach ($check in $CheckName) { + [int]$FieldNumeration = 0; + + $Data = (Get-Help $check) + + foreach ($parameter in $Data.Syntax.syntaxItem.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); + } + } + + 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; + 'is_required' = $Required; + 'var_filter' = $NULL; + } + ); + + [int]$FieldNumeration = [int]$FieldNumeration + 1; + } + } + + # Build Filename with given Timestamp + $TimeStamp = (Get-Date -Format "MM-dd-yyyy-HH-mm-ffff"); + $FileName = "PowerShell_CheckCommands_$TimeStamp.json"; + + # Generate JSON Output from Hashtable + $output = ConvertTo-Json -Depth 100 $Basket -Compress; + + # Determine whether json output via powershell or in file (based on param -OutFile) + if ([string]::IsNullOrEmpty($OutFile) -eq $false) { + $OutFile = (Join-Path -Path $OutFile -ChildPath $FileName); + if ((Test-Path($OutFile)) -eq $false) { + New-Item -Path $OutFile -Force | Out-Null; + } + + if ((Test-Path($OutFile)) -eq $false) { + throw 'Failed to create specified directory. Please try again or use a different target location.'; + } + + Set-Content -Path $OutFile -Value $output; + + # Output-Text + Write-Host "The following commands have been exported:" + foreach ($check in $CheckName) { + Write-Host "- '$check'"; + } + Write-Host "JSON export created in '${OutFile}'" + return; + } + + Write-Host "Check Command JSON for the following commands:" + foreach ($check in $CheckName) { + Write-Host "- '$check'" + } + Write-Host '############################################################'; + + return $output; +} diff --git a/lib/plugins/Invoke-IcingaCheckCommandBasket.psm1 b/lib/plugins/Invoke-IcingaCheckCommandBasket.psm1 deleted file mode 100644 index 70b0cae..0000000 --- a/lib/plugins/Invoke-IcingaCheckCommandBasket.psm1 +++ /dev/null @@ -1,497 +0,0 @@ -<# -.SYNOPSIS - Exports command as JSON for icinga director - -.DESCRIPTION - Invoke-IcingaCheckCommandBasket returns a JSON-file of one or all 'Invoke-IcingaCheck'-Commands, which can be imported via Icinga-Director - When no single command is specified all commands will be exported, and vice versa. - - More Information on https://github.com/LordHepipud/icinga-module-windows - -.FUNCTIONALITY - This module is intended to be used to export one or all PowerShell-Modules with the namespace 'Invoke-IcingaCheck'. - The JSON-Export, which will be egenerated through this module is structured like an Icinga-Director-JSON-Export, so it can be imported via the Icinga-Director the same way. - -.EXAMPLE - PS>Invoke-IcingaCheckCommandBasket - The following commands have been exported: - - 'Invoke-IcingaCheckBiosSerial' - - 'Invoke-IcingaCheckCommandBasket' - - 'Invoke-IcingaCheckCPU' - - 'Invoke-IcingaCheckProcessCount' - - 'Invoke-IcingaCheckService' - - 'Invoke-IcingaCheckUpdates' - - 'Invoke-IcingaCheckUptime' - - 'Invoke-IcingaCheckUsedPartitionSpace' - - 'Invoke-IcingaCheckUsers' - JSON export created to 'C:\Program Files\WindowsPowerShell\Modules\icinga-module-windows\Checks.json' - -.EXAMPLE - PS>Invoke-IcingaCheckCommandBasket Invoke-IcingaCheckCPU - The following commands have been exported: - - 'Invoke-IcingaCheckCPU' - JSON export created to 'C:\Program Files\WindowsPowerShell\Modules\icinga-module-windows\Invoke-IcingaCheckCPU.json' -.PARAMETER CheckName - Used to specify a single command which should be exported. - Has to be a single string. - .INPUTS - System.String - Oder: - None. You cannot pipe objects to Add-Extension. - - .OUTPUTS - System.String - System.Object - - .LINK - https://github.com/LordHepipud/icinga-module-windows - - .NOTES -#> - -function Invoke-IcingaCheckCommandBasket() -{ - param( - $CheckName - ); - - # Check whether all Checks will be exported or just the single one specified - if ($NULL -eq $CheckName) { - $CheckName = (Get-Command Invoke-IcingaCheck*).Name - } - - # Variable definition and initialization - [int]$FieldID = 3; -# [int]$FieldNumeration = 0; - [hashtable]$Basket = @{}; - - # Define basic hashtable structure by adding fields: "Datafield", "DataList", "Command" - $Basket.Add('Datafield', @{}); - $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'; #Gehört noch geändert - '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( - 'PowerShell Verbose', @{ - 'list_name' = 'PowerShell Verbose'; - 'owner' = $env:username; - 'originalId' = '2'; - 'entries' = @( - @{ - 'entry_name' = '0'; - 'entry_value:' = "Show Default"; - 'format' = 'string'; - 'allowed_roles' = $NULL; - }, - @{ - 'entry_name' = '1'; - 'entry_value:' = "Show Operator"; - 'format' = 'string'; - 'allowed_roles' = $NULL; - }, - @{ - 'entry_name' = '2'; - 'entry_value:' = "Show Problems"; - 'format' = 'string'; - 'allowed_roles' = $NULL; - }, - @{ - 'entry_name' = '3'; - 'entry_value:' = "Show All"; - 'format' = 'string'; - 'allowed_roles' = $NULL; - } - ); - } - ); - } - - <# - Loop through either: - $CheckName = (Get-Command Invoke-IcingaCheck*).Name - or one of $CheckName = 'Invoke-IcingaCheckCommand' - #> - foreach ($check in $CheckName) { - [int]$FieldNumeration = 0; - if ($check -eq 'Invoke-IcingaCheckCommandBasket') { - } else { - - # Get Necessary Syntax-Information and more through cmdlet "Get-Help" - $Data = (Get-Help $check) - - # Add Command Structure - $Basket.Command.Add( - $Data.Syntax.syntaxItem.Name, @{ - 'arguments'= @{ - # Gets set for every command as Default - '-C' = @{ - 'value' = [string]::Format('Use-Icinga; {0}', $Data.Syntax.syntaxItem.Name); - 'order' = '0'; - } - } - 'command' = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"; - 'disabled' = $FALSE; - 'fields' = @{}; - 'imports' = @(); - 'is_string' = $NULL; - 'methods_excute' = 'PluginCheck'; - 'object_name' = $Data.Syntax.syntaxItem.Name; - 'object_type' = 'object'; - 'timeout' = '180'; - 'vars' = @{}; - 'zone' = $NULL; - } - ) - - # Loop through Parameter of a given command - foreach ($parameter in $Data.Syntax.syntaxItem.parameter) { - # Filter for Parameter 'core', because its set by default - if ($parameter.name -ne 'core') { - - # Is Numeric Check on position to determine the order value - If (Test-Numeric($parameter.position) -eq $TRUE) { - [string]$Order = [int]$parameter.position + 1 - } else { - [string]$Order = 99 - } - - $IcingaCustomVariable = [string]::Format('$PowerShell_{0}_{1}$', $parameter.type.name, $parameter.Name); - - #Adding arguments to a given command - if ($parameter.type.name -eq 'switch') { - $Basket.Command[$Data.Syntax.syntaxItem.Name].arguments.Add( - [string]::Format('-{0}', $parameter.Name), @{ - 'set_if' = $IcingaCustomVariable; - 'set_if_format' = 'string'; - 'order' = $Order; - } - ); - $Basket.Command[$Data.Syntax.syntaxItem.Name].vars.Add( - $parameter.Name, "0" - ); - # Condotional whether type of parameter is array - } elseif ($parameter.type.name -eq 'array') { - $Basket.Command[$Data.Syntax.syntaxItem.Name].arguments.Add( - [string]::Format('-{0}', $parameter.Name), @{ - 'value' = [string]::Format('(Split-IcingaCheckCommandArgs {0})', $IcingaCustomVariable); - 'order' = $Order; - } - ); - } else { - # Default to Object - $Basket.Command[$Data.Syntax.syntaxItem.Name].arguments.Add( - [string]::Format('-{0}', $parameter.Name), @{ - 'value' = $IcingaCustomVariable; - 'order' = $Order; - } - ); - - if ($parameter.name -ne 'Verbose') { - $Basket.Command[$Data.Syntax.syntaxItem.Name].vars.Add($parameter.Name, '$$null'); - } else { - $Basket.Command[$Data.Syntax.syntaxItem.Name].vars.Add($parameter.Name, "0"); - } - } - - # Fields - - # Determine wether a parameter is required based on given syntax-information - if ($parameter.required -eq $TRUE) { - $Required = 'y'; - } else { - $Required = 'n'; - } - - $IcingaCustomVariable = [string]::Format('PowerShell_{0}_{1}', $parameter.type.name, $parameter.Name); - - $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' = @{}; - } - ); - } - } elseif ($parameter.type.name -eq 'Object') { - if ($parameter.Name -eq 'Verbose') { - $IcingaDataType='Datalist' - } - $IcingaDataType='String'; - } elseif ($parameter.type.name -eq 'Array') { - $IcingaDataType='Array'; - } else { - $IcingaDataType='String'; - } - - if($Basket.Datafield.ContainsKey('0') -eq $FALSE){ - $Basket.Datafield.Add( - '0', @{ - 'varname' = 'PowerShell_switch_NoPerfData'; - 'caption' = 'NoPerfData'; - 'description' = $NULL; - 'datatype' = 'Icinga\\Module\\Director\\DataType\\DataTypeDatalist'; - 'format' = $NULL; - 'originalId' = '0'; - 'settings' = @{ - 'datalist' = 'PowerShell NoPerfData'; - 'datatype' = 'string'; - 'behavior' = 'strict'; - } - } - ) - } - - if($Basket.Datafield.ContainsKey('1') -eq $FALSE){ - $Basket.Datafield.Add( - '1', @{ - 'varname' = 'PowerShell_switch_NoPerfData'; - 'caption' = 'Verbose'; - 'description' = $NULL; - 'datatype' = 'Icinga\\Module\\Director\\DataType\\DataTypeString'; - 'format' = $NULL; - 'originalId' = '1'; - 'settings' = @{ - 'datalist' = 'PowerShell Verbose'; - 'datatype' = 'string'; - 'behavior' = 'strict'; - } - } - ) - } - - if($Basket.Datafield.ContainsKey('2') -eq $FALSE){ - $Basket.Datafield.Add( - '2', @{ - 'varname' = 'Basket_Check_Variable'; - 'caption' = 'Basket_Check'; - 'description' = $NULL; - 'datatype' = 'Icinga\\Module\\Director\\DataType\\DataTypeArray'; - 'format' = $NULL; - 'originalId' = '2'; - 'settings' = @{}; - } - ) - } - - $IcingaDataType = [string]::Format('Icinga\Module\Director\DataType\DataType{0}', $IcingaDataType) - - if ($Basket.Datafield.Values.varname -eq $IcingaCustomVariable) { - } else { - $Basket.Datafield.Add( - [string]$FieldID, @{ - 'varname' = $IcingaCustomVariable; - 'caption' = $parameter.Name; - 'description' = $NULL; - 'datatype' = $IcingaDataType; - 'format' = $NULL; - 'originalId' = [string]$FieldID; - } - ); - - if ($parameter.type.name -eq 'switch' -or $parameter.Name -eq 'Verbose') { - $Basket.Datafield[[string]$FieldID].Add( - 'settings', @{ - 'behavior' = 'strict'; - 'datatype' = 'string'; - 'datalist' = $DataListName; - } - ); - } elseif ($parameter.type.name -eq 'Object') { - $Basket.Datafield[[string]$FieldID].Add( - 'settings', @{ - 'visbility' = 'visible'; - } - ); - } else { - $Basket.Datafield[[string]$FieldID].Add( - 'settings', @{ - 'visbility' = 'visible'; - } - ); - } - [int]$FieldID = [int]$FieldID + 1; - } - } - -[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( - '-Verbose', @{ - 'value' = '$PowerShell_Object_Verbose$'; - 'order' = '99'; - } - ); - - $Basket.Command[$Data.Syntax.syntaxItem.Name].vars.Add( - 'PowerShell_Object_Verbose', "0" - ); - - if ($Basket.Datafield.Values.varname -eq $IcingaCustomVariable) { - } else { - $Basket.Datafield.Add( - [string]$FieldID, @{ - 'varname' = 'PowerShell_Object_Verbose'; - 'caption' = 'Verbose'; - 'description' = $NULL; - 'datatype' = 'Icinga\Module\Director\DataType\DataTypeDatalist'; - 'format' = $NULL; - 'originalId' = [string]$FieldID; - 'settings' = @{ - 'behavior' = 'strict'; - 'data_type' = 'string'; - 'datalist' = 'PowerShell Verbose' - } - } - ); - } - } - - if ($Basket.Command[$Data.Syntax.syntaxItem.Name].arguments.ContainsKey('-NoPerfData') -eq $FALSE) { - $Basket.Command[$Data.Syntax.syntaxItem.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' = $NULL; - 'datatype' = 'Icinga\Module\Director\DataType\DataTypeDatalist'; - 'format' = $NULL; - 'originalId' = [string]$FieldID; - 'settings' = @{ - 'behavior' = 'strict'; - 'data_type' = 'string'; - 'datalist' = 'PowerShell NoPerfData' - } - } - ); - } - } - } - } - foreach ($check in $CheckName) { - [int]$FieldNumeration = 1; - if ($check -eq 'Invoke-IcingaCheckCommandBasket') { - } else { - - $Data = (Get-Help $check) - - foreach ($parameter in $Data.Syntax.syntaxItem.parameter){ - $IcingaCustomVariable = [string]::Format('PowerShell_{0}_{1}', $parameter.type.name, $parameter.Name); - - if ($Basket.Command[$Data.Syntax.syntaxItem.Name].fields.ContainsKey('0') -eq $FALSE){ - $Basket.Command[$Data.Syntax.syntaxItem.Name].fields.Add( - '0', @{ - 'datafield_id' = '2'; - 'is_required' = 'n'; - 'var_filter' = $NULL; - } - ); - } - - - - [hashtable]$translationdatafield = @{} - foreach ($DID in $Basket.Datafield.Keys) - { - if ($translationdatafield.Contains('PowerShell_switch_NoPerfData') -eq $TRUE){ - - }else{ - $translationdatafield.Add($Basket.Datafield.$DID.varname, $DID); - } - } - -# $translationdatafield.Add() - foreach($key in $translationdatafield.Keys) - { - if ([string]$IcingaCustomVariable -eq [string]$key) - { - $otherID = $translationdatafield[$IcingaCustomVariable]; - } else {} - } - # Get Necessary Syntax-Information and more through cmdlet "Get-Help" -# Write-Host $Data.Syntax.syntaxItem.Name -# Write-Host $Parameter.Name - # [int]$FieldID = [int]$FieldID + 1; - - $Basket.Command[$Data.Syntax.syntaxItem.Name].fields.Add( - [string]$FieldNumeration, @{ - 'datafield_id' = [int]$otherID; - 'is_required' = $Required; - 'var_filter' = $NULL; - } - ); - - [int]$FieldNumeration = [int]$FieldNumeration + 1; - } - } -} - - if ($CheckName.Count -eq 1) { - $FileName = "${CheckName}.json"; - } else { - $FileName = "Checks.json"; - } - - $output=ConvertTo-Json -D 100 $Basket > "$FileName"; - - $FilePath = (Get-Location).Path - - # Output-Text - Write-Host "The following commands have been exported:" - foreach ($check in $CheckName) { - if ($check -ne "Invoke-IcingaCheckCommandBasket") { - Write-Host "- '$check'" - } - } - Write-Host "JSON export created in '${FilePath}\${FileName}'" - - return $output; -} \ No newline at end of file From abe0992cccc227cfa26c1ddea54dc20bb3a591a5 Mon Sep 17 00:00:00 2001 From: Crited Date: Fri, 13 Sep 2019 11:46:09 +0200 Subject: [PATCH 07/13] Moved Get-IcingaCheckCommandConfig.psm1 to intended directory --- lib/{plugins => core/tools}/Get-IcingaCheckCommandConfig.psm1 | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename lib/{plugins => core/tools}/Get-IcingaCheckCommandConfig.psm1 (100%) diff --git a/lib/plugins/Get-IcingaCheckCommandConfig.psm1 b/lib/core/tools/Get-IcingaCheckCommandConfig.psm1 similarity index 100% rename from lib/plugins/Get-IcingaCheckCommandConfig.psm1 rename to lib/core/tools/Get-IcingaCheckCommandConfig.psm1 From ed803b739e0c74f3c0dad584d7a3645dcceb2bf0 Mon Sep 17 00:00:00 2001 From: Crited Date: Fri, 13 Sep 2019 14:29:38 +0200 Subject: [PATCH 08/13] Changed Invoke-IcingaCheckService.psm1 to support multiple inputs --- lib/plugins/Invoke-IcingaCheckService.psm1 | 24 ++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/plugins/Invoke-IcingaCheckService.psm1 b/lib/plugins/Invoke-IcingaCheckService.psm1 index 86cc058..2d86bb1 100644 --- a/lib/plugins/Invoke-IcingaCheckService.psm1 +++ b/lib/plugins/Invoke-IcingaCheckService.psm1 @@ -5,10 +5,28 @@ Import-IcingaLib icinga\plugin; function Invoke-IcingaCheckService() { param( + [array]$Service, [string]$Status, - [string]$Service + [int]$Verbose ); + $ServicesPackage = New-IcingaCheckPackage -Name 'Services' -OperatorAnd -Verbose $Verbose; + + if ($Service.Count -ne 1) { + foreach ($services in $Service) { + $IcingaCheck = $null; + + $FoundService = Get-IcingaServices -Service $services; + $ServiceName = Get-IcingaServiceCheckName -ServiceInput $services -Service $FoundService; + $ConvertedStatus = ConvertTo-ServiceStatusCode -Status $Status; + $StatusRaw = $FoundService.Values.configuration.Status.raw; + + $IcingaCheck = New-IcingaCheck -Name $ServiceName -Value $StatusRaw -ObjectExists $FoundService -Translation $ProviderEnums.ServiceStatusName; + $IcingaCheck.CritIfNotMatch($ConvertedStatus) | Out-Null; + $ServicesPackage.AddCheck($IcingaCheck) + } + } else { + $FoundService = Get-IcingaServices -Service $Service; $ServiceName = Get-IcingaServiceCheckName -ServiceInput $Service -Service $FoundService; $Status = ConvertTo-ServiceStatusCode -Status $Status; @@ -16,6 +34,8 @@ function Invoke-IcingaCheckService() $IcingaCheck = New-IcingaCheck -Name $ServiceName -Value $StatusRaw -ObjectExists $FoundService -Translation $ProviderEnums.ServiceStatusName; $IcingaCheck.CritIfNotMatch($Status) | Out-Null; + $ServicesPackage.AddCheck($IcingaCheck); - exit (New-IcingaCheckResult -Check $IcingaCheck -NoPerfData $TRUE -Compile); + } + exit (New-IcingaCheckResult -Name 'Services' -Check $ServicesPackage -NoPerfData $TRUE -Compile); } From 2e960e3de9699898b7ecbcc6254712c46e7ef3bf Mon Sep 17 00:00:00 2001 From: Crited Date: Fri, 13 Sep 2019 15:59:22 +0200 Subject: [PATCH 09/13] Used Icinga-DSL for array-arg splitting --- lib/core/tools/Get-IcingaCheckCommandConfig.psm1 | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/core/tools/Get-IcingaCheckCommandConfig.psm1 b/lib/core/tools/Get-IcingaCheckCommandConfig.psm1 index 3b1f42a..2301c07 100644 --- a/lib/core/tools/Get-IcingaCheckCommandConfig.psm1 +++ b/lib/core/tools/Get-IcingaCheckCommandConfig.psm1 @@ -211,7 +211,16 @@ function Get-IcingaCheckCommandConfig() } elseif ($parameter.type.name -eq 'array') { $Basket.Command[$Data.Syntax.syntaxItem.Name].arguments.Add( [string]::Format('-{0}', $parameter.Name), @{ - 'value' = [string]::Format('(Split-IcingaCheckCommandArgs {0})', $IcingaCustomVariable); + 'value' = @{ + 'type' = 'Function'; + 'body' = [string]::Format( + 'var arr = macro("{0}");{1}if (len(arr) == 0) {2}{1}return "$null";{1}{3}{1}return arr.join(",");', + $IcingaCustomVariable, + "`r`n", + '{', + '}' + ); + } 'order' = $Order; } ); From f17afed3d0cea65af433dc476ba57f88a9402af1 Mon Sep 17 00:00:00 2001 From: Crited Date: Fri, 13 Sep 2019 15:59:45 +0200 Subject: [PATCH 10/13] Added Description for Invoke-IcingaCheckService --- lib/plugins/Invoke-IcingaCheckService.psm1 | 38 ++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/lib/plugins/Invoke-IcingaCheckService.psm1 b/lib/plugins/Invoke-IcingaCheckService.psm1 index 2d86bb1..c29d59e 100644 --- a/lib/plugins/Invoke-IcingaCheckService.psm1 +++ b/lib/plugins/Invoke-IcingaCheckService.psm1 @@ -2,6 +2,44 @@ Import-IcingaLib provider\services; Import-IcingaLib provider\enums; Import-IcingaLib icinga\plugin; +<# +.SYNOPSIS + Checks if a service has a specified status. + +.DESCRIPTION + Invoke-icingaCheckService returns either 'OK' or 'CRITICAL', if a service status is matching status to be checked. + + More Information on https://github.com/LordHepipud/icinga-module-windows + +.FUNCTIONALITY + This module is intended to be used to check whether one or more services have a certain status. + As soon as one of the specified services does not match the status, the function returns 'CRITICAL' instead of 'OK'. + +.EXAMPLE + PS>Invoke-IcingaCheckService -Service WiaRPC, Spooler -Status '1' -Verbose 3 + [CRITICAL]: Check package "Services" is [CRITICAL] (Match All) + \_ [OK]: Service "Ereignisse zum Abrufen von Standbildern (WiaRPC)" is Stopped + \_ [CRITICAL]: Service "Druckwarteschlange (Spooler)" Running is not matching Stopped + +.PARAMETER Service + Used to specify an array of services which should be checked against the status. + Seperated with ',' + +.PARAMETER Status + Status for the specified service or services to check against. + + .INPUTS + System.Array + + .OUTPUTS + System.String + + .LINK + https://github.com/LordHepipud/icinga-module-windows + + .NOTES +#> + function Invoke-IcingaCheckService() { param( From 5888faef3d26a86005e35bde3c42a661956df034 Mon Sep 17 00:00:00 2001 From: Crited Date: Fri, 13 Sep 2019 16:01:46 +0200 Subject: [PATCH 11/13] Fixed Codestyling --- lib/core/tools/Get-IcingaCheckCommandConfig.psm1 | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/core/tools/Get-IcingaCheckCommandConfig.psm1 b/lib/core/tools/Get-IcingaCheckCommandConfig.psm1 index 2301c07..57697c6 100644 --- a/lib/core/tools/Get-IcingaCheckCommandConfig.psm1 +++ b/lib/core/tools/Get-IcingaCheckCommandConfig.psm1 @@ -214,12 +214,12 @@ function Get-IcingaCheckCommandConfig() 'value' = @{ 'type' = 'Function'; 'body' = [string]::Format( - 'var arr = macro("{0}");{1}if (len(arr) == 0) {2}{1}return "$null";{1}{3}{1}return arr.join(",");', - $IcingaCustomVariable, - "`r`n", - '{', - '}' - ); + 'var arr = macro("{0}");{1}if (len(arr) == 0) {2}{1}return "$null";{1}{3}{1}return arr.join(",");', + $IcingaCustomVariable, + "`r`n", + '{', + '}' + ); } 'order' = $Order; } From 33384531c62ba2c89679ec4d543f5d5ffe1f59ce Mon Sep 17 00:00:00 2001 From: Crited Date: Fri, 13 Sep 2019 16:23:34 +0200 Subject: [PATCH 12/13] Added missing description for documented plugin arguments --- lib/core/tools/Get-IcingaCheckCommandConfig.psm1 | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/core/tools/Get-IcingaCheckCommandConfig.psm1 b/lib/core/tools/Get-IcingaCheckCommandConfig.psm1 index 57697c6..aee83f6 100644 --- a/lib/core/tools/Get-IcingaCheckCommandConfig.psm1 +++ b/lib/core/tools/Get-IcingaCheckCommandConfig.psm1 @@ -281,7 +281,7 @@ function Get-IcingaCheckCommandConfig() '0', @{ 'varname' = 'PowerShell_switch_NoPerfData'; 'caption' = 'NoPerfData'; - 'description' = $NULL; + 'description' = 'Specifies if the plugin will return performance data output or not'; 'datatype' = 'Icinga\\Module\\Director\\DataType\\DataTypeDatalist'; 'format' = $NULL; 'originalId' = '0'; @@ -299,7 +299,7 @@ function Get-IcingaCheckCommandConfig() '1', @{ 'varname' = 'PowerShell_switch_NoPerfData'; 'caption' = 'Verbose'; - 'description' = $NULL; + 'description' = 'Specifies if the plugin will return performance data output or not'; 'datatype' = 'Icinga\\Module\\Director\\DataType\\DataTypeString'; 'format' = $NULL; 'originalId' = '1'; @@ -320,7 +320,7 @@ function Get-IcingaCheckCommandConfig() [string]$FieldID, @{ 'varname' = $IcingaCustomVariable; 'caption' = $parameter.Name; - 'description' = $NULL; + 'description' = $parameter.Description.Text; 'datatype' = $IcingaDataType; 'format' = $NULL; 'originalId' = [string]$FieldID; @@ -377,7 +377,7 @@ function Get-IcingaCheckCommandConfig() [string]$FieldID, @{ 'varname' = 'PowerShell_Object_Verbose'; 'caption' = 'Verbose'; - 'description' = $NULL; + 'description' = 'Increase the plugin output for more information on the received result.'; 'datatype' = 'Icinga\Module\Director\DataType\DataTypeDatalist'; 'format' = $NULL; 'originalId' = [string]$FieldID; @@ -407,7 +407,7 @@ function Get-IcingaCheckCommandConfig() [string]$FieldID, @{ 'varname' = 'PowerShell_switch_NoPerfData'; 'caption' = 'Perf Data'; - 'description' = $NULL; + 'description' = 'Specifies if the plugin will return performance data output or not'; 'datatype' = 'Icinga\Module\Director\DataType\DataTypeDatalist'; 'format' = $NULL; 'originalId' = [string]$FieldID; From e2b381e0c1a53cd5610084ab22df61d2cba84f60 Mon Sep 17 00:00:00 2001 From: Crited Date: Fri, 13 Sep 2019 16:24:30 +0200 Subject: [PATCH 13/13] Fixed codestyling --- lib/core/tools/Get-IcingaCheckCommandConfig.psm1 | 9 ++++----- lib/plugins/Invoke-IcingaCheckService.psm1 | 8 ++++---- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/core/tools/Get-IcingaCheckCommandConfig.psm1 b/lib/core/tools/Get-IcingaCheckCommandConfig.psm1 index aee83f6..6ddf2d7 100644 --- a/lib/core/tools/Get-IcingaCheckCommandConfig.psm1 +++ b/lib/core/tools/Get-IcingaCheckCommandConfig.psm1 @@ -25,7 +25,6 @@ - 'Invoke-IcingaCheckUsers' ############################################################ - .EXAMPLE Get-IcingaCheckCommandConfig -OutFile 'C:\Users\icinga\config-exports' The following commands have been exported: @@ -50,16 +49,16 @@ Used to specify an array of commands which should be exported. Seperated with ',' - .INPUTS +.INPUTS System.Array - .OUTPUTS +.OUTPUTS System.String - .LINK +.LINK https://github.com/LordHepipud/icinga-module-windows - .NOTES +.NOTES #> function Get-IcingaCheckCommandConfig() diff --git a/lib/plugins/Invoke-IcingaCheckService.psm1 b/lib/plugins/Invoke-IcingaCheckService.psm1 index c29d59e..3b2c323 100644 --- a/lib/plugins/Invoke-IcingaCheckService.psm1 +++ b/lib/plugins/Invoke-IcingaCheckService.psm1 @@ -28,16 +28,16 @@ Import-IcingaLib icinga\plugin; .PARAMETER Status Status for the specified service or services to check against. - .INPUTS +.INPUTS System.Array - .OUTPUTS +.OUTPUTS System.String - .LINK +.LINK https://github.com/LordHepipud/icinga-module-windows - .NOTES +.NOTES #> function Invoke-IcingaCheckService()