#!/usr/bin/env bash # MDADM SNMP extension for LibreNMS # Version extendVer='2' # Initial portion of json mdadmSNMPOutput='{ "data": [' # Outputs a list of devices list_devices() { for device in "${1}/slaves/"*; do if [ "${2,,}" == 'count' ]; then ((devCount++)) elif [ "${2,,}" != 'missing' ] || [ ! -e "${device}" ]; then printf '%b\t "%s"' "${multiDisk}" "$(basename "${device}")" multiDisk=',\n' fi done [ "${devCount}" ] && echo "${devCount}" } # Outputs either 0, 100, or the value of the file referenced maybe_get() { if [ -f "${1}" ] && [[ $(cat "${1}") =~ " / " ]]; then echo $((100 * $(cat "${1}"))) elif [ -f "${1}" ] && [ "$(cat "${1}")" != 'none' ]; then cat "${1}" else echo 0 fi } main() { if ! which 'jq' > /dev/null 2>&1; then errorCode=1 # The underscore here is a hack since we have to strip spaces without jq errorString='jq_missing!' elif stat "/dev/md"[[:digit:]]* > /dev/null 2>&1; then for mdadmArray in "/dev/md"[[:digit:]]*; do # Ignore partitions [[ "${mdadmArray}" =~ '/dev/md'[[:digit:]]+'p' ]] && continue mdadmName="$(basename "$(realpath "${mdadmArray}")")" # Ignore inactive arrays [[ $(grep "^${mdadmName}" /proc/mdstat) =~ 'inactive' ]] && continue mdadmSysDev="/sys/block/${mdadmName}" degraded=$(maybe_get "${mdadmSysDev}/md/degraded") syncSpeed=$(($(maybe_get "${mdadmSysDev}/md/sync_speed") * 1024)) syncCompleted=$(maybe_get "${mdadmSysDev}/md/sync_completed") if [ $syncCompleted -eq 0 ] && [ $degraded -eq 0 ] && [ $syncSpeed -eq 0 ]; then syncCompleted="100" fi read -r -d '' mdadmOutput < /dev/null || sed 's/\s//g' <<< "${mdadmSNMPOutput//$'\n'/}${metadataOutput//$'\n'/}" } main "${@}"