zabbix_smartctl

15th November 2015 at 6:07pm
Bash CodeSnippets
#!/bin/bash

PATH="$PATH:/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin"

umask 022

mkdir -p "/tmp/zabbix.smartctl/"
temp_discovery="$(mktemp)"
chmod 644 "$temp_discovery"

(
echo -n "{\"data\":["

for device in $(smartctl --scan | awk '{print $1}')
do
    echo -n "{\"{#DEVNAME}\":\"${device##*/}\",\"{#DEVPATH}\":\"${device//\//\\/}\"},"
        (
        smartctl -s on -A $device 2>&1 \
                | egrep '^ *[0-9]{1,3} [A-Za-z_]{5,64} *0x[0-9]*' \
                | while read ID ATTR FLAG VALUE WORST THRESH TYPE UPDATED WHEN_FAILED RAW_VALUE
        do
                echo "${RAW_VALUE// */}" > "/tmp/zabbix.smartctl/${device##*/}.$ATTR"
        done
        ) &
done

wait

echo -n "]}"
) | sed 's/,]}/]}/g' > "$temp_discovery"

mv "$temp_discovery" "/tmp/zabbix.smartctl/device.discovery"