zabbix 安装
系统环境: CentOS Linux release 7.4.1708 (Core)
服务端安装脚本如下:(用到的是HTTP服务,可自行修改为nginx)
#!/bin/bash
#设置解析 注意:网络条件较好时,可以不用自建yum源
# echo '10.0.0.1 mirrors.aliyuncs.com mirrors.aliyun.com repo.zabbix.com' >> /etc/hosts
#安装zabbix源、aliyun YUM源
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
rpm -ivh http://repo.zabbix.com/zabbix/3.0/rhel/7/x86_64/zabbix-release-3.0-1.el7.noarch.rpm
#安装zabbix
yum install -y zabbix-server-mysql zabbix-web-mysql
#安装启动 mariadb数据库
yum install -y mariadb-server
systemctl start mariadb.service
#创建数据库
mysql -e 'create database zabbix character set utf8 collate utf8_bin;'
mysql -e 'grant all privileges on zabbix.* to zabbix@localhost identified by "zabbix";'
#导入数据
zcat /usr/share/doc/zabbix-server-mysql-3.0.13/create.sql.gz|mysql -uzabbix -pzabbix zabbix
#配置zabbixserver连接mysql
sed -i.ori '115a DBPassword=zabbix' /etc/zabbix/zabbix_server.conf
#添加时区
sed -i.ori '18a php_value date.timezone Asia/Shanghai' /etc/httpd/conf.d/zabbix.conf
#解决中文乱码
yum -y install wqy-microhei-fonts
cp /usr/share/fonts/wqy-microhei/wqy-microhei.ttc /usr/share/fonts/dejavu/DejaVuSans.ttf
#启动服务
systemctl start zabbix-server
systemctl start httpd
#写入开机自启动
chmod +x /etc/rc.d/rc.local
cat >>/etc/rc.d/rc.local<<EOF
systemctl start mariadb.service
systemctl start httpd
systemctl start zabbix-server
EOF
#输出信息
echo "浏览器访问 http://`hostname -I|awk '{print $1}'`/zabbix"
客户端安装脚本如下:
#!/bin/bash
#设置解析
echo '10.0.0.1 mirrors.aliyuncs.com mirrors.aliyun.com repo.zabbix.com' >> /etc/hosts
#安装zabbix源、aliyu nYUM源
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
rpm -ivh http://repo.zabbix.com/zabbix/3.0/rhel/7/x86_64/zabbix-release-3.0-1.el7.noarch.rpm
#安装zabbix客户端
yum install zabbix-agent -y
sed -i.ori 's#Server=127.0.0.1#Server=server-IP#' /etc/zabbix/zabbix_agentd.conf
systemctl start zabbix-agent.service
#写入开机自启动
chmod +x /etc/rc.d/rc.local
cat >>/etc/rc.d/rc.local<<EOF
systemctl start zabbix-agent.service
EOF
脚本安装完成的是最为简单的功能自行在配置文件中增加所需功能
检测server与agent的连通性:
在server端安装zabbix_get yum install zabbix-get
server执行测试语句: zabbix_get -s agent-IP -p port -k "system.cpu.load[all,avg1]"
web端进行操作:
浏览器访问http://IP/zabbix 进行初始化操作 登陆密码Admin/zabbix 在设置中选择中文即可汉化
http://ip/zabbix/setup.php
![](https://s1.51cto.com/images/blog/201908/12/7d7f9de766aacb79a94c4af216fb9723.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
在检测信息时,可查看具体的报错信息进行不同的解决
![](https://s1.51cto.com/images/blog/201908/12/b42441674d41fdf4a23427049df8d2c2.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
选择mysql数据库,输入密码即可
![](https://s1.51cto.com/images/blog/201908/12/2bdccd95d6ebd3cfcadbf0213f53e9bc.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
host与port不需要修改,name自定义
![](https://s1.51cto.com/images/blog/201908/12/82272059aebcf9aecb4a432caeeb183a.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
确认信息,正确点击下一步
![](https://s1.51cto.com/images/blog/201908/12/5524ca274f513dd37439139aab3b410c.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
安装完成、点击finsh
![](https://s1.51cto.com/images/blog/201908/12/b1ccd8e95d583f7630451dc527e5f07b.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
进入登陆界面 账号Admin密码zabbix 注意A大写
![](https://s1.51cto.com/images/blog/201908/12/e21344ce7184aec94b9dd97c84fad6f7.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
配置主机
![](https://s1.51cto.com/images/blog/201908/12/ebbcaa984f6e343a3f267d79ce013105.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
添加系统默认的模板
![](https://s1.51cto.com/images/blog/201908/12/a1cf38de444690f367cb7b317b16fc17.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
到这里的话基本上在系统自带的默认模板一些基础的监控项已经建立起来了下面演示自创模板
利用系统自己带的键值:
![](https://s1.51cto.com/images/blog/201908/12/fc36e660f0249a29a732a8b537c440e6.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
里面有各个键值的使用说明
监控MySQL:
- mysql-server 服务器安装zabbix-agent,修改配置文件
在/etc/zabbix/zabbix_agentd.conf配置中增加一条配置
zabbix-server 添加主机
- MySQL上创建给zabbix-agent使用的用户账号,在/etc/zabbix下创建一个隐藏文件.my.cnf,
[client]
user=zabbix
host=127.0.0.1
password=123456
- 测试连接
HOME=/etc/zabbix/ mysqladmin ping | grep -c alive
1
- 修改配置文件/etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf(没有的话自己创建)
修这两条配置
UserParameter=mysql.status[*],echo "show global status where Variable_name='$1';" | HOME=/etc/zabbix mysql -N | awk '{print $$2}' #因为是yum安装的所以改成这样
UserParameter=mysql.ping,HOME=/etc/zabbix mysqladmin ping | grep -c alive
- 然后在web页面进行配置即可
监控nginx
#!/bin/bash
HOST="127.0.0.1"
PORT="88"
function ping {
/sbin/pidof nginx | wc -l
}
function active {
/usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| grep 'Active' | awk '{print $NF}'
}
function reading {
/usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| grep 'Reading' | awk '{print $2}'
}
function writing {
/usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| grep 'Writing' | awk '{print $4}'
}
function waiting {
/usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| grep 'Waiting' | awk '{print $6}'
}
function accepts {
/usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $1}'
}
function handled {
/usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $2}'
}
function requests {
/usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/ null| awk NR==3 | awk '{print $3}'
}
#执行function
$1
- 修改配置文件/etc/zabbix/zabbix_agentd.d/userparameter_nginx.conf(没有的话自己创建)
修这两条配置
UserParameter=nginx.status[*],/etc/zabbix/nginx_check.sh $1
- 导入模板文件,关联主机即可
监控redis
redis 没有可以用的模板,所以模板,监控项和图形都要自己制作
#!/bin/bash
redis_status(){
R_PORT=$1
R_COMMAND=$2
(echo -en "INFO \r\n";sleep 1;) | nc 127.0.0.1` `"$R_PORT" > /etc/zabbix/redis_"$R_PORT".tmp
REDIS_STAT_VALUE=$(grep ""$R_COMMAND":" /etc/zabbix/redis_"$R_PORT".tmp | cut -d ':' -f2)
echo $REDIS_STAT_VALUE
}
help(){
echo "${0} + redis_status + PORT + COMMAND"
}
main(){
case $1 in
redis_status)
redis_status $2 $3
;;
*)
help
;;
esac
}
main $1 $2 $3
- 修改配置文件/etc/zabbix/zabbix_agentd.d/userparameter_redis.conf
vim userparameter_redis.conf
UserParameter=redis_status[*],/etc/zabbix/redis_check.sh $1 $2 $3
- 手动创建监控模板
![](https://s1.51cto.com/images/blog/201908/12/d5cbdd5b263f8f0172629eda36241522.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
- 依次创建监控项
![](https://s1.51cto.com/images/blog/201908/12/e2c602846c29ededdbb75ab60dfb7fd3.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
- 把监控项创建成为图形
![](https://s1.51cto.com/images/blog/201908/12/b4e7d15cd06d741cd60819fb89ec7a04.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
完成redis的监控
配置报警
这我用的是微信报警,注册的企业微信来实现报警的接受,脚本如下:
#!/bin/bash
CropID='wwbda087ee88ce0910
Secret='tmFH9c2wUKr8Sf_W5TV5r_0YAprKqN_ZHB4DbcSCP4k
GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret"
Gtoken=$(/usr/bin/curl -s -G $GURL | awk -F\" '{print $10}')
PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Gtoken"
function body() {
local int AppID=1000002 #企业号 中的应用id
local UserID=$1 #部门成员id,zabbix中定义的微信接收者
local PartyID=1 #部门id,定义了范围,组内成员都可接收到消息
local Msg=$(echo "$@" | cut -d" " -f3-) #过滤出zabbix中传递的第三个参数
printf '{\n'
printf '\t"touser": "'"$User"\"",\n"
printf '\t"toparty": "'"$PartyID"\"",\n"
printf '\t"msgtype": "text",\n'
printf '\t"agentid": "'" $AppID "\"",\n"
printf '\t"text": {\n'
printf '\t\t"content": "'"$Msg"\""\n"
printf '\t},\n'
printf '\t"safe":"0"\n'
printf '}\n'}
/usr/bin/curl --data-ascii "$(body $1 $2 $3)" $PURL
- 设置zabbix脚本运行路径,并把报警脚本放于该目录下
grep "^[^#]" /etc/zabbix/zabbix_server.conf
......
AlertScriptsPath=/etc/zabbix/alertscripts
......
- 脚本设置权限和所属用户
chown zabbix:zabbix /etc/zabbix/alertscripts/weixin.sh
chmod 744 /etc/zabbix/alertscripts/weixin.sh
- 设置监控动作和接收用户信息,进入zabbix后台,选择【管理】-->【报警媒介类型】--> 创建媒介类型
![](https://s1.51cto.com/images/blog/201908/12/f5998a3dab9a3f66951f8a2f86a50131.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
- 选择类型为:【脚本】,脚本名称为第二步中创建的脚本名称,并配置脚本参数
![](https://s1.51cto.com/images/blog/201908/12/ba5f672684282084cbfab7558d491b69.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
- 选择【管理】-->【用户】--> 选择admin用户,选择报警介质,点击添加按钮,关联上admin用户和weixin报警
![](https://s1.51cto.com/images/blog/201908/12/8e794d62319555a327098a5461e50b60.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
- 【配置】-->【动作】中配置的默认信息配置
服务器:{HOST.NAME}出现故障!
服务器:{HOST.NAME}出现故障!\n
告警主机:{HOSTNAME1}
主机地址:{HOST.IP}
告警时间:{EVENT.DATE} {EVENT.TIME}
告警信息:{TRIGGER.NAME}
问题详情:{ITEM.NAME}:{ITEM.VALUE}
当前状态:{TRIGGER.STATUS}:{ITEM.VALUE1}
事件ID:{EVENT.ID}
服务器:{HOST.NAME}已恢复!
服务器:{HOST.NAME}已恢复!\n
恢复主机:{HOSTNAME1}
主机地址:{HOST.IP}
恢复时间:{EVENT.RECOVERY.DATE} {EVENT.RECOVERY.TIME}
故障信息:{TRIGGER.NAME}
问题详情:{ITEM.NAME}:{ITEM.VALUE}
当前状态:{TRIGGER.STATUS}:{ITEM.VALUE1}
事件ID:{EVENT.ID}
- 配置成功
![](https://s1.51cto.com/images/blog/201908/12/0be0782413579559332c4a977c0c5589.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)