摘要
利用
Zabbix
监控MySQL
的时候,zabbix官网自带的插件只提供了很少的监控参数,对于运维和DBA来说远远不够,这里利用Percona官网的插件来监控MySQL服务
依赖
Percona官网监控MySQL的插件是利用PHP
写的,故需要安装PHP环境
yum install php php-mysql
MySQL installation
监控MySQL当然需要安装MySQL服务,对于具体安装和配置MySQL服务的过程请参考:
percona monitoring plugin installation
## first to install percona releaseyum install http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona-release-0.1-4.noarch.rpm## then install percona-zabbix-templatesyum install percona-zabbix-templates
安装插件之后,会在主机的 /var/lib/zabbix/ 目录下生成一个 percona 目录,具体内容如下:
[root@i-5ou6shc5 percona]# ls -lR /var/lib/zabbix/percona/ /var/lib/zabbix/percona/: 总用量 8 drwxr-xr-x 2 zabbix zabbix 4096 3月 16 14:11 scripts drwxr-xr-x 2 zabbix zabbix 4096 3月 10 12:21 templates /var/lib/zabbix/percona/scripts: 总用量 68 -rwxr-xr-x 1 zabbix zabbix 1540 3月 16 14:11 get_mysql_stats_wrapper.sh -rwxr-xr-x 1 zabbix zabbix 59970 3月 15 15:42 ss_get_mysql_stats.php /var/lib/zabbix/percona/templates: 总用量 284 -rw-r--r-- 1 zabbix zabbix 18866 12月 10 02:22 userparameter_percona_mysql.conf -rw-r--r-- 1 zabbix zabbix 269258 12月 10 02:22 zabbix_agent_template_percona_mysql_server_ht_2.0.9-sver1.1.7.xml
文件说明:
scripts/get_mysql_stats_wrapper.sh ## 是监控获取MySQL状态的scripts/ss_get_mysql_stats.php ## 是配置连接数据库用户名密码等,用shell来调用该PHP文件templates/userparameter_percona_mysql.conf ## 实际zabbix agent需要的配置文件templates/zabbix_agent_template_percona_mysql_server_ht_2.0.9-sver1.1.7.xml ## zabbix 模板
具体涉及到的配置:
cp /var/lib/zabbix/percona/templates/userparameter_percona_mysql.conf /etc/zabbix/zabbix_agentd.d/ vim /var/lib/zabbix/percona/ss_get_mysql_stats.php.cnf <?php $mysql_user = 'zabbix'; $mysql_pass = 'zabbix';
验证
按照官网描述,这个时候执行 `/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh gg` 应该返回一个具体的数字是对的,但是如果报如下错误
[root@i-5ou6shc5 percona] /var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh gg ERROR: run the command manually to investigate the problem: /usr/bin/php -q /var/lib/zabbix/percona/scripts/ss_get_mysql_stats.php --host localhost --items gg [root@i-5ou6shc5 percona]/usr/bin/php -q /var/lib/zabbix/percona/scripts/ss_get_mysql_stats.php --host localhost --items gg ERROR: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)[root@i-5ou6shc5 percona]#
解决办法
vim ~zabbix/.my.cnf [mysql] user = zabbix password = zabbix
至此和官网介绍一致,
[root@i-5ou6shc5 opt]# sudo -u zabbix -H /var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh running-slave 0
但是执行别的参数还是报错
[root@i-5ou6shc5 percona]# /usr/bin/php -q /var/lib/zabbix/percona/scripts/ss_get_mysql_stats.php --host localhost --items gg ERROR: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)[root@i-5ou6shc5 percona]#
确认zabbix连接MySQL的账号和权限无误。 It's so strange! 这个时候可以参考文章后面的Q&A 第一个
而且发现官网一个问题:
[root@i-5ou6shc5 percona]# /usr/bin/php -q /var/lib/zabbix/percona/scripts/ss_get_mysql_stats.php --host localhost --items gg --socket /data/db/mysql/data/mysql.socket Unknown option --socket Usage: php ss_get_mysql_stats.php --host <host> --items <item,...> [OPTION] --host MySQL host --items Comma-separated list of the items whose data you want --user MySQL username --pass MySQL password --port MySQL port --socket MySQL socket --flags MySQL flags --connection-timeout MySQL connection timeout --server-id Server id to associate with a heartbeat if heartbeat usage is enabled --nocache Do not cache results in a file --help Show usage
这里列举出来的参数显示有 --socket
, 但是实际加上的时候却提示 Unknown option --socket
查看源代码发现bug,在PHP源代码的174行参数中没有出现 socket, 所以在188行出现Unknown option
字样
而在列举可用参数的时候却出现了socket选项
show usage.png
173 function validate_options($options) { 174 $opts = array('host', 'items', 'user', 'pass', 'nocache', 'port', 'server-id'); ... 186 foreach ( $options as $key => $val ) { 187 if ( !in_array($key, $opts) ) { 188 usage("Unknown option --$key"); 189 } 190 } 191 }
Q&A
first
Q:ERROR: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' A: /data/db/mysql/data/mysql.socket 是真实有效的MySQL socket文件 1, ln -s /data/db/mysql/data/mysql.socket /var/lib/mysql/mysql.sock 2, sed -i "s@#\$mysql_socket = NULL;@\$mysql_socket = '/data/db/mysql/data/mysql.socket';@g" /var/lib/zabbix/percona/scripts/ss_get_mysql_stats.php
second
Q: rm: cannot remove `/tmp/localhost-mysql_cacti_stats.txt': Operation not permitted A: 在agent上面执行如下操作即可 rm -rf /tmp/localhost-mysql_cacti_stats.txt touch /tmp/localhost-mysql_cacti_stats.txt chown -R zabbix:zabbix /tmp/localhost-mysql_cacti_stats.txt
Others
作者:全栈运维
链接:https://www.jianshu.com/p/a40a4e2c2c53