This commit is contained in:
Alexander Stoll 2019-10-31 15:01:34 +01:00
commit 0d16c72db1
14 changed files with 199 additions and 8 deletions

View file

@ -12,7 +12,10 @@ Getting Started
[Net.ServicePointManager]::SecurityProtocol = "tls12, tls11"; [Net.ServicePointManager]::SecurityProtocol = "tls12, tls11";
$ProgressPreference = "SilentlyContinue"; $ProgressPreference = "SilentlyContinue";
$Script = (Invoke-WebRequest -UseBasicParsing -Uri 'https://raw.githubusercontent.com/LordHepipud/icinga-framework-kickstart/master/script/icinga-framework-kickstart.ps1').Content; $global:IcingaFrameworkKickstartSource = 'https://raw.githubusercontent.com/LordHepipud/icinga-framework-kickstart/master/script/icinga-framework-kickstart.ps1';
$Script = (Invoke-WebRequest -UseBasicParsing -Uri $global:IcingaFrameworkKickstartSource).Content;
$Script += "`r`n`r`n Start-IcingaFrameworkWizard;";
Invoke-Command -ScriptBlock ([Scriptblock]::Create($Script)); Invoke-Command -ScriptBlock ([Scriptblock]::Create($Script));
``` ```

View file

@ -25,7 +25,7 @@ Description = 'Icinga 2 Windows Agent Module, which allows to entirely monitor t
PowerShellVersion = '3.0' PowerShellVersion = '3.0'
# Aus diesem Modul zu exportierende Funktionen. Um optimale Leistung zu erzielen, verwenden Sie keine Platzhalter und löschen den Eintrag nicht. Verwenden Sie ein leeres Array, wenn keine zu exportierenden Funktionen vorhanden sind. # Aus diesem Modul zu exportierende Funktionen. Um optimale Leistung zu erzielen, verwenden Sie keine Platzhalter und löschen den Eintrag nicht. Verwenden Sie ein leeres Array, wenn keine zu exportierenden Funktionen vorhanden sind.
FunctionsToExport = @( 'Use-Icinga', 'Import-IcingaLib', 'Publish-IcingaModuleManifests', 'Get-IcingaPluginDir', 'Get-IcingaCustomPluginDir', 'Get-IcingaCacheDir', 'Get-IcingaPowerShellConfigDir' ) FunctionsToExport = @( 'Use-Icinga', 'Import-IcingaLib', 'Publish-IcingaModuleManifests', 'Get-IcingaPluginDir', 'Get-IcingaCustomPluginDir', 'Get-IcingaCacheDir', 'Get-IcingaPowerShellConfigDir', 'Get-IcingaPowerShellModuleFile' )
# Aus diesem Modul zu exportierende Cmdlets. Um optimale Leistung zu erzielen, verwenden Sie keine Plat'zhalter und löschen den Eintrag nicht. Verwenden Sie ein leeres Array, wenn keine zu exportierenden Cmdlets vorhanden sind. # Aus diesem Modul zu exportierende Cmdlets. Um optimale Leistung zu erzielen, verwenden Sie keine Plat'zhalter und löschen den Eintrag nicht. Verwenden Sie ein leeres Array, wenn keine zu exportierenden Cmdlets vorhanden sind.
CmdletsToExport = @() CmdletsToExport = @()

View file

@ -165,3 +165,8 @@ function Get-IcingaPowerShellConfigDir()
{ {
return (Join-Path -Path $PSScriptRoot -ChildPath 'config'); return (Join-Path -Path $PSScriptRoot -ChildPath 'config');
} }
function Get-IcingaPowerShellModuleFile()
{
return (Join-Path -Path $PSScriptRoot -ChildPath 'icinga-module-windows.psm1');
}

View file

@ -0,0 +1,23 @@
function Expand-IcingaZipArchive()
{
param(
$Path,
$Destination
);
if ((Test-Path $Path) -eq $FALSE -Or (Test-Path $Destination) -eq $FALSE) {
Write-Host 'The path to the zip archive or the destination path do not exist';
return $FALSE;
}
Add-Type -AssemblyName System.IO.Compression.FileSystem;
try {
[System.IO.Compression.ZipFile]::ExtractToDirectory($Path, $Destination);
return $TRUE;
} catch {
throw $_.Exception;
}
return $FALSE;
}

