SMART-Discovery+Monitoring: Add bash version

This commit is contained in:
Michael Höß 2020-09-29 12:45:47 +00:00
parent 2310741f47
commit 69d357a03b
1 changed files with 146 additions and 0 deletions

146
zbx-smart.bash Normal file
View File

@ -0,0 +1,146 @@
#!/bin/bash
# By MH 2020, MIT-License
_smart_is_usb()
{
rem=$(lsblk -d "$1" -o SUBSYSTEMS | grep "usb" | wc -l)
if [[ "${rem}" == "0" ]]; then
echo 0
else
echo 1
fi
}
_smart_getval()
{
echo -e "$1" | grep -i "^${2} = " | cut -d "=" -f 2 | cut -d '"' -f 2
}
_smart_getval_i()
{
res=$(echo -e "$1" | grep -i "^${2} = " | cut -d "=" -f 2 | cut -d ' ' -f 2 | tr -d ";")
if [[ "$res" == "" ]]; then
res=0
fi
echo "${res}"
}
smart_disco() {
c=0
devs=""
while read ln; do
dev=$(echo ${ln} | cut -d " " -f 1 | sed -e "s/\/dev\///g")
typ=$(echo ${ln} | cut -d " " -f 3)
# Removable
# rem=$(lsblk -d /dev/${dev} -o SUBSYSTEMS | grep "usb" | wc -l)
rem=$(_smart_is_usb "/dev/${dev}")
if [[ "${rem}" == "0" ]]; then
smart="$(smartctl -n standby -a /dev/${dev} --json=g)"
model="$(_smart_getval "${smart}" "json.model_name")"
sn="$(_smart_getval "${smart}" "json.serial_number")"
rot="$(_smart_getval_i "${smart}" "json.rotation_rate")"
lbs="$(_smart_getval_i "${smart}" "json.logical_block_size")"
#SSD: TODO
isSSD=0
fi
if (( c == 1 )); then
devs="${devs},\n"
fi
c=1
devs="${devs} {\n"
if [[ "${rem}" == "0" ]]; then
devs="${devs} \"name\": \"${dev}\",\n"
devs="${devs} \"type\": \"${typ}\",\n"
devs="${devs} \"model\": \"${model}\",\n"
devs="${devs} \"sn\": \"${sn}\",\n"
devs="${devs} \"rotations\": ${rot},\n"
devs="${devs} \"lbs\": ${lbs},\n"
devs="${devs} \"isExternal\": ${rem},\n"
devs="${devs} \"isSSD\": ${isSSD}\n"
else
devs="${devs} \"name\": \"${dev}\",\n"
devs="${devs} \"isExternal\": ${rem}\n"
fi
devs="${devs} }"
done < <(smartctl -n standby --scan)
json2="\n{"
json2="${json2} \"devices\": [\n"
json2="${json2} ${devs}\n"
json2="${json2} ]\n"
json2="${json2}}"
echo -e "$json2"
# Remove newline for use as parameter
sentval="$(echo -e $json2)"
#json=$(smartctl --json --scan)
#json=$(echo "${json}" | sed -e "s/\/dev\///g")
#echo $json
zabbix_sender -vv -k "8o_smartcheck.disco_devs" -o "${sentval}" -c /etc/zabbix/zabbix_agentd.conf 2>&1 | logger -t "zabbix-smart_disco"
}
smart_check() {
(for dev in $(smartctl --scan | sed -e "s/\/dev\///g" | cut -d " " -f 1 ); do
rem=$(_smart_is_usb "/dev/${dev}")
if [[ "${rem}" == "1" ]] && [[ "${SMART_WITH_EXTERNAL}" == "0" ]]; then
#echo "X" 1>&2
continue;
fi
smartctl -s on /dev/${dev} > /dev/null
status="$(smartctl -n standby -q errorsonly -H /dev/${dev})"
if [[ "${status}" == "" ]]; then
status="OK"
fi
smart="$(smartctl -n standby -a /dev/${dev} --json=g)"
poh="$(_smart_getval_i "${smart}" "json.power_on_time.hours")"
poc="$(_smart_getval_i "${smart}" "json.power_cycle_count")"
temp="$(_smart_getval_i "${smart}" "json.temperature.current")"
#zabbix_sender -vv -k "8o_smartcheck.[${dev}.health]" -o "${status}" -c /etc/zabbix/zabbix_agentd.conf \
# 2>&1 | logger -t "zabbix-smart_health"
echo "- 8o_smartcheck.[${dev}.health] ${status}"
echo "- 8o_smartcheck.[${dev}.power_on_hours] ${poh}"
echo "- 8o_smartcheck.[${dev}.power_cycle_count] ${poc}"
echo "- 8o_smartcheck.[${dev}.temperature] ${temp}"
#echo "8o_smartcheck.[${dev}.health]"
#echo ${dev}
done) | zabbix_sender -vv -i - -c /etc/zabbix/zabbix_agentd.conf 2>&1 | logger -t "zabbix-smart_health"
}
SMART_WITH_EXTERNAL=0
if [ "$1" == "--smart-with-external" ]; then
SMART_WITH_EXTERNAL=1
fi
if [ "$1" == "--disco" ]; then
smart_disco
else
smart_check
fi