zabbix4.0安装percona插件监控MySQL

  • by

1、zabbix server agent已经部署完毕,请参考本站点zabbix目录。

2、yum安装php和php-mysql

[root@new scripts]# yum install php php-mysql

3、percona插件安装:

[root@new scripts]# cd /usr/local/src/

官网下载percona的rpm包,我这里是下载的是1.1.8版本的

[root@new src]# wget https://www.percona.com/downloads/percona-monitoring-plugins/percona-monitoring-plugins-1.1.8/binary/redhat/7/x86_64/percona-zabbix-templates-1.1.8-1.noarch.rpm

安装percona的rpm包

[root@new src]#rpm -ivh percona-zabbix-templates-1.1.8-1.noarch.rpm

4、进入templates目录,把模板文件复制到zabbix_agentd.conf.d目录下:

[root@new src]# cd /var/lib/zabbix/percona/templates

[root@new templates]

# ls
userparameter_percona_mysql.conf zabbix_agent_template_percona_mysql_server_ht_2.0.9-sver1.1.8.xml

[root@new templates]# cp userparameter_percona_mysql.conf /usr/local/zabbix/etc/zabbix_agentd.conf.d/

5、编辑配置文件/usr/local/zabbix/etc/zabbix_agentd.conf,添加模板文件路径

[root@new templates]# vim /usr/local/zabbix/etc/zabbix_agentd.conf
Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/

6、修改MySQL的执行命令文件,更改mysql的用户与密码:

[root@new templates]# vim /var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh

7、修改 ss_get_mysql_stats.php文件,设置MySQL的用户名和密码,用户密码为本地;

 [root@nes templates]# vim /var/lib/zabbix/percona/scripts/ss_get_mysql_stats.php

8、重启zabbix_agentd

9、在zabbix监控页面中添加MySQL监控,并选择Percona MySQL Server Template模板:–>配置 –->模板 –->导入–->选择文件–->导入 ,如图1所示

注意:这个zabbix_agent_template_percona_mysql_server_ht_2.0.9-sver1.1.7.xml 文件在/var/lib/zabbix/percona/templates目录下,下载到桌面上传就好。 

10、导入出错出现“标签无效 “/zabbix_export/date”: “YYYY-MM-DDThh:mm:ssZ” 预计”

11、解决方法:

手动修改模板文件解决报错比较麻烦,有人提到可以将此模板导入 Zabbix 2.X 系列后,再导出到 Zabbix 3.X、4.X。

这里直接使用现成可用的 Zabbix 4.X 的 percona-zabbix-templates

zbx_percona_mysql_template.xml模板地址

重新导入到zabbix web监控端

重新导入

12、注意以下几点:

A、修改zabbix目录用户和用户组为zabbix,如

chown -R zabbix.zabbix /usr/local/zabbix

B、修改/tmp目录下 localhost-mysql_cacti_stats.txt 文件用户组和权限

chown -R zabbix.zabbix /tmp/localhost-mysql_cacti_stats.txt

chmod 755 /tmp/localhost-mysql_cacti_stats.txt

如果没有此文件,创建  touch /tmp/localhost-mysql_cacti_stats.txt

C、修改 /var/lib/zabbix/percona/scripts目录下两个文件参数

D、检查agent、server端是否能获取数据

(1)agentd端mysql服务器获取数据

[root@db_m2_slave1 ~]# /usr/bin/php -q/var/lib/zabbix/percona/scripts/ss_get_mysql_stats.php –host localhost –itemsgg

gg:6

(2)zabbix_server端获取不到数据

[root@zabbix_serv_121_12 scripts]

#/usr/local/zabbix/bin/zabbix_get -s 192.161.3.72 -p10050 -k “MySQL.Threads-connected”

ERROR: run the command manually toinvestigate the problem: /usr/bin/php -q/var/lib/zabbix/percona/scripts/ss_get_mysql_stats.php –host localhost –itemsgg

