diff --git a/Operating_Systems/Linux/template_ZBX_disk_stat/5.4/README.md b/Operating_Systems/Linux/template_ZBX_disk_stat/5.4/README.md new file mode 100644 index 000000000..c643ed227 --- /dev/null +++ b/Operating_Systems/Linux/template_ZBX_disk_stat/5.4/README.md @@ -0,0 +1,85 @@ +# ZBX_Disk_Stat +Send statistic from /proc/diskstats to Zabbix + +This is small project for my Home server + +# Source +[github: dusharu/ZBX_Disk_Stat](https://github.com/dusharu/ZBX_Disk_Stat) + + +# Features + * AutoDiscovery BlockDev and Filter by [LLD Override](https://www.zabbix.com/documentation/current/manual/discovery/low_level_discovery#override) + * Collect Read\Write Operations per second + * Collect Read\Write Sectors per second + * Calculate Time for 1 Read\Write Operations + * Trigger when IO_Time >30ms at 10 times per 20min + * Collect IO Queue + * Convert DM(Device Mapper) device to pretty Name + * Get stat for `dm-1` but print for user `vg00-lv_root` + * After reboot `dm-1` may change to `dm-2`, but stat will be correct + +# Requires + +# Install + 1. Add script to server + +``` +# Create dir for zabbix sript +mkdir -p /usr/libexec/zabbix/ + +# Copy script to your server +curl 'https://raw.githubusercontent.com/dusharu/ZBX_Disk_Stat/master/scripts/disk_stat.sh' -o /usr/libexec/zabbix/disk_stat.sh +chmod 755 /usr/libexec/zabbix/disk_stat.sh +``` + 2. [Import](https://www.zabbix.com/documentation/current/ru/manual/xml_export_import) [Template_ZBX/ZBX_Disk_Stat.xml](ZBX_Disk_Stat.xml) + 3. Check [LLD Override](https://www.zabbix.com/documentation/current/manual/discovery/low_level_discovery#override). Default value: + * ^$ - result FALSE - device was remove + * .*snapshot.* - result FALSE - filter LVM snapshot + * -real$ - result FALSE - [filter LVM snapshot](https://rwmj.wordpress.com/2010/09/28/how-lvm-does-snapshots/) + * -cow$ - result FALSE - [filter LVM snapshot](https://rwmj.wordpress.com/2010/09/28/how-lvm-does-snapshots/) + * ^xvd[a-z]*[0-9]+$ - result FALSE - filter partitions + * ^[hs]d[a-z]*[0-9]+$ - result FALSE - filter partitions + * ^loop[0-9]*$ - result FALSE - filter loop device + * ^sr[0-9]*$ - result FALSE - filter CD-ROM + 4. Add host to group "Disk_Stat: io_block_dev" + 5. Wait before Zabbix Discovery and Get Some Data + + +# Screenshoot +Graph create by [Grafana](https://grafana.com/) and [Zabbix plugin](https://grafana.com/grafana/plugins/alexanderzobnin-zabbix-app) +![1-Disk_Stat_IOPS.png](files/1-Disk_Stat_IOPS.png) +![2-Disk_Stat_RW_Sectors.png](files/2-Disk_Stat_RW_Sectors.png) +![3-Disk_Stat_IO_time.png](files/3-Disk_Stat_IO_time.png) +![4-Disk_Stat_IO_Queue.png](files/4-Disk_Stat_IO_Queue.png) + +Graph create by Zabbix +![10-Disk_Stat_IOPS_zbx.png](files/10-Disk_Stat_IOPS_zbx.png) +![11-Disk_Stat_IO_Queue_zbx.png](files/11-Disk_Stat_IO_Queue_zbx.png) +![12-Disk_Stat_RW_Sectors_zbx.png](files/12-Disk_Stat_RW_Sectors_zbx.png) + + +# Debug Command +## Print HELP +``` +zabbix_get -s -k 'system.run[ "/usr/libexec/zabbix/disk_stat.sh" ]' +``` +## Discovery +``` +zabbix_get -s -k 'system.run[ "/usr/libexec/zabbix/disk_stat.sh blkdev_discovery" ]' +``` +## Get statistic +``` +zabbix_get -s -k 'system.run[ "/usr/libexec/zabbix/disk_stat.sh blkdev_all_stat " ]' +``` + + +# Docs + 1. [kernel.org: Describe /proc/diskstats](https://www.kernel.org/doc/Documentation/ABI/testing/procfs-diskstats) + 2. [kernel.org: I/O statistics fields](https://www.kernel.org/doc/Documentation/admin-guide/iostats.rst) + 3. [Wikipedia.org: maximum IOPS on different Disk](https://en.wikipedia.org/wiki/IOPS) + 4. [RAID calcuator](https://wintelguy.com/raidperf.pl) + 5. [Linux Filesystem Hierarchy Standard: /usr/libexec](https://refspecs.linuxfoundation.org/FHS_3.0/fhs/ch04s07.html) + +# ToDo + 1. Add link with information about maximum IO_time + 2. Add plugin for zabbix_agent2 diff --git a/Operating_Systems/Linux/template_ZBX_disk_stat/5.4/ZBX_Disk_Stat.yaml b/Operating_Systems/Linux/template_ZBX_disk_stat/5.4/ZBX_Disk_Stat.yaml new file mode 100644 index 000000000..42cfd2fd6 --- /dev/null +++ b/Operating_Systems/Linux/template_ZBX_disk_stat/5.4/ZBX_Disk_Stat.yaml @@ -0,0 +1,494 @@ +zabbix_export: + version: '5.4' + date: '2022-04-10T08:45:50Z' + groups: + - + uuid: 64f54166a4c84403b4d8cce5896d71ab + name: io_block_dev + templates: + - + uuid: e19f5e5855094aac8c4e3fd8b4d70aa8 + template: io_block_dev + name: Disk_Stat + description: | + Parse /proc/diskstats + https://www.kernel.org/doc/Documentation/ABI/testing/procfs-diskstats + + Need script: /usr/libexec/zabbix/disk_stat.sh + + + More infomation in github: + https://github.com/dusharu/ZBX_Disk_Stat + groups: + - + name: io_block_dev + discovery_rules: + - + uuid: b747da8902084de89248006a2f5fb4c7 + name: get_block_dev + key: 'system.run[/usr/libexec/zabbix/disk_stat.sh blkdev_discovery]' + delay: 1800s + filter: + evaltype: AND + conditions: + - + macro: '{#DEVBLK}' + value: '.*' + formulaid: A + description: | + Discovery block dev from /proc/diskstats. + DM(Device Mapper) device transform from dm-XX to link name in /dev/mapper/ + item_prototypes: + - + uuid: 646e0e2043024013b19af48312d44ab1 + name: 'IO_queue on $1' + type: DEPENDENT + key: 'custom.blkdev.io_queue[{#DEVBLK}]' + delay: '0' + history: 30d + description: | + https://www.kernel.org/doc/Documentation/admin-guide/iostats.rst + I/Os currently in progress + The only field that should go to zero. Incremented as requests are given to appropriate struct request_queue and decremented as they finish. + preprocessing: + - + type: JSONPATH + parameters: + - '$[''ZBX_Disk_Stat''][0][''io_queue'']' + master_item: + key: 'system.run[/usr/libexec/zabbix/disk_stat.sh blkdev_all_stat {#DEVBLK}]' + request_method: POST + tags: + - + tag: Application + value: io_block_dev + - + uuid: 56964eb0d1684865b364b877e1af3b95 + name: 'Read_IO_time on $1' + type: CALCULATED + key: 'custom.blkdev.read_io_time[{#DEVBLK}]' + delay: 60s + history: 30d + value_type: FLOAT + units: ms + params: | + last(//custom.blkdev.read_sum_time_pm[{#DEVBLK}])/ + (last(//custom.blkdev.read_operation_pm[{#DEVBLK}])+count(//custom.blkdev.read_operation_pm[{#DEVBLK}],#1,"eq","0")) + description: 'Time for one Read Operation' + request_method: POST + tags: + - + tag: Application + value: io_block_dev + trigger_prototypes: + - + uuid: 57903a8e652d48bfa526db35a2482661 + expression: 'count(/io_block_dev/custom.blkdev.read_io_time[{#DEVBLK}],#20,"ge","30")>10' + name: 'Read_IO_time on {#DEVBLK} is critically big' + priority: AVERAGE + description: | + <10ms - Awesome + 10ms-30ms - OK + >30ms - BAD for typical Disk usage + + Not use this metric for Backup Disk + - + uuid: 16f484305f6c47c59c85691851ac4c63 + name: 'Read_operation_per_min on $1' + type: DEPENDENT + key: 'custom.blkdev.read_operation_pm[{#DEVBLK}]' + delay: '0' + history: 30d + description: | + https://www.kernel.org/doc/Documentation/admin-guide/iostats.rst + + This is the total number of reads completed successfully. + preprocessing: + - + type: JSONPATH + parameters: + - '$[''ZBX_Disk_Stat''][0][''read_complete'']' + - + type: SIMPLE_CHANGE + parameters: + - '' + master_item: + key: 'system.run[/usr/libexec/zabbix/disk_stat.sh blkdev_all_stat {#DEVBLK}]' + request_method: POST + tags: + - + tag: Application + value: io_block_dev + - + uuid: f0b50166177b4f0293def17f8c207858 + name: 'Read_operation_per_sec on $1' + type: DEPENDENT + key: 'custom.blkdev.read_operation_ps[{#DEVBLK}]' + delay: '0' + history: 30d + value_type: FLOAT + description: | + https://www.kernel.org/doc/Documentation/admin-guide/iostats.rst + + This is the total number of reads completed successfully divided by total second. + preprocessing: + - + type: JSONPATH + parameters: + - '$[''ZBX_Disk_Stat''][0][''read_complete'']' + - + type: CHANGE_PER_SECOND + parameters: + - '' + master_item: + key: 'system.run[/usr/libexec/zabbix/disk_stat.sh blkdev_all_stat {#DEVBLK}]' + request_method: POST + tags: + - + tag: Application + value: io_block_dev + - + uuid: df381b21e01041a2b52757864f2739e4 + name: 'Read_sectors_per_sec on $1' + type: DEPENDENT + key: 'custom.blkdev.read_sectors_ps[{#DEVBLK}]' + delay: '0' + history: 30d + value_type: FLOAT + description: | + https://www.kernel.org/doc/Documentation/admin-guide/iostats.rst + + This is the total number of sectors read successfully divided by total second. + preprocessing: + - + type: JSONPATH + parameters: + - '$[''ZBX_Disk_Stat''][0][''read_sector'']' + - + type: CHANGE_PER_SECOND + parameters: + - '' + master_item: + key: 'system.run[/usr/libexec/zabbix/disk_stat.sh blkdev_all_stat {#DEVBLK}]' + request_method: POST + tags: + - + tag: Application + value: io_block_dev + - + uuid: 7eaf4ece64c54d018603848d2cb90fdb + name: 'The total number of milliseconds spent by all reads on $1' + type: DEPENDENT + key: 'custom.blkdev.read_sum_time_pm[{#DEVBLK}]' + delay: '0' + history: 30d + description: | + https://www.kernel.org/doc/Documentation/admin-guide/iostats.rst + + This is the total number of milliseconds spent by all reads (as measured from __make_request() to end_that_request_last()). + preprocessing: + - + type: JSONPATH + parameters: + - '$[''ZBX_Disk_Stat''][0][''read_time'']' + - + type: SIMPLE_CHANGE + parameters: + - '' + master_item: + key: 'system.run[/usr/libexec/zabbix/disk_stat.sh blkdev_all_stat {#DEVBLK}]' + request_method: POST + tags: + - + tag: Application + value: io_block_dev + - + uuid: aee254f8516e454bb721f200b9c404bc + name: 'Write_IO_time on $1' + type: CALCULATED + key: 'custom.blkdev.write_io_time[{#DEVBLK}]' + delay: 60s + history: 30d + value_type: FLOAT + units: ms + params: | + last(//custom.blkdev.write_sum_time_pm[{#DEVBLK}])/ + (last(//custom.blkdev.write_operation_pm[{#DEVBLK}])+count(//custom.blkdev.write_operation_pm[{#DEVBLK}],#1,"eq","0")) + description: 'Time for one Write Operation.' + request_method: POST + tags: + - + tag: Application + value: io_block_dev + trigger_prototypes: + - + uuid: 57e4554ca12a4b4eaf51f777d5660606 + expression: 'count(/io_block_dev/custom.blkdev.write_io_time[{#DEVBLK}],#20,"ge","30")>10' + name: 'Write_IO_time on {#DEVBLK} is critically big' + priority: AVERAGE + description: | + <10ms - Awesome + 10ms-30ms - OK + >30ms - BAD for typical Disk usage + + Not use this metric for Backup Disk + - + uuid: d7cc189d85f44997aba400bd6bbb6087 + name: 'Write_operation_per_min on $1' + type: DEPENDENT + key: 'custom.blkdev.write_operation_pm[{#DEVBLK}]' + delay: '0' + history: 30d + description: | + https://www.kernel.org/doc/Documentation/admin-guide/iostats.rst + This is the total number of writes completed successfully. + preprocessing: + - + type: JSONPATH + parameters: + - '$[''ZBX_Disk_Stat''][0][''write_complete'']' + - + type: SIMPLE_CHANGE + parameters: + - '' + master_item: + key: 'system.run[/usr/libexec/zabbix/disk_stat.sh blkdev_all_stat {#DEVBLK}]' + request_method: POST + tags: + - + tag: Application + value: io_block_dev + - + uuid: 870df6f7bd704e7bbdd0fadaaafc154b + name: 'Write_operation_per_sec on $1' + type: DEPENDENT + key: 'custom.blkdev.write_operation_ps[{#DEVBLK}]' + delay: '0' + history: 30d + value_type: FLOAT + description: | + https://www.kernel.org/doc/Documentation/admin-guide/iostats.rst + This is the total number of writes completed successfully divided by total second. + preprocessing: + - + type: JSONPATH + parameters: + - '$[''ZBX_Disk_Stat''][0][''write_complete'']' + - + type: CHANGE_PER_SECOND + parameters: + - '' + master_item: + key: 'system.run[/usr/libexec/zabbix/disk_stat.sh blkdev_all_stat {#DEVBLK}]' + request_method: POST + tags: + - + tag: Application + value: io_block_dev + - + uuid: 3dae9ed472c6455bb742a3784cc6079c + name: 'Write_sectors_per_sec on $1' + type: DEPENDENT + key: 'custom.blkdev.write_sectors_ps[{#DEVBLK}]' + delay: '0' + history: 30d + value_type: FLOAT + description: | + https://www.kernel.org/doc/Documentation/admin-guide/iostats.rst + This is the total number of sectors written successfully divided by total second. + preprocessing: + - + type: JSONPATH + parameters: + - '$[''ZBX_Disk_Stat''][0][''write_sector'']' + - + type: CHANGE_PER_SECOND + parameters: + - '' + master_item: + key: 'system.run[/usr/libexec/zabbix/disk_stat.sh blkdev_all_stat {#DEVBLK}]' + request_method: POST + tags: + - + tag: Application + value: io_block_dev + - + uuid: 910b2c7c32fa45b5a7dab2149fb688c9 + name: 'The total number of milliseconds spent by all writes on $1' + type: DEPENDENT + key: 'custom.blkdev.write_sum_time_pm[{#DEVBLK}]' + delay: '0' + history: 30d + description: | + https://www.kernel.org/doc/Documentation/admin-guide/iostats.rst + This is the total number of milliseconds spent by all writes (as measured from __make_request() to end_that_request_last()). + preprocessing: + - + type: JSONPATH + parameters: + - '$[''ZBX_Disk_Stat''][0][''write_time'']' + - + type: SIMPLE_CHANGE + parameters: + - '' + master_item: + key: 'system.run[/usr/libexec/zabbix/disk_stat.sh blkdev_all_stat {#DEVBLK}]' + request_method: POST + tags: + - + tag: Application + value: io_block_dev + - + uuid: 76805b5fc1b64ee49a0f6ebe69f5ef68 + name: 'All_stat on $1' + key: 'system.run[/usr/libexec/zabbix/disk_stat.sh blkdev_all_stat {#DEVBLK}]' + delay: 60s + history: '0' + trends: '0' + value_type: TEXT + description: | + JSON raw data + Describe in https://www.kernel.org/doc/Documentation/admin-guide/iostats.rst + request_method: POST + tags: + - + tag: Application + value: io_block_dev + graph_prototypes: + - + uuid: e70ed52043504d1c8a09e9d9563512ef + name: 'IOPS on {#DEVBLK}' + graph_items: + - + color: '000066' + item: + host: io_block_dev + key: 'custom.blkdev.read_operation_ps[{#DEVBLK}]' + - + sortorder: '1' + color: 3333FF + item: + host: io_block_dev + key: 'custom.blkdev.write_operation_ps[{#DEVBLK}]' + - + sortorder: '2' + color: 1A7C11 + yaxisside: RIGHT + item: + host: io_block_dev + key: 'custom.blkdev.read_io_time[{#DEVBLK}]' + - + sortorder: '3' + color: 66FF66 + yaxisside: RIGHT + item: + host: io_block_dev + key: 'custom.blkdev.write_io_time[{#DEVBLK}]' + - + uuid: 6e3f56fafb4c40cfae38c9a83d1959cb + name: 'IO_queue on {#DEVBLK}' + graph_items: + - + color: '000066' + item: + host: io_block_dev + key: 'custom.blkdev.io_queue[{#DEVBLK}]' + - + sortorder: '1' + color: 1A7C11 + yaxisside: RIGHT + item: + host: io_block_dev + key: 'custom.blkdev.read_io_time[{#DEVBLK}]' + - + sortorder: '2' + color: 66FF66 + yaxisside: RIGHT + item: + host: io_block_dev + key: 'custom.blkdev.write_io_time[{#DEVBLK}]' + - + uuid: feb8451d097b4f25bf3cfecd08715d44 + name: 'Read\Write sectors(512bytes) on {#DEVBLK}' + graph_items: + - + color: '000066' + item: + host: io_block_dev + key: 'custom.blkdev.read_sectors_ps[{#DEVBLK}]' + - + sortorder: '1' + color: 3333FF + item: + host: io_block_dev + key: 'custom.blkdev.write_sectors_ps[{#DEVBLK}]' + - + sortorder: '2' + color: 1A7C11 + yaxisside: RIGHT + item: + host: io_block_dev + key: 'custom.blkdev.read_io_time[{#DEVBLK}]' + - + sortorder: '3' + color: 66FF66 + yaxisside: RIGHT + item: + host: io_block_dev + key: 'custom.blkdev.write_io_time[{#DEVBLK}]' + request_method: POST + overrides: + - + name: 'SKIP DISK' + step: '1' + stop: STOP + filter: + conditions: + - + macro: '{#DEVBLK}' + value: '^sr[0-9]*$' + formulaid: A + - + macro: '{#DEVBLK}' + value: '-cow$' + formulaid: B + - + macro: '{#DEVBLK}' + value: ^$ + formulaid: C + - + macro: '{#DEVBLK}' + value: '.*snapshot.*' + formulaid: D + - + macro: '{#DEVBLK}' + value: '-real$' + formulaid: E + - + macro: '{#DEVBLK}' + value: '^xvd[a-z]*[0-9]+$' + formulaid: F + - + macro: '{#DEVBLK}' + value: '^[hs]d[a-z]*[0-9]+$' + formulaid: G + - + macro: '{#DEVBLK}' + value: '^loop[0-9]*$' + formulaid: H + operations: + - + operationobject: ITEM_PROTOTYPE + operator: REGEXP + value: '.*' + discover: NO_DISCOVER + - + operationobject: TRIGGER_PROTOTYPE + operator: REGEXP + value: '.*' + discover: NO_DISCOVER + - + operationobject: GRAPH_PROTOTYPE + operator: REGEXP + value: '.*' + discover: NO_DISCOVER diff --git a/Operating_Systems/Linux/template_ZBX_disk_stat/5.4/files/1-Disk_Stat_IOPS.png b/Operating_Systems/Linux/template_ZBX_disk_stat/5.4/files/1-Disk_Stat_IOPS.png new file mode 100644 index 000000000..9b87fe852 Binary files /dev/null and b/Operating_Systems/Linux/template_ZBX_disk_stat/5.4/files/1-Disk_Stat_IOPS.png differ diff --git a/Operating_Systems/Linux/template_ZBX_disk_stat/5.4/files/10-Disk_Stat_IOPS_zbx.png b/Operating_Systems/Linux/template_ZBX_disk_stat/5.4/files/10-Disk_Stat_IOPS_zbx.png new file mode 100644 index 000000000..1c90db9fc Binary files /dev/null and b/Operating_Systems/Linux/template_ZBX_disk_stat/5.4/files/10-Disk_Stat_IOPS_zbx.png differ diff --git a/Operating_Systems/Linux/template_ZBX_disk_stat/5.4/files/11-Disk_Stat_IO_Queue_zbx.png b/Operating_Systems/Linux/template_ZBX_disk_stat/5.4/files/11-Disk_Stat_IO_Queue_zbx.png new file mode 100644 index 000000000..595d0e5c3 Binary files /dev/null and b/Operating_Systems/Linux/template_ZBX_disk_stat/5.4/files/11-Disk_Stat_IO_Queue_zbx.png differ diff --git a/Operating_Systems/Linux/template_ZBX_disk_stat/5.4/files/12-Disk_Stat_RW_Sectors_zbx.png b/Operating_Systems/Linux/template_ZBX_disk_stat/5.4/files/12-Disk_Stat_RW_Sectors_zbx.png new file mode 100644 index 000000000..696fe7608 Binary files /dev/null and b/Operating_Systems/Linux/template_ZBX_disk_stat/5.4/files/12-Disk_Stat_RW_Sectors_zbx.png differ diff --git a/Operating_Systems/Linux/template_ZBX_disk_stat/5.4/files/2-Disk_Stat_RW_Sectors.png b/Operating_Systems/Linux/template_ZBX_disk_stat/5.4/files/2-Disk_Stat_RW_Sectors.png new file mode 100644 index 000000000..745fbd10f Binary files /dev/null and b/Operating_Systems/Linux/template_ZBX_disk_stat/5.4/files/2-Disk_Stat_RW_Sectors.png differ diff --git a/Operating_Systems/Linux/template_ZBX_disk_stat/5.4/files/3-Disk_Stat_IO_time.png b/Operating_Systems/Linux/template_ZBX_disk_stat/5.4/files/3-Disk_Stat_IO_time.png new file mode 100644 index 000000000..47e9265c7 Binary files /dev/null and b/Operating_Systems/Linux/template_ZBX_disk_stat/5.4/files/3-Disk_Stat_IO_time.png differ diff --git a/Operating_Systems/Linux/template_ZBX_disk_stat/5.4/files/4-Disk_Stat_IO_Queue.png b/Operating_Systems/Linux/template_ZBX_disk_stat/5.4/files/4-Disk_Stat_IO_Queue.png new file mode 100644 index 000000000..4b77be97e Binary files /dev/null and b/Operating_Systems/Linux/template_ZBX_disk_stat/5.4/files/4-Disk_Stat_IO_Queue.png differ diff --git a/Operating_Systems/Linux/template_ZBX_disk_stat/5.4/files/disk_stat.sh b/Operating_Systems/Linux/template_ZBX_disk_stat/5.4/files/disk_stat.sh new file mode 100755 index 000000000..a49f20722 --- /dev/null +++ b/Operating_Systems/Linux/template_ZBX_disk_stat/5.4/files/disk_stat.sh @@ -0,0 +1,186 @@ +#!/bin/bash +################################################################################ +# # +# Zabbix Disk_Stat monitoring # +# mailto:dusharu17@gmail.com # +# 2022.03.13 # +################################################################################ +export PATH="$PATH:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin" + +#################### VAR +##### EXIT_CODE +EXIT_CODE_INVALID_INPUT_PARAMETRS=200 +EXIT_CODE_INVALID_DISK=201 +EXIT_CODE_DISK_NOT_FOUND=220 # used in functions blkdev_all_stat* + + + +#################### Function +####################################### +# Print Help +# Globals: +# None +# Arguments: +# None +####################################### +function PrintHelp { + echo "PRINT HELP:" + echo "$0 blkdev_discovery # return json with list of block devices" + echo "$0 blkdev_all_stat # return json with statistic for /dev/sdX or LVM device(dev/vgX/lvX)" + echo + echo "More infomation in my github:" + echo "https://github.com/dusharu/ZBX_Disk_Stat" +} + + + +####################################### +# Discovery block devices +# Globals: +# None +# Arguments: +# None +####################################### +function blkdev_discovery { + # json header + echo -n -e "{\n\t\"data\":[" + + i=0 + # shellcheck disable=SC2034 # Unused variables used for skip some items + while read -r MAJOR MINOR DEV OTHER; do + # skip invalid DEV + if [[ "$DEV" =~ ^[0-9]*$ ]]; then + # echo "skip DEV: $DEV" >&2 + # echo "reason: Dev can't be num or empty" >&2 + # echo "string(debug): $MAJOR $MINOR $DEV $OTHER" >&2 + continue + fi + + # print comma(,) for pervious line + ((++i)) + if [[ "$i" -gt 1 ]]; then + echo -n -e "\t}," + fi + + # print blkdev Item Header + echo -n -e "\n\t\t{\t\"{#DEVBLK}\":\"" + + # print blkdev Item Name + if [[ "$DEV" =~ ^dm-[0-9]*$ ]]; then + lsblk -l -o Name,KNAME |\ + grep -m 1 "\s*${DEV}$" |\ + cut -d " " -f 1 |\ + tr -d '\n' + else + echo -n "$DEV" + fi + + # print blkd Item finisher + echo -n \" + done < /proc/diskstats + + # json footer + echo -n -e "\t}\n" + echo -n -e "\t]\n}\n" +} + + +####################################### +# Return Disk statistics in json +# Globals: +# None +# Arguments: +# DISK name; +####################################### +function blkdev_all_stat { + DISK="$1" + EXIT_CODE_DISK_NOT_FOUND=220 + + # count parametrs and make correct fields name for column utility + DISK_PARAMETERS_ALL='major,minor,name,read_complete,read_merge,read_sector,read_time,write_complete,write_merge,write_sector,write_time,io_queue,io_time,io_time_weight,discard_complete,discard_merge,discard_sector,discard_time,flush_request,flush_time' + DISK_PARAMETERS_COUNT="$(awk '{print NF; exit}' /proc/diskstats)" + DISK_PARAMETERS="$(echo "$DISK_PARAMETERS_ALL" | cut -d , -f "1-$DISK_PARAMETERS_COUNT" )" + + # translate blk-name to dm-XX + if [ ! -b "/dev/$DISK" ]; then + DISK="$(lsblk -l -o KNAME,Name | grep -m 1 "$DISK" |awk '{print $1}')" + fi + + # generate json + if [ "$DISK" != "" ]; then + # sed if end of cmd for Backward cmpability + # modern version of column print table-name in lower case + grep -m 1 " $DISK " /proc/diskstats |\ + column \ + --json \ + --table-name ZBX_Disk_Stat \ + --table-columns "$DISK_PARAMETERS" |\ + sed -e 's/zbx_disk_stat/ZBX_Disk_Stat/g' + else + exit "$EXIT_CODE_DISK_NOT_FOUND" + fi +} + + + +####################################### +# Return Disk statistics in json without utility column +# This is old version and will be delete soon +# Globals: +# None +# Arguments: +# DISK name; +####################################### +function blkdev_all_stat_without_column { + DISK="$1" + EXIT_CODE_DISK_NOT_FOUND=220 + + # translate blk-name to dm-XX + if [ ! -b "/dev/$DISK" ]; then + DISK="$(lsblk -l -o KNAME,Name | grep -m 1 "$DISK" |awk '{print $1}')" + fi + + # generate json + if [ "$DISK" != "" ]; then + grep -m1 " $DISK " /proc/diskstats | awk ' + BEGIN { printf("{\n\"ZBX_Disk_Stat\": [ {"); } + { + printf("\n\t\"name\": \"" $3 "\","); + printf("\n\t\"read_complete\": \"" $4 "\","); + printf("\n\t\"read_sector\": \"" $6 "\","); + printf("\n\t\"read_time\": \"" $7 "\","); + printf("\n\t\"write_complete\": \"" $8 "\","); + printf("\n\t\"write_sector\": \"" $10 "\","); + printf("\n\t\"write_time\": \"" $11 "\","); + printf("\n\t\"io_queue\": \"" $12 "\""); + } + END { printf("\n\t} ]\n}"); }' + else + exit "$EXIT_CODE_DISK_NOT_FOUND" + fi +} + + + + + +#################### MAIN +case "$1" in + blkdev_discovery) blkdev_discovery ;; + blkdev_all_stat) + if [[ -n "$2" ]]; then + COLUMN_VERSION="$(column --version|grep -oe '[0-9\.]*')" + if [[ "$(echo -e "2.30\n${COLUMN_VERSION}"| sort -V | head -n 1)" == 2.30 ]]; then + # column version >= 2.30 and has --json option + # https://mirrors.edge.kernel.org/pub/linux/utils/util-linux/v2.30/v2.30-ReleaseNotes + blkdev_all_stat "$2" + else + # column version < 2.30 and doesn't have --json option + blkdev_all_stat_without_column "$2" + fi + else + echo "ERROR Unknow DISK: $2"; PrintHelp; exit "$EXIT_CODE_INVALID_DISK" + fi + ;; + *) echo "ERROR Unknow action: $1"; PrintHelp; exit "$EXIT_CODE_INVALID_INPUT_PARAMETRS" ;; +esac