From e137a4dbcf200e7d25bd34acda8ca6dbc786b87a Mon Sep 17 00:00:00 2001 From: Crited Date: Thu, 12 Sep 2019 09:10:55 +0200 Subject: [PATCH] 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