⛳️ 1.Zabbix介绍
Zabbix 是一个基于 WEB 界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案。
它能监视各种网络参数,保证服务器系统的安全运营;
并提供灵活的通知机制以让系统管理员快速定位/解决存在的各种问题
⛳️ 2.Zabbix架构
zabbix 软件组成
➢ zabbix-server: 服务端
➢ zabbix-agent: 客户端
➢ zabbix-web: 网站服务
➢ php: 处理动态请求
➢ mysql: 数据库存储数据
➢ zabbix: 负责收集 agent 信息汇总告知 zabbix-server
⛳️ 3.安装 Zabbix
🐴 3.1 添加yum源
cat > /zabbix_aliyun.sh <<"EOF" #!/bin/bash echo -e "请给出要安装的 zabbix 版本号 \033[31musage:./zabbix_aliyun.sh 4.0|4.4|4.5|5.0 \033[0m" echo "例如要安装 5.0 版本,则使用命令: sh zabbix_aliyun.sh 5.0" if [ -z $1 ];then exit fi VERSION=$1 if [ -f /etc/yum.repos.d/zabbix.repo ];then rm -rf /etc/repos.d/zabbix.repo fi rpm -qa | grep zabbix-release && rpm -e zabbix-release rpm -Uvh https://mirrors.aliyun.com/zabbix/zabbix/$VERSION/rhel/7/x86_64/zabbix-release-$VERSION-1.el7.noarch.rpm sed -i "s@zabbix/.*/rhel@zabbix/$VERSION/rhel@g" /etc/yum.repos.d/zabbix.repo sed -i 's@repo.zabbix.com@mirrors.aliyun.com/zabbix@g' /etc/yum.repos.d/zabbix.repo [ $? -eq 0 ] && echo "阿里云的 zabbix 源替换成功" || exit 1 yum clean all yum makecache fast EOF
chmod +x /zabbix_aliyun.sh
sh /zabbix_aliyun.sh 5.0
🐴 3.2 安装组件
yum -y install zabbix-server-mysql zabbix-agent
##centos扩展包
yum -y install centos-release-scl
yum-config-manager --enable zabbix-frontend
yum install -y zabbix-web-mysql-scl zabbix-apache-conf-scl
🐴 3.3 修改配置文件
##修改数据库密码为 jem
sed -ri.bak ‘/# DBPassword=/cDBPassword=jem’ /etc/zabbix/zabbix_server.conf
##修改时区为国内
sed -ri.bak ‘s/^.date.timezone.$/php_value[date.timezone] = Asia/Shanghai/’ /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf
🐴 3.4 配置数据库服务
[root@jemzabbix5 ~]# mkdir /soft [root@jeames soft]# docker cp mysql-5.7.30-1.el7.x86_64.rpm-bundle.tar jemzabbix5:/soft [root@jemzabbix5 ~]# cd /soft/ [root@jemzabbix5 soft]# tar -xvf mysql-5.7.30-1.el7.x86_64.rpm-bundle.tar yum localinstall -y mysql-community-*.rpm systemctl start mysqld grep 'temporary password' /var/log/mysqld.log mysql -uroot -p alter user root@'localhost' identified by 'Jem@jem123'; uninstall plugin validate_password; grant all on *.* to root@'%' identified by 'root'; grant all on *.* to root@'localhost' identified by 'root'; create database zabbix character set utf8 collate utf8_bin; grant all on zabbix.* to zabbix@localhost identified by 'jem'; grant all on zabbix.* to zabbix@'%' identified by 'jem'; flush privileges; mysql> select Host,User from mysql.user; # 上传 create.sql.gz 文件 docker cp create.sql.gz jemzabbix5:/soft zcat create.sql.gz | mysql -uroot -proot zabbix -- 修改参数/etc/my.cnf default-time-zone = '+8:00' log_timestamps = SYSTEM character_set_server=utf8mb4
🐴 3.4 启动资源
systemctl enable zabbix-server zabbix-agent httpd rh-php72-php-fpm
systemctl restart zabbix-server zabbix-agent httpd rh-php72-php-fpm
systemctl status zabbix-server zabbix-agent httpd rh-php72-php-fpm
systemctl status zabbix-server zabbix-agent httpd rh-php72-php-fpm mariadb mysqld | grep Active -B 3
http://192.168.1.54:280/zabbix
默认用户名密码:Admin/zabbix
⛳️ 4.Zabbix监控
🐴 4.1 监控MySQL
1.agent安装
rpm -ivh http://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
sed -i “s/gpgcheck=1/gpgcheck=0/g” /etc/yum.repos.d/zabbix.repo
yum -y install zabbix-agent
2.配置文件
vi /etc/zabbix/zabbix_agentd.conf
Server=172.17.0.5
ServerActive=172.17.0.5
Hostname=mysql_monitor
Timeout=10
systemctl restart zabbix-agent
systemctl enable zabbix-agent
systemctl status zabbix-agent
3.配置Mysql
mkdir -p /var/lib/zabbix
vi /var/lib/zabbix/.my.cnf
[mysql]
host=localhost
user=root
password=root
socket=/tmp/mysql.sock
[mysqladmin]
host=localhost
user=root
password=root
socket=/tmp/mysql.sock
4.配置模板
##此模板适用于Mysql5.7版本
vi /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf
UserParameter=mysql.ping[], mysqladmin -h"$1" -P"$2" ping
UserParameter=mysql.get_status_variables[], mysql -h"$1" -P"$2" -sNX -e “show global status”
UserParameter=mysql.version[], mysqladmin -s -h"$1" -P"$2" version
UserParameter=mysql.db.discovery[], mysql -h"$1" -P"$2" -sN -e “show databases”
UserParameter=mysql.dbsize[], mysql -h"$1" -P"$2" -sN -e “SELECT SUM(DATA_LENGTH + INDEX_LENGTH) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA=’$3’”
UserParameter=mysql.replication.discovery[], mysql -h"$1" -P"$2" -sNX -e “show slave status”
UserParameter=mysql.slave_status[*], mysql -h"$1" -P"$2" -sNX -e “show slave status”
5.Zabbix Server添加主机
🐴 4.1 监控postgreSQL
请参考以下博客
https://blog.csdn.net/weixin_41645135/article/details/120300581
⛳️ 5.邮件告警
1.首先你应该注册一个163邮箱,用于发信,再注册时开启SMTP/POP3功能,使用加密密码认证.
2.在Zabbix主机上新建send.py文件,将以下代码复制编辑,设置发件用户、密码.
[root@localhost ~]# vim send.py #!/usr/bin/python #coding:utf-8 import smtplib from email.mime.text import MIMEText import sys mail_host = 'smtp.163.com' mail_user = 'smtpwho' mail_pass = '密码' mail_postfix = '163.com' def send_mail(to_list,subject,content): me = "Zabbix 监控告警通知"+"<"+mail_user+"@"+mail_postfix+">" msg = MIMEText(content, 'plain', 'utf-8') msg['Subject'] = subject msg['From'] = me msg['to'] = to_list s = smtplib.SMTP() s.connect(mail_host) s.login(mail_user,mail_pass) s.sendmail(me,to_list,msg.as_string()) s.close() if __name__ == "__main__": send_mail(sys.argv[1], sys.argv[2], sys.argv[3])
3.紧接着我们给脚本相应的权限测试是否能够发信成功,并拷贝到zabbix的默认脚本目中.
[root@localhost ~]# chmod +x send.py
[root@localhost ~]# python send.py 2243967774@qq.com zabbix checkok
[root@localhost ~]# cp -a send.py /usr/lib/zabbix/alertscripts/
[root@localhost ~]# chmod 755 -R /usr/lib/zabbix/alertscripts/
4.登录Zabbix主机,查看配置zabbix_server.conf,找到AlertScriptsPath取消注释.
[root@localhost ~]# vim /etc/zabbix/zabbix_server.conf [root@localhost ~]# vim /usr/local/zabbix/etc/zabbix_server.conf ### Option: AlertScriptsPath # Full path to location of custom alert scripts. # Default depends on compilation options. # To see the default path run command "zabbix_server --help". # # Mandatory: no # Default: # AlertScriptsPath=${datadir}/zabbix/alertscripts AlertScriptsPath=/usr/lib/zabbix/alertscripts #解除注释,找到配置项
5.点击图形界面,选择管理->报警媒介.
6.点击图形界面,选择配置->动作->创建动作,首先创建一个动作.
7.紧接着点击操作->配置一下故障报警消息格式.
标题改为: 故障{TRIGGER.STATUS}, 服务器: {HOSTNAME1} 发生: {TRIGGER.NAME}故障! 信息改为: 告警主机:{HOSTNAME1} 告警地址:{HOST.IP} 告警时间:{EVENT.DATE} {EVENT.TIME} 告警等级:{TRIGGER.SEVERITY} 告警信息: {TRIGGER.NAME} 告警项目:{TRIGGER.KEY1} 问题详情:{ITEM.NAME}:{ITEM.VALUE} 当前状态:{TRIGGER.STATUS}: {ITEM.VALUE1} 事件 ID:{EVENT.ID}
8.当我们配置完成一个故障报警后,下面就继续配置 一个恢复后的提示.
标题改为: 恢复{TRIGGER.STATUS}, 服务器: {HOSTNAME1}: {TRIGGER.NAME}已恢复! 信息改为: 告警主机:{HOSTNAME1} 告警地址:{HOST.IP} 告警时间:{EVENT.DATE} {EVENT.TIME} 告警等级:{TRIGGER.SEVERITY} 告警信息: {TRIGGER.NAME} 告警项目:{TRIGGER.KEY1} 问题详情:{ITEM.NAME}:{ITEM.VALUE} 当前状态:{TRIGGER.STATUS}: {ITEM.VALUE1} 事件 ID:{EVENT.ID}
最终点击小add添加选项,继续点击大ADD完成动作增加
9.配置告警用户,点击管理->用户->Admin->报警媒介,修改以下内容.
10.强制关闭虚拟机,测试是否有邮件报警即可.