zabbix 自定義監控項
ZABBIX 監控MYSQL
mkdir /etc/zabbix/scripts/
vim mysql.sh
chmod +x mysql.sh
客戶端編輯配置檔案
1 2 3 4 |
[root@master zabbix] # vim /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf
UserParameter=mysql.status[*], /etc/zabbix/scripts/mysql .sh $1
UserParameter=mysql. ping ,mysqladmin -uzabbix -p123 -hlocalhost ping | grep -c alive
|
1) 監控mysql主從狀態(8分)
1 2 3 4 5 6 7 8 9 |
slave_conn= "/usr/local/mysql/bin/mysql -uroot -p123456 -S /tmp/mysql.sock2"
check_slave(){<br> #結果是2 否則。。
${slave_conn} -e "show slave status\G;" 2> /dev/null | egrep "Running|Yes" | head -n2| awk -F ':' '{print $2}' | wc -l
}
case $1 in
slave)
check_slave
;;
esac
|
2.1) mysqlbandwidth(頻寬/流量/吞吐量)
mysqladmin -uroot -p123456 -S /tmp/mysql.sock1 extended-status|grep -w "Bytes_sent"|awk '{print $4}' -->傳送
mysqladmin -uroot -p123456 -S /tmp/mysql.sock1 extended-status|grep -w "Bytes_received"|awk '{print $4}' -->接收
2.2) mysql operations(操作)
1 |
Com_begin(){
|
1 |
#每秒開始的操作<br>mysqladmin extended-status|grep -w "Com_begin"|awk '{print $4}'<br>}<br>Com_commit(){<br>#每秒提交的操作
|
1 2 3 4 5 6 |
mysqladmin extended-status| grep -w "Com_commit" | awk '{print $4}' <br>}<br>Com_delete() {
mysqladmin extended-status | awk '/<Com_delete>/{print $4}' <br><br> #mysqladmin extended-status|grep -w "Com_delete"|awk '{print $4}'
}
Com_insert() {
mysqladmin extended-status | awk '/<Com_insert>/{print $4}'
}<br>Com_rollback(){
|
1 2 3 4 5 6 7 8 |
mysqladmin extended-status| grep -w "Com_rollback" | awk '{print $4}' <br>}
Com_update() {
mysqladmin extended-status | awk '/<Com_update>/{print $4}'
}
Com_select() {
mysqladmin extended-status | awk '/<Com_select>/{print $4}'
}
|
3) 監控mysql的qps和tps(8分)
QPS(Questions Per second:):每秒查詢處理量,表示每秒能處理多少次請求,這裡是指是Mysql每秒處理查詢數,同時適用於InnoDB和MysqlSAM引擎
如何計算得到呢並計算QPS呢?
很簡單,通過"msyqladmin status" 就是先獲取到Questions和uptime對應的數值,隨後通過Questions/Uptime即可獲取
question=mysqladmin status |awk '{print $6}
uptime= mysqladmin status |awk '{print $2}'
QPS=question/uptime
浮點數可以用:
小數點後保留兩位
echo "scale=2;$question/$uptime"|bc
TPS(Transactions Per Second)
每秒處理事務數,簡單的來說就是資料庫傳輸事務處理個數,這是指單臺數據庫伺服器在單位時間內處理的事務的個數。,支援事務的儲存引擎如InnoDB等特性指標
基於com_commit和com_rollback相加併除以uptim計算出TPS
rollback=mysqladmin extended-status | awk '/\<Com_rollback\>/{print $4}'
commit=mysqladmin extended-status | awk '/\<Com_commit\>/{print $4}'
tps=(com_rollback+com_commit)/uptime
awk
BEGIN
模式:是指 awk將在讀取任何輸入行之前立即執行BEGIN
中指定的動作。END
模式:是指 awk 將在它正式退出前執行END
中指定的動作。
printf
%s %c %d %f都是格式替代符
(-表示左對齊,沒有則表示右對齊)
%-4.2f 指格式化為小數,其中.2指保留2位小數。
1 2 3 4 5 6 7 8 9 10 11 |
qps)
${mysqladmin_check} status|awk '{print $6/$2}'
;;
tps)
uptime=`${mysqladmin_check} status|awk '{print $2}'`
rollback=`${mysqladmin_check} extended-status|grep -w "Com_rollback" |awk '{print $4}'`
commit=`${mysqladmin_check} extended-status|grep -w "Com_commit" |awk '{print $4}'`
count=$[$rollback+$commit]
##chmod 777 /tmp/re.txt
echo "$count $uptime" >/tmp/re.txt
cat /tmp/re.txt|awk '{print $1/$2}'
|
新增監控項 注意 資訊型別是浮點數
4) 監控資料庫表的大小(8分)資訊型別=浮點數
mysql 函式
sum 求和
round 四捨五入
concat 用於將多個字串連線成一個字串
位元組轉換成M(1024/1204)
把 information_schema 看作是一個數據庫,確切說是資訊資料庫。
其中儲存著關於MySQL伺服器所維護的所有其他資料庫的資訊。如資料庫名,資料庫的表,表欄的資料型別與訪問權 限等。
1 2 3 4 5 6 7 |
DB_size() {
mysql -D information_schema -e "select concat(round(sum(data_length/1024/1024),2)) as data from tables where table_schema='zabbix'" | awk 'NR==2'
}
Table_size() {
mysql -Dinformation_schema -e "select concat(round(sum(data_length/1024/1024),2)) as data from tables where table_schema='zabbix' and table_name='items'" | awk 'NR==2'
}
|
完整指令碼
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
#!/bin/bash
uptime=` /usr/local/mysql/bin/mysqladmin -uroot -p123456 -S /tmp/mysql .sock1 status 2> /dev/null | awk '{print $2}' `
Mysqladmin= "/usr/local/mysql/bin/mysqladmin -uroot -p123456 -S /tmp/mysql.sock1"
Mysql= "/usr/local/mysql/bin/mysql -uroot -p123456 -S /tmp/mysql.sock1"
##mysql主從狀態
slave(){
/usr/local/mysql/bin/mysql -uroot -p123456 -S /tmp/mysql .sock2 -e "show slave status\G" 2> /dev/null | egrep "Running|Yes" | head -n2| awk -F ':' '{print $2}' | wc -l
}
##mysql 流量
Bytes_sent(){
$Mysqladmin extended-status 2> /dev/null | grep -w "Bytes_sent" | awk '{print $4}'
}
Bytes_received(){
$Mysqladmin extended-status 2> /dev/null | grep -w "Bytes_received" | awk '{print $4}'
}
####################
##mysql的操作資訊##
Com_insert(){
$Mysqladmin extended-status 2> /dev/null | grep -w "Com_insert" | awk '{print $4}'
}
Com_update(){
$Mysqladmin extended-status 2> /dev/null | grep -w "Com_update" | awk '{print $4}'
}
Com_select(){
$Mysqladmin extended-status 2> /dev/null | grep -w "Com_select" | awk '{print $4}'
}
Com_delete(){
$Mysqladmin extended-status 2> /dev/null | grep -w "Com_delete" | awk '{print $4}'
}
Com_rollback(){
$Mysqladmin extended-status 2> /dev/null | grep -w "Com_rollback" | awk '{print $4}'
}
Com_commit(){
$Mysqladmin extended-status 2> /dev/null | grep -w "Com_commit" | awk '{print $4}'
}
########################
db_size() {
$Mysql -D information_schema -e "select round(sum(data_length/1024/1024),2) as data from tables where table_schema='mysql'" 2> /dev/null | awk 'NR==2'
}
table_size(){
$Mysql -D information_schema -e "select round(sum(data_length/1024/1024),2) as data from tables where table_schema='mysql' and table_name='user'" 2> /dev/null | awk 'NR==2'
}
$1
|
如果zabbix_get 取不到值,顯示為NUll,原因是zabbix使用者沒有執行指令碼的許可權,需要修改以下兩個地方
1 2 3 4 5 6 |
mysql執行命令寫完整路徑UserParameter=mysql.status[*], sudo /etc/zabbix/scripts/mysql .sh $1
visudo 修改
## Same thing without a password
# %wheel ALL=(ALL) NOPASSWD: ALL
zabbix ALL=(ALL) NOPASSWD: ALL
|
報錯資訊如下
[root@localhost fonts]# zabbix_get -s 192.168.1.9 -k mysql_status[slave_status]
ERROR 1227 (42000) at line 1: Access denied; you need (at least one of) the SUPER,REPLICATION CLIENT privilege(s) for this operation
0
解決:在你的檢測指令碼上寫上mysql的使用者名稱和密碼就可以了
在從庫上做一個授權賬號:check, 密碼為:123456
1 2 3 4 5 6 |
#!/bin/bash
case $1 in
slave_status)
mysql -ucheck -p123456 -e "show slave status\G"|grep "Running"|grep "Yes"|awk -F ':' '{print $2}'|wc -l
;;
esac
|