forked from juangranados/nagios-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install_icinga.ps1
93 lines (88 loc) · 3.17 KB
/
install_icinga.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<#
.SYNOPSIS
Check version Icinga Windows Agent.
.DESCRIPTION
Check version Icinga Windows Agent and optionally updates it.
.OUTPUTS
OK: Version up to date.
WARNING: Version needs updating.
CRITICAL: Error updating agent.
.PARAMETER downloadURL
Icinga agent last version download url.
Default: "http://packages.icinga.com/windows/Icinga2-v2.11.11-x86_64.msi"
.PARAMETER changeLoginToSystem
Changes login service from Network Service to System.
.EXAMPLE
install_icinga.ps1 -downloadURL "http://packages.icinga.com/windows/Icinga2-v2.11.11-x86_64.msi" -changeLoginToSystem
.NOTES
Author: Juan Granados
#>
Param(
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$downloadURL = "http://packages.icinga.com/windows/Icinga2-v2.11.11-x86_64.msi",
[Parameter()]
[switch]$changeLoginToSystem
)
#Requires -RunAsAdministrator
Function StartProcess {
param([string]$executable, [bool]$flushNewLines, [string]$arguments);
$processData = New-Object System.Diagnostics.ProcessStartInfo;
$processData.FileName = $executable;
$processData.RedirectStandardError = $true;
$processData.RedirectStandardOutput = $true;
$processData.UseShellExecute = $false;
$processData.Arguments = $arguments;
$process = New-Object System.Diagnostics.Process;
$process.StartInfo = $processData;
$process.Start() | Out-Null;
$stdout = $process.StandardOutput.ReadToEnd();
$stderr = $process.StandardError.ReadToEnd();
$process.WaitForExit();
if ($flushNewLines) {
$stdout = $stdout.Replace("`n", '').Replace("`r", '');
$stderr = $stderr.Replace("`n", '').Replace("`r", '');
}
else {
if ($stdout.Contains("`n")) {
$stdout = $stdout.Substring(0, $stdout.LastIndexOf("`n"));
}
}
$r = @{};
$r.Add('message', $stdout);
$r.Add('error', $stderr);
$r.Add('exitcode', $process.ExitCode);
return $r;
}
Start-Transcript "$($PSScriptRoot)\install_icinga2.txt"
$localInstaller = "$($PSScriptRoot)\icinga2.msi"
Write-Host "Downloading icinga from $downloadURL"
(New-Object System.Net.WebClient).DownloadFile($downloadURL, $localInstaller)
Write-Host "Unlocking $localInstaller"
Unblock-File $localInstaller
Write-Host "Installing icinga"
$R = StartProcess "msiexec.exe" $false "/I $localInstaller /qn /norestart"
if ($R.exitcode -ne 0) {
Write-Host "Error installing Icinga2"
Write-Host $R.error
Remove-Item $localInstaller -Force
Stop-Transcript
Exit(2)
}
Write-Host "Removing $localInstaller"
Remove-Item $localInstaller -Force
if ($changeLoginToSystem) {
Write-Host "Change servide login to System"
(Get-Service icinga2).WaitForStatus('Running')
$changeServiceResult = Get-CimInstance win32_service -filter "name='icinga2'" | Invoke-CimMethod -Name Change -Arguments @{StartName = "LocalSystem" }
if ($changeServiceResult.ReturnValue -ne 0) {
Write-Host "Error changing icinga2 login to System"
Stop-Transcript
Exit(2)
}
Write-Host "Restarting icinga service"
Restart-Service icinga2
}
Write-Output "OK: Version updated to: $((Get-Item 'C:\Program Files\ICINGA2\sbin\icinga2.exe').VersionInfo.ProductVersion)"
Stop-Transcript
Exit(0)