-
Notifications
You must be signed in to change notification settings - Fork 0
/
install_agent.ps1
253 lines (190 loc) · 10.1 KB
/
install_agent.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<#-------------------------------------------------#
####---- Zabbix Agent 2 Installer for Win ----####
#################---- v2.2.1 ----#################
#########---- by Pizzi della Maremma ----#########
#-------------------------------------------------#>
# Auto-admin restart
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit }
### .conf file creator
function CompileFile($ip, $port, $hn, $metadata, $defpath, $confpath, $confname) {
Add-Content -Path "$defpath\$confname" -Value "##### Zabbix Configuration file from Powershell #####"
Add-Content -Path "$defpath\$confname" -Value ""
Add-Content -Path "$defpath\$confname" -Value ""
Add-Content -Path "$defpath\$confname" -Value "## Global settings ##"
Add-Content -Path "$defpath\$confname" -Value "LogType=file"
Add-Content -Path "$defpath\$confname" -Value "LogFile=$defpath\zabbix_agent2.log"
Add-Content -Path "$defpath\$confname" -Value "LogFileSize=1"
Add-Content -Path "$defpath\$confname" -Value ""
Add-Content -Path "$defpath\$confname" -Value ""
Add-Content -Path "$defpath\$confname" -Value "## Passive check related ##"
Add-Content -Path "$defpath\$confname" -Value "Server=$ip"
Add-Content -Path "$defpath\$confname" -Value "ListenPort=$port"
Add-Content -Path "$defpath\$confname" -Value ""
Add-Content -Path "$defpath\$confname" -Value ""
Add-Content -Path "$defpath\$confname" -Value "## Active check related ##"
Add-Content -Path "$defpath\$confname" -Value "ServerActive=$ip"
Add-Content -Path "$defpath\$confname" -Value "Hostname=$hn"
if ($metadata -ne "") {Add-Content -Path "$defpath\$confname" -Value "HostMetadata=$metadata"}
Add-Content -Path "$defpath\$confname" -Value ""
Add-Content -Path "$defpath\$confname" -Value ""
Add-Content -Path "$defpath\$confname" -Value "## Other params ##"
Add-Content -Path "$defpath\$confname" -Value "UnsafeUserParameters=1"
Add-Content -Path "$defpath\$confname" -Value "Include=$confpath\*.conf"
Add-Content -Path "$defpath\$confname" -Value "LogFile=$defpath\zabbix_agent2.log"
Add-Content -Path "$defpath\$confname" -Value ""
Add-Content -Path "$defpath\$confname" -Value ""
}
function CompileCommands($defpath) {
# create dir in default path
New-Item "$defpath" -ItemType directory -Name "commands" | Out-Null
# create file and append content
New-Item "$defpath\commands" -ItemType file -Name "cmd_install.cmd" | Out-Null
Add-Content -Path "$defpath\commands\cmd_install.cmd" -Value "$defpath\zabbix_agent2.exe --config $defpath\zabbix_agent2.conf --install"
New-Item "$defpath\commands" -ItemType file -Name "cmd_start.cmd" | Out-Null
Add-Content -Path "$defpath\commands\cmd_start.cmd" -Value "$defpath\zabbix_agent2.exe --config $defpath\zabbix_agent2.conf --start"
New-Item "$defpath\commands" -ItemType file -Name "cmd_stop.cmd" | Out-Null
Add-Content -Path "$defpath\commands\cmd_stop.cmd" -Value "$defpath\zabbix_agent2.exe --config $defpath\zabbix_agent2.conf --stop"
New-Item "$defpath\commands" -ItemType file -Name "cmd_uninstall.cmd" | Out-Null
Add-Content -Path "$defpath\commands\cmd_uninstall.cmd" -Value "$defpath\zabbix_agent2.exe --config $defpath\zabbix_agent2.conf --uninstall"
}
Clear-Host
Write-Host -ForegroundColor Yellow "Zabbix Agent 2 configuration creator v2.2.1"
Write-Host ""
################### If service already exists ###################
If (Get-Service "Zabbix Agent 2" -ErrorAction SilentlyContinue) {
if ((Get-Service "Zabbix Agent 2").Status -eq 'Running') {
Write-Host "Zabbix agent already installed and running! Stopping and uninstalling..."
try {
Start-Process C:\Zabbix\zabbix_agent2.exe -Verb runAs -ArgumentList "--config C:\Zabbix\zabbix_agent2.conf --stop" -Wait
Write-Host "Agent stopped successfully!"
Start-Sleep -Seconds 2
Start-Process C:\Zabbix\zabbix_agent2.exe -Verb runAs -ArgumentList "--config C:\Zabbix\zabbix_agent2.conf --uninstall" -Wait
Write-Host "Agent uninstalled successfully!"
} catch {Write-Host -ForegroundColor Red "cannot stop or uninstall agent service!"}
if ((Test-Path -Path "C:\Zabbix") -eq $True) {Remove-Item -Path C:\Zabbix -Recurse}
Write-Host "Previous agent uninstalled. Relaunch the script in order to install agent."
Read-Host
exit
} else {
Write-Host "Zabbix agent already installed! Uninstalling..."
Write-Host ""
Write-Host ""
try {
Start-Process C:\Zabbix\zabbix_agent2.exe -Verb runAs -ArgumentList "--config C:\Zabbix\zabbix_agent2.conf --uninstall" -Wait
Write-Host "Agent uninstall successfully!"
} catch {Write-Host -ForegroundColor Red "cannot uninstall agent service!"}
Start-Sleep -Seconds 2
if ((Test-Path -Path "C:\Zabbix") -eq $True) {Remove-Item -Path C:\Zabbix -Recurse}
Write-Host "Previous agent uninstalled. Relaunch the script in order to install agent."
Read-Host
exit
}
} else {
Write-Host "no Zabbix agent installed found, beginning installation..."
Write-Host ""
Write-Host "---------------------------------------------------------------------------------"
Write-Host ""
}
################### Create folders and copy files ###################
$prefix = Read-Host "Client name prefix [same name of the host group, leave blank for exclude the prefix]"
if ($prefix -eq "") {$hn = $env:computername}
else {$hn = "$prefix $env:computername"}
$ip = Read-Host "IP address of server/proxy"
$port = Read-Host "Port of server/proxy [default 10050]"
if ($port -eq "") {$port = 10050}
Write-Host ""
Write-Host "Host metadata needs to be the same for all agents to connect automatically to an Autoreg rule on server side."
Write-Host "[leave blank to not write this line in the conf file]"
$metadata = Read-Host "Host metadata"
$defpath = "C:\Zabbix"
$confname = "zabbix_agent2.conf"
$exename = "zabbix_agent2.exe"
$scriptpath = "$defpath\scripts"
$confpath = "$defpath\customconfigs"
### Make dir in dafault path
if ((Test-Path $defpath) -eq $false) {
New-Item "$defpath" -ItemType "directory" | Out-Null
}
### Make empty text file .CONF
$l = Test-Path "$defpath\$confname"
if ($l -eq $True) {
do {
$Prompt = Read-Host "$confname already exists! Want to replace it? [y/n]"
Switch ($Prompt) {
y {
Remove-Item -Path "$defpath\$confname"
New-Item "$defpath" -ItemType file -Name "$confname" | Out-Null
CompileFile $ip $port $hn $metadata $defpath $confpath $confname
}
n {
Write-Host "exiting..."
Read-Host
exit
}
}
} while ($Prompt -notmatch "[YN]")
}
if ($l -eq $False) {
New-Item "$defpath" -ItemType file -Name "$confname" | Out-Null
CompileFile $ip $port $hn $metadata $defpath $confpath $confname
}
### Make folders in subdir
if ((Test-Path "$scriptpath") -eq $False) {New-Item "$scriptpath" -ItemType "directory" | Out-Null}
### Make folder for custom configs in subdir
if ((Test-Path "$confpath") -eq $False) {
New-Item "$confpath" -ItemType "directory" | Out-Null
New-Item "$confpath\place_custom_parameters_here.conf" -ItemType "file" | Out-Null
}
### Copy CMDs for service control ### In test crea tutto da zero
if ((Test-Path "$defpath\commands") -eq $False) {CompileCommands $defpath}
### Copy .exe file
if ((Test-Path "$defpath\$exename") -eq $False) {
if ((Test-Path "$PSScriptRoot\$exename") -eq $True) {
Copy-Item -Path "$PSScriptRoot\$exename" -Destination "$defpath"
}
}
################### Firewall rule ###################
Write-Host ""
$k = Get-NetFirewallRule -DisplayName 'Zabbix Agent TCP inbound connection' 2> $null
if ($k) {
Write-Host "Firewall rule already exists."
}
else {
do {
$Prompt = Read-Host "Create firewall rule for inbound connection on TCP $port ? [y/n]"
Switch ($Prompt) {
y {
try {
New-NetFirewallRule -DisplayName "Zabbix Agent TCP inbound connection" -Direction Inbound -LocalPort $port -Protocol TCP -Action Allow
Write-Host -ForegroundColor Green "Firewall rule configured!"
}
catch {
Write-Host -ForegroundColor Red "Error opening port! Check it manually!"
}
}
n {
Write-Host "No firewall rule added."
}
}
Write-Host ""
} while ($Prompt -notmatch "[YN]")
}
Start-Sleep -Seconds 2
################### Install and run the service ###################
try {
Start-Process "$defpath\$exename" -Verb runAs -ArgumentList "--config $defpath\$confname --install" -Wait
Write-Host -ForegroundColor Green "Agent installed successfully!"
} catch {Write-Host -ForegroundColor Red "cannot install agent service!"}
try {
sc.exe failure "Zabbix Agent 2" reset= 60000 actions= restart/30000/restart/60000/restart/90000
Write-Host -ForegroundColor Green "Agent service recovery restarts configured successfully!"
} catch {Write-Host -ForegroundColor Red "cannot configure restart retries on agent service!"}
Start-Sleep -Seconds 2
try {
Start-Process "$defpath\$exename" -Verb runAs -ArgumentList "--config $defpath\$confname --start" -Wait
Write-Host -ForegroundColor Green "Agent started successfully!"
} catch {Write-Host -ForegroundColor Red "cannot start agent service!"}
Write-Host "Finished! Run with cmds in $defpath\commands folder or from Windows Services page."
Write-Host "Press enter key to exit..."
Read-Host
exit