Centos7下通过Zabbix自动发现并监控硬盘

来源:互联网 发布:极品飞车ol车辆数据 编辑:程序博客网 时间:2024/05/21 09:01

操作环境

Centos 7.2

zabbix 3.2

zabbix server :10.10.200.193

zabbix agent :10.10.200.227

操作步骤

之前已经讲过如何在centos7下搭建zabbix监控环境《Centos 7下搭建Zabbix监控软件》,可以参照该文件中步骤将10.10.200.227添加至zabbix server中。

1.在zabbix agent获取硬盘信息,可通过下述脚本获取信息disk_scan.sh
#!/bin/bashdiskarray=(`cat /proc/diskstats |grep -E "\bsd[abcdefg]\b|\bvd[abcdefg]\b"|grep -i "\b$1\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 "\"{#DISKNAME}\":\"${diskarray[$i]}\"}"         if [ $i -lt $[$length-1] ];then                 printf ','         fidoneprintf  "\n\t]\n"printf "}\n"

在zabbix agent端通过zabbix用户运行该脚本结果如下:

[root@ovirt-host-227 ~]# su -s /bin/bash zabbixbash-4.2$ /etc/zabbix/zabbix_agentd.d/disk_scan.sh {        "data":[                {"{#DISKNAME}":"sda"}        ]}

2.在zabbix agent通过iostat获取硬盘性能信息,并将该信息写入文本中

#nohup iostat -m -x -d 30 >> /tmp/iostat_output

为了以后方便使用,可将该命令写入到自动启动中。

我们查看下iostat的性能参数

[root@ovirt-host-227 ~]# iostat -mx -d 30Linux 3.10.0-327.36.2.el7.x86_64 (ovirt-host-227)       11/30/2016      _x86_64_        (16 CPU)Device:         rrqm/s   wrqm/s     r/s     w/s    rMB/s    wMB/s avgrq-sz avgqu-sz   await r_await w_await  svctm  %utilsda               0.00     0.17    1.80    6.99     0.06     0.06    27.62     0.06    7.12    5.38    7.57   4.39   3.85dm-0              0.00     0.00    0.01    0.00     0.00     0.00    16.30     0.00   24.89   24.89    0.00  24.13   0.01dm-1              0.00     0.00    0.20    0.14     0.01     0.00    90.54     0.01   17.26   23.12    9.06   5.32   0.18dm-2              0.00     0.00    1.57    6.32     0.04     0.06    26.50     0.08   10.11    2.90   11.90   4.68   3.69

关于iostat的这些参数解释,可以参考man iostat,我们在监控时,选择监控所有的性能参数

       Device Utilization Report              The second report generated by the iostat command is the Device Utilization Report. The device report provides statistics on a per physical device or partition basis. Block devices and partitions for which statistics  are  to  be  displayed  may  be              entered on the command line.  If no device nor partition is entered, then statistics are displayed for every device used by the system, and providing that the kernel maintains statistics for it.  If the ALL keyword is given on the command line, then              statistics are displayed for every device defined by the system, including those that have never been used.  Transfer rates are shown in 1K blocks by default, unless the environment variable POSIXLY_CORRECT is set, in which case 512-byte blocks  are              used.  The report may show the following fields, depending on the flags used:              Device:                     This column gives the device (or partition) name as listed in the /dev directory.              tps                     Indicate the number of transfers per second that were issued to the device. A transfer is an I/O request to the device. Multiple logical requests can be combined into a single I/O request to the device. A transfer is of indeterminate size.              Blk_read/s (kB_read/s, MB_read/s)                     Indicate the amount of data read from the device expressed in a number of blocks (kilobytes, megabytes) per second. Blocks are equivalent to sectors and therefore have a size of 512 bytes.              Blk_wrtn/s (kB_wrtn/s, MB_wrtn/s)                     Indicate the amount of data written to the device expressed in a number of blocks (kilobytes, megabytes) per second.              Blk_read (kB_read, MB_read)                     The total number of blocks (kilobytes, megabytes) read.              Blk_wrtn (kB_wrtn, MB_wrtn)                     The total number of blocks (kilobytes, megabytes) written.              rrqm/s                     The number of read requests merged per second that were queued to the device.              wrqm/s                     The number of write requests merged per second that were queued to the device.              r/s                     The number (after merges) of read requests completed per second for the device.              w/s                     The number (after merges) of write requests completed per second for the device.              rsec/s (rkB/s, rMB/s)                     The number of sectors (kilobytes, megabytes) read from the device per second.              wsec/s (wkB/s, wMB/s)                     The number of sectors (kilobytes, megabytes) written to the device per second.              avgrq-sz                     The average size (in sectors) of the requests that were issued to the device.              avgqu-sz                     The average queue length of the requests that were issued to the device.              await                     The average time (in milliseconds) for I/O requests issued to the device to be served. This includes the time spent by the requests in queue and the time spent servicing them.              r_await                     The average time (in milliseconds) for read requests issued to the device to be served. This includes the time spent by the requests in queue and the time spent servicing them.              w_await                     The average time (in milliseconds) for write requests issued to the device to be served. This includes the time spent by the requests in queue and the time spent servicing them.              svctm                     The average service time (in milliseconds) for I/O requests that were issued to the device. Warning! Do not trust this field any more.  This field will be removed in a future sysstat version.              %util                     Percentage of elapsed time during which I/O requests were issued to the device (bandwidth utilization for the device). Device saturation occurs when this value is close to 100%.

