zabbix-server6.0与zabbix-agent部署及配置

环境:

1
2
3
4
5
6
7
8
9
10
11
 Static hostname: test
Icon name: computer-vm
Chassis: vm
Machine ID: 10869ed987ba4bcba46b1e1b58f99aad
Boot ID: ef892fd8036f4c0291c3d1accdb112bf
Virtualization: vmware
Operating System: Ubuntu 22.04.5 LTS
Kernel: Linux 5.15.0-174-generic
Architecture: x86-64
Hardware Vendor: VMware, Inc.
Hardware Model: VMware Virtual Platform
环境
服务端 10.0.0.10 ubuntu20.04
客户端 10.0.0.11 ubuntu20.04

一、zabbix6.0服务端部署脚本

  • 该脚本是幂等性的 可以重复执行 但需要注意 已经存在服务端的机器不可执行 否则会导致原有监控数据丢失💣
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
#!/bin/bash
# 部署zabbix6.0
User=root
chushi_Passwd=zabbix
mem_CacheSize="1G"
date_z=`$(date +%F_%H_%M)`
set -e
systemctl stop apache2 ||true
systemctl disable apache2 ||true
ubuntu_zabbix_twenty() {
# Install Zabbix repository Documentation
wget https://repo.zabbix.com/zabbix/6.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_latest_6.0+ubuntu20.04_all.deb
dpkg -i zabbix-release_latest_6.0+ubuntu20.04_all.deb
apt update
# Install Zabbix server, frontend, agent
apt install zabbix-server-mysql zabbix-frontend-php zabbix-nginx-conf zabbix-sql-scripts zabbix-agent -y
apt install unzip -y
# mysql8.0
apt-get install mysql-server-8.0 mysql-client-8.0 -y
}


mysql_configuration() {
# mysql 配置
# create database zabbix character set utf8mb4 collate utf8mb4_bin;
# create user zabbix@localhost identified by 'zabbix';
# $(which mysql) -u${User} --connect-expired-password -p${chushi_Passwd} -e "
mysql -u root -e "
show databases;
ALTER USER root@localhost IDENTIFIED BY 'zabbix';
FLUSH PRIVILEGES;
CREATE DATABASE IF NOT EXISTS zabbix
CHARACTER SET utf8mb4
COLLATE utf8mb4_bin;
CREATE USER IF NOT EXISTS 'zabbix'@'localhost' IDENTIFIED BY 'zabbix';
ALTER USER 'zabbix'@'localhost' IDENTIFIED BY 'zabbix';
grant all privileges on zabbix.* to zabbix@localhost;
set global log_bin_trust_function_creators = 1;
FLUSH PRIVILEGES;
show databases;"
}

source_zabbix_sql() {
# DROP DATABASE IF EXISTS zabbix;
# CREATE DATABASE IF NOT EXISTS zabbix
# CHARACTER SET utf8mb4
# COLLATE utf8mb4_bin;
# CREATE USER IF NOT EXISTS 'zabbix'@'localhost' IDENTIFIED BY 'zabbix';
gzip -vdc /usr/share/zabbix-sql-scripts/mysql/server.sql.gz > /usr/share/zabbix-sql-scripts/mysql/server.sql
$(which mysql) -u${User} --connect-expired-password -p${chushi_Passwd} -e "
use zabbix;
source /usr/share/zabbix-sql-scripts/mysql/server.sql;" || true
}

zabbix_s_conf() {

FILE=/etc/zabbix/zabbix_server.conf
cp $FILE ${FILE}_bak_${date_z}
# DBHost 取消注释并确保值正确
grep -q "^DBHost=" $FILE && \
sed -i 's/^DBHost=.*/DBHost=localhost/' $FILE || \
sed -i 's/^# DBHost=.*/DBHost=localhost/' $FILE

# DBName
grep -q "^DBName=" $FILE && \
sed -i 's/^DBName=.*/DBName=zabbix/' $FILE || \
echo "DBName=zabbix" >> $FILE

# DBUser
grep -q "^DBUser=" $FILE && \
sed -i 's/^DBUser=.*/DBUser=zabbix/' $FILE || \
echo "DBUser=zabbix" >> $FILE

# DBPassword(可能不存在)
grep -q "^DBPassword=" $FILE && \
sed -i 's/^DBPassword=.*/DBPassword=zabbix/' $FILE || \
echo "DBPassword=zabbix" >> $FILE

# AllowUnsupportedDBVersions
grep -q "^AllowUnsupportedDBVersions=" $FILE && \
sed -i 's/^AllowUnsupportedDBVersions=.*/AllowUnsupportedDBVersions=1/' $FILE || \
sed -i 's/^# AllowUnsupportedDBVersions=.*/AllowUnsupportedDBVersions=1/' $FILE

# VMwareCacheSize
grep -q "^VMwareCacheSize=" $FILE && \
sed -i 's/^VMwareCacheSize=.*/VMwareCacheSize=2G/' $FILE || \
sed -i 's/^# VMwareCacheSize=.*/VMwareCacheSize=2G/' $FILE

# CacheSize
grep -q "^CacheSize=" $FILE && \
sed -i "s/^CacheSize=.*/CacheSize=$mem_CacheSize/" $FILE || \
sed -i "s/^# CacheSize=.*/CacheSize=$mem_CacheSize/" $FILE
# 验证
grep -E "DBPassword=zabbix|AllowUnsupportedDBVersions=1|VMwareCacheSize=2G|CacheSize=$mem_CacheSize|DBHost=localhost \
|DBName=zabbix|DBUser=zabbix|DBHost=localhost" /etc/zabbix/zabbix_server.conf

}


