多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] # 通过zabbix官方模板监控mysql ## 准备环境 - 服务端:Linux CentOs7.2 192.168.0.62 - 客户端:Linux CentOs7.2 192.168.0.62 - zabbix版本:4.0.1 LTS ## 客户端 ### 1.为数据库监控创建用户 生成环境上需要注意授权用户可管理的数据库,比如只能监控zabbix的库 ``` mysql -uroot -p grant all on zabbix.* to 'zabbix'@'localhost' identified by 'zabbix'; flush privileges; ``` ### 2.创建.my.cnf文件 ``` vim /etc/zabbix/.my.cnf [client] user=zabbix host=127.0.0.1 password=zabbix ``` ### 3.修改userparameter_mysql.conf yum安装的agent默认就存在该文件,路径为 /etc/zabbix/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 ``` 其中HOME=/var/lib/zabbix和yum安装的路径不符,需要修改成HOME=/etc/zabbix 所以修改后的文件为 ``` # 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 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=/etc/zabbix mysql -N' UserParameter=mysql.ping,HOME=/etc/zabbix mysqladmin ping | grep -c alive UserParameter=mysql.version,mysql -V ``` **生效需要重启agent** ``` systemctl restart zabbix-agent ``` ### 测试执行情况 - 客户端测试 ``` 先使用root账户执行 HOME=/etc/zabbix sudo mysqladmin ping | grep -c alive 1 再使用zabbix账户执行 sudo -u zabbix sudo HOME=/etc/zabbix mysqladmin ping | grep -c alive 1 如果出错,一般都是zabbix权限不够的问题,可以看后面的sudo权限提升 ``` - 服务端测试 ``` zabbix_get -s 192.168.0.131 -k "mysql.status[Bytes_sent]" 167002 ``` ## 服务端 为目标主机添加模板链接Template DB MySQL ![https://i.loli.net/2019/09/19/7soFxbHnN2Bl8Vw.png](https://i.loli.net/2019/09/19/7soFxbHnN2Bl8Vw.png) 查看生成的图形 ![https://i.loli.net/2019/09/19/kuvjBrQ5x2SR6hP.png](https://i.loli.net/2019/09/19/kuvjBrQ5x2SR6hP.png) ## sudo提升 > 默认普通用户无法使用sudo > 需要修改/etc/sudoers文件,默认文件属性为440 > -r--r-----. 1 root root 3972 Jul 14 16:03 /etc/sudoers 直接使用visudo修改 如果你希望之后执行sudo命令时不需要输入密码,那么可以形如,注意修改行下需空一行,退出重新登录后生效 ``` root ALL=(ALL) ALL zabbix ALL=(ALL) NOPASSWD:/var/lib/mysql/bin #可以无密码使用mysqld restart命令 ``` 使用不同账户,执行执行脚本时候sudo经常会碰到 sudo: sorry, you must have a tty to run sudo这个情况,其实修改一下sudo的配置就好了 ``` 注释掉 Default requiretty 一行 #Default requiretty ``` 意思就是sudo默认需要tty终端。注释掉就可以在后台执行了。 要测试用户权限是否生效可以使用 ``` sudo -u happy sudo /var/lib/zabbix/mysqld restart ```