zabbix應用之Low-level discovery監控磁盤IO

zabbix自帶的「Template OS Linux」模板支持監控已掛載的磁盤空間利用率,是利用LLD(Low-level discovery)實現的,卻沒有對磁盤IO監控。本篇文章就介紹利用zabbix LLD功能監控磁盤IO的方法。 linux

    思路:首先建立Discovery rules,在agent端配置對應的UserParameter,調用編寫好的shell腳本,動態查找出已經掛載的磁盤分區;而後建立Item prototypes,使用vfs.dev.read[device,<type>,<mode>]和vfs.dev.write[device,<type>,<mode>]來監控磁盤分區的IO,包括磁盤讀寫速率、每秒讀寫扇區數、每秒讀寫操做數;而後建立Graph prototypes,生成磁盤IO圖表。 shell

    效果圖: json

wKiom1SrfI7Ck94QAAVYlQSogRw530.jpg

    磁盤讀寫速率: bash

wKioL1Srlk3jjHPpAALFZQoinzs867.jpg

    磁盤每秒讀寫扇區數: 測試

wKiom1SrlZzShsciAALhGqG-xEc215.jpg

    磁盤每秒讀寫操做數: ui

wKiom1SrlamSrDbtAAM4ADAbjhA423.jpg

    下面給出具體監控步驟: this

    1、編寫腳本查找已掛載的磁盤分區 spa

    1)在zabbix客戶端建立查找磁盤分區的腳本mount_disk_discovery.sh,放到/etc/zabbix/monitor_scripts目錄下,內容以下: prototype


#!/bin/bash
#Function: low-level discovery mounted disk
#Script_name: mount_disk_discovery.sh 
 
mount_disk_discovery()
{
    local regexp="\b(btrfs|ext2|ext3|ext4|jfs|reiser|xfs|ffs|ufs|jfs|jfs2|vxfs|hfs|ntfs|fat32|zfs)\b"
    local tmpfile="/tmp/mounts.tmp"
    :> "$tmpfile"
    # 過濾全部已掛載的文件系統
    egrep "$regexp" /proc/mounts > "$tmpfile"
    local num=$(cat "$tmpfile" | wc -l)
    printf '{\n'
    printf '\t"data":[ '
    while read line;do
        # 磁盤分區名稱
        DEV_NAME=$(echo $line | awk '{print $1}')
        # 文件系統名稱,即磁盤分區的掛載點
        FS_NAME=$(echo $line | awk '{print $2}')
        # blockdev命令獲取扇區大小,用於計算磁盤讀寫速率
        SEC_SIZE=$(sudo /sbin/blockdev --getss $DEV_NAME 2>/dev/null)
        printf '\n\t\t{'
        printf "\"{#DEV_NAME}\":\"${DEV_NAME}\","
        printf "\"{#FS_NAME}\":\"${FS_NAME}\","
        printf "\"{#SEC_SIZE}\":\"${SEC_SIZE}\"}"
        ((num--))
        [ "$num" == 0 ] && break
        printf ","
    done < "$tmpfile"
    printf '\n\t]\n'
    printf '}\n'
}
 
case "$1" in
    mount_disk_discovery)
        "$1"
        ;;
    *)
        echo "Bad Parameter."
        echo "Usage: $0 mount_disk_discovery"
        exit 1
        ;;
esac


    腳本首先過濾/proc/mounts中的磁盤類型,而後輸出包含磁盤名稱、文件系統名稱和扇區大小的Json數據。 rest

    2)設置相關文件權限:


touch /tmp/mounts.tmp
chown zabbix:zabbix /tmp/mounts.tmp
chown -R zabbix:zabbix /etc/zabbix/monitor_scripts
chmod 755 /etc/zabbix/monitor_scripts/mount_disk_discovery.sh


    3)測試一下:

1


