赞
踩
注意事项:
本文所用目录是/data/;阅读本文需要有一定Linux知识的技术人员,写的不好,小白可能看不懂,十分抱歉哈。
部署环境:CentOS 7.9 PHP7.2 MySQL5.7 Nginx1.18
1、添加源
vi /etc/yum.repos.d/zabbix.repo
[zabbix]
name=Zabbix Official Repository - $basearch
baseurl=http://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
[zabbix-frontend]
name=Zabbix Official Repository frontend - $basearch
baseurl=http://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/$basearch/frontend
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
[zabbix-debuginfo]
name=Zabbix Official Repository debuginfo - $basearch
baseurl=http://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/$basearch/debuginfo/
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
gpgcheck=1
[zabbix-non-supported]
name=Zabbix Official Repository non-supported - $basearch
baseurl=http://mirrors.aliyun.com/zabbix/non-supported/rhel/7/$basearch/
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
gpgcheck=1
rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
yum clean all
2、安装zabbix
yum install zabbix-server-mysql zabbix-agent
yum install centos-release-scl
vi /etc/yum.repos.d/zabbix.repo
[zabbix-frontend] ...
enabled=1 ...
3、安装Zabbix frontend packages
yum install zabbix-web-mysql-scl zabbix-apache-conf-scl
4、安装PHP
yum install epel-release -y
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum install -y php72w php72w-cli php72w-fpm php72w-common php72w-devel php72w-embedded php72w-gd php72w-mbstring php72w-mysqlnd php72w-opcache php72w-pdo php72w-xml
5、安装mysql
mkdir /data && cd /data && yum -y install wget && wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm && rpm -ivh mysql57-community-release-el7-9.noarch.rpm && yum install -y mysql-server && systemctl start mysqld && systemctl enable mysqld && cp /etc/my.cnf /etc/my.cnfbak && echo "character_set_server=utf8" >>/etc/my.cnf && echo "init_connect='SET NAMES utf8'" >>/etc/my.cnf && grep 'temporary password' /var/log/mysqld.log && mysql_secure_installation
6、设置MySQL
mysql> create database zabbix character set utf8 collate utf8_bin;
mysql> grant all privileges on zabbix.* to zabbix@localhost identified by 'password';
mysql> grant all privileges on *.* to root@"%" identified by "ADbc!@#123" with grant option;
mysql> flush privileges;
mysql> quit;
7、导入zabbix数据
zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p
8、编辑zabbix配置
vi /etc/zabbix/zabbix_server.conf
DBPassword=password
9、编辑php配置
vi /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf
php_value[date.timezone] = Asia/Shanghai
10、nginx安装
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm && yum install -y nginx
11、nginx配置
拷贝zabbix文件:cp -r /usr/share/zabbix/* /data/zabbix/html
vi /etc/nginx/conf.d/zabbix.conf
server
{
listen 80;
server_name localhost;
index index.php index.html index.html;
root /data/zabbix/html;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ .*\.(php)?$
{
expires -1s;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
access_log /data/zabbix/zabbixaccess.log;
}
}
12、启动程序
启动:
systemctl start zabbix-server zabbix-agent httpd rh-php72-php-fpm nginx
开启开机启动:
systemctl enable zabbix-server zabbix-agent httpd rh-php72-php-fpm nginx
查看状态:
systemctl status zabbix-server zabbix-agent httpd rh-php72-php-fpm nginx
停止服务:
systemctl stop zabbix-server zabbix-agent httpd rh-php72-php-fpm nginx
13、登录控制台
使用浏览器访问http://ip/zabbix 即可访问 zabbix 的 web 页面,
之后可以一路下一步,直至完成。最后输入账号Admin密码zabbix ,登录管理页面。
14、钉钉警告功能接入
a、建立钉钉群,之后在PC版钉钉上---群设置---智能群助手---添加机器人---选择自定义关键词zabbix,记录好Webhook,脚本上需要用。
b、警告脚本:
vi /usr/lib/zabbix/alertscripts/zabbix.py
#!/usr/bin/env python
#coding:utf-8
#zabbix钉钉警告
import requests,json,sys,os,datetime
webhook="https://oapi.dingtalk.com/robot/send?access_token=xxxxxx"
user=sys.argv[1]
text=sys.argv[3]
data={
"msgtype": "text",
"text": {
"content": text
},
"at": {
"atMobiles": [
user
],
"isAtAll": False
}
}
headers = {'Content-Type': 'application/json'}
x=requests.post(url=webhook,data=json.dumps(data),headers=headers)
if os.path.exists("/usr/lib/zabbix/logs/dingding.log"):
f=open("/usr/lib/zabbix/logs/dingding.log","a+")
else:
f=open("/usr/lib/zabbix/logs/dingding.log","w+")
f.write("\n"+"--"*30)
if x.json()["errcode"] == 0:
f.write("\n"+str(datetime.datetime.now())+" "+str(user)+" "+"发送成功"+"\n"+str(text))
f.close()
else:
f.write("\n"+str(datetime.datetime.now()) + " " + str(user) + " " + "发送失败" + "\n" + str(text))
f.close()
授权:
mkdir -p /usr/lib/zabbix/logs/
touch /usr/lib/zabbix/logs/dingding.log
chmod 755 dingding.py
chown zabbix.zabbix dingding.py
chown zabbix.zabbix /usr/lib/zabbix/logs/dingding.log
测试:
./dingding.py a b "zabbix: test" 若部署正确,钉钉群会收到告警信息。
c、设置
管理----报警媒介类型
{ALERT.SENDTO} {ALERT.SUBJECT} {ALERT.MESSAGE}
配置----动作----操作
主题:故障{TRIGGER.STATUS},服务器:{HOST.NAME}发生: {TRIGGER.NAME}故障!
消息:
zabbix:
告警主机:{HOST.NAME}
告警地址:{HOST.IP}
监控项目:{ITEM.NAME}
监控取值:{ITEM.LASTVALUE}
告警等级:{TRIGGER.SEVERITY}
当前状态:{TRIGGER.STATUS}
告警信息:{TRIGGER.NAME}
告警时间:{EVENT.DATE} {EVENT.TIME}
事件ID:{EVENT.ID}
主题:恢复{TRIGGER.STATUS}, 服务器:{HOSTNAME1}: {TRIGGER.NAME}已恢复!
消息:
zabbix:
告警主机:{HOST.NAME}
告警地址:{HOST.IP}
监控项目:{ITEM.NAME}
监控取值:{ITEM.LASTVALUE}
告警等级:{TRIGGER.SEVERITY}
当前状态:{TRIGGER.STATUS}
告警信息:{TRIGGER.NAME}
告警时间:{EVENT.DATE} {EVENT.TIME}
恢复时间:{EVENT.RECOVERY.DATE} {EVENT.RECOVERY.TIME}
持续时间:{EVENT.AGE}
事件ID:{EVENT.ID}
用户管理----报警媒介----类型选择钉钉警告。
15、监控Windows磁盘使用率
监控项:
名称:c盘总量 键值:vfs.fs.size[c:,total]
名称:c盘剩余量 键值:vfs.fs.size[c:,free]
名称:c盘使用量 键值:vfs.fs.size[c:,used]
选择数字(无正负),单位空
名称:c盘剩余百分比 键值:vfs.fs.size[c:,pfree]
名称:c盘使用百分比 键值:vfs.fs.size[c:,pused]
选择浮点数,单位%
触发器:
问题表现形式:{windows:vfs.fs.size[c:,pfree].last()}<=5
恢复表达式:{windows:vfs.fs.size[c:,pfree].last()}>5
其问题处理
1、导入数据库报错处理:
ERROR 1046 (3D000) at line 1: No database selected
vi /usr/share/doc/zabbix-server-mysql-4.0.7/create.sql.gz
# 在第一行加上
use zabbix;
2、汉字显示乱码问题处理:
yum install wqy-microhei-fonts -y
cp /usr/share/fonts/wqy-microhei/wqy-microhei.ttc /usr/share/fonts/dejavu/DejaVuSans.ttf
systemctl restart zabbix-server
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。