Zabbix5.0部署及相关配置使用


以下两个安装脚本,分别是zabbix服务端的安装和agent端。该脚本是根据zabbix官方教程进行整合编写的,为了方便部署安装所以写了脚本,也可以参考官方文档 :https://www.zabbix.com/cn/download https://www.zabbix.com/documentation/5.0/zh/manual
建议安装ansbile + zabbix 进行管理集群的,有更高安全需要还可以配置跳板机。


zabbix是一个功能强大的开源监控程序,可以监控各类的负载,以可视化形式进行展示,可以定制自己需要监控的配置。支持多种告警触发器的发送,可以高效的协助运维人员定位问题,及时解决故障。

Zabbix服务端安装脚本

在服务端安装

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
#!/bin/env bash
#安装zabbix服务端
kou () {
freekou=(10050 10051 80 22 3306 )
for i in ${!freekou[@]}
do
firewall-cmd --permanent --zone=public --add-port=${freekou[$i]}/tcp
done
firewall-cmd --reload
}

pgrep -lo firewalld &>/dev/null
if [ $? -eq 0 ];then
kou
else
service firewalld restart
kou
fi

hostnamectl set-hostname zabbix-server
[ -f /usr/bin/expect ]||yum -y install expect
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0
yum install ntpdate -y
ntpdate -u ntp.aliyun.com
rpm -Uvh https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
sed -i 's#http://repo.zabbix.com/#https://mirrors.aliyun.com/zabbix/#' /etc/yum.repos.d/zabbix.repo
sed -i 's/enabled=0/enabled=1/' /etc/yum.repos.d/zabbix.repo
yum clean all
yum makecache
sleep 1
yum install zabbix-server-mysql zabbix-agent -y
yum install centos-release-scl -y # 安装scl防止影响系统环境
yum install zabbix-web-mysql-scl zabbix-apache-conf-scl -y
yum install mariadb-server -y
systemctl enable --now mariadb
systemctl enable --now httpd
sleep 1
read -p "请输入需要初始化的mysql密码:" pass
read -p "请输入需要初始化的mysql用户zabbix密码:" pass2
echo '初始化数据库,请稍等'
/usr/bin/expect <<-END &>/dev/null
spawn mysql_setpermission #初始化数据库
expect "MySQL:" { send "\r";exp_continue }
expect "]:" { send "1\r" }
expect "password:" { send "root\r" }
expect "y/n" { send "y\r" }
expect "root:" { send "$pass\r" }
expect "again:" { send "$pass\r" }
expect "(case sensitive):" { send "localhost\r" }
expect "yes/no" { send "yes\r" }
expect "]:" { send "\r" }
expect eof
END
[ $? -eq 0 ]&&echo 'mariadb安装成功'
systemctl restart mariadb
mysql -uroot -p$pass -e 'create database zabbix character set utf8 collate utf8_bin;'
mysql -uroot -p$pass -e "create user zabbix@localhost identified by '${pass2}';"
mysql -uroot -p$pass -e 'grant all privileges on zabbix.* to zabbix@localhost;'
mysql -uroot -p$pass -e 'flush privileges;'
`zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p${pass2} zabbix`
sed -i "s/# DBPassword=/DBPassword=$pass2/" /etc/zabbix/zabbix_server.conf
sed -i 's/^;//' /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf
sed -i 's#Europe/Riga#Asia/Shanghai#' /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf
systemctl restart zabbix-server zabbix-agent httpd rh-php72-php-fpm
systemctl enable zabbix-server zabbix-agent httpd rh-php72-php-fpm
yum -y install wqy-microhei-fonts #处理中文监控图形文字乱码
\cp /usr/share/fonts/wqy-microhei/wqy-microhei.ttc /usr/share/fonts/dejavu/DejaVuSans.ttf
echo '安装成功,请登录管理zabbix http://本机ip/zabbix '
echo '默认登录信息为 用户名:Admin 密码:zabbix'

Agent2安装脚本

在需被监控端节点安装,该脚本运行前需要先赋值,执行时在脚本后加参数

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
#!/bin/env bash
#安装agent2,配置主动注册

ip=$1 #请赋值,输入zabbix-server服务端的ip
name=$2 #请赋值,输入设置该节点主机名
if [[ "$ip" = '' || "$name" = '' ]];then
echo "没有给脚本中的服务端ip和主机name赋值,请修改再执行"
exit
fi

sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0
kou () {
freekou=(10050 22 )
for i in ${!freekou[@]}
do
firewall-cmd --permanent --zone=public --add-port=${freekou[$i]}/tcp
done
firewall-cmd --reload
}

pgrep -lo firewalld &>/dev/null
if [ $? -eq 0 ];then
kou
else
service firewalld restart
kou
fi

hostnamectl set-hostname $name
yum install ntpdate -y
ntpdate -u ntp.aliyun.com
mv /etc/localtime{,.bak}
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
rpm -Uvh https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
sed -i 's#http://repo.zabbix.com/#https://mirrors.aliyun.com/zabbix/#' /etc/yum.repos.d/zabbix.repo
yum install zabbix-agent2 -y
systemctl enable --now zabbix-agent2
sed -i 's/# HostnameItem=system.hostname/HostnameItem=system.hostname/' /etc/zabbix/zabbix_agent2.conf
sed -i "s#\(Server=\)127.0.0.1#\1$ip#" /etc/zabbix/zabbix_agent2.conf
sed -i "s#\(ServerActive=\)127.0.0.1#\1$ip#" /etc/zabbix/zabbix_agent2.conf
sed -i "s/Hostname=Zabbix server/Hostname=$name/" /etc/zabbix/zabbix_agent2.conf
systemctl restart zabbix-agent2
echo "agent安装成功
需要在服务器端配置->自动发现->动作 Autoregistration actions 创建主动注册【左上角】
点击【操作】 依次添加 主机 【再添加】 主机群组Linux server 链接到模板Template OS Linux by Zabbix agent"

