1.建立zabbix监控用户
mysql -uroot -p -e "grant usage on *.* to zabbix@'localhost' identified by 'zabbix';"
2.编写shell脚本
cd /usr/local/zabbixmkdir scriptvim /usr/local/zabbix/script/mysql_status.sh
#!/bin/bash#User='zabbix'#Password='zabbix'Mysql='/alidata/server/mysql/bin/mysql'Mysqladmin='/alidata/server/mysql/bin/mysqladmin'command(){$Mysql -e "show global status" | awk '$1 ~ /'"$1"'$/ {print $2}'}case $1 in Com_select) command $1 ;; Com_insert) command $1 ;; Com_update) command $1 ;; Com_delete) command $1 ;; Com_begin) command $1 ;; Com_commit) command $1 ;; Com_rollback) command $1 ;; Questions) command $1 ;; Slow_queries) command $1 ;; Bytes_received) command $1 ;; Bytes_sent) command $1 ;; Uptime) command $1 ;; Version) $Mysql -V | awk -F '[ ,]' '{print $6}' ;; Ping) $Mysqladmin ping | wc -l ;; *) echo "Usage: $0 { Com_select|Com_insert|Com_update|Com_delete|Com_begin|Com_commit|Com_rollback|Questions|Slow_queries|Bytes_received|Bytes_sent|Ping|Uptime|Version }" ;;esac
3.赋权限
chmod a+x /usr/local/zabbix/script/mysql_status.sh
4.添加自定义key
vi /usr/local/zabbix/etc/zabbix_agentd.conf
UnsafeUserParameters修改为1 允许自定义 添加以下内容
UserParameter=mysql.status[*],/usr/local/zabbix/script/mysql_status.sh $1UserParameter=mysql.ping,/usr/local/zabbix/script/mysql_status.sh PingUserParameter=mysql.version,/usr/local/zabbix/script/mysql_status.sh Version
5.修改my.cnf
vi /etc/my.cnf
添加以下内容,根据实际情况配置
[mysql] host=localhost user=zabbix password=zabbix socket=/tmp/mysql.sock[mysqladmin] host=localhost user=zabbix password=zabbix socket=/tmp/mysql.sock
6.在服务器使用get测试有无错误
/usr/local/zabbix/bin/zabbix_get -s 192.168.2.192 -p10050 -k mysql.status[Com_commit]
7.服务器端 对hosts添加模板Template App MySQL
8.重启客户端程序 等待几分钟就可以观察到监控图像产生