[root@localhost ~]# /etc/zabbix/monitor_scripts/mount_disk_discovery.sh mount_disk_discovery
{
    "data":[ 
        {"{#DEV_NAME}":"/dev/sda3","{#FS_NAME}":"/","{#SEC_SIZE}":"512"},
        {"{#DEV_NAME}":"/dev/sda1","{#FS_NAME}":"/boot","{#SEC_SIZE}":"512"}
    ]
}

    上面的數據表示,磁盤分區/dev/sda3掛載到了根目錄,扇區大小爲512B;/dev/sda1掛載到了/boot目錄,扇區大小爲512B。


    2、配置zabbix客戶端

    1)編輯zabbix agent配置文件,設置Include目錄:

1

Include=/etc/zabbix/zabbix_agentd.d/

    agent啓動時會自動加載Include目錄下的配置文件。

    2)在/etc/zabbix/zabbix_agentd.d/目錄下創建disk_lld.conf,內容以下:

1

UserParameter=mount_disk_discovery,/bin/bash /etc/zabbix/monitor_scripts/mount_disk_discovery.sh mount_disk_discovery

    表示:創建用戶自定義參數mount_disk_discovery,當zabbix server向agent請求這個item key時,agent會調用/etc/zabbix/monitor_scripts/mount_disk_discovery.sh腳本,返回json數據。

    3)因爲zabbix agent的啓動用戶是zabbix,因此要受權zabbix用戶無密碼運行/sbin/blockdev命令。使用visudo命令,在/etc/sudoers裏註釋掉Defaults requiretty:

1

#Defaults requiretty

    並在最後面加上:

1

zabbix ALL=(ALL)       NOPASSWD: /sbin/blockdev

    這樣在腳本里執行相似「sudo /sbin/blockdev --getss /dev/sda1」這樣的指令就沒問題了。

    4)最後重啓zabbix agent

1

/etc/init.d/zabbix-agent restart

    5)測試一下:

    咱們從zabbix server上遠程get一下mount_disk_discovery

wKioL1SrmB2z2bO-AAEv3eBm9w0950.jpg

    如上圖DEV_NAME、FS_NAME、SEC_SIZE都有值,說明客戶端配置正確。


    3、配置zabbix前臺

    1)登陸zabbix前臺,選擇Configuration->Templates->Import,導入「Template Linux DiskIO」模板(見附件)。

    2)進入主機配置界面,連接「Template Linux DiskIO」模板

wKiom1SrlBeCKotjAAG5PXwjM6I328.jpg

    至此,所有配置結束了,耐心等待監控數據的出現吧~

    下面介紹下模板裏LLD的配置。

    3)模板的Discovery rule介紹

    Discovery rule的Key爲mount_disk_discovery,對應zabbix agent配置裏的:

1

UserParameter=mount_disk_discovery,/bin/bash /etc/zabbix/monitor_scripts/mount_disk_discovery.sh mount_disk_discovery

    每隔一段時間,server會向agent請求mount_disk_discovery,而後agent調用/etc/zabbix/monitor_scripts/mount_disk_discovery.sh腳本,返回json數據給server。

wKioL1SroEPxQs8GAAGhMcWqqF4073.jpg

    4)模板的Item prototypes介紹

wKiom1SrmFDDcxcEAAOermcg-TI132.jpg

    其中Sectors和Operations的監控項都是Zabbix agent類型的。經過閱讀zabbix C源碼,咱們會發現zabbix agent是根據/proc/diskstats文件計算每秒Sectors和Operations,默認取一分鐘內的平均值。有興趣的同窗能夠讀一讀src/libs/zbxsysinfo/linux/diskio.c。

    磁盤讀寫speed監控項是Calculated類型的,經過每秒讀寫扇區數乘以扇區大小計算而來的,見下圖:

wKioL1Srmo-woHCfAALFbRDOYjU046.jpg

    5)模板的Graph prototypes介紹

    模板對每一個磁盤分區都創建三個圖表,包括磁盤讀寫速率圖表、讀寫扇區數圖表、讀寫操做數圖表。

wKioL1SrmPzSlBOfAAIxIlfBAzQ692.jpg



    附:客戶端一鍵配置腳本

#!/bin/sh
#
# Filename:    autoMonitorDiskIO.sh
# Date:        2014/01/05
# Author:      Qicheng
# Website:     http://qicheng0211.blog.51cto.com/
# Description: 部署zabbix low-level discovery 監控磁盤IO
# Notes: 在被監控客戶端運行此腳本,前提條件已經安裝好zabbix agent
#
 
