Merge pull request #656 from Icinga:feature/add_improved_document_writer

Feature: Adds new feature to write documents easier

Adds new feature to write document content easier by storing it in memory first and then allowing to write it to disk at once with proper UTF8 encoding

New cmdlets:
* New-IcingaDocumentObject
* Add-IcingaDocumentContent
* Write-IcingaDocumentFile
This commit is contained in:
Lord Hepipud 2023-07-27 16:20:16 +02:00 committed by GitHub
commit f028ade6c3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 154 additions and 70 deletions

View file

@ -39,6 +39,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
* [#643](https://github.com/Icinga/icinga-powershell-framework/pull/643) Adds support for `-RebuildCache` flag on `icinga` cmd to rebuild component cache as well * [#643](https://github.com/Icinga/icinga-powershell-framework/pull/643) Adds support for `-RebuildCache` flag on `icinga` cmd to rebuild component cache as well
* [#644](https://github.com/Icinga/icinga-powershell-framework/pull/644) Adds progress bar output to repository interaction (sync, update, new) instead of plain text output * [#644](https://github.com/Icinga/icinga-powershell-framework/pull/644) Adds progress bar output to repository interaction (sync, update, new) instead of plain text output
* [#655](https://github.com/Icinga/icinga-powershell-framework/pull/655) Adds [IWKB](https://icinga.com/docs/icinga-for-windows/latest/doc/knowledgebase/IWKB000016/) and test/manage Cmdlets for SCOM intercept counters * [#655](https://github.com/Icinga/icinga-powershell-framework/pull/655) Adds [IWKB](https://icinga.com/docs/icinga-for-windows/latest/doc/knowledgebase/IWKB000016/) and test/manage Cmdlets for SCOM intercept counters
* [#656](https://github.com/Icinga/icinga-powershell-framework/pull/656) Adds new feature to write document content easier by storing it in memory first and then allowing to write it to disk at once with proper UTF8 encoding
### Enhancements ### Enhancements

View file

@ -0,0 +1,24 @@
function Add-IcingaDocumentContent()
{
param (
[string]$Name = $null,
[string]$Content = '',
[switch]$NoNewLine = $FALSE
);
if ([string]::IsNullOrEmpty($Name)) {
Write-IcingaConsoleError 'You have to specify an internal name for the documentation object';
return;
}
if ($Global:Icinga.Private.Documentation.ContainsKey($Name) -eq $false) {
Write-IcingaConsoleError 'A documentation object with the name "{0}" does not exist' -Objects $Name;
return;
}
if ($NoNewLine) {
$Global:Icinga.Private.Documentation[$Name]['Content'].Append($Content) | Out-Null;
} else {
$Global:Icinga.Private.Documentation[$Name]['Content'].AppendLine($Content) | Out-Null;
}
}

View file

@ -0,0 +1,38 @@
function New-IcingaDocumentObject()
{
param (
[string]$Name = $null,
[string]$Path = $null,
[switch]$Force = $FALSE
);
if ([string]::IsNullOrEmpty($Name)) {
Write-IcingaConsoleError 'You have to specify an internal name for the documentation object';
return;
}
if ([string]::IsNullOrEmpty($Path)) {
Write-IcingaConsoleError 'You have to specify a path on where the document should be written to';
return;
}
if ($Global:Icinga.Private.Documentation.ContainsKey($Name)) {
if ($Force -eq $FALSE) {
Write-IcingaConsoleError 'A documentation object with the name "{0}" does already exist in memory. Use -Force to overwrite it' -Objects $Name;
return;
}
$Global:Icinga.Private.Documentation[$Name] = @{
'Path' = $Path;
'Content' = (New-Object -TypeName 'System.Text.StringBuilder');
}
} else {
$Global:Icinga.Private.Documentation.Add(
$Name,
@{
'Path' = $Path;
'Content' = (New-Object -TypeName 'System.Text.StringBuilder');
}
);
}
}

View file

@ -0,0 +1,18 @@
function Write-IcingaDocumentFile()
{
param (
[string]$Name = $null,
[switch]$ClearCache = $FALSE
);
if ([string]::IsNullOrEmpty($Name)) {
Write-IcingaConsoleError 'You have to specify an internal name for the documentation object';
return;
}
Write-IcingaFileSecure -File $Global:Icinga.Private.Documentation[$Name]['Path'] -Value $Global:Icinga.Private.Documentation[$Name]['Content'].ToString();
if ($ClearCache) {
$Global:Icinga.Private.Documentation.Remove($Name);
}
}

View file

@ -24,6 +24,7 @@ function New-IcingaEnvironmentVariable()
$Global:Icinga.Add('Private', @{ }); $Global:Icinga.Add('Private', @{ });
$Global:Icinga.Private.Add('Daemons', @{ }); $Global:Icinga.Private.Add('Daemons', @{ });
$Global:Icinga.Private.Add('Documentation', @{ });
$Global:Icinga.Private.Add('Timers', @{ }); $Global:Icinga.Private.Add('Timers', @{ });
$Global:Icinga.Private.Add('ProgressStatus', @{ }); $Global:Icinga.Private.Add('ProgressStatus', @{ });

View file

@ -14,6 +14,8 @@ function Publish-IcingaPluginDocumentation()
[string]$PluginDocFile = Join-Path -Path $ModulePath -ChildPath 'doc/10-Icinga-Plugins.md'; [string]$PluginDocFile = Join-Path -Path $ModulePath -ChildPath 'doc/10-Icinga-Plugins.md';
[string]$PluginDocDir = Join-Path -Path $ModulePath -ChildPath 'doc/plugins'; [string]$PluginDocDir = Join-Path -Path $ModulePath -ChildPath 'doc/plugins';
New-IcingaDocumentObject -Name 'Plugins Base' -Path $PluginDocFile;
if ((Test-Path $PluginDocDir) -eq $FALSE) { if ((Test-Path $PluginDocDir) -eq $FALSE) {
New-Item -Path $PluginDocDir -ItemType Directory -Force | Out-Null; New-Item -Path $PluginDocDir -ItemType Directory -Force | Out-Null;
} }
@ -22,34 +24,34 @@ function Publish-IcingaPluginDocumentation()
[int]$FileCount = $MDFiles.Count; [int]$FileCount = $MDFiles.Count;
[string]$FileCountStr = ''; [string]$FileCountStr = '';
Set-Content -Path $PluginDocFile -Value '# Icinga Plugins'; Add-IcingaDocumentContent -Name 'Plugins Base' -Content '# Icinga Plugins';
Add-Content -Path $PluginDocFile -Value ''; Add-IcingaDocumentContent -Name 'Plugins Base' -Content '';
Add-Content -Path $PluginDocFile -Value 'Below you will find a documentation for every single available plugin provided by this repository. Most of the plugins allow the usage of default Icinga threshold range handling, which is defined as follows:'; Add-IcingaDocumentContent -Name 'Plugins Base' -Content 'Below you will find a documentation for every single available plugin provided by this repository. Most of the plugins allow the usage of default Icinga threshold range handling, which is defined as follows:';
Add-Content -Path $PluginDocFile -Value ''; Add-IcingaDocumentContent -Name 'Plugins Base' -Content '';
Add-Content -Path $PluginDocFile -Value '| Argument | Throws error on | Ok range |'; Add-IcingaDocumentContent -Name 'Plugins Base' -Content '| Argument | Throws error on | Ok range |';
Add-Content -Path $PluginDocFile -Value '| --- | --- | --- |'; Add-IcingaDocumentContent -Name 'Plugins Base' -Content '| --- | --- | --- |';
Add-Content -Path $PluginDocFile -Value '| 20 | < 0 or > 20 | 0 .. 20 |'; Add-IcingaDocumentContent -Name 'Plugins Base' -Content '| 20 | < 0 or > 20 | 0 .. 20 |';
Add-Content -Path $PluginDocFile -Value '| 20: | < 20 | between 20 .. ∞ |'; Add-IcingaDocumentContent -Name 'Plugins Base' -Content '| 20: | < 20 | between 20 .. ∞ |';
Add-Content -Path $PluginDocFile -Value '| ~:20 | > 20 | between -∞ .. 20 |'; Add-IcingaDocumentContent -Name 'Plugins Base' -Content '| ~:20 | > 20 | between -∞ .. 20 |';
Add-Content -Path $PluginDocFile -Value '| 30:40 | < 30 or > 40 | between {30 .. 40} |'; Add-IcingaDocumentContent -Name 'Plugins Base' -Content '| 30:40 | < 30 or > 40 | between {30 .. 40} |';
Add-Content -Path $PluginDocFile -Value '| `@30:40 | ≥ 30 and ≤ 40 | outside -∞ .. 29 and 41 .. ∞ |'; Add-IcingaDocumentContent -Name 'Plugins Base' -Content '| `@30:40 | ≥ 30 and ≤ 40 | outside -∞ .. 29 and 41 .. ∞ |';
Add-Content -Path $PluginDocFile -Value ''; Add-IcingaDocumentContent -Name 'Plugins Base' -Content '';
Add-Content -Path $PluginDocFile -Value 'Please ensure that you will escape the `@` if you are configuring it on the Icinga side. To do so, you will simply have to write an *\`* before the `@` symbol: \``@`'; Add-IcingaDocumentContent -Name 'Plugins Base' -Content 'Please ensure that you will escape the `@` if you are configuring it on the Icinga side. To do so, you will simply have to write an *\`* before the `@` symbol: \``@`';
Add-Content -Path $PluginDocFile -Value ''; Add-IcingaDocumentContent -Name 'Plugins Base' -Content '';
Add-Content -Path $PluginDocFile -Value 'To test thresholds with different input values, you can use the Framework Cmdlet `Get-IcingaHelpThresholds`.'; Add-IcingaDocumentContent -Name 'Plugins Base' -Content 'To test thresholds with different input values, you can use the Framework Cmdlet `Get-IcingaHelpThresholds`.';
Add-Content -Path $PluginDocFile -Value ''; Add-IcingaDocumentContent -Name 'Plugins Base' -Content '';
Add-Content -Path $PluginDocFile -Value 'Each plugin ships with a constant Framework argument `-ThresholdInterval`. This can be used to modify the value your thresholds are compared against from the current, fetched value to one collected over time by the Icinga for Windows daemon. In case you [Collect Metrics Over Time](https://icinga.com/docs/icinga-for-windows/latest/doc/110-Installation/06-Collect-Metrics-over-Time/) for specific time intervals, you can for example set the argument to `15m` to get the average value of 15m as base for your monitoring values. Please note that in this example, you will require to have collected the `15m` average for `Invoke-IcingaCheckCPU`.'; Add-IcingaDocumentContent -Name 'Plugins Base' -Content 'Each plugin ships with a constant Framework argument `-ThresholdInterval`. This can be used to modify the value your thresholds are compared against from the current, fetched value to one collected over time by the Icinga for Windows daemon. In case you [Collect Metrics Over Time](https://icinga.com/docs/icinga-for-windows/latest/doc/110-Installation/06-Collect-Metrics-over-Time/) for specific time intervals, you can for example set the argument to `15m` to get the average value of 15m as base for your monitoring values. Please note that in this example, you will require to have collected the `15m` average for `Invoke-IcingaCheckCPU`.';
Add-Content -Path $PluginDocFile -Value ''; Add-IcingaDocumentContent -Name 'Plugins Base' -Content '';
Add-Content -Path $PluginDocFile -Value '```powershell'; Add-IcingaDocumentContent -Name 'Plugins Base' -Content '```powershell';
Add-Content -Path $PluginDocFile -Value 'icinga> icinga { Invoke-IcingaCheckCPU -Warning 20 -Critical 40 -Core _Total -ThresholdInterval 15m }' Add-IcingaDocumentContent -Name 'Plugins Base' -Content 'icinga> icinga { Invoke-IcingaCheckCPU -Warning 20 -Critical 40 -Core _Total -ThresholdInterval 15m }'
Add-Content -Path $PluginDocFile -Value '' Add-IcingaDocumentContent -Name 'Plugins Base' -Content ''
Add-Content -Path $PluginDocFile -Value '[WARNING] CPU Load: [WARNING] Core Total (29,14817700%)' Add-IcingaDocumentContent -Name 'Plugins Base' -Content '[WARNING] CPU Load: [WARNING] Core Total (29,14817700%)'
Add-Content -Path $PluginDocFile -Value '\_ [WARNING] Core Total: 29,14817700% is greater than threshold 20% (15m avg.)'; Add-IcingaDocumentContent -Name 'Plugins Base' -Content '\_ [WARNING] Core Total: 29,14817700% is greater than threshold 20% (15m avg.)';
Add-Content -Path $PluginDocFile -Value "| 'core_total_1'=31.545677%;;;0;100 'core_total_15'=29.148177%;20;40;0;100 'core_total_5'=28.827410%;;;0;100 'core_total_20'=30.032942%;;;0;100 'core_total_3'=27.731669%;;;0;100 'core_total'=33.87817%;;;0;100"; Add-IcingaDocumentContent -Name 'Plugins Base' -Content "| 'core_total_1'=31.545677%;;;0;100 'core_total_15'=29.148177%;20;40;0;100 'core_total_5'=28.827410%;;;0;100 'core_total_20'=30.032942%;;;0;100 'core_total_3'=27.731669%;;;0;100 'core_total'=33.87817%;;;0;100";
Add-Content -Path $PluginDocFile -Value '```'; Add-IcingaDocumentContent -Name 'Plugins Base' -Content '```';
Add-Content -Path $PluginDocFile -Value '' Add-IcingaDocumentContent -Name 'Plugins Base' -Content ''
Add-Content -Path $PluginDocFile -Value '| Plugin Name | Description |' Add-IcingaDocumentContent -Name 'Plugins Base' -Content '| Plugin Name | Description |'
Add-Content -Path $PluginDocFile -Value '| --- | --- |' Add-IcingaDocumentContent -Name 'Plugins Base' -Content '| --- | --- |'
$AvailablePlugins = Get-ChildItem -Path $PluginDir -Recurse -Filter *.psm1; $AvailablePlugins = Get-ChildItem -Path $PluginDir -Recurse -Filter *.psm1;
foreach ($plugin in $AvailablePlugins) { foreach ($plugin in $AvailablePlugins) {
@ -84,7 +86,9 @@ function Publish-IcingaPluginDocumentation()
} }
[string]$PluginDescriptionFile = Join-Path -Path $PluginDocDir -ChildPath $PluginDocName; [string]$PluginDescriptionFile = Join-Path -Path $PluginDocDir -ChildPath $PluginDocName;
Add-Content -Path $PluginDocFile -Value ([string]::Format( New-IcingaDocumentObject -Name $PluginName -Path $PluginDescriptionFile;
Add-IcingaDocumentContent -Name 'Plugins Base' -Content ([string]::Format(
'| [{0}](plugins/{1}) | {2} |', '| [{0}](plugins/{1}) | {2} |',
$PluginName, $PluginName,
$PluginDocName, $PluginDocName,
@ -93,31 +97,31 @@ function Publish-IcingaPluginDocumentation()
$PluginHelp = Get-Help $PluginName -Full; $PluginHelp = Get-Help $PluginName -Full;
Set-Content -Path $PluginDescriptionFile -Value ([string]::Format('# {0}', $PluginHelp.Name)); Add-IcingaDocumentContent -Name $PluginName -Content ([string]::Format('# {0}', $PluginHelp.Name));
Add-Content -Path $PluginDescriptionFile -Value ''; Add-IcingaDocumentContent -Name $PluginName -Content '';
Add-Content -Path $PluginDescriptionFile -Value '## Description'; Add-IcingaDocumentContent -Name $PluginName -Content '## Description';
Add-Content -Path $PluginDescriptionFile -Value ''; Add-IcingaDocumentContent -Name $PluginName -Content '';
Add-Content -Path $PluginDescriptionFile -Value $PluginHelp.details.description.Text; Add-IcingaDocumentContent -Name $PluginName -Content $PluginHelp.details.description.Text;
Add-Content -Path $PluginDescriptionFile -Value ''; Add-IcingaDocumentContent -Name $PluginName -Content '';
Add-Content -Path $PluginDescriptionFile -Value $PluginHelp.description.Text; Add-IcingaDocumentContent -Name $PluginName -Content $PluginHelp.description.Text;
Add-Content -Path $PluginDescriptionFile -Value ''; Add-IcingaDocumentContent -Name $PluginName -Content '';
Add-Content -Path $PluginDescriptionFile -Value '## Permissions'; Add-IcingaDocumentContent -Name $PluginName -Content '## Permissions';
Add-Content -Path $PluginDescriptionFile -Value ''; Add-IcingaDocumentContent -Name $PluginName -Content '';
if ([string]::IsNullOrEmpty($PluginHelp.Role)) { if ([string]::IsNullOrEmpty($PluginHelp.Role)) {
Add-Content -Path $PluginDescriptionFile -Value 'No special permissions required.'; Add-IcingaDocumentContent -Name $PluginName -Content 'No special permissions required.';
} else { } else {
Add-Content -Path $PluginDescriptionFile -Value 'To execute this plugin you will require to grant the following user permissions.'; Add-IcingaDocumentContent -Name $PluginName -Content 'To execute this plugin you will require to grant the following user permissions.';
Add-Content -Path $PluginDescriptionFile -Value ''; Add-IcingaDocumentContent -Name $PluginName -Content '';
Add-Content -Path $PluginDescriptionFile -Value $PluginHelp.Role; Add-IcingaDocumentContent -Name $PluginName -Content $PluginHelp.Role;
} }
if ($null -ne $PluginHelp.parameters.parameter) { if ($null -ne $PluginHelp.parameters.parameter) {
Add-Content -Path $PluginDescriptionFile -Value ''; Add-IcingaDocumentContent -Name $PluginName -Content '';
Add-Content -Path $PluginDescriptionFile -Value '## Arguments'; Add-IcingaDocumentContent -Name $PluginName -Content '## Arguments';
Add-Content -Path $PluginDescriptionFile -Value ''; Add-IcingaDocumentContent -Name $PluginName -Content '';
Add-Content -Path $PluginDescriptionFile -Value '| Argument | Type | Required | Default | Description |'; Add-IcingaDocumentContent -Name $PluginName -Content '| Argument | Type | Required | Default | Description |';
Add-Content -Path $PluginDescriptionFile -Value '| --- | --- | --- | --- | --- |'; Add-IcingaDocumentContent -Name $PluginName -Content '| --- | --- | --- | --- | --- |';
foreach ($parameter in $PluginHelp.parameters.parameter) { foreach ($parameter in $PluginHelp.parameters.parameter) {
[string]$ParamDescription = $parameter.description.Text; [string]$ParamDescription = $parameter.description.Text;
@ -134,44 +138,42 @@ function Publish-IcingaPluginDocumentation()
$parameter.defaultValue, $parameter.defaultValue,
$ParamDescription $ParamDescription
); );
Add-Content -Path $PluginDescriptionFile -Value $TableContent; Add-IcingaDocumentContent -Name $PluginName -Content $TableContent;
} }
Add-Content -Path $PluginDescriptionFile -Value '| ThresholdInterval | String | | | Change the value your defined threshold checks against from the current value to a collected time threshold of the Icinga for Windows daemon, as described [here](https://icinga.com/docs/icinga-for-windows/latest/doc/service/10-Register-Service-Checks/). An example for this argument would be 1m or 15m which will use the average of 1m or 15m for monitoring. |'; Add-IcingaDocumentContent -Name $PluginName -Content '| ThresholdInterval | String | | | Change the value your defined threshold checks against from the current value to a collected time threshold of the Icinga for Windows daemon, as described [here](https://icinga.com/docs/icinga-for-windows/latest/doc/service/10-Register-Service-Checks/). An example for this argument would be 1m or 15m which will use the average of 1m or 15m for monitoring. |';
} }
if ($null -ne $PluginHelp.examples) { if ($null -ne $PluginHelp.examples) {
[int]$ExampleIndex = 1; [int]$ExampleIndex = 1;
Add-Content -Path $PluginDescriptionFile -Value ''; Add-IcingaDocumentContent -Name $PluginName -Content '';
Add-Content -Path $PluginDescriptionFile -Value '## Examples'; Add-IcingaDocumentContent -Name $PluginName -Content '## Examples';
Add-Content -Path $PluginDescriptionFile -Value ''; Add-IcingaDocumentContent -Name $PluginName -Content '';
foreach ($example in $PluginHelp.examples.example) { foreach ($example in $PluginHelp.examples.example) {
[string]$ExampleDescription = $example.remarks.Text; [string]$ExampleDescription = $example.remarks.Text;
if ([string]::IsNullOrEmpty($ExampleDescription) -eq $FALSE) { if ([string]::IsNullOrEmpty($ExampleDescription) -eq $FALSE) {
} }
Add-Content -Path $PluginDescriptionFile -Value ([string]::Format('### Example Command {0}', $ExampleIndex)); Add-IcingaDocumentContent -Name $PluginName -Content ([string]::Format('### Example Command {0}', $ExampleIndex));
Add-Content -Path $PluginDescriptionFile -Value ''; Add-IcingaDocumentContent -Name $PluginName -Content '';
Add-Content -Path $PluginDescriptionFile -Value '```powershell'; Add-IcingaDocumentContent -Name $PluginName -Content '```powershell';
Add-Content -Path $PluginDescriptionFile -Value $example.code; Add-IcingaDocumentContent -Name $PluginName -Content $example.code;
Add-Content -Path $PluginDescriptionFile -Value '```'; Add-IcingaDocumentContent -Name $PluginName -Content '```';
Add-Content -Path $PluginDescriptionFile -Value ''; Add-IcingaDocumentContent -Name $PluginName -Content '';
Add-Content -Path $PluginDescriptionFile -Value ([string]::Format('### Example Output {0}', $ExampleIndex)); Add-IcingaDocumentContent -Name $PluginName -Content ([string]::Format('### Example Output {0}', $ExampleIndex));
Add-Content -Path $PluginDescriptionFile -Value ''; Add-IcingaDocumentContent -Name $PluginName -Content '';
Add-Content -Path $PluginDescriptionFile -Value '```powershell'; Add-IcingaDocumentContent -Name $PluginName -Content '```powershell';
Add-Content -Path $PluginDescriptionFile -Value $ExampleDescription; Add-IcingaDocumentContent -Name $PluginName -Content $ExampleDescription;
Add-Content -Path $PluginDescriptionFile -Value '```'; Add-IcingaDocumentContent -Name $PluginName -Content '```';
Add-Content -Path $PluginDescriptionFile -Value ''; Add-IcingaDocumentContent -Name $PluginName -Content '';
$ExampleIndex += 1; $ExampleIndex += 1;
} }
$Content = Get-Content -Path $PluginDescriptionFile; Write-IcingaDocumentFile $PluginName -ClearCache;
Set-Content -Path $PluginDescriptionFile -Value '';
for ($entry = 0; $entry -lt ($Content.Count - 1); $entry++) {
Add-Content -Path $PluginDescriptionFile -Value $Content[$entry];
}
} }
} }
Write-IcingaDocumentFile 'Plugins Base' -ClearCache;
} }