結合 Zabbix Agent 自定義腳本與內建模板,實現 MySQL 關鍵指標採集。
路徑:/usr/local/zabbix/scripts/chk_mysql.sh
#!/bin/bash
MYSQL_USER='root'
MYSQL_PWD='your_password'
MYSQL_HOST='127.0.0.1'
MYSQL_PORT='3306'
MYSQL_CONN="/usr/local/mysql/bin/mysqladmin -u$MYSQL_USER -p$MYSQL_PWD -h$MYSQL_HOST -P$MYSQL_PORT"
case $1 in
Uptime) result=`$MYSQL_CONN status | cut -f2 -d":" | cut -f1 -d"T"` ;;
Com_update) result=`$MYSQL_CONN extended-status | grep -w "Com_update" | cut -d"|" -f3` ;;
Slow_queries) result=`$MYSQL_CONN status | cut -f5 -d":" | cut -f1 -d"O"` ;;
Com_select) result=`$MYSQL_CONN extended-status | grep -w "Com_select" | cut -d"|" -f3` ;;
Com_rollback) result=`$MYSQL_CONN extended-status | grep -w "Com_rollback" | cut -d"|" -f3` ;;
Questions) result=`$MYSQL_CONN status | cut -f4 -d":" | cut -f1 -d"S"` ;;
Com_insert) result=`$MYSQL_CONN extended-status | grep -w "Com_insert" | cut -d"|" -f3` ;;
Com_delete) result=`$MYSQL_CONN extended-status | grep -w "Com_delete" | cut -d"|" -f3` ;;
Com_commit) result=`$MYSQL_CONN extended-status | grep -w "Com_commit" | cut -d"|" -f3` ;;
Bytes_sent) result=`$MYSQL_CONN extended-status | grep -w "Bytes_sent" | cut -d"|" -f3` ;;
Bytes_received) result=`$MYSQL_CONN extended-status | grep -w "Bytes_received" | cut -d"|" -f3` ;;
Com_begin) result=`$MYSQL_CONN extended-status | grep -w "Com_begin" | cut -d"|" -f3` ;;
*) echo "Usage: $0 {Uptime|Com_update|...}"; exit 1 ;;
esac
echo $result
授權:chmod +x chk_mysql.sh
編輯 /usr/local/zabbix-agentd/etc/zabbix_agentd.conf:
# 獲取版本
UserParameter=mysql.version,mysql -V
# 性能指標(調用腳本)
UserParameter=mysql.status[*],bash /usr/local/zabbix/scripts/chk_mysql.sh $1
# 存活檢測
UserParameter=mysql.ping,/usr/local/mysql/bin/mysqladmin -uroot -p$MYSQL_PWD -P3306 -h127.0.0.1 ping | grep -c alive
# 線程數
UserParameter=mysql.process,/usr/local/mysql/bin/mysql -uroot -p$MYSQL_PWD -P3306 -h127.0.0.1 -e "show processlist" 2>/dev/null | wc -l
systemctl restart zabbix-agent
Zabbix Web 界面:配置 → 主機 → 模板 → 關聯 Template App MySQL(或自建模板映射上述 Key)。
Threads_connected / max_connectionsQuestions、Com_commit、Com_rollbackSlow_queriesInnodb_buffer_pool_read_requests vs readsSeconds_Behind_Master(從庫)