ROOT_UID=0
if [ "$UID" -ne "$ROOT_UID" ];then
    echo "Error: Please run this script as root user."
    exit 1
fi
 
# 自行修改成你的zabbix agent配置文件路徑
AGENT_CONF="/data/zabbix/etc/zabbix_agentd.conf"
ZABBIX_DIR="/data/zabbix" 
mkdir -p ${ZABBIX_DIR}/monitor_scripts
# 建立 low-level discovery mounted disk 腳本
cat > ${ZABBIX_DIR}/monitor_scripts/mount_disk_discovery.sh << 'EOF'
#!/bin/bash
#Function: low-level discovery mounted disk
#Script_name: mount_disk_discovery.sh 


mount_disk_discovery()
{
    local regexp="\b(btrfs|ext2|ext3|ext4|jfs|reiser|xfs|ffs|ufs|jfs|jfs2|vxfs|hfs|ntfs|fat32|zfs)\b"
    local tmpfile="/tmp/mounts.tmp"
    :> "$tmpfile"
    egrep "$regexp" /proc/mounts > "$tmpfile"
    local num=$(cat "$tmpfile" | wc -l)
    printf '{\n'
    printf '\t"data":[ '
    while read line;do
        DEV_NAME=$(echo $line | awk '{print $1}')
        FS_NAME=$(echo $line | awk '{print $2}')
        SEC_SIZE=$(sudo /sbin/blockdev --getss $DEV_NAME 2>/dev/null)
        printf '\n\t\t{'
        printf "\"{#DEV_NAME}\":\"${DEV_NAME}\","
        printf "\"{#FS_NAME}\":\"${FS_NAME}\","
        printf "\"{#SEC_SIZE}\":\"${SEC_SIZE}\"}"
        ((num--))
        [ "$num" == 0 ] && break
        printf ","
    done < "$tmpfile"
    printf '\n\t]\n'
    printf '}\n'
}


case "$1" in
    mount_disk_discovery)
        "$1"
        ;;
    *)
        echo "Bad Parameter."
        echo "Usage: $0 mount_disk_discovery"
        exit 1
        ;;
esac
EOF
touch /tmp/mounts.tmp
chown zabbix:zabbix /tmp/mounts.tmp
chown -R zabbix:zabbix ${ZABBIX_DIR}/monitor_scripts
chmod 755 ${ZABBIX_DIR}/monitor_scripts/mount_disk_discovery.sh
 
# 判斷配置文件是否存在
[ -f "${AGENT_CONF}" ] || { echo "ERROR: File ${AGENT_CONF} does not exist.";exit 1;}
 
include=`grep '^Include' ${AGENT_CONF} | cut -d'=' -f2`
# 在配置文件中添加自定義參數
if [ -d "$include" ];then
    echo "UserParameter=mount_disk_discovery,/bin/bash ${ZABBIX_DIR}/monitor_scripts/mount_disk_discovery.sh mount_disk_discovery" >> ${AGENT_CONF} >> $include/disk_lld.conf 
else
    grep -q '^UserParameter=mount_disk_discovery' ${AGENT_CONF} || echo "UserParameter=mount_disk_discovery,/bin/bash ${ZABBIX_DIR}/monitor_scripts/mount_disk_discovery.sh mount_disk_discovery" >> ${AGENT_CONF} 
fi


# 受權zabbix用戶無密碼運行/sbin/blockdev命令
chmod +w /etc/sudoers 
sed -i '/^Defaults\s\+requiretty/s/^/#/' /etc/sudoers
grep -q '^zabbix ALL=(ALL).*blockdev' /etc/sudoers || echo 'zabbix ALL=(ALL)       NOPASSWD: /sbin/blockdev' >> /etc/sudoers
chmod 440 /etc/sudoers


# 重啓agent服務
[ -f '/etc/init.d/zabbix_agentd' ] && /etc/init.d/zabbix_agentd restart || echo "需手動重啓zabbix agent服務."
相關文章
相關標籤/搜索