2020-04-28 09:24:57 -04:00
<#
. SYNOPSIS
2022-01-14 16:18:59 -05:00
Register the current host within the Icinga Director by using the
2020-04-28 09:24:57 -04:00
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
2022-01-14 16:18:59 -05:00
to allow the host to fetch detailed configurations like zones and endpoints
2020-04-28 09:24:57 -04:00
. 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
2021-09-09 12:53:48 -04:00
$response = Invoke-IcingaWebRequest -Uri $EndpointUrl -UseBasicParsing -Headers @ { 'accept' = 'application/json' ; 'X-Director-Accept' = 'application/json' } -Method 'POST' -Body $DirectorConfigJson -NoErrorMessage ;
2019-10-06 10:56:59 -04:00
if ( $response . StatusCode -ne 200 ) {
2021-09-09 12:53:48 -04:00
$ErrorMessage = '' ;
switch ( $response . StatusCode ) {
400 {
Write-IcingaConsoleWarning 'Failed to register host inside Icinga Director. The host is probably already registered.'
return $null ;
} ;
404 {
$ErrorMessage = 'Failed to register host with the given API key "{0}" inside Icinga Director. Please ensure the template key you are using is correct and the template is set as "Icinga2 Agent" object. Non-Agent templates will not work over the Self-Service API.' ;
break ;
} ;
901 {
$ErrorMessage = 'Failed to register host over Self-Service API inside Icinga Director because of SSL/TLS error. Please ensure the certificate is valid and use "Enable-IcingaUntrustedCertificateValidation" for self-signed certificates or install the certificate on this machine.' ;
break ;
}
Default {
$ErrorMessage = ( [ string ] :: Format ( 'Failed to register host inside Icinga Director because of unhandled exception: {0}' , $response . StatusCode ) ) ;
break ;
} ;
}
Write-IcingaConsoleError $ErrorMessage -Objects $ApiKey ;
throw $ErrorMessage ;
2019-10-06 10:56:59 -04:00
}
$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 ;
}