3.在了解硬盘的基本信息以及性能信息后,我们来设置zabbix-agent,添加下述内容至zabbix agent配置文件中

UserParameter=io.scandisk[*],/etc/zabbix/zabbix_agentd.d/disk_scan.sh $1UserParameter=io.rps[*],/usr/bin/tail /tmp/iostat_output |grep "\b$1\b"|tail -1|awk '{print $$4}'UserParameter=io.wps[*],/usr/bin/tail /tmp/iostat_output |grep "\b$1\b" |tail -1|awk '{print $$5}'UserParameter=io.rMBps[*],/usr/bin/tail /tmp/iostat_output |grep "\b$1\b" |tail -1|awk '{print $$6}'UserParameter=io.wMBps[*],/usr/bin/tail /tmp/iostat_output |grep "\b$1\b" |tail -1|awk '{print $$7}'UserParameter=io.avgrq-sz[*],/usr/bin/tail /tmp/iostat_output |grep "\b$1\b" |tail -1|awk '{print $$8}'UserParameter=io.avgqu-sz[*],/usr/bin/tail /tmp/iostat_output |grep "\b$1\b" |tail -1|awk '{print $$9}'UserParameter=io.await[*],/usr/bin/tail /tmp/iostat_output |grep "\b$1\b" |tail -1|awk '{print $$10}'UserParameter=io.r_await[*],/usr/bin/tail /tmp/iostat_output |grep "\b$1\b" |tail -1|awk '{print $$11}'UserParameter=io.w_await[*],/usr/bin/tail /tmp/iostat_output |grep "\b$1\b" |tail -1|awk '{print $$12}'UserParameter=io.svctm[*],/usr/bin/tail /tmp/iostat_output |grep "\b$1\b" |tail -1|awk '{print $$13}'UserParameter=io.util[*],/usr/bin/tail /tmp/iostat_output |grep "\b$1\b" |tail -1|awk '{print $$14}'

重启zabbix-agent

[root@ovirt-host-227 ~]# systemctl restart zabbix-agent

以上就完成了zabbix-agent端的设置,下面我们在zabbix server通过zabbix_get来进行测试

[root@centos7-zabbix-193 ~]# su -s /bin/bash zabbixbash-4.2$ zabbix_get -s 10.10.200.227 -p 10050 -k io.scandisk{        "data":[                {"{#DISKNAME}":"sda"}        ]}bash-4.2$ zabbix_get -s 10.10.200.227 -p 10050 -k io.util[sda]                                                                                                                                                                                                               4.05bash-4.2$ zabbix_get -s 10.10.200.227 -p 10050 -k io.rps[sda]                                                                                                                                                                                                                0.00bash-4.2$ zabbix_get -s 10.10.200.227 -p 10050 -k io.wps[sda]                                                                                                                                                                                                                6.77

通过以上zabbix_get的测试,可以确定zabbix-agent与zabbix-server之前获取数值没有问题,下面我们在zabbix web console来创建Template



创建discovery rule,注意在key中填写io.scandisk为zabbix-agent中设置的参数


创建item prototype,注意这里是在discovery rule下面的item prototype



下面我们来创建item prototype,以io.util为例,注意“#DISKNAME”参数,为zabbix-agent的disk_scan.sh脚本中的参数,io.util为zabbix-agent配置文件中的参数,在这里设置

数据类型为Numeric(float)


创建完成所有item prototype,如下图所示



我们将该template添加至zabbix-agent 10.10.200.227中,在添加完成以后,我们查看10.10.200.227的item列表中,可以查看到自动发现的硬盘以及上面所创建的监控参数



我们创建graphs来显示这些内容

显示硬盘各种利用率


创建完成后点击preview可以查看该graph


依次内推,可以创建其他的硬盘性能图




以上就完成了zabbix监控硬盘的所有配置。

下面内容为通过上述创建template所导出的xml文件内容,可直接导入至zabbix-server中进行使用