安装ansbile

yum -y install epel-release
yum install ansible -y
配置主机清单inventory,添加节点的机器信息
vi /etc/ansible/hosts

1
2
sed -i 's/#host_key_checking = False/host_key_checking = False/' /etc/ansible/ansible.cfg   
#不检测host key

不配置密钥,可以直接配置密码,如果机器都推好公钥了,就直接填ip即可,建议配置秘钥

[test]
192.168.23.3  #节点的ip

[test:vars]
ansible_ssh_pass=123456  #节点密码

命令例子:ansible test -m script -a /root/agent2.sh 192.168.23.3 zabbix-agent #在所有节点执行安装agent2脚本

基本操作记录,不定时更新

启用php-fpm状态详解 https://www.php.cn/php-notebook-98468.html
启用nginx status状态详解 https://www.cnblogs.com/xiaoyaojinzhazhadehangcheng/articles/8043353.html

手动创建zabbix的配置文件,用于自定义key

配置文件 /etc/zabbix/zabbix_agent2.conf

[root@localhost zabbixagent2.d]# cat /etc/zabbix/zabbixagent2.conf | grep /etc/zabbix/zab

Include=/etc/zabbix/zabbix_agent2.d/*.conf

查看key编写规则 cat /etc/zabbix/zabbix_agent2.conf | grep UserParameter

key存放路径 /etc/zabbix/zabbix_agent2.d

echo ‘UserParameter=login.user,who|wc -l’ > /etc/zabbix/zabbix_agent2.d/test_login.conf

systemctl restart zabbix-agent2

server端key测试 zabbix_get -s ‘agent端的ip’ -p 10050 -k ‘login.user’

自定义监控项模板

创建模板

创建应用集

创建监控项,自定义item,你具体想监控的内容

创建触发器,当监控项获取到值时,进行和触发器比较,判断,决定是否报警

创建图形

将具体的主机和该模板链接,关联

邮件报警

管理->报警媒介类型->创建媒体类型

smtp服务器

set smtp=smtps://smtp.qq.com:465

set smtp-auth-user=1714594511@qq.com

set smtp-auth-password=qcpqqybawczrhdag

用户设置->报警媒介

配置行动启用

自主发现

配置->自动发现->动作 [Discovery actions] 创建自主发现

所有主机加hosts echo “severip 主机名” > /etc/hosts

主动注册,被动模式

anget:

vi /etc/zabbix/zabbix_agent2.conf

去除注释 HostnameItem=system.hostname

[root@node1 ~]# grep -Ev ‘^#|^$’ /etc/zabbix/zabbix_agent2.conf

PidFile=/var/run/zabbix/zabbix_agent2.pid

LogFile=/var/log/zabbix/zabbix_agent2.log

LogFileSize=0

Server=192.168.159.135

ServerActive=192.168.159.135

Hostname=node1

HostnameItem=system.hostname

Include=/etc/zabbix/zabbix_agent2.d/*.conf

配置->自动发现->动作 Autoregistration actions 创建主动注册

点击操作依次添加 主机 添加主机群组 链接到模板

zabbix-proxy代理服务器配置

rpm -Uvh https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm

sed -i ‘s#http://repo.zabbix.com/#https://mirrors.aliyun.com/zabbix/#' /etc/yum.repos.d/zabbix.repo

yum install zabbix-proxy-mysql zabbix-get -y

yum install mariadb-server mariadb -y

create database zabbixproxy character set utf8 collate utf8bin;’

grant all privileges on zabbix_proxy. to zabbix@’localhost’ identified by ‘zabbix’;

flush privileges;

rpm -ql zabbix-proxy-mysql

zcat /usr/share/doc/zabbix- proxy -mysqL-5.0.6/schema sql.gz | mysql -uzabbix -pzabbix zabbix_proxy

sed -i .ori ‘162a DBPas sword=zabbix’ /etc/ zabbix/ zabbix_ proxy . conf

sed -i’ s#Server=127.0.0.1#Server=10.0.1.50#’ /etc/ zabbix/ zabbix_ proxy . conf

sed -i’ S #Hostname=Zabbix proxy#Hos tname= zbz - agent01# ‘

vi /etc/zabbix/zabbix_ proxy.conf

Server=severip

Hostname=代理主机名

systemctl restart zabbix-proxy

/etc/hosts 配置所有主机信息

在管理选线创建agent代理程序,配置 主机 添加主机,勾选agent代理。客户端的配置指向的服务器ip修改为代理服务的ip,重启客户端。

snmp监控

server:

yum -y install net-snmp net-snmp-utils

sed -i ‘57a view systemview included .1’ /etc/snmp/snmpd.conf

systemctl start snmpd.service

snmpwalk -v 2c -c public 127.0.0.1 sysname