View file

@ -0,0 +1,47 @@
function Get-IcingaFrameworkServiceBinary()
{
param(
[string]$FrameworkServiceUrl,
[string]$ServiceDirectory
);
$ProgressPreference = "SilentlyContinue";
if ([string]::IsNullOrEmpty($FrameworkServiceUrl)) {
if ((Get-IcingaAgentInstallerAnswerInput -Prompt 'Do you provide a custom source of the service binary?' -Default 'n').result -eq 1) {
$LatestRelease = (Invoke-WebRequest -Uri 'https://github.com/LordHepipud/icinga-windows-service/releases/latest' -UseBasicParsing).BaseResponse.ResponseUri.AbsoluteUri;
$FrameworkServiceUrl = $LatestRelease.Replace('/tag/', '/download/');
$Tag = $FrameworkServiceUrl.Split('/')[-1];
$FrameworkServiceUrl = [string]::Format('{0}/icinga-service-{1}.zip', $FrameworkServiceUrl, $Tag);
} else {
$FrameworkServiceUrl = (Get-IcingaAgentInstallerAnswerInput -Prompt 'Please enter the full path to your service binary repository' -Default 'v').answer;
}
}
if ([string]::IsNullOrEmpty($ServiceDirectory)) {
$ServiceDirectory = (Get-IcingaAgentInstallerAnswerInput -Prompt 'Please enter the path you wish to install the service to' -Default 'v' -DefaultInput 'C:\Program Files\icinga-framework-service\').answer;
}
if ((Test-Path $ServiceDirectory) -eq $FALSE) {
New-Item -Path $ServiceDirectory -Force -ItemType Directory | Out-Null;
}
$ZipArchive = Join-Path -Path $ServiceDirectory -ChildPath ($FrameworkServiceUrl.Split('/')[-1]);
$ServiceBin = Join-Path -Path $ServiceDirectory -ChildPath 'icinga-service.exe';
Invoke-WebRequest -Uri $FrameworkServiceUrl -UseBasicParsing -OutFile $ZipArchive;
if ((Expand-IcingaZipArchive -Path $ZipArchive -Destination $ServiceDirectory) -eq $FALSE) {
throw 'Failed to expand the downloaded ZIP archive';
}
if ((Test-IcingaZipBinaryChecksum -Path $ServiceBin) -eq $FALSE) {
throw 'The checksum of the downloaded file and the required MD5 hash are not matching';
}
return @{
'FrameworkServiceUrl' = $FrameworkServiceUrl;
'ServiceDirectory' = $ServiceDirectory;
'ServiceBin' = $ServiceBin;
};
}

View file

@ -0,0 +1,26 @@
function Install-IcingaFrameworkService()
{
param(
$Path,
$User,
[SecureString]$Password
);
if ((Test-Path $Path) -eq $FALSE) {
throw 'Please specify the path directly to the service binary';
}
$Path = [string]::Format(
'{0} \"{1}\"',
$Path,
(Get-IcingaPowerShellModuleFile)
);
$ServiceCreation = Start-IcingaProcess -Executable 'sc.exe' -Arguments ([string]::Format('create icingapowershell binPath= "{0}" DisplayName= "Icinga PowerShell Service" start= auto', $Path));
if ($ServiceCreation.ExitCode -ne 0) {
throw ([string]::Format('Failed to install Icinga PowerShell Service: {0}{1}', $ServiceCreation.Message, $ServiceCreation.Error));
}
return (Set-IcingaAgentServiceUser -User $User -Password $Password -Service 'icingapowershell');
}

View file

@ -0,0 +1,10 @@
function Restart-IcingaService()
{
param(
$Service
);
if (Get-Service $Service -ErrorAction SilentlyContinue) {
Restart-Service $Service;
}
}

View file

@ -0,0 +1,10 @@
function Start-IcingaService()
{
param(
$Service
);
if (Get-Service $Service -ErrorAction SilentlyContinue) {
Start-Service $Service;
}
}

View file

@ -0,0 +1,10 @@
function Stop-IcingaService()
{
param(
$Service
);
if (Get-Service $Service -ErrorAction SilentlyContinue) {
Stop-Service $Service;
}
}

View file

@ -0,0 +1,23 @@
function Test-IcingaZipBinaryChecksum()
{
param(
$Path
);
$MD5Path = [string]::Format('{0}.md5', $Path);
if ((Test-Path $MD5Path) -eq $FALSE) {
return $TRUE;
}
[string]$MD5Checksum = Get-Content $MD5Path;
$MD5Checksum = ($MD5Checksum.Split(' ')[0]).ToLower();
$FileHash = ((Get-FileHash $Path -Algorithm MD5).Hash).ToLower();
if ($MD5Checksum -ne $FileHash) {
return $FALSE;
}
return $TRUE;
}

View file

@ -9,6 +9,8 @@ function Uninstall-IcingaAgent()
Write-Host 'Removing current installed Icinga Agent'; Write-Host 'Removing current installed Icinga Agent';
Stop-IcingaService 'icinga2';
$Uninstaller = Start-IcingaProcess -Executable 'MsiExec.exe' -Arguments ([string]::Format('{0} /q', $IcingaData.Uninstaller)) -FlushNewLine; $Uninstaller = Start-IcingaProcess -Executable 'MsiExec.exe' -Arguments ([string]::Format('{0} /q', $IcingaData.Uninstaller)) -FlushNewLine;
if ($Uninstaller.ExitCode -ne 0) { if ($Uninstaller.ExitCode -ne 0) {

View file

@ -24,7 +24,11 @@ function Start-IcingaAgentInstallWizard()
[switch]$RunInstaller, [switch]$RunInstaller,
[switch]$Reconfigure, [switch]$Reconfigure,
[string]$ServiceUser, [string]$ServiceUser,
[securestring]$ServicePass = $null [securestring]$ServicePass = $null,
$InstallFrameworkService = $null,
$FrameworkServiceUrl = $null,
$ServiceDirectory = $null,
$ServiceBin = $null
); );
[array]$InstallerArguments = @(); [array]$InstallerArguments = @();
@ -249,6 +253,20 @@ function Start-IcingaAgentInstallWizard()
} }
} }
if ($null -eq $InstallFrameworkService) {
if ((Get-IcingaAgentInstallerAnswerInput -Prompt 'Do you want to install the PowerShell Framework as Service?' -Default 'y').result -eq 1) {
$result = Get-IcingaFrameworkServiceBinary;
$InstallerArguments += "-InstallFrameworkService 1";
$InstallerArguments += [string]::Format("-FrameworkServiceUrl '{0}'", $result.FrameworkServiceUrl);
$InstallerArguments += [string]::Format("-ServiceDirectory '{0}'", $result.ServiceDirectory);
$InstallerArguments += [string]::Format("-ServiceBin '{0}'", $result.ServiceBin);
$ServiceBin = $result.ServiceBin;
}
} elseif ($InstallFrameworkService -eq $TRUE) {
$result = Get-IcingaFrameworkServiceBinary -FrameworkServiceUrl $FrameworkServiceUrl -ServiceDirectory $ServiceDirectory;
$ServiceBin = $result.ServiceBin;
}
if ($InstallerArguments.Count -ne 0) { if ($InstallerArguments.Count -ne 0) {
$InstallerArguments += "-RunInstaller"; $InstallerArguments += "-RunInstaller";
Write-Host 'The wizard is complete. These are the configured settings:'; Write-Host 'The wizard is complete. These are the configured settings:';
@ -276,12 +294,15 @@ function Start-IcingaAgentInstallWizard()
Set-IcingaAcl "$Env:ProgramData\icinga2\etc"; Set-IcingaAcl "$Env:ProgramData\icinga2\etc";
Set-IcingaAcl "$Env:ProgramData\icinga2\var"; Set-IcingaAcl "$Env:ProgramData\icinga2\var";
Set-IcingaAcl (Get-IcingaCacheDir); Set-IcingaAcl (Get-IcingaCacheDir);
Install-IcingaFrameworkService -Path $ServiceBin -User $ServiceUser -Password $ServicePass | Out-Null;
Register-IcingaBackgroundDaemon -Command 'Start-IcingaServiceCheckDaemon';
Install-IcingaAgentBaseFeatures; Install-IcingaAgentBaseFeatures;
Install-IcingaAgentCertificates -Hostname $Hostname -Endpoint $CAEndpoint -Port $CAPort -CACert $CAFile -Ticket $Ticket | Out-Null; Install-IcingaAgentCertificates -Hostname $Hostname -Endpoint $CAEndpoint -Port $CAPort -CACert $CAFile -Ticket $Ticket | Out-Null;
Write-IcingaAgentApiConfig -Port $CAPort; Write-IcingaAgentApiConfig -Port $CAPort;
Write-IcingaAgentZonesConfig -Endpoints $Endpoints -EndpointConnections $EndpointConnections -ParentZone $ParentZone -GlobalZones $GlobalZoneConfig -Hostname $Hostname; Write-IcingaAgentZonesConfig -Endpoints $Endpoints -EndpointConnections $EndpointConnections -ParentZone $ParentZone -GlobalZones $GlobalZoneConfig -Hostname $Hostname;
Test-IcingaAgent; Test-IcingaAgent;
Restart-Service icinga2; Restart-IcingaService 'icingapowershell';
Restart-IcingaService 'icinga2';
} }
} }
} }