<?xml version="1.0" encoding="UTF-8"?><zabbix_export>    <version>2.0</version>    <date>2016-11-30T07:01:24Z</date>    <groups>        <group>            <name>Linux servers</name>        </group>    </groups>    <templates>        <template>            <template>linux disk io monitor</template>            <name>linux disk io monitor</name>            <groups>                <group>                    <name>Linux servers</name>                </group>            </groups>            <applications>                <application>                    <name>iostat</name>                </application>            </applications>            <items/>            <discovery_rules>                <discovery_rule>                    <name>disk discovery</name>                    <type>0</type>                    <snmp_community/>                    <snmp_oid/>                    <key>io.scandisk</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>:</filter>                    <lifetime>30</lifetime>                    <description/>                    <item_prototypes>                        <item_prototype>                            <name>io avgqu-sz on $1</name>                            <type>0</type>                            <snmp_community/>                            <multiplier>0</multiplier>                            <snmp_oid/>                            <key>io.avgqu-sz[{#DISKNAME}]</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>iostat</name>                                </application>                            </applications>                            <valuemap/>                            <logtimefmt/>                        </item_prototype>                        <item_prototype>                            <name>io avgrq-sz on $1</name>                            <type>0</type>                            <snmp_community/>                            <multiplier>0</multiplier>                            <snmp_oid/>                            <key>io.avgrq-sz[{#DISKNAME}]</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>iostat</name>                                </application>                            </applications>                            <valuemap/>                            <logtimefmt/>                        </item_prototype>                        <item_prototype>                            <name>io await on $1</name>                            <type>0</type>                            <snmp_community/>                            <multiplier>0</multiplier>                            <snmp_oid/>                            <key>io.await[{#DISKNAME}]</key>                            <delay>30</delay>                            <history>90</history>                            <trends>365</trends>                            <status>0</status>                            <value_type>0</value_type>                            <allowed_hosts/>                            <units>%</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>iostat</name>                                </application>                            </applications>                            <valuemap/>                            <logtimefmt/>                        </item_prototype>                        <item_prototype>                            <name>io rMBps on $1</name>                            <type>0</type>                            <snmp_community/>                            <multiplier>0</multiplier>                            <snmp_oid/>                            <key>io.rMBps[{#DISKNAME}]</key>                            <delay>30</delay>                            <history>90</history>                            <trends>365</trends>                            <status>0</status>                            <value_type>0</value_type>                            <allowed_hosts/>                            <units>MB</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>iostat</name>                                </application>                            </applications>                            <valuemap/>                            <logtimefmt/>                        </item_prototype>                        <item_prototype>                            <name>io rps on $1</name>                            <type>0</type>                            <snmp_community/>                            <multiplier>0</multiplier>                            <snmp_oid/>                            <key>io.rps[{#DISKNAME}]</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>iostat</name>                                </application>                            </applications>                            <valuemap/>                            <logtimefmt/>                        </item_prototype>                        <item_prototype>                            <name>io svctm on$1</name>                            <type>0</type>                            <snmp_community/>                            <multiplier>0</multiplier>                            <snmp_oid/>                            <key>io.svctm[{#DISKNAME}]</key>                            <delay>30</delay>                            <history>90</history>                            <trends>365</trends>                            <status>0</status>                            <value_type>0</value_type>                            <allowed_hosts/>                            <units>%</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>iostat</name>                                </application>                            </applications>                            <valuemap/>                            <logtimefmt/>                        </item_prototype>                        <item_prototype>                            <name>io utils on $1</name>                            <type>0</type>                            <snmp_community/>                            <multiplier>0</multiplier>                            <snmp_oid/>                            <key>io.util[{#DISKNAME}]</key>                            <delay>30</delay>                            <history>90</history>                            <trends>365</trends>                            <status>0</status>                            <value_type>0</value_type>                            <allowed_hosts/>                            <units>%</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>iostat</name>                                </application>                            </applications>                            <valuemap/>                            <logtimefmt/>                        </item_prototype>                        <item_prototype>                            <name>io wMBps on $1</name>                            <type>0</type>                            <snmp_community/>                            <multiplier>0</multiplier>                            <snmp_oid/>                            <key>io.wMBps[{#DISKNAME}]</key>                            <delay>30</delay>                            <history>90</history>                            <trends>365</trends>                            <status>0</status>                            <value_type>0</value_type>                            <allowed_hosts/>                            <units>MB</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>iostat</name>                                </application>                            </applications>                            <valuemap/>                            <logtimefmt/>                        </item_prototype>                        <item_prototype>                            <name>io wps on $1</name>                            <type>0</type>                            <snmp_community/>                            <multiplier>0</multiplier>                            <snmp_oid/>                            <key>io.wps[{#DISKNAME}]</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>iostat</name>                                </application>                            </applications>                            <valuemap/>                            <logtimefmt/>                        </item_prototype>                    </item_prototypes>                    <trigger_prototypes/>                    <graph_prototypes>                        <graph_prototype>                            <name>io util on {#DISKNAME}</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>C80000</color>                                    <yaxisside>0</yaxisside>                                    <calc_fnc>2</calc_fnc>                                    <type>0</type>                                    <item>                                        <host>linux disk io monitor</host>                                        <key>io.util[{#DISKNAME}]</key>                                    </item>                                </graph_item>                            </graph_items>                        </graph_prototype>                    </graph_prototypes>                    <host_prototypes/>                </discovery_rule>            </discovery_rules>            <macros/>            <templates/>            <screens/>        </template>    </templates></zabbix_export>



0 0