1. 程式人生 > >結合zabbix監控系統io相關性能服務

結合zabbix監控系統io相關性能服務

zabbix監牢io服務腳本

一、環境及說明

本次實驗基於CentOS6.x_x64 zabbix2.4.5(其實可以是其他版本的zabbix服務端),i測試的客戶端機器:10.168.118.61(安裝zabbix-agent的機器)上,所使用到的工具如下:

iostat來源於syssat軟件包

#rpm -qa |grep sysstat

如果沒有安裝 請執行

#yum install sysstat -y


iostat 工具常用說明:

常用 $iostat -dxkt 1 輸出選項說明:
rrqm/s 每秒進行merge的讀操作數目。即delta(rmerge)/s
wrqm/s 每秒進行merge的寫操作數目。即delta(wmerge)/s

r/s 每秒完成的讀I/O設備次數。即delta(rio)/s
w/s 每秒完成的寫I/O設備次數。即delta(wio)/s
rsec/s 每秒讀扇區數。即delta(rsect)/s
wsec/s 每秒寫扇區數。即delta(wsect)/s
rkB/s 每秒讀K字節數。是rsect/s的一半,因為每扇區大小為512字節。(需要計算)
wkB/s 每秒寫K字節數。是wsect/s的一半。(需要計算)
avgrq-sz 平均每次設備I/O操作的數據大小(扇區)。delta(rsect+wsect)/delta(rio+wio)
avgqu-sz 平均I/O隊列長度。即delta(aveq)/s/1000(因為aveq的單位為毫秒)。
await 平均每次設備I/O操作的等待時間(毫秒)。即delta(ruse+wuse)/delta(rio+wio)
svctm 平均每次設備I/O操作的服務時間(毫秒)。即delta(use)/delta(rio+wio)
%util
一秒中有百分之多少的時間用於I/O操作,或者說一秒中有多少時間I/O隊列是非空的。即delta(use)/s/10
00(因為use的單位為毫秒)


二、利用iostat -dxkt 1 手動監控disk io

1、編寫發現磁盤的腳本 (shell):
$cd /etc/zabbix/bin
$cat disk_discovery.sh

#!/bin/bash
diskarray=(`cat /proc/diskstats |grep -E "\bsd[a-z]\b|\bxvd[a-z]\b|\bvd[a-z]\b"|awk ‘{print
$3}‘|sort|uniq 2>/dev/null`)
length=${#diskarray[@]}
printf "{\n"
printf ‘\t‘"\"data\":["
for ((i=0;i<$length;i++))
do
printf ‘\n\t\t{‘
printf "\"{#DISK_NAME}\":\"${diskarray[$i]}\"}"
if [ $i -lt $[$length-1] ];then
printf ‘,‘
fi
done
printf "\n\t]\n"
printf "}\n"
$ sh disk_discovery.sh
{
"data":[
{"{#DISK_NAME}":"xvda"},
{"{#DISK_NAME}":"xvdb"}
]
}

2、編寫獲取磁盤I/O信息的腳本
nohup /usr/bin/iostat -dxkt 1 > /tmp/iostat_output 2>/dev/null &
運行並放入/etc/rc.local 臨時解決方案,看後面添加 iostat sysv服務開機啟動並定期清除
iostat_output文件大小
3、編寫disk_status.sh腳本用於實時檢測磁盤io
$cd /etc/zabbix/bin
$vim disk_status.sh

#/bin/sh
device=$1
item=$2
case $item in
rrqm)
/usr/bin/tail -n20 /tmp/iostat_output |grep "\b$device\b"|tail -1|awk ‘{print $2}‘
;;
wrqm)
/usr/bin/tail -n20 /tmp/iostat_output |grep "\b$device\b"|tail -1|awk ‘{print $3}‘
;;
rps)
/usr/bin/tail -n20 /tmp/iostat_output |grep "\b$device\b"|tail -1|awk ‘{print $4}‘
;;
wps)
/usr/bin/tail -n20 /tmp/iostat_output |grep "\b$device\b" |tail -1|awk ‘{print
$5}‘
;;
rKBps)
/usr/bin/tail -n20 /tmp/iostat_output |grep "\b$device\b" |tail -1|awk ‘{print
$6}‘
;;
wKBps)
/usr/bin/tail -n20 /tmp/iostat_output |grep "\b$device\b" |tail -1|awk ‘{print
$7}‘
;;
avgrq-sz)
/usr/bin/tail -n20 /tmp/iostat_output |grep "\b$device\b" |tail -1|awk ‘{print
$8}‘
;;
avgqu-sz)
/usr/bin/tail -n20 /tmp/iostat_output |grep "\b$device\b" |tail -1|awk ‘{print
$9}‘
;;
await)
/usr/bin/tail -n20 /tmp/iostat_output |grep "\b$device\b" |tail -1|awk ‘{print
$10}‘
;;
svctm)
/usr/bin/tail -n20 /tmp/iostat_output |grep "\b$device\b" |tail -1|awk ‘{print
$11}‘
;;
util)
/usr/bin/tail -n20 /tmp/iostat_output |grep "\b$device\b" |tail -1|awk ‘{print
$12}‘
;;
esac

4、修改zabbix agent配置文件
cd /etc/zabbix/zabbix_agentd.d/
vim disk_status.conf

UserParameter=disk.discovery,/bin/bash /etc/zabbix/bin/disk_discovery.sh
UserParameter=disk.status[*],/bin/bash /ect/zabbix/bin/disk_status.sh $1 $2


