Linux下Zabbix監控之MySQL監控添加

Mysql數據庫監控

  • zabbix頁面添加Mysql監控默認模板
Template DB MySQL
  • 創建mysqladmin鏈接配置
touch /etc/zabbix/scripts/.my.cnf
[mysqladmin]
host=192.168.1.253
user=zabbix
password=zabbix
  • Mysql監控數據獲取腳本chk_mysql.sh
# 數據連接
MYSQL_CONN="/usr/local/mysql/bin/mysqladmin --defaults-extra-file=/etc/zabbix/scripts/.my.cnf"

# 參數是否正確
if [ $# -ne "1" ];then
    echo "arg error!"
fi

# 獲取數據
case $1 in
    Uptime)
        result=`${MYSQL_CONN} status|cut -f2 -d":"|cut -f1 -d"T"`
        echo $result
        ;;
    Com_update)
        result=`${MYSQL_CONN} extended-status |grep -w "Com_update"|cut -d"|" -f3`
        echo $result
        ;;
    Slow_queries)
        result=`${MYSQL_CONN} status |cut -f5 -d":"|cut -f1 -d"O"`
        echo $result
        ;;
    Com_select)
        result=`${MYSQL_CONN} extended-status |grep -w "Com_select"|cut -d"|" -f3`
        echo $result
                ;;
    Com_rollback)
        result=`${MYSQL_CONN} extended-status |grep -w "Com_rollback"|cut -d"|" -f3`
                echo $result
                ;;
    Questions)
        result=`${MYSQL_CONN} status|cut -f4 -d":"|cut -f1 -d"S"`
                echo $result
                ;;
    Com_insert)
        result=`${MYSQL_CONN} extended-status |grep -w "Com_insert"|cut -d"|" -f3`
                echo $result
                ;;
    Com_delete)
        result=`${MYSQL_CONN} extended-status |grep -w "Com_delete"|cut -d"|" -f3`
                echo $result
                ;;
    Com_commit)
    Com_update)
        result=`${MYSQL_CONN} extended-status |grep -w "Com_update"|cut -d"|" -f3`
        echo $result
        ;;
    Slow_queries)
        result=`${MYSQL_CONN} status |cut -f5 -d":"|cut -f1 -d"O"`
        echo $result
        ;;
    Com_select)
        result=`${MYSQL_CONN} extended-status |grep -w "Com_select"|cut -d"|" -f3`
        echo $result
                ;;
    Com_rollback)
        result=`${MYSQL_CONN} extended-status |grep -w "Com_rollback"|cut -d"|" -f3`
                echo $result
                ;;
    Questions)
        result=`${MYSQL_CONN} status|cut -f4 -d":"|cut -f1 -d"S"`
                echo $result
                ;;
    Com_insert)
        result=`${MYSQL_CONN} extended-status |grep -w "Com_insert"|cut -d"|" -f3`
                echo $result
                ;;
    Com_delete)
        result=`${MYSQL_CONN} extended-status |grep -w "Com_delete"|cut -d"|" -f3`
                echo $result
                ;;
    Com_commit)
        result=`${MYSQL_CONN} extended-status |grep -w "Com_commit"|cut -d"|" -f3`
                echo $result
                ;;
    Bytes_sent)
        result=`${MYSQL_CONN} extended-status |grep -w "Bytes_sent" |cut -d"|" -f3`
                echo $result
                ;;
    Bytes_received)
        result=`${MYSQL_CONN} extended-status |grep -w "Bytes_received" |cut -d"|" -f3`
                echo $result
                ;;
    Com_begin)
        result=`${MYSQL_CONN} extended-status |grep -w "Com_begin"|cut -d"|" -f3`
                echo $result
                ;;

        *)
        echo "Usage:$0(Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions|Com_insert|Com_delete|Com_commit|Bytes_sent|Bytes_received|Com_begin)"
        ;;
esac
  • 配置userparameter_mysql.conf
 vim /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf
UserParameter=mysql.status[*],/etc/zabbix/script/mysql/chk_mysql.sh $1    #Mysql數據庫狀態數據抽取;
UserParameter=mysql.ping,netstat -ntpl |grep 3306|grep mysql|wc |awk '{print $1}'    #Mysql數據庫狀態;
UserParameter=mysql.version,/usr/local/mysql/bin/mysql -V    #Mysql數據庫版本信息;
  • 權限配置
netstat 命令中,-p參數需要root用戶權限;故做以下配置
chmod +s /bin/netstat
vim /etc/sudoers
#Defaults specification    #找到此行并注釋
zabbix  ALL=(ALL)       NOPASSWD:/bin/netstat
  • zabbix-server上測試
 zabbix_get -s192.168.1.253 -k "mysql.ping"
1
 zabbix_get -s192.168.1.253 -k "mysql.status[Uptime]"
1547
  • 重啟zabbix-agent服務
systemctl restart zabbix-agent.service

注意事項
  1. mysqladmin相關報錯:
zabbix_get -s192.168.1.253 -k "mysql.status[Com_rollback]"
mysqladmin: connect to server at '192.168.1.253' failed
error: 'Can't connect to MySQL server on '192.168.1.253' (13)'
Check that mysqld is running on 192.168.1.253 and that the port is 3306.
You can check this by doing 'telnet 192.168.1.253 3306'

此報錯跟mysql賬號權限有關,請測試mysqladmin -uroot -pXXXX -h192.168.1.253 是否可以登錄,如果沒有權限需要鑒權;

  1. 獲取狀態異常:
 zabbix_get -s100.101.156.225 -k "mysql.ping"
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
0

此報錯跟netstat權限有關,請參照“權限配置”;

  1. Warning: Using a password on the command line interface can be insecure.
    此報錯是因為mysql5.6及以后的版本對命令中明文密碼的保護機制,可以采用--defaults-extra-file指定配置文件的方式解決;
  2. zabbix-agent本地測試沒問題,zabbix_get測試獲取數據異常
    此報錯是mysqlmysqladmin命令需要填寫絕對路徑;
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容