2020-04-28 09:24:57 -04:00
|
|
|
<#
|
|
|
|
|
.SYNOPSIS
|
|
|
|
|
Register the current host wihtin the Icinga Director by using the
|
|
|
|
|
Self-Service API and return the host key
|
|
|
|
|
.DESCRIPTION
|
|
|
|
|
This function will register the current host within the Icinga Director in case
|
|
|
|
|
it is not already registered and returns the host key for storing it on disk
|
|
|
|
|
to allow the host to fetch detailed configurations like zones and endppoints
|
|
|
|
|
.FUNCTIONALITY
|
|
|
|
|
Register a host within the Icinga Director by using the Self-Service API
|
|
|
|
|
.EXAMPLE
|
|
|
|
|
PS>Register-IcingaDirectorSelfServiceHost -DirectorUrl 'https://example.com/icingaweb2/director -Hostname 'examplehost' -ApiKey 457g6b98054v76vb5490ß276bv0457v6054b76 -Endpoint 'icinga.example.com';
|
|
|
|
|
.PARAMETER DirectorUrl
|
|
|
|
|
The URL pointing directly to the Icinga Web 2 Director module
|
|
|
|
|
.PARAMETER Hostname
|
|
|
|
|
The name of the current host to register within the Icinga Director
|
|
|
|
|
.PARAMETER ApiKey
|
|
|
|
|
The template key to authenticate against the Self-Service API
|
|
|
|
|
.PARAMETER Endpoint
|
|
|
|
|
The IP or FQDN to one of the parent Icinga 2 nodes this Agent will connect to
|
|
|
|
|
for determining which network interface shall be used by Icinga for connecting
|
|
|
|
|
and to apply hostalive/ping checks to
|
|
|
|
|
.INPUTS
|
|
|
|
|
System.String
|
|
|
|
|
.OUTPUTS
|
|
|
|
|
System.Object
|
|
|
|
|
.LINK
|
|
|
|
|
https://github.com/Icinga/icinga-powershell-framework
|
|
|
|
|
#>
|
|
|
|
|
|
2019-10-06 10:56:59 -04:00
|
|
|
function Register-IcingaDirectorSelfServiceHost()
|
|
|
|
|
{
|
|
|
|
|
param(
|
|
|
|
|
$DirectorUrl,
|
|
|
|
|
$Hostname,
|
2019-12-12 09:49:56 -05:00
|
|
|
$ApiKey = $null,
|
|
|
|
|
[string]$Endpoint = $null
|
2019-10-06 10:56:59 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ([string]::IsNullOrEmpty($DirectorUrl)) {
|
|
|
|
|
throw 'Please enter a valid Url to your Icinga Director';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ([string]::IsNullOrEmpty($Hostname)) {
|
|
|
|
|
throw 'Please enter the hostname to use';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ([string]::IsNullOrEmpty($ApiKey)) {
|
|
|
|
|
throw 'Please enter the API key of the template you wish to use';
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-31 04:20:53 -04:00
|
|
|
Set-IcingaTLSVersion;
|
2019-10-06 10:56:59 -04:00
|
|
|
$ProgressPreference = "SilentlyContinue";
|
2019-12-12 09:49:56 -05:00
|
|
|
$DirectorConfigJson = $null;
|
|
|
|
|
|
2021-02-19 04:09:42 -05:00
|
|
|
if ([string]::IsNullOrEmpty($Endpoint)) {
|
|
|
|
|
if ($DirectorUrl.Contains('https://') -Or $DirectorUrl.Contains('http://')) {
|
|
|
|
|
$Endpoint = $DirectorUrl.Split('/')[2];
|
|
|
|
|
} else {
|
|
|
|
|
$Endpoint = $DirectorUrl.Split('/')[0];
|
|
|
|
|
}
|
2019-12-12 09:49:56 -05:00
|
|
|
}
|
2019-10-06 10:56:59 -04:00
|
|
|
|
2021-02-19 04:09:42 -05:00
|
|
|
$Interface = Get-IcingaNetworkInterface $Endpoint;
|
|
|
|
|
$DirectorConfigJson = [string]::Format('{0} "address":"{2}" {1}', '{', '}', $Interface);
|
|
|
|
|
|
2019-11-02 12:42:39 -04:00
|
|
|
$EndpointUrl = Join-WebPath -Path $DirectorUrl -ChildPath ([string]::Format('/self-service/register-host?name={0}&key={1}', $Hostname, $ApiKey));
|
2019-10-06 10:56:59 -04:00
|
|
|
|
2020-11-19 10:59:31 -05:00
|
|
|
$response = Invoke-IcingaWebRequest -Uri $EndpointUrl -UseBasicParsing -Headers @{ 'accept' = 'application/json'; 'X-Director-Accept' = 'application/json' } -Method 'POST' -Body $DirectorConfigJson;
|
2019-10-06 10:56:59 -04:00
|
|
|
|
|
|
|
|
if ($response.StatusCode -ne 200) {
|
|
|
|
|
throw $response.Content;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$JsonContent = ConvertFrom-Json -InputObject $response.Content;
|
|
|
|
|
|
|
|
|
|
if (Test-PSCustomObjectMember -PSObject $JsonContent -Name 'error') {
|
2019-11-02 12:42:39 -04:00
|
|
|
if ($JsonContent.error -like '*already been registered*') {
|
|
|
|
|
return $null;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-06 10:56:59 -04:00
|
|
|
throw 'Icinga Director Self-Service has thrown an error: ' + $JsonContent.error;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-02 12:42:39 -04:00
|
|
|
Set-IcingaPowerShellConfig -Path 'IcingaDirector.SelfService.ApiKey' -Value $JsonContent;
|
|
|
|
|
|
2020-05-13 10:53:15 -04:00
|
|
|
Write-IcingaConsoleNotice 'Host was successfully registered within Icinga Director';
|
2019-12-06 14:28:09 -05:00
|
|
|
|
2019-10-06 10:56:59 -04:00
|
|
|
return $JsonContent;
|
|
|
|
|
}
|