重啟zabbix agent

#service zabbix-agent restart

5、測試(zabbix server上):
$sudo zabbix_get -s 10.168.118.61 -k "disk.discovery"
{
"data":[
{
"{#DISK_NAME}":"xvda"
},
{
"{#DISK_NAME}":"xvdb"
}
]
}
如果能獲得10.168.118.61上磁盤信息說明disk_discovery.sh腳本正確
$sudo zabbix_get -s 10.168.118.61 -k disk.status[xvda,wps]
10.00
如上能獲取xvda盤的wps值說明disk_status.sh腳本正常

三、到zabbix server前端web上添加磁盤IO監控模板
configuration(中文叫組態) --Templates(模板)--選擇一個你常用的模板(或先創建一個模塊) Applications(應用集) --Create application(創建應用)輸入名為 DISK_IO


Discovery -- Create discovery rule
Name:Disk_Discovery
Key:disk.discovery
Update interval(in sec):3600
Filters:{#DISK_NAME}
Name Key Interval
Item prototypes--Create item prototypes -- Write
requests merqed per second on {#DISK_NAME}
disk.status[{#DISK_NAME},wrqm] 60 90 120 Zabbix
agent DISK_IO Enabled
Write
requests issued per second to {#DISK_NAME} disk.status[{#DISK_NAME},wps] 60 90 120
Zabbix agent DISK_IO Enabled
Requests
average size(sectors) issued to {#DISK_NAME} disk.status[{#DISK_NAME},avgrq-sz] 60 90
120 Zabbix agent DISK_IO Enabled
Requests
average queue length issued to {#DISK_NAME} disk.status[{#DISK_NAME},avgqu-sz] 60 90
120 Zabbix agent DISK_IO Enabled
Read
requests merqed per second on{#DISK_NAME} disk.status[{#DISK_NAME},rrqm] 60 90 120
Zabbix agent DISK_IO Enabled
Read
requests issued per second to {#DISK_NAME} disk.status[{#DISK_NAME},rps] 60 90 120
Zabbix agent DISK_IO Enabled
I/O
requests average time(Miliseconds) issued to {#DISK_NAME} disk.status[{#DISK_NAME},await]
60 90 120 Zabbix agent DISK_IO Enabled
Bytes
written per second on {#DISK_NAME} disk.status[{#DISK_NAME},wKBps] 60 90 120 Zabbix
agent DISK_IO Enabled
Bytes
read per second on {#DISK_NAME} disk.status[{#DISK_NAME},rKBps] 60 90 120 Zabbix
agent DISK_IO Enabled
Bandwidth utilization for {#DISK_NAME} disk.status[{#DISK_NAME},util] 60 90 120 Zabbix
agent DISK_IO Enabled
在添加item的時候註意,由於使用的是iostat -k
獲取每秒的讀寫大小,所以iostat顯示的是以KB為單位,在zabbix上以B為基本單位,需要用到Use
custom multiplier 這個選項將zabbix agent返回的KB值乘以1024變成B,然後zabbix
agent再以B為單位顯示成B或KB或MB便於查看。
以上的監控不能算完美,缺陷在於iostat_output文件大小會不斷增長一般一個月差不多到1G,所以為了讓
它開機能自動啟動並能重置iostat_output文件大小大小,做成系統服務並添加到計劃任務中,指定時間重
啟,這樣就會自動重置iostat_output文件大小

四、添加iostate系統服務並添加到計劃任務
cat iostat

#!/bin/sh
# chkconfig: - 99 11
# description: iostat
# processname: iostatus
# Author: san 2015-12-29
. /etc/init.d/functions
iostat=${IOSTAT-/usr/bin/iostat}
prog=iostat
iostat_tmp=/tmp/iostat_output
pidfile=${PIDFILE-/var/run/iostat.pid}
lockfile=${LOCKFILE-/var/lock/subsys/iostat}
RETVAL=0
if [ ! -f "$iostat" ]
then
echo "iostat startup: command cannot found.cannot start."
exit 2
fi
start(){
if [ ! -f ${pidfile} ]; then
echo -n $"Starting $prog:"
$iostat -dxkt 1 > $iostat_tmp 2>/dev/null &
[ "$?" -eq 0 ] && success $"$base startup" || failure $"$base startup"
iostat_pid=$(ps aux |grep iostat|grep dxkt |grep -v grep|awk ‘{print $2}‘)
echo $iostat_pid >$pidfile
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch ${lockfile}
return $RETVAL
else
status -p ${pidfile}
exit 0
fi
}
stop(){
echo -n $"Stoping $prog: "
killproc -p ${pidfile} iostat
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
rm -rf $iostat_tmp
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p ${pidfile} $iostat
RETVAL=$?
;;
restart)
$0 stop
$0 start
;;
*)
echo "Usage: iostat { start | stop | restart | status } "
exit 1
esac

添加到系統服務和開機自啟

#chkconfig --add iostat

#chkconfig iostat on


#service iostat start
Starting iostat: [確定]
#service iostat status
iostat (pid 2810) 正在運行...
cat /tmp/iostat_output
-rw-r--r-- 1 root root 443M 1月 13 10:48 iostat_output
###運行13天 iostat_output文件大小
添加到計劃任務中 每月1號1時1分重啟iostat
$crontab -l
1 1 1 1 * service iostat restart
到此完美自動監控系統io


本文出自 “學無止境,學以致用” 博客,請務必保留此出處http://dyc2005.blog.51cto.com/270872/1943394

結合zabbix監控系統io相關性能服務