一、登录mysql赋权zabbix访问
主要赋权zabbix-agent执行脚本调用mysql的status表
#mysql -uroot -p
GRANT USAGE,PROCESS,SUPER,REPLICATION CLIENT,REPLICATION SLAVE ON *.* TO 'zabbixagent'@'localhost' IDENTIFIED BY 'zabbixagent#1234';
flush privileges;
exit
二、创建监控脚本
接下来准备mysql的运行信息监控,需要准备新的mysql监控脚本chk_mysql.sh,/etc/zabbix/chk_mysql.sh
# vim /etc/zabbix/chk_mysql.sh
#!/bin/sh
# -------------------------------------------------------------------------------
# FileName: check_mysql.sh
# Revision: 1.1
# Date: 2017/10/12
# Author: yakamoz
MYSQL_USER='zabbixagent'
MYSQL_PWD='zabbixagent#1234'
MYSQL_HOST='127.0.0.1'
MYSQL_PORT='3306'
ARGS=1
MYSQL_CONN="/usr/bin/mysqladmin -u$MYSQL_USER -p$MYSQL_PWD -h$MYSQL_HOST -P$MYSQL_PORT"
if [ $# -ne "$ARGS" ] ;then
echo "Please input one arguement:"
fi
case "$1" in
Uptime)
result=`${MYSQL_CONN} status|cut -f2 -d":"|cut -f1 -d"T"`
echo $result
;;
Com_update)
result=`${MYSQL_CONN} extended-status |grep -w "Com_update"|cut -d"|" -f3`
echo $result
;;
Slow_queries)
result=`${MYSQL_CONN} status |cut -f5 -d":"|cut -f1 -d"O"`
echo $result
;;
Com_select)
result=`${MYSQL_CONN} extended-status |grep -w "Com_select"|cut -d"|" -f3`
echo $result
;;
Com_rollback)
result=`${MYSQL_CONN} extended-status |grep -w "Com_rollback"|cut -d"|" -f3`
echo $result
;;
Questions)
result=`${MYSQL_CONN} status|cut -f4 -d":"|cut -f1 -d"S"`
echo $result
;;
Com_insert)
result=`${MYSQL_CONN} extended-status |grep -w "Com_insert"|cut -d"|" -f3`
echo $result
;;
Com_delete)
result=`${MYSQL_CONN} extended-status |grep -w "Com_delete"|cut -d"|" -f3`
echo $result
;;
Com_commit)
result=`${MYSQL_CONN} extended-status |grep -w "Com_commit"|cut -d"|" -f3`
echo $result
;;
Bytes_sent)
result=`${MYSQL_CONN} extended-status |grep -w "Bytes_sent" |cut -d"|" -f3`
echo $result
;;
Bytes_received)
result=`${MYSQL_CONN} extended-status |grep -w "Bytes_received" |cut -d"|" -f3`
echo $result
;;
Com_begin)
result=`${MYSQL_CONN} extended-status |grep -w "Com_begin"|cut -d"|" -f3`
echo $result
;;
*)
echo "Usage:0(Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions)"
;;
esac
#chmod +x /etc/zabbix/chk_mysql.sh
测试一下:
# sh /etc/zabbix/chk_mysql.sh Uptime
27392367
三、在zabbix-agent中添加监控项
# vim /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf
UserParameter=mysql.version,mysql -V
UserParameter=mysql.status[*],/etc/zabbix/chk_mysql.sh $1
UserParameter=mysql.ping,mysqladmin -uzabbixagent -p'zabbixagent#1234' -P3306 -h127.0.0.1 ping | grep -c alive重启zabbix-agent
# service zabbix-agent restart
四、在zabbix-server中为该centos添加模板
现在server端测试下,如能正常返回数字则添加为主机添加模板:
# zabbix_get -s 192.168.1.240 -p10050 -k mysql.status[Uptime]
27390614
默认的图形结果
五、补充
当mysql down了的时候,zabbix监控不到。因为默认的MySQL is down 的触发器的触发条件是mysql.ping.last(0),mysql.ping的实现方式是UserParameter=mysql.ping,mysqladmin -uzabbix -pys_ipowerlong0418-P3306 -h127.0.0.1 ping | grep -c alive,当mysql服务停止了后,mysqladmin执行报错是一推错误信息返回不了数字0,所以zabbix触发不了,所以zabbix界面会看到problems但是不会触发报警动作的,也就意味着你不会第一时间接到email、短信、电话报警了。
所以必须改正不能用mysqladmin这种方式,想到了用telnet mysql端口的方式来判断,如下所示:UserParameter=mysql.ping,netstat -ntpl |grep 3306 |grep mysql |wc -l
3306就是mysql运行的端口号,这样当mysqlis alive那么返回为1,当mysql is down返回为0,就满足了zabbix的mysql.ping.last(0)=0的触发条件了。
修改zabbix_agentd.conf配置信息:
# 先移除默认的配置
mv /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf /usr/local/zabbix/
# 添加新的配置
vim /etc/zabbix/zabbix_agentd.conf
UserParameter=mysql.version,mysql -V
UserParameter=mysql.status[*],/usr/local/zabbix/chk_mysql.sh $1
UserParameter=mysql.ping,netstat -ntpl |grep 3317 |grep mysql |wc -l
另外zabbix-server端提取mysql.ping的数值一直是0
#zabbix_get -s 192.168.1.240 -p10050 -k mysql.ping
(Not all processes could be identified,non-owned process info
willnot be shown, you would have to be root to see it all.)
0
故需要将netstat提权:
chmod +s /bin/netstat