View file

@ -2,7 +2,8 @@ function Set-IcingaAgentServiceUser()
{ {
param( param(
[string]$User, [string]$User,
[securestring]$Password [securestring]$Password,
[string]$Service = 'icinga2'
); );
if ([string]::IsNullOrEmpty($User)) { if ([string]::IsNullOrEmpty($User)) {
@ -14,14 +15,14 @@ function Set-IcingaAgentServiceUser()
$User = [string]::Format('.\{0}', $User); $User = [string]::Format('.\{0}', $User);
} }
$ArgString = 'config icinga2 obj= "{0}" password="{1}"'; $ArgString = 'config {0} obj= "{1}" password="{2}"';
if($null -eq $Password) { if($null -eq $Password) {
$ArgString = 'config icinga2 obj= "{0}"{1}'; $ArgString = 'config {0} obj= "{1}"{2}';
} }
$Output = Start-IcingaProcess ` $Output = Start-IcingaProcess `
-Executable 'sc.exe' ` -Executable 'sc.exe' `
-Arguments ([string]::Format($ArgString, $User, (ConvertFrom-IcingaSecureString $Password))) ` -Arguments ([string]::Format($ArgString, $Service, $User, (ConvertFrom-IcingaSecureString $Password))) `
-FlushNewLines $TRUE; -FlushNewLines $TRUE;
if ($Output.ExitCode -eq 0) { if ($Output.ExitCode -eq 0) {

View file

@ -1,5 +1,9 @@
function Start-IcingaPowerShellDaemon() function Start-IcingaPowerShellDaemon()
{ {
param(
[switch]$RunAsService
);
$ScriptBlock = { $ScriptBlock = {
param($IcingaDaemonData); param($IcingaDaemonData);
@ -32,4 +36,10 @@ function Start-IcingaPowerShellDaemon()
$global:IcingaDaemonData.Add('Config', (Read-IcingaPowerShellConfig)); $global:IcingaDaemonData.Add('Config', (Read-IcingaPowerShellConfig));
New-IcingaThreadInstance -Name "Icinga_PowerShell_Background_Daemon" -ThreadPool $IcingaDaemonData.IcingaThreadPool.BackgroundPool -ScriptBlock $ScriptBlock -Arguments @( $global:IcingaDaemonData ) -Start; New-IcingaThreadInstance -Name "Icinga_PowerShell_Background_Daemon" -ThreadPool $IcingaDaemonData.IcingaThreadPool.BackgroundPool -ScriptBlock $ScriptBlock -Arguments @( $global:IcingaDaemonData ) -Start;
if ($RunAsService) {
while ($TRUE) {
Start-Sleep -Seconds 100;
}
}
} }