zabbix监控磁盘IO

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了zabbix监控磁盘IO相关的知识,希望对你有一定的参考价值。

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(‘
‘):
    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‘] += ‘{}
‘.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

<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export>
<version>3.2</version>
<date>2018-06-29T03:26:15Z</date>
<groups>
<group>
<name>Templates</name>
</group>
</groups>
<templates>
<template>
<template>Template Disk IO</template>
<name>Template Disk IO</name>
<description/>
<groups>
<group>
<name>Templates</name>
</group>
</groups>
<applications>
<application>
<name>Disk IO</name>
</application>
</applications>
<items/>
<discovery_rules>
<discovery_rule>
<name>discovery disk</name>
<type>0</type>
<snmp_community/>
<snmp_oid/>
<key>Disk.discovery</key>
<delay>30</delay>
<status>0</status>
<allowed_hosts/>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<delay_flex/>
<params/>
<ipmi_sensor/>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<filter>
<evaltype>0</evaltype>
<formula/>
<conditions/>
</filter>
<lifetime>30</lifetime>
<description/>
<item_prototypes>
<item_prototype>
<name>平均IO队列长度 $1</name>
<type>0</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>Disk.Status[{#DISK_NAME}.io_actice]</key>
<delay>30</delay>
<history>90</history>
<trends>365</trends>
<status>0</status>
<value_type>3</value_type>
<allowed_hosts/>
<units/>
<delta>0</delta>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description/>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>Disk IO</name>
</application>
</applications>
<valuemap/>
<logtimefmt/>
<application_prototypes/>
</item_prototype>
<item_prototype>
<name>每秒读IO设备的次数 $1</name>
<type>0</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>Disk.Status[{#DISK_NAME}.read_ops]</key>
<delay>30</delay>
<history>90</history>
<trends>365</trends>
<status>0</status>
<value_type>3</value_type>
<allowed_hosts/>
<units>ops/second</units>
<delta>1</delta>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description/>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>Disk IO</name>
</application>
</applications>
<valuemap/>
<logtimefmt/>
<application_prototypes/>
</item_prototype>
<item_prototype>
<name>每秒读的字节数 $1</name>
<type>0</type>
<snmp_community/>
<multiplier>1</multiplier>
<snmp_oid/>
<key>Disk.Status[{#DISK_NAME}.read_sectors]</key>
<delay>30</delay>
<history>90</history>
<trends>365</trends>
<status>0</status>
<value_type>3</value_type>
<allowed_hosts/>
<units>B/sec</units>
<delta>1</delta>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<formula>512</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description/>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>Disk IO</name>
</application>
</applications>
<valuemap/>
<logtimefmt/>
<application_prototypes/>
</item_prototype>
<item_prototype>
<name>每秒进程下发IO并发 Tps $1</name>
<type>0</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>Disk.Status[{#DISK_NAME}.tps]</key>
<delay>30</delay>
<history>90</history>
<trends>365</trends>
<status>0</status>
<value_type>0</value_type>
<allowed_hosts/>
<units/>
<delta>0</delta>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description/>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>Disk IO</name>
</application>
</applications>
<valuemap/>
<logtimefmt/>
<application_prototypes/>
</item_prototype>
<item_prototype>
<name>每秒写IO设备的次数 $1</name>
<type>0</type>
<snmp_community/>
<multiplier>0</multiplier>
<snmp_oid/>
<key>Disk.Status[{#DISK_NAME}.write_ops]</key>
<delay>30</delay>
<history>90</history>
<trends>365</trends>
<status>0</status>
<value_type>3</value_type>
<allowed_hosts/>
<units>ops/second</units>
<delta>1</delta>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<formula>1</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description/>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>Disk IO</name>
</application>
</applications>
<valuemap/>
<logtimefmt/>
<application_prototypes/>
</item_prototype>
<item_prototype>
<name>每秒写的字节数 $1</name>
<type>0</type>
<snmp_community/>
<multiplier>1</multiplier>
<snmp_oid/>
<key>Disk.Status[{#DISK_NAME}.write_sectors]</key>
<delay>30</delay>
<history>90</history>
<trends>365</trends>
<status>0</status>
<value_type>3</value_type>
<allowed_hosts/>
<units>B/sec</units>
<delta>1</delta>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<formula>512</formula>
<delay_flex/>
<params/>
<ipmi_sensor/>
<data_type>0</data_type>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description/>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>Disk IO</name>
</application>
</applications>
<valuemap/>
<logtimefmt/>
<application_prototypes/>
</item_prototype>
</item_prototypes>
<trigger_prototypes/>
<graph_prototypes>
<graph_prototype>
<name>平均IO队列长度 {#DISK_NAME}.io_actice</name>
<width>900</width>
<height>200</height>
<yaxismin>0.0000</yaxismin>
<yaxismax>100.0000</yaxismax>
<show_work_period>1</show_work_period>
<show_triggers>1</show_triggers>
<type>0</type>
<show_legend>1</show_legend>
<show_3d>0</show_3d>
<percent_left>0.0000</percent_left>
<percent_right>0.0000</percent_right>
<ymin_type_1>0</ymin_type_1>
<ymax_type_1>0</ymax_type_1>
<ymin_item_1>0</ymin_item_1>
<ymax_item_1>0</ymax_item_1>
<graph_items>
<graph_item>
<sortorder>0</sortorder>
<drawtype>0</drawtype>
<color>1A7C11</color>
<yaxisside>0</yaxisside>
<calc_fnc>2</calc_fnc>
<type>0</type>
<item>
<host>Template Disk IO</host>
<key>Disk.Status[{#DISK_NAME}.io_actice]</key>
</item>
</graph_item>
</graph_items>
</graph_prototype>
<graph_prototype>
<name>每秒IO设备的读写次数 {#DISK_NAME}</name>
<width>900</width>
<height>200</height>
<yaxismin>0.0000</yaxismin>
<yaxismax>100.0000</yaxismax>
<show_work_period>1</show_work_period>
<show_triggers>1</show_triggers>
<type>0</type>
<show_legend>1</show_legend>
<show_3d>0</show_3d>
<percent_left>0.0000</percent_left>
<percent_right>0.0000</percent_right>
<ymin_type_1>0</ymin_type_1>
<ymax_type_1>0</ymax_type_1>
<ymin_item_1>0</ymin_item_1>
<ymax_item_1>0</ymax_item_1>
<graph_items>
<graph_item>
<sortorder>0</sortorder>
<drawtype>0</drawtype>
<color>1A7C11</color>
<yaxisside>0</yaxisside>
<calc_fnc>2</calc_fnc>
<type>0</type>
<item>
<host>Template Disk IO</host>
<key>Disk.Status[{#DISK_NAME}.write_ops]</key>
</item>
</graph_item>
<graph_item>
<sortorder>1</sortorder>
<drawtype>0</drawtype>
<color>F63100</color>
<yaxisside>0</yaxisside>
<calc_fnc>2</calc_fnc>
<type>0</type>
<item>
<host>Template Disk IO</host>
<key>Disk.Status[{#DISK_NAME}.read_ops]</key>
</item>
</graph_item>
</graph_items>
</graph_prototype>
<graph_prototype>
<name>每秒进程下发IO并发 Tps {#DISK_NAME}</name>
<width>900</width>
<height>200</height>
<yaxismin>0.0000</yaxismin>
<yaxismax>100.0000</yaxismax>
<show_work_period>1</show_work_period>
<show_triggers>1</show_triggers>
<type>0</type>
<show_legend>1</show_legend>
<show_3d>0</show_3d>
<percent_left>0.0000</percent_left>
<percent_right>0.0000</percent_right>
<ymin_type_1>0</ymin_type_1>
<ymax_type_1>0</ymax_type_1>
<ymin_item_1>0</ymin_item_1>
<ymax_item_1>0</ymax_item_1>
<graph_items>
<graph_item>
<sortorder>0</sortorder>
<drawtype>0</drawtype>
<color>1A7C11</color>
<yaxisside>0</yaxisside>
<calc_fnc>2</calc_fnc>
<type>0</type>
<item>
<host>Template Disk IO</host>
<key>Disk.Status[{#DISK_NAME}.tps]</key>
</item>
</graph_item>
</graph_items>
</graph_prototype>
<graph_prototype>
<name>读写流量 {#DISK_NAME}</name>
<width>900</width>
<height>200</height>
<yaxismin>0.0000</yaxismin>
<yaxismax>100.0000</yaxismax>
<show_work_period>1</show_work_period>
<show_triggers>1</show_triggers>
<type>0</type>
<show_legend>1</show_legend>
<show_3d>0</show_3d>
<percent_left>0.0000</percent_left>
<percent_right>0.0000</percent_right>
<ymin_type_1>0</ymin_type_1>
<ymax_type_1>0</ymax_type_1>
<ymin_item_1>0</ymin_item_1>
<ymax_item_1>0</ymax_item_1>
<graph_items>
<graph_item>
<sortorder>0</sortorder>
<drawtype>0</drawtype>
<color>1A7C11</color>
<yaxisside>0</yaxisside>
<calc_fnc>2</calc_fnc>
<type>0</type>
<item>
<host>Template Disk IO</host>
<key>Disk.Status[{#DISK_NAME}.write_sectors]</key>
</item>
</graph_item>
<graph_item>
<sortorder>1</sortorder>
<drawtype>0</drawtype>
<color>F63100</color>
<yaxisside>0</yaxisside>
<calc_fnc>2</calc_fnc>
<type>0</type>
<item>
<host>Template Disk IO</host>
<key>Disk.Status[{#DISK_NAME}.read_sectors]</key>
</item>
</graph_item>
</graph_items>
</graph_prototype>
</graph_prototypes>
<host_prototypes/>
</discovery_rule>
</discovery_rules>
<httptests/>
<macros/>
<templates/>
<screens/>
</template>
</templates>
</zabbix_export>

以上是关于zabbix监控磁盘IO的主要内容,如果未能解决你的问题,请参考以下文章

zabbix监控磁盘io

zabbix监控磁盘io

zabbix监控磁盘io

zabbix自动发现规则之磁盘IO监控

zabbix监控磁盘IO

ZABBIX之磁盘IO监控