使用Zabbix官方模板监控MySQL
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了使用Zabbix官方模板监控MySQL,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5421字,纯文字阅读大概需要8分钟。
内容图文
![使用Zabbix官方模板监控MySQL](/upload/InfoBanner/zyjiaocheng/1099/b02dad8efa5d417b97a712c3cfc43977.jpg)
关于Zabbix和MySQL安装就不在说明,请自行参考相关文档
复制官方MySQL监控的userparameter到安装目录,我这里是采用的3.4.1源码安装,安装目录在/usr/local/zabbix目录,如果你是rpm包安装请自行寻找文件和目录
cp zabbix-3.4.1/conf/zabbix_agentd/userparameter_mysql.conf /usr/local/zabbix/etc/zabbix_agentd.conf.d/
使用root用户登录到MySQL并授权一个专门监控使用的用户,当然也可以直接使用root,但不安全推荐使用普通用户,并且指定需要的权限即可
#登录数据库 mysql -uroot -p #下面是需要在数据库里面执行的命令,为了安装请不要指定all权限,也不要指定所有库,并且需要有一个安全的密码 grant all on *.* to "username"@"localhost" identified by "USER_PASSWORD";
编辑/usr/local/zabbix/etc/zabbix_agentd.conf.d/userparameter_mysql.conf
[root@db ~]# cat /usr/local/zabbix/etc/zabbix_agentd.conf.d/userparameter_mysql.conf # For all the following commands HOME should be set to the directory that has .my.cnf file with password information. # Flexible parameter to grab global variables. On the frontend side, use keys like mysql.status[Com_insert]. # Key syntax is mysql.status[variable]. UserParameter=mysql.status[*],echo "show global status where Variable_name=‘$1‘;" | HOME=/var/lib/zabbix mysql -N | awk ‘{print $$2}‘ # Flexible parameter to determine database or table size. On the frontend side, use keys like mysql.size[zabbix,history,data]. # Key syntax is mysql.size[<database>,<table>,<type>]. # Database may be a database name or "all". Default is "all". # Table may be a table name or "all". Default is "all". # Type may be "data", "index", "free" or "both". Both is a sum of data and index. Default is "both". # Database is mandatory if a table is specified. Type may be specified always. # Returns value in bytes. # ‘sum‘ on data_length or index_length alone needed when we are getting this information for whole database instead of a single table UserParameter=mysql.size[*],bash -c ‘echo "select sum($(case "$3" in both|"") echo "data_length+index_length";; data|index) echo "$3_length";; free) echo "data_free";; esac)) from information_schema.tables$([[ "$1" = "all" || ! "$1" ]] || echo " where table_schema=\"$1\"")$([[ "$2" = "all" || ! "$2" ]] || echo "and table_name=\"$2\"");" | HOME=/var/lib/zabbix mysql -N‘ UserParameter=mysql.ping,HOME=/var/lib/zabbix mysqladmin ping | grep -c alive UserParameter=mysql.version,mysql -V
需要更改的地方:
-
如果你的zabbix不是yum安装,那么需要更改HOME后面的变量为zabbix配置文件所在目录,如:/usr/local/zabbix/etc
-
如果你的MySQL不是yum安装,那么需要添加MySQL的可执行文件到环境变量,或者更改为绝对路径
-
更改默认MySQL访问的用户名
-
也可以添加自定义Key
更改后的内容如下:(根据自己需求)
[root@db ~]# cat /usr/local/zabbix/etc/zabbix_agentd.conf.d/userparameter_mysql.conf # For all the following commands HOME should be set to the directory that has .my.cnf file with password information. # Flexible parameter to grab global variables. On the frontend side, use keys like mysql.status[Com_insert]. # Key syntax is mysql.status[variable]. UserParameter=mysql.status[*],echo "show global status where Variable_name=‘$1‘;" | HOME=/usr/local/zabbix/etc mysql -uzabbix -N | awk ‘{print $$2}‘ # Flexible parameter to determine database or table size. On the frontend side, use keys like mysql.size[zabbix,history,data]. # Key syntax is mysql.size[<database>,<table>,<type>]. # Database may be a database name or "all". Default is "all". # Table may be a table name or "all". Default is "all". # Type may be "data", "index", "free" or "both". Both is a sum of data and index. Default is "both". # Database is mandatory if a table is specified. Type may be specified always. # Returns value in bytes. # ‘sum‘ on data_length or index_length alone needed when we are getting this information for whole database instead of a single table UserParameter=mysql.size[*],bash -c ‘echo "select sum($(case "$3" in both|"") echo "data_length+index_length";; data|index) echo "$3_length";; free) echo "data_free";; esac)) from information_schema.tables$([[ "$1" = "all" || ! "$1" ]] || echo " where table_schema=\"$1\"")$([[ "$2" = "all" || ! "$2" ]] || echo "and table_name=\"$2\"");" | HOME=/usr/local/zabbix/etc mysql -uzabbix -N‘ UserParameter=mysql.ping,HOME=/usr/local/zabbix/etc mysqladmin -uzabbix ping | grep -c alive UserParameter=mysql.version,mysql -V #下面是自己新增的Key UserParameter=mysql.variables[*],HOME=/usr/local/zabbix/etc mysql -uzabbix -e "show variables like ‘$1‘;" | tail -n 1 | awk ‘{print $$2}‘
在配置文件指定的HOME目录创建指定的.my.cnf配置文件,主要定义连接MySQL的信息
[root@db ~]# vim /usr/local/zabbix/etc/.my.cnf [mysql] host=localhost user=zabbix password=slf8gadffdasdPRW port=3387 [mysqladmin] host=localhost user=zabbix password=slf8gadffdasdPRW port=3387
修改zabbix_agentd配置文件,最文件最后增加一行,如果是yum安装,请根据需求更改
Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/
重启zabbix agent
/etc/init.d/zabbix_agentd restart
在zabbix监控平台对应的主机添加自带的MySQL监控模板,最一可以就可看监控数据,如果没有数据请根据报错自行解决,大致会碰到以下一些问题
-
userparameter配置文件修改错误,比如:指定的用户不对,路径不对
-
授权数据库用户权限不对,无法获取数据
-
.my.cnf配置文件错误或者路径错误
-
zabbix_agentd配置文件没有包含userparameter_mysql.conf配置文件
监控效果图如下:
最后我们新增一个自定义的监控,因为我们有一个自定义Key,如下图所示,我们的Key为mysql.variables[variables_name],需要传一个参数,这个参数就相当于show variables like "variables_name";,用于获取指定variables的值,比如:connect_timeout
注意更改间隔,时间间隔根据自己需求更改
最后我们可以根据上一个自定义监控,写一个关于show status;的监控,都没有问题,或者更复杂的一个业务监控,关于zabbix自定义监控还需要读者查阅更多相关文档
本文出自 “蓝色_风暴” 博客,请务必保留此出处http://270142877.blog.51cto.com/12869137/1975010
原文:http://270142877.blog.51cto.com/12869137/1975010
内容总结
以上是互联网集市为您收集整理的使用Zabbix官方模板监控MySQL全部内容,希望文章能够帮你解决使用Zabbix官方模板监控MySQL所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。