-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinux_install_zabbix_proxy.sh
More file actions
executable file
·761 lines (689 loc) · 20.3 KB
/
linux_install_zabbix_proxy.sh
File metadata and controls
executable file
·761 lines (689 loc) · 20.3 KB
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
#!/bin/bash
#
# Install Zabbix Proxy [Linux]
#
# Please ensure to run this script as root (or at least with sudo)
#
# Generated with info from
# https://www.zabbix.com/download?zabbix=7.0&os_distribution=debian&os_version=12&components=agent_2&db=&ws=
#
# Syntax:
# --noninteractive - Run in non-interactive mode, (will not ask for prompts)
# --version=<string> - Version of Zabbix to install DEFAULT=7.0
# --server=<string> - Hostname or IP of Zabbix server (optional unless non-interactive)
# --hostname=<string> - Hostname of local device for matching with a Zabbix host entry (optional unless non-interactive)
#
# TRMM Arguments:
# --noninteractive
# --version=7.0
# --server={{client.zabbix_hostname}}
# --hostname={{agent.fqdn}}
#
# Supports:
# Debian 12
# Ubuntu 24.04
# Rocky 8, 9
# CentOS 8, 9
# RHEL 8, 9
#
# Category:
# Monitoring
#
# License:
# AGPLv3
#
# Author:
# Charlie Powell <cdp1337@bitsnbytes.dev>
#
# Link:
# https://github.com/eVAL-Agency/ScriptsCollection
#
# @TRMM-TIMEOUT 120
#
# Changelog:
# 20250130 - Change category to Monitoring
# 20250101 - Initial release
##
# Simple check to enforce the script to be run as root
if [ $(id -u) -ne 0 ]; then
echo "This script must be run as root or with sudo!" >&2
exit 1
fi
##
# Get the operating system
#
# almalinux, alpine, amzn, antergos, arch, archarm, arcolinux,
# centos, clear-linux-os, clearos,
# debian,
# elementary, endeavouros,
# fedora, freebsd,
# gentoo,
# kali,
# linuxmint,
# mageia, manjaro,
# nixos,
# opensuse, ol,
# pop,
# raspbian, rhel, rocky,
# scientific, slackware, sles,
# ubuntu,
# virtuozzo
#
function os() {
if [ "$(uname -s)" == 'FreeBSD' ]; then
echo 'freebsd'
elif [ -f '/etc/os-release' ]; then
local DISTRO="$(egrep '^ID=' /etc/os-release | sed 's:ID=::')"
if [[ "$DISTRO" =~ '"' ]]; then
# Strip quotes around the OS name
DISTRO="$(echo "$DISTRO" | sed 's:"::g')"
fi
# Cleanup a few known distro names
if [ "$DISTRO" == "manjaro-arm" ]; then
# Manjaro on ARM
DISTRO="manjaro"
elif [ "$DISTRO" == "opensuse-leap" ]; then
# OpenSuSE Leap 15.x
DISTRO="opensuse"
elif [ "$DISTRO" == "sles_sap" ]; then
# SuSE Enterprise SAP 12.x
DISTRO="sles"
fi
echo "$DISTRO"
else
echo 'unknown'
fi
}
##
# Get the operating system version
#
# Just the major version number is returned
#
function os_version() {
if [ "$(uname -s)" == 'FreeBSD' ]; then
local _V="$(uname -K)"
if [ ${#_V} -eq 6 ]; then
echo "${_V:0:1}"
elif [ ${#_V} -eq 7 ]; then
echo "${_V:0:2}"
fi
elif [ -f '/etc/os-release' ]; then
local VERS="$(egrep '^VERSION_ID=' /etc/os-release | sed 's:VERSION_ID=::')"
if [[ "$VERS" =~ '"' ]]; then
# Strip quotes around the OS name
VERS="$(echo "$VERS" | sed 's:"::g')"
fi
if [[ "$VERS" =~ \. ]]; then
# Remove the decimal point and everything after
# Trims "24.04" down to "24"
VERS="${VERS/\.*/}"
fi
if [[ "$VERS" =~ "v" ]]; then
# Remove the "v" from the version
# Trims "v24" down to "24"
VERS="${VERS/v/}"
fi
echo "$VERS"
else
echo 0
fi
}
##
# Check if the OS is "like" a certain type
#
# Returns 0 if true, 1 if false
#
# Usage:
# if os_like debian; then ... ; fi
#
function os_like() {
local OS="$1"
if [ -f '/etc/os-release' ]; then
ID="$(egrep '^ID=' /etc/os-release | sed 's:ID=::')"
LIKE="$(egrep '^ID_LIKE=' /etc/os-release | sed 's:ID_LIKE=::')"
if [[ "$LIKE" =~ "$OS" ]] || [ "$ID" == "$OS" ]; then
return 0;
fi
fi
return 1
}
##
# Check if the OS is "like" a certain type
#
# ie: "ubuntu" will be like "debian"
#
# Returns 0 if true, 1 if false
# Prints 1 if true, 0 if false
#
# Usage:
# if [ "$(os_like_debian)" -eq 1 ]; then ... ; fi
# if os_like_debian -q; then ... ; fi
#
function os_like_debian() {
local QUIET=0
while [ $# -ge 1 ]; do
case $1 in
-q)
QUIET=1;;
esac
shift
done
if os_like debian || os_like ubuntu; then
if [ $QUIET -eq 0 ]; then echo 1; fi
return 0;
fi
if [ $QUIET -eq 0 ]; then echo 0; fi
return 1
}
##
# Check if the OS is "like" a certain type
#
# ie: "ubuntu" will be like "debian"
#
# Returns 0 if true, 1 if false
# Prints 1 if true, 0 if false
#
# Usage:
# if [ "$(os_like_ubuntu)" -eq 1 ]; then ... ; fi
# if os_like_ubuntu -q; then ... ; fi
#
function os_like_ubuntu() {
local QUIET=0
while [ $# -ge 1 ]; do
case $1 in
-q)
QUIET=1;;
esac
shift
done
if os_like ubuntu; then
if [ $QUIET -eq 0 ]; then echo 1; fi
return 0;
fi
if [ $QUIET -eq 0 ]; then echo 0; fi
return 1
}
##
# Check if the OS is "like" a certain type
#
# ie: "ubuntu" will be like "debian"
#
# Returns 0 if true, 1 if false
# Prints 1 if true, 0 if false
#
# Usage:
# if [ "$(os_like_rhel)" -eq 1 ]; then ... ; fi
# if os_like_rhel -q; then ... ; fi
#
function os_like_rhel() {
local QUIET=0
while [ $# -ge 1 ]; do
case $1 in
-q)
QUIET=1;;
esac
shift
done
if os_like rhel || os_like fedora || os_like rocky || os_like centos; then
if [ $QUIET -eq 0 ]; then echo 1; fi
return 0;
fi
if [ $QUIET -eq 0 ]; then echo 0; fi
return 1
}
##
# Check if the OS is "like" a certain type
#
# ie: "ubuntu" will be like "debian"
#
# Returns 0 if true, 1 if false
# Prints 1 if true, 0 if false
#
# Usage:
# if [ "$(os_like_suse)" -eq 1 ]; then ... ; fi
# if os_like_suse -q; then ... ; fi
#
function os_like_suse() {
local QUIET=0
while [ $# -ge 1 ]; do
case $1 in
-q)
QUIET=1;;
esac
shift
done
if os_like suse; then
if [ $QUIET -eq 0 ]; then echo 1; fi
return 0;
fi
if [ $QUIET -eq 0 ]; then echo 0; fi
return 1
}
##
# Check if the OS is "like" a certain type
#
# ie: "ubuntu" will be like "debian"
#
# Returns 0 if true, 1 if false
# Prints 1 if true, 0 if false
#
# Usage:
# if [ "$(os_like_arch)" -eq 1 ]; then ... ; fi
# if os_like_arch -q; then ... ; fi
#
function os_like_arch() {
local QUIET=0
while [ $# -ge 1 ]; do
case $1 in
-q)
QUIET=1;;
esac
shift
done
if os_like arch; then
if [ $QUIET -eq 0 ]; then echo 1; fi
return 0;
fi
if [ $QUIET -eq 0 ]; then echo 0; fi
return 1
}
##
# Check if the OS is "like" a certain type
#
# ie: "ubuntu" will be like "debian"
#
# Returns 0 if true, 1 if false
# Prints 1 if true, 0 if false
#
# Usage:
# if [ "$(os_like_bsd)" -eq 1 ]; then ... ; fi
# if os_like_bsd -q; then ... ; fi
#
function os_like_bsd() {
local QUIET=0
while [ $# -ge 1 ]; do
case $1 in
-q)
QUIET=1;;
esac
shift
done
if [ "$(uname -s)" == 'FreeBSD' ]; then
if [ $QUIET -eq 0 ]; then echo 1; fi
return 0;
else
if [ $QUIET -eq 0 ]; then echo 0; fi
return 1
fi
}
##
# Check if the OS is "like" a certain type
#
# ie: "ubuntu" will be like "debian"
#
# Returns 0 if true, 1 if false
# Prints 1 if true, 0 if false
#
# Usage:
# if [ "$(os_like_macos)" -eq 1 ]; then ... ; fi
# if os_like_macos -q; then ... ; fi
#
function os_like_macos() {
local QUIET=0
while [ $# -ge 1 ]; do
case $1 in
-q)
QUIET=1;;
esac
shift
done
if [ "$(uname -s)" == 'Darwin' ]; then
if [ $QUIET -eq 0 ]; then echo 1; fi
return 0;
else
if [ $QUIET -eq 0 ]; then echo 0; fi
return 1
fi
}
##
# Disable a package from a given yum repo
function yum_repo_excludepkg() {
local REPO_FILE="$1"
local PACKAGE="$2"
if [ ! -e "$REPO_FILE" ]; then
# If the repo file does not exist at all, nothing to do.
return 1
fi
local STARTED=0
local FOUND=0
local TMP_FILE="$(mktemp)"
local SECTION=""
if [ -z "$TMP_FILE" ]; then
# Ensure a temp file exists for temporary writing, (even if mktemp fails)
TMP_FILE="/tmp/tmp.$(tr -dc A-Za-z0-9 </dev/urandom | head -c 13)"
touch $TMP_FILE
fi
while read LINE; do
if [[ "$LINE" =~ ^\[.*\] ]]; then
# "[...] indicates the start of a new section
if [ $STARTED -eq 1 -a $FOUND -eq 0 ]; then
# If a new section has started with the directive being written, ensure it's written
# prior to the next section starting.
# Exception is the first section in the file.
echo "Excluding $PACKAGE from $SECTION in $REPO_FILE"
echo "excludepkgs=$PACKAGE" >> $TMP_FILE
echo "" >> $TMP_FILE
fi
SECTION="$LINE"
STARTED=1
FOUND=0
fi
if [[ "$LINE" =~ ^excludepkgs= ]]; then
# Line contains "excludepkgs", just append the requested package if necessary!
FOUND=1
if [ -z "$(echo $LINE | grep "$PACKAGE")" ]; then
echo "Excluding $PACKAGE from $SECTION in $REPO_FILE"
if [ "$LINE" == "excludepkgs=" ]; then
LINE="$LINE$PACKAGE"
else
LINE="$LINE,$PACKAGE"
fi
fi
fi
if [ "$LINE" == "" -a $STARTED -eq 1 ]; then
# End of a section
if [ $FOUND -eq 0 ]; then
echo "Excluding $PACKAGE from $SECTION in $REPO_FILE"
echo "excludepkgs=$PACKAGE" >> $TMP_FILE
FOUND=1
fi
fi
echo "$LINE" >> $TMP_FILE
done <$REPO_FILE
if [ $FOUND -eq 0 ]; then
# Last section in the file, if it was not written yet, ensure it's set.
echo "Excluding $PACKAGE from $SECTION in $REPO_FILE"
echo "excludepkgs=$PACKAGE" >> $TMP_FILE
fi
# Now that all operations are complete, replace the original file with the parsed one.
mv $TMP_FILE $REPO_FILE
}
##
# Install a package with the system's package manager.
#
# Uses Redhat's yum, Debian's apt-get, and SuSE's zypper.
#
# Usage:
#
# ```syntax-shell
# package_install apache2 php7.0 mariadb-server
# ```
#
# @param $1..$N string
# Package, (or packages), to install. Accepts multiple packages at once.
#
#
# CHANGELOG:
# 2026.01.09 - Cleanup os_like a bit and add support for RHEL 9's dnf
# 2025.04.10 - Set Debian frontend to noninteractive
#
function package_install (){
echo "package_install: Installing $*..."
if os_like_bsd -q; then
pkg install -y $*
elif os_like_debian -q; then
DEBIAN_FRONTEND="noninteractive" apt-get -o Dpkg::Options::="--force-confold" -o Dpkg::Options::="--force-confdef" install -y $*
elif os_like_rhel -q; then
if [ "$(os_version)" -ge 9 ]; then
dnf install -y $*
else
yum install -y $*
fi
elif os_like_arch -q; then
pacman -Syu --noconfirm $*
elif os_like_suse -q; then
zypper install -y $*
else
echo 'package_install: Unsupported or unknown OS' >&2
echo 'Please report this at https://github.com/eVAL-Agency/ScriptsCollection/issues' >&2
exit 1
fi
}
##
# Setup the Zabbix repo for this OS, shared between agent, agent2, server, and proxy.
function zabbix_repo_setup() {
local ZABBIX_VERSION="$1"
local OS_NAME="$(os)"
local OS_VERSION="$(os_version)"
local BASE="https://repo.zabbix.com/zabbix/${ZABBIX_VERSION}"
if [ -z "$(which wget)" ]; then
package_install wget
fi
case "${ZABBIX_VERSION}_${OS_NAME}" in
"7.2_almalinux" ) local SRC="https://repo.zabbix.com/zabbix/7.2/release/alma/${OS_VERSION}/noarch/zabbix-release-latest-7.2.el${OS_VERSION}.noarch.rpm";;
"7.2_debian" ) local SRC="https://repo.zabbix.com/zabbix/7.2/release/debian/pool/main/z/zabbix-release/zabbix-release_latest_7.2+debian${OS_VERSION}_all.deb";;
"7.2_raspbian" ) local SRC="https://repo.zabbix.com/zabbix/7.2/release/raspbian/pool/main/z/zabbix-release/zabbix-release_latest_7.2+debian${OS_VERSION}_all.deb";;
"7.2_rhel" ) local SRC="https://repo.zabbix.com/zabbix/7.2/release/rhel/${OS_VERSION}/noarch/zabbix-release-latest-7.2.el${OS_VERSION}.noarch.rpm";;
"7.2_rocky" ) local SRC="https://repo.zabbix.com/zabbix/7.2/release/rocky/${OS_VERSION}/noarch/zabbix-release-latest-7.2.el${OS_VERSION}.noarch.rpm";;
"7.2_ubuntu" ) local SRC="https://repo.zabbix.com/zabbix/7.2/release/ubuntu/pool/main/z/zabbix-release/zabbix-release_latest_7.2+ubuntu${OS_VERSION}.04_all.deb";;
"7.0_almalinux" ) local SRC="https://repo.zabbix.com/zabbix/7.0/alma/${OS_VERSION}/x86_64/zabbix-release-latest-7.0.el${OS_VERSION}.noarch.rpm";;
"7.0_debian" ) local SRC="https://repo.zabbix.com/zabbix/7.0/debian/pool/main/z/zabbix-release/zabbix-release_latest_7.0+debian${OS_VERSION}_all.deb";;
"7.0_raspbian" ) local SRC="https://repo.zabbix.com/zabbix/7.0/raspbian/pool/main/z/zabbix-release/zabbix-release_latest_7.0+debian${OS_VERSION}_all.deb";;
"7.0_rhel" ) local SRC="https://repo.zabbix.com/zabbix/7.0/rhel/${OS_VERSION}/x86_64/zabbix-release-latest-7.0.el${OS_VERSION}.noarch.rpm";;
"7.0_rocky" ) local SRC="https://repo.zabbix.com/zabbix/7.0/rocky/${OS_VERSION}/x86_64/zabbix-release-latest-7.0.el${OS_VERSION}.noarch.rpm";;
"7.0_ubuntu" ) local SRC="https://repo.zabbix.com/zabbix/7.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_latest_7.0+ubuntu${OS_VERSION}.04_all.deb";;
"6.0_almalinux" ) local SRC="https://repo.zabbix.com/zabbix/6.0/rhel/${OS_VERSION}/x86_64/zabbix-release-latest-6.0.el${OS_VERSION}.noarch.rpm";;
"6.0_debian" ) local SRC="https://repo.zabbix.com/zabbix/6.0/debian/pool/main/z/zabbix-release/zabbix-release_latest_6.0+debian${OS_VERSION}_all.deb";;
"6.0_raspbian" ) local SRC="https://repo.zabbix.com/zabbix/6.0/raspbian/pool/main/z/zabbix-release/zabbix-release_latest_6.0+debian${OS_VERSION}_all.deb";;
"6.0_rhel" ) local SRC="https://repo.zabbix.com/zabbix/6.0/rhel/${OS_VERSION}/x86_64/zabbix-release-latest-6.0.el${OS_VERSION}.noarch.rpm";;
"6.0_rocky" ) local SRC="https://repo.zabbix.com/zabbix/6.0/rocky/${OS_VERSION}/x86_64/zabbix-release-latest-6.0.el${OS_VERSION}.noarch.rpm";;
"6.0_ubuntu" ) local SRC="https://repo.zabbix.com/zabbix/6.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_latest_6.0+ubuntu${OS_VERSION}.04_all.deb";;
esac
local FILE="$(basename "$SRC")"
# We will use this directory as a working directory for source files that need downloaded.
[ -d /opt/script-collection ] || mkdir -p /opt/script-collection
if [ ! -e /opt/script-collection/$FILE ]; then
wget $SRC -O /opt/script-collection/$FILE
if [ $? -ne 0 ]; then
echo "Failed to download $SRC" >&2
exit 1
fi
if [ $(os_like_debian) -eq 1 ]; then
dpkg -i /opt/script-collection/$FILE
apt update
elif [ $(os_like_rhel) -eq 1 ]; then
yum_repo_excludepkg /etc/yum.repos.d/epel.repo "zabbix*"
rpm -Uvh /opt/script-collection/$FILE
dnf clean all
else
echo "Unable to install $_FILE, unsupported OS [ $OS ] version [ $OSVERSIONMAJ ]" >&2
fi
fi
}
##
# Use sed to set a line in a config file
#
# If the target line does not exist, it will simply get appended to the end
#
# Arguments:
# $1 Line match
# $2 Line replace
# $3 filename
#
# Example:
# setconfigfile_orappend "^Password=.*" "Password=1234" "/etc/myapp/myapp.conf"
#
#
# CHANGELOG:
# 2025.04.10 - Escape '?' characters in the sed search
function setconfigfile_orappend() {
# Swap '/' with '\/' since sed here uses '/' as the delimiter
# Additionally, '?' characters in the SED search need escaped
SED_SEARCH="$(echo "$1" | sed 's:/:\\/:g' | sed 's:?:\\\?:g')"
SED_REPLACE="$(echo "$2" | sed 's:/:\\/:g')"
GREP_SEARCH="$1"
GREP_REPLACE="$2"
FILENAME="$3"
if grep -Eq "$GREP_SEARCH" "$FILENAME"; then
if [ "$OSFAMILY" == "bsd" ]; then
sed -i '' "s/$SED_SEARCH/$SED_REPLACE/" "$FILENAME"
else
sed -i "s/$SED_SEARCH/$SED_REPLACE/" "$FILENAME"
fi
else
echo "$GREP_REPLACE" >> "$FILENAME"
fi
}
##
# Prompt user for a text response
#
# Arguments:
# --default="..." Default text to use if no response is given
#
# Returns:
# text as entered by user
#
function prompt_text() {
local DEFAULT=""
local PROMPT="Enter some text"
local RESPONSE=""
while [ $# -ge 1 ]; do
case $1 in
--default=*) DEFAULT="${1#*=}";;
*) PROMPT="$1";;
esac
shift
done
echo "$PROMPT" >&2
echo -n '> : ' >&2
read RESPONSE
if [ "$RESPONSE" == "" ]; then
echo "$DEFAULT"
else
echo "$RESPONSE"
fi
}
##
# Print a header message
#
# CHANGELOG:
# 2025.11.09 - Port from _common to bz_eval_tui
# 2024.12.25 - Initial version
#
function print_header() {
local header="$1"
echo "================================================================================"
printf "%*s\n" $(((${#header}+80)/2)) "$header"
echo ""
}
##
# Generate a random password, (using characters that are easy to read and type)
function random_password() {
< /dev/urandom tr -dc _cdefhjkmnprtvwxyACDEFGHJKLMNPQRTUVWXY2345689 | head -c${1:-24};echo;
}
# Variable setup
ZABBIX_AGENT_CONFIGURATION="/etc/zabbix/zabbix_proxy.conf"
function usage() {
cat >&2 <<EOD
Usage: $0 [options]
Options:
--noninteractive - Run in non-interactive mode, (will not ask for prompts)
--version=<string> - Version of Zabbix to install DEFAULT=7.0
--server=<string> - Hostname or IP of Zabbix server (optional unless non-interactive)
--hostname=<string> - Hostname of local device for matching with a Zabbix host entry (optional unless non-interactive)
Please ensure to run this script as root (or at least with sudo)
Generated with info from
https://www.zabbix.com/download?zabbix=7.0&os_distribution=debian&os_version=12&components=agent_2&db=&ws=
EOD
exit 1
}
# Parse arguments
NONINTERACTIVE=0
VERSION="7.0"
ZABBIX_SERVER=""
ZABBIX_AGENT_HOSTNAME=""
while [ "$#" -gt 0 ]; do
case "$1" in
--noninteractive) NONINTERACTIVE=1;;
--version=*|--version)
[ "$1" == "--version" ] && shift 1 && VERSION="$1" || VERSION="${1#*=}"
[ "${VERSION:0:1}" == "'" ] && [ "${VERSION:0-1}" == "'" ] && VERSION="${VERSION:1:-1}"
[ "${VERSION:0:1}" == '"' ] && [ "${VERSION:0-1}" == '"' ] && VERSION="${VERSION:1:-1}"
;;
--server=*|--server)
[ "$1" == "--server" ] && shift 1 && ZABBIX_SERVER="$1" || ZABBIX_SERVER="${1#*=}"
[ "${ZABBIX_SERVER:0:1}" == "'" ] && [ "${ZABBIX_SERVER:0-1}" == "'" ] && ZABBIX_SERVER="${ZABBIX_SERVER:1:-1}"
[ "${ZABBIX_SERVER:0:1}" == '"' ] && [ "${ZABBIX_SERVER:0-1}" == '"' ] && ZABBIX_SERVER="${ZABBIX_SERVER:1:-1}"
;;
--hostname=*|--hostname)
[ "$1" == "--hostname" ] && shift 1 && ZABBIX_AGENT_HOSTNAME="$1" || ZABBIX_AGENT_HOSTNAME="${1#*=}"
[ "${ZABBIX_AGENT_HOSTNAME:0:1}" == "'" ] && [ "${ZABBIX_AGENT_HOSTNAME:0-1}" == "'" ] && ZABBIX_AGENT_HOSTNAME="${ZABBIX_AGENT_HOSTNAME:1:-1}"
[ "${ZABBIX_AGENT_HOSTNAME:0:1}" == '"' ] && [ "${ZABBIX_AGENT_HOSTNAME:0-1}" == '"' ] && ZABBIX_AGENT_HOSTNAME="${ZABBIX_AGENT_HOSTNAME:1:-1}"
;;
-h|--help) usage;;
*) echo "Unknown argument: $1" >&2; usage;;
esac
shift 1
done
if [ -z "$VERSION" ]; then
VERSION="7.0"
fi
if [ "$VERSION" == "latest" ]; then
VERSION="7.2"
fi
# User prompts (if not in non-interactive mode)
if [ $NONINTERACTIVE -eq 0 ]; then
if [ -z "$ZABBIX_SERVER" ]; then
ZABBIX_SERVER="$(prompt_text "Hostname or IP of Zabbix server")"
fi
if [ -z "$ZABBIX_AGENT_HOSTNAME" ]; then
ZABBIX_AGENT_HOSTNAME="$(prompt_text "Hostname of local device for matching with a Zabbix host entry" --default="$(hostname -f)")"
fi
else
if [ -z "$ZABBIX_SERVER" ]; then
usage
fi
if [ -z "$ZABBIX_AGENT_HOSTNAME" ]; then
usage
fi
fi
# Setup Zabbix repo
zabbix_repo_setup "$VERSION"
# Install the proxy
package_install zabbix-proxy-mysql zabbix-sql-scripts
if [ $(os_like_rhel) -eq 1 ]; then
package_install zabbix-selinux-policy
fi
# Setup config from user-definable parameters
setconfigfile_orappend "^Server=.*" "Server=${ZABBIX_SERVER}" "$ZABBIX_AGENT_CONFIGURATION"
setconfigfile_orappend "^Hostname=.*" "Hostname=${ZABBIX_AGENT_HOSTNAME}" "$ZABBIX_AGENT_CONFIGURATION"
setconfigfile_orappend "^EnableRemoteCommands=.*" "EnableRemoteCommands=1" "$ZABBIX_AGENT_CONFIGURATION"
setconfigfile_orappend "^AllowUnsupportedDBVersions=.*" "AllowUnsupportedDBVersions=1" "$ZABBIX_AGENT_CONFIGURATION"
# If no password has been set, presume that Zabbix isn't configured yet.
if grep -q '# DBPassword=' /etc/zabbix/zabbix_proxy.conf; then
# Generate a random password for Zabbix
DBPASS="$(random_password)"
mysql -e "create database zabbix_proxy character set utf8mb4 collate utf8mb4_bin"
mysql -e "create user zabbix@localhost identified by '$DBPASS'"
mysql -e "grant all privileges on zabbix_proxy.* to zabbix@localhost"
mysql -e "set global log_bin_trust_function_creators = 1"
# Install the initial data
cat /usr/share/zabbix-sql-scripts/mysql/proxy.sql | mysql --default-character-set=utf8mb4 zabbix_proxy
# Disable log_bin_trust_function_creators option after importing database schema.
mysql -e "set global log_bin_trust_function_creators = 0"
# Set the password in Zabbix's config file
sed -i "s/# DBPassword=/DBPassword=$DBPASS/g" /etc/zabbix/zabbix_proxy.conf
fi
# Restart and enable the service
systemctl restart zabbix-proxy
systemctl enable zabbix-proxy
print_header 'Zabbix proxy installed'
echo "Agent Version: $VERSION"
echo "Zabbix Server: $ZABBIX_SERVER"
echo "Device Hostname: $ZABBIX_AGENT_HOSTNAME"
echo ''
echo 'If you have not done so already, please create a new Proxy in Zabbix for this device.'
echo '(Administration -> Proxies -> Create Proxy)'
echo ''
echo 'Followed by setting its IP range and checks enabled'
echo '(Data collection -> Discovery)'