zabbix监控mysql
2017-04-19 13:28 abce 阅读(349) 评论(0) 编辑 收藏 举报1.创建用来连接mysql的数据库用户
1 2 | > mysql -uroot -p -e "GRANT USAGE ON *.* TO 'zabbix'@'localhost' IDENTIFIED BY 'password'" ; > mysql -uroot -p -e "flush privileges" ; |
2.自定义key,创建mysql监控配置
1 2 | # cd /usr/local/zabbix/etc/zabbix_agentd.conf.d # vi userparameter_mysql.conf |
这个文件可以从zabbix_server服务端安装时解压目录/usr/local/src/zabbix3.x.x/conf/zabbix_agentd/userparameter_mysql.conf拷贝过来,添加以下内容:(mysql的参数和zabbix需要使用的命令)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | # 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= /etc/zabbix /usr/local/mysql/bin/mysql -N | awk '{print $$2}' # My line # 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=/etc/zabbix /usr/local/mysql/bin/mysql -N' #Default below UserParameter=mysql. ping ,HOME= /etc/zabbix /usr/local/mysql/bin/mysqladmin ping | grep -c alive #My line UserParameter=mysql.uptime,HOME= /etc/zabbix /usr/local/mysql/bin/mysqladmin status | cut -f2 -d ":" | cut -f1 -d "T" | tr -d " " UserParameter=mysql.threads,HOME= /etc/zabbix /usr/local/mysql/bin/mysqladmin status | cut -f3 -d ":" | cut -f1 -d "Q" | tr -d " " UserParameter=mysql.questions,HOME= /etc/zabbix /usr/local/mysql/bin/mysqladmin status | cut -f4 -d ":" | cut -f1 -d "S" | tr -d " " UserParameter=mysql.slowqueries,HOME= /etc/zabbix /usr/local/mysql/bin/mysqladmin status | cut -f5 -d ":" | cut -f1 -d "O" | tr -d " " UserParameter=mysql.qps,HOME= /etc/zabbix /usr/local/mysql/bin/mysqladmin status | cut -f9 -d ":" | tr -d " " UserParameter=mysql.version, /usr/local/mysql/bin/mysql -V |
这里HOME是.my.cnf文件所在的目录
另外,要注意userparameter_mysql.conf文件里的mysql命令路径(可以提前做好mysql的系统环境变量,以防mysql命令不被系统识别)
3.编辑zabbix agentd客户端的配置
1 | # vim /etc/zabbix/etc/zabbix_agentd.conf |
1 2 | Include= /etc/zabbix/etc/zabbix_agentd .conf.d/ #扩展配置目录 UnsafeUserParameters=1 #允许自定义key |
4.配置zabbix连接mysql用户信息配置
1 2 | # cd /etc/zabbix # vim .my.cnf |
1 2 3 4 5 6 7 8 9 10 | [mysql] host=localhost user=zabbix password=password socket= /var/run/mysqld/mysqld .sock [mysqladmin] host=localhost user=zabbix password=password socket= /var/run/mysqld/mysqld .sock |
如果在数据库grant授权时,针对的是localhost,这个.my.cnf里面就不用加host参数了。
但如果grant授权时针对的是本机的ip(如xxx.xxx.xxx.xxx),那么在.my.cnf文件里就要加上host参数进行指定,即在.my.cnf文件就要加上:
host=xxx.xxx.xxx.xxx
socket=/var/run/mysqld/mysqld.sock #这一行可以不用加上,默认会找到的
5.重启zabbix client
1 2 3 | # service zabbix_agentd restart 或者 # /etc/zabbix/sbin/zabbix_agentd |
6.在zabbix server上通过zabbix_get测试是否能获取zabbix agentd的mysql status
1 2 | # zabbix_get -s 192.168.1.25 -p 10050 -k "mysql.status[Uptime]" 578 |
如果出现类似这一串key的数字,就说明配置ok,服务端能监控到客户端的mysql数据了!
最后在监控界面增加主机对应的MySQL模板。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
2016-04-19 DG - 开启Active Data Guard
2016-04-19 snapshot standby database