Skip to main content

Zabbix服务器监控

软件简介

Zabbix 是一款企业级的开源监控解决方案,专为监测和管理分布式系统的健康状况和网络参数而设计。其核心功能包括对服务器、网络设备、应用程序和服务的性能监控、故障检测、通知报警以及数据可视化。

安装步骤

安装zabbix服务端

# 关闭防火墙
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
systemctl disable --now firewalld
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.

# Install Zabbix repository
rpm -Uvh https://repo.zabbix.com/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 clean all

# Install Zabbix server and agent
yum install zabbix-server-mysql zabbix-agent

# 安装 Software Collections,可以在系统中安装多个版本的软件,并且不会影响系统中的安装包
yum install centos-release-scl -y

# Install Zabbix frontend
yum-config-manager --enable rhel-server-rhscl-7-rpms

# 编辑配置文件 /etc/yum.repos.d/zabbix.repo and enable zabbix-frontend repository.
vi /etc/yum.repos.d/zabbix.repo

# 编辑下面配置
[zabbix-frontend]
...
enabled=1
...

# Install Zabbix frontend packages.
yum install zabbix-web-mysql-scl zabbix-nginx-conf-scl

# 安装数据库软件,并为zabbix准备数据库和用户
yum install -y mariadb

mysql -uroot -p

# mysql> create database zabbix character set utf8 collate utf8_bin;
# mysql> create user zabbix@localhost identified by 'zabbix';
# mysql> grant all privileges on zabbix.* to zabbix@localhost;
# mysql> quit;

# 导入初始架构和数据
zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix

# 本机mysql是5.6版本,无法导入sql,所以将数据库部署在了服务器40(MySQL5.7)
zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz|/usr/local/mysql/bin/mysql -uzabbix -p -h192.168.137.40 zabbix

# 为Zabbix server配置数据库
vim /etc/zabbix/zabbix_server.conf

grep "DBPass" /etc/zabbix/zabbix_server.conf
### Option: DBPassword
# DBPassword=zabbix

# 修改Nginx配置,设置监听的域名和端口
vim /etc/opt/rh/rh-nginx116/nginx/conf.d/zabbix.conf

grep "listen\|server_name" /etc/opt/rh/rh-nginx116/nginx/conf.d/zabbix.conf
# listen 8088;
# server_name zabbix.he94.com;

# 修改php的时区设置
vim /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf

grep -E "timezone|acl_users" /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf
# listen.acl_users = apache,nginx
# php_value[date.timezone] = Asia/Shanghai

# 启动Zabbix server和agent进程,并为他们设置开机自启动
systemctl restart zabbix-server zabbix-agent rh-nginx116-nginx rh-php72-php-fpm
systemctl enable zabbix-server zabbix-agent rh-nginx116-nginx rh-php72-php-fpm

# 修改nginx的默认端口,因为服务器上已经安装了宝塔软件,多个nginx都默认使用80端口会产生冲突
vim /etc/opt/rh/rh-nginx116/nginx/nginx.conf
grep "listen" /etc/opt/rh/rh-nginx116/nginx/nginx.conf
# listen 8088 default_server;
# listen [::]:8088 default_server;

# 查看nginx和php-fpm是否启动
ps aux|grep /rh
# root 23307 0.0 0.6 336888 11580 ? Ss 23:47 0:00 php-fpm: master process (/etc/opt/rh/rh-php72/php-fpm.conf)
# root 24016 0.0 0.0 46500 1044 ? Ss 23:50 0:00 nginx: master process /opt/rh/rh-nginx116/root/usr/sbin/nginx
# root 24592 0.0 0.0 112812 976 pts/0 R+ 23:53 0:00 grep --color=auto /rh

# 访问前端页面
# 可能需要:域名解析,配置云服务器安全组,宝塔开放接口

# 安装字体,否则面板设置为中文时,部分汉字显示为小方块
yum -y install wqy-microhei-fonts
cp /usr/share/fonts/wqy-microhei/wqy-microhei.ttc /usr/share/fonts/dejavu/DejaVuSans.ttf

# zabbix server is not running 问题:
# 查看日志
tail -n 100 /var/log/zabbix/zabbix_server.log
# 3810:20220318:223816.384 database is down: reconnecting in 10 seconds
# 3810:20220318:223826.384 [Z3001] connection to database 'zabbix' failed: [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

# 修改配置文件
vim /etc/zabbix/zabbix_server.conf
grep "DBHost" /etc/zabbix/zabbix_server.conf
### Option: DBHost
# DBHost=127.0.0.1

安装Zabbix客户端

# curl: (60) The certificate issuer's certificate has expired. Check your system date and time.
yum update ca-certificates

# 设置yum源
rpm -Uvh https://repo.zabbix.com/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 clean all

# 安装 zabbix-agent2
yum install zabbix-agent2 -y

# 设置开机自启动
systemctl enable --now zabbix-agent2

# 修改配置
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.137.10
# ServerActive=192.168.137.10
# Hostname=Zabbix server
# Include=/etc/zabbix/zabbix_agent2.d/*.conf
# ControlSocket=/tmp/agent.sock

# 服务端测试
zabbix_get -s '192.168.137.20' -p 10050 -k 'agent.ping'

服务端添加主机

配置》主机》创建主机》模板选择(搜索linux)