2019-09-14 10:14:24 -04:00
|
|
|
function New-IcingaCheckCommand()
|
|
|
|
|
{
|
|
|
|
|
param(
|
|
|
|
|
[string]$Name = '',
|
|
|
|
|
[array]$Arguments = @(
|
|
|
|
|
'Warning',
|
|
|
|
|
'Critical',
|
|
|
|
|
'[switch]NoPerfData',
|
2019-09-25 14:18:27 -04:00
|
|
|
'[int]Verbose'
|
2021-08-07 05:04:05 -04:00
|
|
|
)
|
2019-09-14 10:14:24 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ([string]::IsNullOrEmpty($Name) -eq $TRUE) {
|
|
|
|
|
throw 'Please specify a command name';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($Name -match 'Invoke' -or $Name -match 'IcingaCheck') {
|
|
|
|
|
throw 'Please specify a command name only without PowerShell Cmdlet naming';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[string]$CommandName = [string]::Format(
|
|
|
|
|
'Invoke-IcingaCheck{0}',
|
|
|
|
|
(Get-Culture).TextInfo.ToTitleCase($Name.ToLower())
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
[string]$CommandFile = [string]::Format(
|
2020-04-01 03:07:42 -04:00
|
|
|
'icinga-powershell-{0}.psm1',
|
|
|
|
|
$Name.ToLower()
|
2019-09-14 10:14:24 -04:00
|
|
|
);
|
2020-04-01 03:07:42 -04:00
|
|
|
[string]$PSDFile = [string]::Format(
|
|
|
|
|
'icinga-powershell-{0}.psd1',
|
|
|
|
|
$Name.ToLower()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
[string]$ModuleFolder = Join-Path -Path (Get-IcingaFrameworkRootPath) -ChildPath (
|
|
|
|
|
[string]::Format('icinga-powershell-{0}', $Name.ToLower())
|
|
|
|
|
);
|
|
|
|
|
[string]$ScriptFile = Join-Path -Path $ModuleFolder -ChildPath $CommandFile;
|
|
|
|
|
[string]$PSDFile = Join-Path -Path $ModuleFolder -ChildPath $PSDFile;
|
2019-09-14 10:14:24 -04:00
|
|
|
|
2020-04-01 03:07:42 -04:00
|
|
|
if ((Test-Path $ModuleFolder) -eq $TRUE) {
|
|
|
|
|
throw 'This module folder does already exist.';
|
|
|
|
|
}
|
2019-09-14 10:14:24 -04:00
|
|
|
|
|
|
|
|
if ((Test-Path $ScriptFile) -eq $TRUE) {
|
2020-04-01 03:07:42 -04:00
|
|
|
throw 'This check command does already exist.';
|
2019-09-14 10:14:24 -04:00
|
|
|
}
|
|
|
|
|
|
2020-04-01 03:07:42 -04:00
|
|
|
New-Item -Path $ModuleFolder -ItemType Directory | Out-Null;
|
|
|
|
|
|
2019-09-14 10:14:24 -04:00
|
|
|
Add-Content -Path $ScriptFile -Value '';
|
|
|
|
|
Add-Content -Path $ScriptFile -Value "function $CommandName()";
|
|
|
|
|
Add-Content -Path $ScriptFile -Value "{";
|
|
|
|
|
|
|
|
|
|
if ($Arguments.Count -ne 0) {
|
|
|
|
|
Add-Content -Path $ScriptFile -Value " param(";
|
|
|
|
|
[int]$index = $Arguments.Count - 1;
|
|
|
|
|
foreach ($argument in $Arguments) {
|
|
|
|
|
|
|
|
|
|
if ($argument.Contains('$') -eq $FALSE) {
|
|
|
|
|
if ($argument.Contains(']') -eq $TRUE) {
|
|
|
|
|
$splittedArguments = $argument.Split(']');
|
|
|
|
|
$argument = [string]::Format('{0}]${1}', $splittedArguments[0], $splittedArguments[1]);
|
|
|
|
|
} else {
|
|
|
|
|
$argument = [string]::Format('${0}', $argument);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($index -ne 0) {
|
|
|
|
|
[string]$content = [string]::Format('{0},', $argument);
|
|
|
|
|
} else {
|
|
|
|
|
[string]$content = [string]::Format('{0}', $argument);
|
|
|
|
|
}
|
|
|
|
|
Add-Content -Path $ScriptFile -Value " $content";
|
|
|
|
|
|
|
|
|
|
$index -= 1;
|
|
|
|
|
}
|
|
|
|
|
Add-Content -Path $ScriptFile -Value " );";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Add-Content -Path $ScriptFile -Value "";
|
2019-09-25 14:18:27 -04:00
|
|
|
Add-Content -Path $ScriptFile -Value ' <# Icinga Basic Check-Plugin Template. Below you will find an example structure. #>';
|
|
|
|
|
Add-Content -Path $ScriptFile -Value ([string]::Format(' $CheckPackage = New-IcingaCheckPackage -Name {0}New Package{0} -OperatorAnd -Verbose $Verbose;', "'"));
|
|
|
|
|
Add-Content -Path $ScriptFile -Value ([string]::Format(' $IcingaCheck = New-IcingaCheck -Name {0}New Check{0} -Value 10 -Unit {0}%{0}', "'"));
|
|
|
|
|
Add-Content -Path $ScriptFile -Value ([string]::Format(' $IcingaCheck.WarnOutOfRange($Warning).CritOutOfRange($Critical) | Out-Null;', "'"));
|
|
|
|
|
Add-Content -Path $ScriptFile -Value ([string]::Format(' $CheckPackage.AddCheck($IcingaCheck);', "'"));
|
|
|
|
|
Add-Content -Path $ScriptFile -Value "";
|
|
|
|
|
Add-Content -Path $ScriptFile -Value ([string]::Format(' return (New-IcingaCheckresult -Check $CheckPackage -NoPerfData $NoPerfData -Compile);', "'"));
|
|
|
|
|
|
2019-09-14 10:14:24 -04:00
|
|
|
Add-Content -Path $ScriptFile -Value "}";
|
|
|
|
|
|
2020-05-13 10:53:15 -04:00
|
|
|
Write-IcingaConsoleNotice ([string]::Format('The Check-Command "{0}" was successfully added.', $CommandName));
|
2019-09-14 10:14:24 -04:00
|
|
|
|
|
|
|
|
# Try to open the default Editor for the new Cmdlet
|
|
|
|
|
$DefaultEditor = (Get-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.psm1\OpenWithList' -Name a).a;
|
2019-09-19 10:22:05 -04:00
|
|
|
$DefaultEditor = $DefaultEditor.Replace('.exe', '');
|
2019-09-14 10:14:24 -04:00
|
|
|
|
2020-04-01 03:07:42 -04:00
|
|
|
New-ModuleManifest `
|
|
|
|
|
-Path $PSDFile `
|
|
|
|
|
-ModuleToProcess $CommandFile `
|
|
|
|
|
-RequiredModules @('icinga-powershell-framework') `
|
|
|
|
|
-FunctionsToExport @('*') `
|
|
|
|
|
-CmdletsToExport @('*') `
|
|
|
|
|
-VariablesToExport '*' | Out-Null;
|
|
|
|
|
|
|
|
|
|
Unblock-IcingaPowerShellFiles -Path $ModuleFolder;
|
|
|
|
|
|
2019-09-25 14:18:27 -04:00
|
|
|
Import-Module $ScriptFile -Global;
|
|
|
|
|
|
2020-04-01 03:07:42 -04:00
|
|
|
if ([string]::IsNullOrEmpty($DefaultEditor) -eq $FALSE -And ($null -eq (Get-Command $DefaultEditor -ErrorAction SilentlyContinue)) -And ((Test-Path $DefaultEditor) -eq $FALSE)) {
|
2020-05-13 10:53:15 -04:00
|
|
|
Write-IcingaConsoleWarning 'No default editor for .psm1 files found. Specify a default editor to automaticly open the newly generated check plugin.';
|
2019-09-14 10:14:24 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
& $DefaultEditor "$ScriptFile";
|
|
|
|
|
}
|