那么问题在哪里呢?这要从zabbix-sever和zabbix-agentd的原理流程分析起了,sever是通过zabbix的根目录去调用/etc/zabbix/zabbix_agentd.d/userparameter_percona_mysql.conf里面获取参数MySQL.Threads-connected的,所以我们去找这个userparameter_percona_mysql.conf的此参数值的获取方法。

root@db_m2_slave1 ~]# more /etc/zabbix/zabbix_agentd.d/userparameter_percona_mysql.conf |grep MySQL.Threads-connected

UserParameter=MySQL.Threads-connected,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh  iu

[root@db_m2_slave1 ~]

#

然后执行此参数方法:查看执行记录,果然报错,调用不出来记录:

[root@db_m2_slave1 ~]

# sh /var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh  kt

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

使用bash跟踪问题,查看到问题在于“+ ‘[‘ -e /tmp/localhost-mysql_zabbix_stats.txt ‘]’

”后报错,如下所示:

[root@db_m2_slave1 ~]

# bash -x /var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh  kt

  • echo ”
  • ITEM=kt
  • HOST=localhost

++ dirname /var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh

  • DIR=/var/lib/zabbix/percona/scripts
  • CMD=’/usr/bin/php -q /var/lib/zabbix/percona/scripts/ss_get_mysql_stats.php –host localhost –items gg’
  • CACHEFILE=/tmp/localhost-mysql_zabbix_stats.txt
  • ‘[‘ kt = running-slave ‘]’
  • ‘[‘ -e /tmp/localhost-mysql_zabbix_stats.txt ‘]’
  • /usr/bin/php -q /var/lib/zabbix/percona/scripts/ss_get_mysql_stats.php –host localhost –items gg
  • ‘[‘ -e /tmp/localhost-mysql_zabbix_stats.txt ‘]’
  • echo ‘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’

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

那么就去看下这个文件在不在,果然不在,那么建个空文件,然后赋予zabbix帐号权限:

[root@db_m2_slave1 ~]

# vim/tmp/localhost-mysql_zabbix_stats.txt

[root@db_m2_slave1 ~]

# chown -R zabbix:zabbix /tmp/localhost-mysql_zabbix_stats.txt

[root@db_m2_slave1 ~]

#

然后再bash调用执行命令:

[root@db_m2_slave1 ~]

# bash -x /var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh  kt

  • echo ”
  • ITEM=kt
  • HOST=localhost

++ dirname /var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh

  • DIR=/var/lib/zabbix/percona/scripts
  • CMD=’/usr/bin/php -q /var/lib/zabbix/percona/scripts/ss_get_mysql_stats.php –host localhost –items gg’
  • CACHEFILE=/tmp/localhost-mysql_zabbix_stats.txt
  • ‘[‘ kt = running-slave ‘]’
  • ‘[‘ -e /tmp/localhost-mysql_zabbix_stats.txt ‘]’

++ stat -c %Y /tmp/localhost-mysql_zabbix_stats.txt

  • TIMEFLM=1463491777

++ date +%s

  • TIMENOW=1463491788

++ expr 1463491788 – 1463491777

  • ‘[‘ 11 -gt 300 ‘]’
  • ‘[‘ -e /tmp/localhost-mysql_zabbix_stats.txt ‘]’
  • cat /tmp/localhost-mysql_zabbix_stats.txt
  • sed ‘s/ /\n/g; s/-1/0/g’
  • grep kt
  • awk -F: ‘{print $2}’

[root@db_m2_slave1 ~]#

然后在zabbix-server上测试验证:

[root@zabbix_serv_121_12 scripts]

# /usr/local/zabbix/bin/zabbix_get -s 192.161.3.72 -p10050 -k “MySQL.Threads-connected”

9

[root@zabbix_serv_121_12 scripts]#


发表评论

电子邮件地址不会被公开。 必填项已用*标注

2 × 3 =