Icinga2Agent.psm1: upgrade to latest upstream...

...from https://github.com/LordHepipud/icinga2-powershell-module
This commit is contained in:
Christian Stein 2016-07-20 23:17:57 +02:00 committed by Thomas Gelf
parent c2f3609cc6
commit 757b4d430d

View File

@ -201,7 +201,7 @@ function Icinga2AgentModule {
$this.setProperty('initialized', $TRUE);
# Set the default config dir
$this.setProperty('config_dir', $Env:ProgramData + '\icinga2\etc\icinga2\');
$this.setProperty('api_dir', $Env:ProgramData + '\icinga2\var\lib\icinga2\api\');
$this.setProperty('api_dir', $Env:ProgramData + '\icinga2\var\lib\icinga2\api');
# Generate endpoint nodes based on iput
# parameters
$this.generateEndpointNodes();
@ -229,10 +229,7 @@ function Icinga2AgentModule {
#
$installer | Add-Member -membertype ScriptMethod -name 'generateEndpointNodes' -value {
if (-Not $this.config('endpoints')) {
throw 'You require to specify atleast one endpoint with parameter -Endpoints <nodes>';
}
if ($this.config('endpoints')) {
$endpoint_objects = '';
$endpoint_nodes = '';
foreach ($endpoint in $this.config('endpoints')) {
@ -245,6 +242,10 @@ function Icinga2AgentModule {
}
$this.setProperty('endpoint_nodes', $endpoint_nodes);
$this.setProperty('endpoint_objects', $endpoint_objects);
$this.setProperty('generate_config', 'true');
} else {
$this.setProperty('generate_config', 'false');
}
}
#
@ -492,7 +493,8 @@ function Icinga2AgentModule {
$installer | Add-Member -membertype ScriptMethod -name 'flushIcingaApiDirectory' -value {
if (Test-Path $this.getApiDirectory()) {
$this.info('Flushing content of ' + $this.getApiDirectory());
Get-ChildItem -Path $this.getApiDirectory() -Recurse | Remove-Item -force -recurse
$folder = New-Object -ComObject Scripting.FileSystemObject;
$folder.DeleteFolder($this.getApiDirectory());
}
}
@ -515,12 +517,13 @@ function Icinga2AgentModule {
}
$installer | Add-Member -membertype ScriptMethod -name 'generateIcingaConfiguration' -value {
if ($this.getProperty('generate_config') -eq 'true') {
$this.checkConfigInputParametersAndThrowException();
$icingaCurrentConfig = '';
if (Test-Path $this.getIcingaConfigFile()) {
$icingaCurrentConfig = Get-Content $this.getIcingaConfigFile() -Raw;
$icingaCurrentConfig = [System.IO.File]::ReadAllText($this.getIcingaConfigFile());
}
$icingaNewConfig =
@ -558,6 +561,7 @@ object ApiListener "api" {
$this.setProperty('new_icinga_config', $icingaNewConfig);
$this.setProperty('old_icinga_config', $icingaCurrentConfig);
}
}
#
# Generate a hash for old and new config
@ -565,6 +569,9 @@ object ApiListener "api" {
#
$installer | Add-Member -membertype ScriptMethod -name 'hasConfigChanged' -value {
if ($this.getProperty('generate_config') -eq 'false') {
return $FALSE;
}
if (-Not $this.getProperty('new_icinga_config')) {
throw 'New Icinga 2 configuration not generated. Please call "generateIcingaConfiguration" before.';
}
@ -650,10 +657,7 @@ object ApiListener "api" {
#
$installer | Add-Member -membertype ScriptMethod -name 'generateCertificates' -value {
if (-Not $this.config('agent_name') -Or -Not $this.config('ca_server') -Or -Not $this.config('ticket')) {
throw 'One or more of the following arguments is missing: -AgentName <name> -CAServer <server> -Ticket <ticket>';
}
if ($this.config('agent_name') -And $this.config('ca_server') -And $this.config('ticket')) {
$icingaPkiDir = $this.getProperty('config_dir') + 'pki\';
$icingaBinary = $this.getInstallPath() + '\sbin\icinga2.exe';
$agentName = $this.config('agent_name');
@ -674,6 +678,9 @@ object ApiListener "api" {
$this.printAndAssertResultBasedOnExitCode($result, $LASTEXITCODE);
$this.setProperty('require_restart', 'true');
} else {
$this.info('Skipping certificate generation. One or more of the following arguments is not set: -AgentName <name> -CAServer <server> -Ticket <ticket>');
}
}
#
@ -787,14 +794,17 @@ object ApiListener "api" {
# our Icinga 2 Agent
#
$installer | Add-Member -membertype ScriptMethod -name 'applyPossibleConfigChanges' -value {
if ($this.hasConfigChanged()) {
if ($this.hasConfigChanged() -And $this.getProperty('generate_config') -eq 'true') {
$this.backupDefaultConfig();
$this.writeConfig();
# Check if the config is valid and rollback otherwise
if (-Not $this.isIcingaConfigValid()) {
$this.error('Icinga 2 config validation failed. Rolling back to previous version.');
$this.rollbackConfig();
if (-Not $this.hasCertificates()) {
$this.error('Icinga 2 certificates not found. Please generate the certificates over this module or add them manually.');
}
# $this.rollbackConfig();
if ($this.isIcingaConfigValid($FALSE)) {
$this.info('Rollback of Icinga 2 configuration successfull.');
} else {
@ -804,7 +814,7 @@ object ApiListener "api" {
$this.info('Icinga 2 configuration check successfull.');
}
} else {
$this.info('icinga2.conf did not change. Nothing to do');
$this.info('icinga2.conf did not change or required parameters not set. Nothing to do');
}
}