nginx_conf() {

FILE="/etc/zabbix/nginx.conf"
BACKUP=${FILE}.bak

# 只备份一次(幂等)
[ -f "$BACKUP" ] || cp -a $FILE $BACKUP

ensure_in_server() {
local key="$1"
local value="$2"

if sed -n '/server\s*{/,/}/p' $FILE | grep -q "$key"; then
sed -i "/server\s*{/,/}/ s|^[[:space:]]*#\?\s*$key.*| $key $value;|" $FILE
else
sed -i "/server\s*{/a\ $key $value;" $FILE
fi
}

ensure_in_server listen "8080"
ensure_in_server server_name "10.0.0.10"

}

start_service() {

systemctl restart zabbix-server zabbix-agent nginx php7.4-fpm
systemctl enable zabbix-server zabbix-agent nginx php7.4-fpm
systemctl status zabbix-server
systemctl status zabbix-agent
systemctl status nginx
systemctl status php7.4-fpm
[ $? = 0 ] && echo "zabbix server 6.0安装完成!"
hostname -I && ss -ntulp |grep 80

}


main() {
ubuntu_zabbix_twenty
mysql_configuration
source_zabbix_sql
zabbix_s_conf
nginx_conf
start_service
}
main

二、zabbix-agent二进制部署脚本

  • 该脚本是幂等性的 可以重复执行 但需要注意 已经存在客户端的机器不可执行 否则会导致原有监控数据丢失💣

  • zabbix-agent端配置注意事项 主要三项

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    root@test:/tmp# grep -E  "Server=10.0.0.10|Hostname|ServerActive" /usr/local/zabbix_agent/conf/zabbix_agentd.conf
    Server=10.0.0.10 # 服务端IP
    ### Option: ServerActive
    # ServerActive=127.0.0.1:10051
    # ServerActive=127.0.0.1:20051,zabbix.domain,[::1]:30051,::1,[12fc::1]
    # ServerActive=zabbix.cluster.node1;zabbix.cluster.node2:20051;zabbix.cluster.node3
    # ServerActive=zabbix.cluster.node1;zabbix.cluster.node2:20051,zabbix.cluster2.node1;zabbix.cluster2.node2,zabbix.domain
    # ServerActive=
    ServerActive=10.0.0.10 # 服务端IP
    ### Option: Hostname
    # Value is acquired from HostnameItem if undefined.
    # Hostname=
    Hostname=10.0.0.11 # 客户端IP
    ### Option: HostnameItem
    # Item used for generating Hostname if it is undefined. Ignored if Hostname is defined.
    # HostnameItem=system.hostname
    # Aliases can be used in HostMetadataItem but not in HostnameItem parameters.

部署脚本如下:zabbix-agent.sh

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
#!/bin/bash
# zabbix-agent

# # Install Zabbix repository Documentation
# wget https://repo.zabbix.com/zabbix/6.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_latest_6.0+ubuntu20.04_all.deb
# dpkg -i zabbix-release_latest_6.0+ubuntu20.04_all.deb
# apt update
# # Install Zabbix server, frontend, agent
# apt install zabbix-agent -y
# apt install unzip -y
# # mysql8.0
zabbix_agent="zabbix_agent-6.0.45-linux-3.0-amd64-static.tar.gz"
# server端IP
Server_ip="10.0.0.10"
# client端ip
Hostname_client_ip=`hostname -I|awk '{print $1}'`
# 二进制安装zabbix-agent
zabbix_agent() {
mkdir -p /usr/local/zabbix_agent ||true
wget https://cdn.zabbix.com/zabbix/binaries/stable/6.0/6.0.45/${zabbix_agent}
tar -zxvf ${zabbix_agent} -C /usr/local/zabbix_agent
useradd zabbix ||true
chown -R zabbix:zabbix /usr/local/zabbix_agent ||true
}

agent_confluence_run() {

CONFIG="/usr/local/zabbix_agent/conf/zabbix_agentd.conf" && \
sed -i.bak \
-e "s/^Server=.*/Server=${Server_ip}/" \
-e "s/^ServerActive=.*/ServerActive=${Server_ip}/" \
-e "s/^Hostname=.*/Hostname=${Hostname_client_ip}/" \
"$CONFIG" && echo "更新完成,原文件备份为 $CONFIG.bak"

grep -E "Server=${Server_ip}|Hostname|ServerActive" /usr/local/zabbix_agent/conf/zabbix_agentd.conf
}

run_agent() {
useradd zabbix ||true
chown -R zabbix:zabbix /usr/local/zabbix_agent ||true

cat > /usr/lib/systemd/system/zabbix-agent.service << 'eric'
[Unit]
Description=zabbix-agent
After=network.target

[Service]
Type=forking
User=zabbix
Group=zabbix
Environment="CONFFILE=/usr/local/zabbix_agent/conf/zabbix_agentd.conf"
ExecStart=/usr/local/zabbix_agent/sbin/zabbix_agentd -c $CONFFILE
ExecReload=/bin/kill -SIGHUP $MAINPID
ExecStop=/bin/kill -SIGTERM $MAINPID
PIDFile=/tmp/zabbix_agentd.pid
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target
eric
systemctl daemon-reload
systemctl start zabbix-agent.service
systemctl enable zabbix-agent.service
systemctl restart zabbix-agent.service
systemctl status zabbix-agent.service

}


main() {

zabbix_agent
agent_confluence_run
run_agent
}
main

三、zabbix-agent配置

  • Configuration >Hostsgroup
  • Configuration >Hosts