mirror of
https://github.com/Icinga/icinga-powershell-framework.git
synced 2025-12-21 15:19:58 -05:00
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
This commit is contained in:
parent
e7950c1616
commit
e137a4dbcf
1 changed files with 211 additions and 157 deletions
|
|
@ -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)
|
||||
|
||||
# "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' = @{};
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
$Basket.DataList["PowerShell NoPerfData"].entries.Add(
|
||||
'0', @{
|
||||
'entries' = @(
|
||||
@{
|
||||
'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)
|
||||
}
|
||||
);
|
||||
}
|
||||
# "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'; #Gehört noch geändert
|
||||
'entries' = @{};
|
||||
}
|
||||
);
|
||||
}
|
||||
$Basket.DataList["PowerShell Verbose"].entries.Add(
|
||||
'0', @{
|
||||
'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,11 +163,11 @@ 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
|
||||
|
|
@ -134,7 +177,7 @@ function Invoke-IcingaCheckCommandBasket()
|
|||
|
||||
$IcingaCustomVariable = [string]::Format('$PowerShell_{0}_{1}$', $parameter.type.name, $parameter.Name);
|
||||
|
||||
# Conditional whether type of parameter is switch
|
||||
#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), @{
|
||||
|
|
@ -162,19 +205,24 @@ function Invoke-IcingaCheckCommandBasket()
|
|||
'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?
|
||||
|
||||
# Determine wether a parameter is required based on given syntax-information
|
||||
if ($parameter.required -eq $TRUE) {
|
||||
$Required = 'y';
|
||||
} else {
|
||||
$Required = 'n';
|
||||
}
|
||||
|
||||
[int]$FieldID = $FieldID + 1;
|
||||
$Basket.Command[$Data.Syntax.syntaxItem.Name].fields.Add(
|
||||
[string]$FieldID, @{
|
||||
'datafield_id' = [int]$FieldID;
|
||||
|
|
@ -209,13 +257,11 @@ 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;
|
||||
if ($Basket.Datafield.Values.varname -eq $IcingaCustomVariable) {
|
||||
} else {
|
||||
$Basket.Datafield.Add(
|
||||
[string]$FieldID, @{
|
||||
'varname' = $IcingaCustomVariable;
|
||||
|
|
@ -227,7 +273,6 @@ function Invoke-IcingaCheckCommandBasket()
|
|||
}
|
||||
);
|
||||
|
||||
|
||||
if ($parameter.type.name -eq 'switch' -or $parameter.Name -eq 'Verbose') {
|
||||
$Basket.Datafield[[string]$FieldID].Add(
|
||||
'settings', @{
|
||||
|
|
@ -248,22 +293,22 @@ function Invoke-IcingaCheckCommandBasket()
|
|||
);
|
||||
}
|
||||
}
|
||||
[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(
|
||||
|
|
@ -284,9 +329,7 @@ function Invoke-IcingaCheckCommandBasket()
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
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,6 +340,7 @@ 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(
|
||||
|
|
@ -316,17 +360,27 @@ function Invoke-IcingaCheckCommandBasket()
|
|||
);
|
||||
}
|
||||
}
|
||||
$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;
|
||||
}
|
||||
Loading…
Reference in a new issue