forked from Twikki/Zabbix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZabbix_Automated_Install_Main_Update.ps1
343 lines (220 loc) · 12.4 KB
/
Zabbix_Automated_Install_Main_Update.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# Zabbix Powershell script that installs the agent, or updates the agent and configuration
# Tested on Windows Server 2012, 2012 R2, 2016, 2019
# Version 1.1
# Installs Zabbix Agent 5.0.0 with SSL
#
# Variables
#
# URL to zabbix agent download.
$Url = "https://www.zabbix.com/downloads/4.4.6/zabbix_agent-4.4.6-windows-amd64-openssl.zip"
$GithubToken = "Yourtokenhere"
$Githubrepo = "youtubetest"
$newestagentversion = "4.4.6"
$RequredPowershell = 5
$InstalledPowershell = $PSVersionTable.PSVersion.Major
$counter = 0
$ZabbixBackupFolderPath = "C:\zabbixbackup\"
$ZabbixFolderPath = "C:\Zabbix\"
# Checks Powershell version before executing anything, Exits if lower than version 5.
If ($InstalledPowershell -lt $RequredPowershell)
{
Write-Host "Requred Powershell version is not installed, Required Powershell is version" $RequredPowershell - "But Version" $InstalledPowershell "is installed"
exit
}
function Unzip
{
param([string]$zipfile, [string]$outpath)
[System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}
# Function that provides backup for Zabbix folder
Function ZabbixBackup
{
$FolderPathTest = Test-Path $ZabbixBackupFolderPath
If ($FolderPathTest -eq $false)
{
mkdir c:\zabbixbackup
}
# Copies the files from the zabbix folder to the newly created folder with the date
Copy-Item $ZabbixFolderPath -Recurse -Destination "C:\zabbixbackup\Backup_$((Get-Date).ToString('dd-MM-yyyy_hh.mm.ss'))"
}
# Function that installs Zabbix agent
Function ZabbixInstall
{
# Creates Zabbix directory
mkdir c:\zabbix
# Creates the scripts folder in the zabbix folder
mkdir c:\zabbix\scripts
# Downloads version 4.4.6 with SSL from https://www.zabbix.com/download_agents
Invoke-WebRequest $url -outfile c:\zabbix\zabbix.zip
Add-Type -AssemblyName System.IO.Compression.FileSystem
# Unzipping file to c:\zabbix
Unzip "c:\zabbix\zabbix.zip" "c:\zabbix"
# 32 bit has been removed in V. 4.4.6, so it's copying the only Agent file.
Move-Item C:\zabbix\bin\zabbix_agentd.exe -Destination c:\zabbix\
# Removes the original config file
Remove-Item C:\zabbix\conf\zabbix_agentd.conf
# Downloads the latest configuration file from Github.com
$configfileagent = Invoke-RestMethod https://api.github.com/repos/Twikki/$Githubrepo/contents/zabbix_agentd.win.conf?access_token=$GithubToken -Headers @{”Accept”= “application/vnd.github.v3.raw”}
#Places the config file in c:\zabbix
Set-Content -Path 'C:\zabbix\zabbix_agentd.win.conf' -Value $configfileagent
# Downloads the latest metadata file from Github.com
$configfilemetadata = Invoke-RestMethod https://api.github.com/repos/Twikki/$Githubrepo/contents/zabbix_agentd.metadata.conf?access_token=$GithubToken -Headers @{”Accept”= “application/vnd.github.v3.raw”}
# Places the metadata file in c:\zabbix
Set-Content -Path 'C:\zabbix\conf\zabbix_agentd.metadata.conf' -Value $configfilemetadata
# Downloads the latest userparam file from Github.com
$configfileuserparam = Invoke-RestMethod https://api.github.com/repos/Twikki/$Githubrepo/contents/zabbix_agentd.userparams.conf?access_token=$GithubToken -Headers @{”Accept”= “application/vnd.github.v3.raw”}
# Places the userparam file in c:\zabbix
Set-Content -Path 'C:\zabbix\conf\zabbix_agentd.userparams.conf' -Value $configfileuserparam
# Downloads the latest show_config_versions.ps1 file from Github.com
$configversions = Invoke-RestMethod https://api.github.com/repos/Twikki/$Githubrepo/contents/show_config_versions.ps1?access_token=$GithubToken -Headers @{”Accept”= “application/vnd.github.v3.raw”}
# Places the show_config_versions.ps1 file in c:\zabbix\scripts
Set-Content -Path 'C:\zabbix\scripts\show_config_versions.ps1' -Value $configversions
# Downloads the latest show_agent_Version.ps1 file from Github.com
$agentversion = Invoke-RestMethod https://api.github.com/repos/Twikki/$Githubrepo/contents/show_agent_version.ps1?access_token=$GithubToken -Headers @{”Accept”= “application/vnd.github.v3.raw”}
# Places the show_agent_version.ps1 file in c:\zabbix\scripts
Set-Content -Path 'C:\zabbix\scripts\show_agent_version.ps1' -Value $agentversion
# Attempts to install the agent with the config in c:\zabbix
c:\zabbix\zabbix_agentd.exe --config c:\zabbix\zabbix_agentd.win.conf --install
# Attempts to start the agent
c:\zabbix\zabbix_agentd.exe --start
# Creates a firewall rule for the Zabbix server
New-NetFirewallRule -DisplayName "Allow Zabbix communication" -Direction Inbound -Program "c:\zabbix\zabbix_agentd.exe" -RemoteAddress LocalSubnet -Action Allow
}
# Function that Uninstalls Zabbix agent
Function ZabbixUninstall
{
# Attempts to stop the Zabbix service on the Windows machine
c:\zabbix\zabbix_agentd.exe --stop
# Attempts to uninstall the Zabbix agent on the Windows machine
c:\zabbix\zabbix_agentd.exe --uninstall
# Cleans up in c:\
Remove-Item c:\zabbix -Force -Recurse
# Cleans up logs in c:\
Remove-Item c:\zabbix_agentd.log
}
# Function that Maintains Zabbix configuration files
Function ZabbixMaintain
{ # Open the Zabbix Uninstall function bracket
# Makes a directory for temporary files
mkdir c:\zabbix\maintain
# Checks if zabbix_agentd.win.conf file exists on the server. If yes, it pulls the latest version from Github
$ChkFile = "C:\Zabbix\zabbix_agentd.win.conf"
$FileExists = Test-Path $ChkFile
If ($FileExists -eq $True) {
$configfileagent = Invoke-RestMethod https://api.github.com/repos/Twikki/$Githubrepo/contents/zabbix_agentd.win.conf?access_token=$GithubToken -Headers @{”Accept”= “application/vnd.github.v3.raw”}
Set-Content -Path 'C:\zabbix\maintain\zabbix_agentd.win.conf' -Value $configfileagent
# compares the file downloaded from Github with the existing file on the server. And replaces it if there is a content difference
if(Compare-Object -ReferenceObject $(Get-Content C:\zabbix\maintain\zabbix_agentd.win.conf) -DifferenceObject $(Get-Content c:\zabbix\zabbix_agentd.win.conf))
{Move-Item "C:\zabbix\maintain\zabbix_agentd.win.conf" "C:\zabbix\zabbix_agentd.win.conf" -Force
$counter++
}
}
# Checks if Zabbix_Agentd.userparams.conf file exists on the server. If yes, it pulls the latest version from Github
$ChkFile = "C:\Zabbix\conf\zabbix_agentd.userparams.conf"
$FileExists = Test-Path $ChkFile
If ($FileExists -eq $True) {
$configfileagent = Invoke-RestMethod https://api.github.com/repos/Twikki/$Githubrepo/contents/zabbix_agentd.userparams.conf?access_token=$GithubToken -Headers @{”Accept”= “application/vnd.github.v3.raw”}
Set-Content -Path 'C:\zabbix\maintain\zabbix_agentd.userparams.conf' -Value $configfileagent
# compares the file downloaded from Github with the existing file on the server. And replaces it if there is a content difference
if(Compare-Object -ReferenceObject $(Get-Content C:\zabbix\maintain\zabbix_agentd.userparams.conf) -DifferenceObject $(Get-Content c:\zabbix\conf\zabbix_agentd.userparams.conf))
{Move-Item "C:\zabbix\maintain\zabbix_agentd.userparams.conf" "C:\zabbix\conf\zabbix_agentd.userparams.conf" -Force
$counter++
}
}
# Checks if zabbix_agentd.metadata.conf file exists on the server. If yes, it pulls the latest version from Github
$ChkFile = "C:\Zabbix\conf\zabbix_agentd.metadata.conf"
$FileExists = Test-Path $ChkFile
If ($FileExists -eq $True) {
$configfileagent = Invoke-RestMethod https://api.github.com/repos/Twikki/$Githubrepo/contents/zabbix_agentd.metadata.conf?access_token=$GithubToken -Headers @{”Accept”= “application/vnd.github.v3.raw”}
Set-Content -Path 'C:\zabbix\maintain\zabbix_agentd.metadata.conf' -Value $configfileagent
# compares the file downloaded from Github with the existing file on the server. And replaces it if there is a content difference
if(Compare-Object -ReferenceObject $(Get-Content C:\zabbix\maintain\zabbix_agentd.metadata.conf) -DifferenceObject $(Get-Content c:\zabbix\conf\zabbix_agentd.metadata.conf))
{Move-Item "C:\zabbix\maintain\zabbix_agentd.metadata.conf" "C:\zabbix\conf\zabbix_agentd.metadata.conf" -Force
$counter++
}
}
# Checks if show_config_versions.ps1 file exists on the server. If yes, it pulls the latest version from Github
$ChkFile = "C:\Zabbix\scripts\show_config_versions.ps1"
$FileExists = Test-Path $ChkFile
If ($FileExists -eq $false)
{
# Downloads the latest show_config_versions.ps1 file from Github.com
$configversions = Invoke-RestMethod https://api.github.com/repos/Twikki/$Githubrepo/contents/show_config_versions.ps1?access_token=$GithubToken -Headers @{”Accept”= “application/vnd.github.v3.raw”}
# Places the show_config_versions.ps1 file in c:\zabbix\scripts
Set-Content -Path 'C:\zabbix\scripts\show_config_versions.ps1' -Value $configversions
$counter ++
}
else
{
$configfileagent = Invoke-RestMethod https://api.github.com/repos/Twikki/$Githubrepo/contents/show_config_versions.ps1?access_token=$GithubToken -Headers @{”Accept”= “application/vnd.github.v3.raw”}
Set-Content -Path 'C:\zabbix\maintain\show_config_versions.ps1' -Value $configfileagent
# compares the file downloaded from Github with the existing file on the server. And replaces it if there is a content difference
if(Compare-Object -ReferenceObject $(Get-Content C:\zabbix\maintain\show_config_versions.ps1) -DifferenceObject $(Get-Content c:\zabbix\scripts\show_config_versions.ps1))
{Move-Item "C:\zabbix\maintain\show_config_versions.ps1" "C:\zabbix\scripts\show_config_versions.ps1" -Force
$counter++
}
}
# Checks if show_agent_version.ps1 file exists on the server. If yes, it pulls the latest version from Github
$ChkFile = "C:\Zabbix\scripts\show_agent_version.ps1"
$FileExists = Test-Path $ChkFile
If ($FileExists -eq $false)
{
# Downloads the latest show_agent_Version.ps1 file from Github.com
$agentversion = Invoke-RestMethod https://api.github.com/repos/Twikki/$Githubrepo/contents/show_agent_version.ps1?access_token=$GithubToken -Headers @{”Accept”= “application/vnd.github.v3.raw”}
# Places the show_agent_version.ps1 file in c:\zabbix\scripts
Set-Content -Path 'C:\zabbix\scripts\show_agent_version.ps1' -Value $agentversion
$counter ++
}
else
{
$configfileagent = Invoke-RestMethod https://api.github.com/repos/Twikki/$Githubrepo/contents/show_agent_version.ps1?access_token=$GithubToken -Headers @{”Accept”= “application/vnd.github.v3.raw”}
Set-Content -Path 'C:\zabbix\maintain\show_agent_version.ps1' -Value $configfileagent
# compares the file downloaded from Github with the existing file on the server. And replaces it if there is a content difference
if(Compare-Object -ReferenceObject $(Get-Content C:\zabbix\maintain\show_agent_version.ps1) -DifferenceObject $(Get-Content c:\zabbix\scripts\show_agent_version.ps1))
{Move-Item "C:\zabbix\maintain\show_agent_version.ps1" "C:\zabbix\scripts\show_agent_version.ps1" -Force
$counter ++
}
}
# This will restart the agent if counter is equal to 1 or above
If ($counter -ge 1)
{
Write-Host $Counter "Files has been updated! Restarting agent..."
# Attempts to stop the agent
c:\zabbix\zabbix_agentd.exe --stop
# Attempts to start the agent
c:\zabbix\zabbix_agentd.exe --start
}
# Cleans up old files
Remove-Item C:\zabbix\maintain -Force -Recurse
# Closing the Zabbix Uninstall function bracket
}
#
# Installation script starts here
#
# Checks if the Zabbix folder exists.
$ChkFile = "C:\Zabbix\"
$FileExists = Test-Path $ChkFile
If ($FileExists -eq $True)
{
# Gets the version that is currently installed on the server, and then determine what to do.
$currentagentversion = (Get-Item C:\Zabbix\zabbix_agentd.exe).VersionInfo.ProductVersion
# If versions match, do this.
If ($currentagentversion -eq $newestagentversion)
{
Write-Host "Detected that Zabbix agent Version is the same, updating configuration files!"
ZabbixMaintain
}
else
{
Write-Host "Detected that Zabbix agent version is NOT the same, updating agent to newest version!" $newestagentversion
ZabbixBackup
ZabbixUninstall
ZabbixInstall
}
}
else
{
Write-Host "Detected that Zabbix agent is not installed on this Windows machine. Installing agent version!" $newestagentversion
ZabbixInstall
}