zabbix 监控磁盘IO

搜索了一些博文发现有两种方式 , 一种是通过 iostat 获取需要的信息 ; 此外第二种是 通过 /proc/diskstatus 获取需要的信息

本来是想通过第二种方式获取监控信息, 发现tps的计算方式有点复杂. 最后结果是使用两种混合的方式.

监控脚本

/usr/bin/python

-- coding:utf-8 --

This script is used to discovery disk on the server

python 2.6

import subprocess
import sys
import json

def discovery_disk():
args = "cat /proc/diskstats |grep -E '\ssd[a-z]\s|\sxvd[a-z]\s|\svd[a-z]\s'|awk '{print $3}'|sort|uniq 2>/dev/null"
t = subprocess.Popen(args, shell=True, stdout=subprocess.PIPE).communicate()[0].decode()
disks = []

for disk in t.split('\n'):
    if len(disk) != 0:
        disks.append({'{#DISK_NAME}': disk})

print(json.dumps({'data': disks}, indent=4, separators=(',', ':')))

def collector(command): #转换返回结果unicode为str
result = { 'message': ''}
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

while p.poll() is None:
    line = p.stdout.readline()
    line = line.strip()
    if line:
        # result['message'] += '{}\n'.format(line.encode('utf-8'))
        result['message'] = line.decode('utf-8')

return result['message'].strip()

def disk_status(disk,status):
if status == 'read_ops':
args = "cat /proc/diskstats | grep %s | head -1 | awk '{print $4}'" % (disk)
ReadOps = collector(args)
print(ReadOps)
elif status == 'write_ops':
args = "cat /proc/diskstats | grep %s | head -1 | awk '{print $8}'" % (disk)
WriteOps = collector(args)
print(WriteOps)
elif status == 'io_actice':
args = "cat /proc/diskstats | grep %s | head -1 | awk '{print $12}'" % (disk)
IoActice = collector(args)
print(IoActice)
elif status == 'read_sectors':
args = "cat /proc/diskstats | grep %s | head -1 | awk '{print $6}'" % (disk)
ReadSectors = collector(args)
print(ReadSectors)
elif status == 'write_sectors':
args = "cat /proc/diskstats | grep %s | head -1 | awk '{print $10}'" % (disk)
WriteSectors = collector(args)
print(WriteSectors)
else:
args = "iostat -d | grep %s | awk '{print $2}'" %(disk)
Tps = collector(args)
print(Tps)

if len(sys.argv) == 1 :
discovery_disk()
else:
args = sys.argv[1]
DiskName,Status = args.split('.') #将字符串格式化为list,同时此处zabbix定义键值Disk.Status[{#DISK_NAME}.read_ops]
disk_status(DiskName,Status)

zabbix_agentd增加配置文件

UserParameter=Disk.Status[*],/usr/bin/python /usr/local/zabbix/scripts/discovery_disk.py $1
UserParameter=Disk.discovery,/usr/bin/python /usr/local/zabbix/scripts/discovery_disk.py

template



3.2
2018-06-29T03:26:15Z


Templates