zabbix监控mysql
这里可以采用zabbix自带的mysql模版,但是也需要在mysql服务器上准备获取mysql status的脚本chk_mysql.sh,zabbix通过调用这个脚本来获取mysql的运行信息。
在配置文件zabbix_agentd.conf里面添加mysql监控信息:
# vim /etc/zabbix/zabbix_agentd.conf UserParameter=mysql.version,mysql -V UserParameter=mysql.status[*],/usr/local/zabbix/scripts/chk_mysql.sh $1 UserParameter=mysql.ping,mysqladmin -uzabbix -pys_ipowerlong0418 -P3306 -h127.0.0.1 ping | grep -c alive |
添加mysql帐号:
GRANT PROCESS,SUPER,REPLICATION CLIENT ON *.* TO zabbix@'127.0.0.1' IDENTIFIED BY 'ys_ipowerlong0418'; |
添加check脚本权限:
chmod u+x /usr/local/zabbix/chk_mysql.sh chown -R zabbix.zabbix /usr/local/zabbix/chk_mysql.sh |
去zabbix服务器上面检查基本mysql服务器信息是否能正常获取到,zabbix_get -s 192.168.1..62 -p10050 -k"system.cpu.load[all,avg15]";:
[root@hch_test_121_12 html]# zabbix_get -s 192.168.1..62 -p10050 -k "system.cpu.load[all,avg15]"; 0.050000 [root@hch_test_121_12 html]# |
接下来准备mysql的运行信息监控,需要准备新的mysql监控脚本chk_mysql.sh,/usr/local/zabbix/chk_mysql.sh:
#!/bin/sh # ------------------------------------------------------------------------------- # FileName: check_mysql.sh # Revision: 1.0 # Date: 2016/04/22 # Author: tim # Email: mchdba@sohu.com MYSQL_SOCK="/usr/local/mysql/mysql.sock" MYSQL_USER='zabbix' MYSQL_PWD='ys_ipowerlong0418' MYSQL_HOST='127.0.0.1' MYSQL_PORT='3306' ARGS=1 if [ $# -ne "$ARGS" ];then echo "Please input one arguement:" fi case $1 in Uptime) result=`/usr/local/mysql/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -S $MYSQL_SOCK status|cut -f2 -d":"|cut -f1 -d"T"` echo $result ;; Com_update) result=`/usr/local/mysql/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w "Com_update"|cut -d"|" -f3` echo $result ;; Slow_queries) result=`/usr/local/mysql/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -S $MYSQL_SOCK status |cut -f5 -d":"|cut -f1 -d"O"` echo $result ;; Com_select) result=`/usr/local/mysql/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w "Com_select"|cut -d"|" -f3` echo $result ;; Com_rollback) result=`/usr/local/mysql/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w "Com_rollback"|cut -d"|" -f3` echo $result ;; Questions) result=`/usr/local/mysql/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -S $MYSQL_SOCK status|cut -f4 -d":"|cut -f1 -d"S"` echo $result ;; Com_insert) result=`/usr/local/mysql/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w "Com_insert"|cut -d"|" -f3` echo $result ;; Com_delete) result=`/usr/local/mysql/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w "Com_delete"|cut -d"|" -f3` echo $result ;; Com_commit) result=`/usr/local/mysql/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w "Com_commit"|cut -d"|" -f3` echo $result ;; Bytes_sent) result=`/usr/local/mysql/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w "Bytes_sent" |cut -d"|" -f3` echo $result ;; Bytes_received) result=`/usr/local/mysql/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w "Bytes_received" |cut -d"|" -f3` echo $result ;; Com_begin) result=`/usr/local/mysql/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -S $MYSQL_SOCK 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 |
脚本准备完后,重启agent,然后在zabbix-server服务器上测试是否可以获取到mysql的status信息:
[root@hch_test_121_12 html]# zabbix_get -s 192.168.1..71 -p10050 -k mysql.status[Com_update] Warning: Using a password on the command line interface can be insecure. 78055 [root@hch_test_121_12 html]# [root@hch_test_121_12 html]# [root@hch_test_121_12 html]# zabbix_get -s 192.168.1..71 -p10050 -k mysql.status[Com_insert] Warning: Using a password on the command line interface can be insecure. 51332 [root@hch_test_121_12 html]# |
6,监控性能视图
监控脚本运行正常后,就会在zabbix-server的host下面的graph里面看到mysql的监控性能视图,monitoringààGraphsààHost(选择mysql服务器)ààGraph(选择mysql选项),可以看到性能监控视图,zabbix自带模版默认有2个性能图,这些图要等运行一段时间后才能画出来:
041.png,MySQL operations:
042.png,MySQL bandwidth:
7,mysqladmin的缺陷
当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 3317 |grep mysql |wc|awk '{print $1}'
3317就是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 |awk '{print $1}' |
8,报错汇总记录
8.1报错信息
[root@db_m1_slave_1 zabbix-3.0.2]# tail -f/var/log/zabbix/zabbix_agentd.log
……
17336:20160422:163813.319 no active checkson server [192.168.121.12:10051]: host [hch_test_121_12] not found
17336:20160422:164013.503 no active checks onserver [192.168.121.12:10051]: host [hch_test_121_12] not found
解决办法:
出现该错误的原因是一般是zabbix_agentd.conf里面的Hostname和前端zabbix web里面的配置不一样所造成的,即在zabbix web页面Monitoring->Configuration->Hosts 页面的Host Name和zabbix_agentd.conf里面的Hostname要一样。
再看后台日志信息,正常如下:
[root@db_m1_slave_1 zabbix-3.0.2]# tail -f/var/log/zabbix/zabbix_agentd.log
17468:20160422:164350.284 IPv6 support: YES
17468:20160422:164350.284 TLS support: YES
17468:20160422:164350.284**************************
17468:20160422:164350.284 using configurationfile: /etc/zabbix/zabbix_agentd.conf
17468:20160422:164350.293 agent #0 started[main process]
17469:20160422:164350.294 agent #1 started[collector]
17470:20160422:164350.294 agent #2 started[listener #1]
17471:20160422:164350.294 agent #3 started[listener #2]
17472:20160422:164350.295 agent #4 started[listener #3]
17473:20160422:164350.295 agent #5 started[active checks #1]
8.2问题is not suitable for value type [Numeric (unsigned)] and datatype [Decimal]
12394:20160422:200640.676 error reason for "db_master_1:mysql.status[Questions]" changed: Received value [Warning: Using a password on the command line interface can be insecure.18463244] is not suitable for value type [Numeric (float)] 12393:20160422:200642.678 error reason for "db_master_1:mysql.status[Uptime]" changed: Received value [Warning: Using a password on the command line interface can be insecure.580661] is not suitable for value type [Numeric (unsigned)] and data type [Decimal]
后台zabbix服务器日志: [root@hch_test_121_12 ~]# /usr/local/zabbix/bin/zabbix_get -s 192.168.1..71 -p10050 -k mysql.status[Com_update] Warning: Using a password on the command line interface can be insecure. 92315 [root@hch_test_121_12 ~]#
解决办法是,在zabbix客户端,添加用户名密码配置,这样避免shell密码外泄也不会报错,(这里报这种错误是因为返回的结果集中有字符串提醒信息,而后台zabbix的item的是decimal所以存储不进去报错): vim /usr/local/mysql/my.cnf [mysqladmin] user=zabbix password=ys_ipowerlong0418
然后重启zabbix_agent 然后在后台zabbix测试,没有报多余的信息: [root@hch_test_121_12 ~]# /usr/local/zabbix/bin/zabbix_get -s 192.168.1..71 -p10050 -k mysql.status[Com_insert] 58272 [root@hch_test_121_12 ~]#
之后再看zabbix_server后台日志,正常了不会报错了,如下所示: 12855:20160422:233520.636 item "db_master_2:mysql.status[Bytes_received]" became supported 12857:20160422:233521.636 item "db_master_2:mysql.status[Bytes_sent]" became supported 12854:20160422:233522.637 item "db_master_2:mysql.status[Com_begin]" became supported 12856:20160422:233523.638 item "db_master_2:mysql.status[Com_commit]" became supported 12857:20160422:233524.641 item "db_master_2:mysql.status[Com_delete]" became supported 12855:20160422:233525.642 item "db_master_2:mysql.status[Com_insert]" became supported 12856:20160422:233526.642 item "db_master_2:mysql.status[Com_rollback]" became supported 12854:20160422:233527.643 item "db_master_2:mysql.status[Com_select]" became supported 12855:20160422:233528.645 item "db_master_2:mysql.status[Com_update]" became supported 12857:20160422:233529.646 item "db_master_2:mysql.status[Questions]" became supported 12856:20160422:233530.646 item "db_master_2:mysql.status[Slow_queries]" became supported
|
8.3报错Check access restrictions in Zabbix agent configuration:
[root@hch_test_121_12 alertscripts]#zabbix_get -s 192.168.1..72 -p10050 -k "system.hostname";
zabbix_get [17730]: Check accessrestrictions in Zabbix agent configuration
[root@hch_test_121_12 alertscripts]#
解决:
配置文件坏了,copy一个备份的过来,改下,重启agent。
在zabbix服务器上测试:
[root@hch_test_121_12 alertscripts]#/usr/local/zabbix/bin/zabbix_get -s 192.168.1.63 -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
[root@hch_test_121_12 alertscripts]#
去mysql服务器上赋予s权限
[root@db_m1_slave2 soft]# chmod +s /bin/netstat
You have new mail in /var/spool/mail/root
[root@db_m1_slave2 soft]#
8.4 zabbix_get [1291]: Get value error
[root@hch_test_121_12 ~]#/usr/local/zabbix/bin/zabbix_get -s 192.168.1.21 -p10050 -k system.info
zabbix_get [1291]: Get value error: cannotconnect to [[10.254.1.21]:10050]: [111] Connection refused
[root@hch_test_121_12 ~]#