#!/bin/sh # checks/hw.sh # Hardware/host metrics: load avg, uptime, free mem, zroot usage, and per # pool health/capacity plus scrub/error status for every zpool on the box. set -eu DIR=$(dirname "$0") . "$DIR/../lib/common.sh" LOAD=$(sysctl -n vm.loadavg | tr -d '{}' | sed 's/^ *//;s/ *$//') json_line "hardware" "load avg" "$LOAD" "ok" UPTIME=$(uptime | sed 's/.*up //;s/,.*load.*//') json_line "hardware" "uptime" "$UPTIME" "ok" MEM_FREE=$(sysctl -n vm.stats.vm.v_free_count) MEM_PAGE=$(sysctl -n hw.pagesize) MEM_FREE_MB=$((MEM_FREE * MEM_PAGE / 1024 / 1024)) json_line "hardware" "free mem" "${MEM_FREE_MB} MB" "ok" DISK=$(df -h /zroot 2>/dev/null | awk 'NR==2{print $5}') DISK_PCT=${DISK%\%} if [ -z "$DISK_PCT" ]; then DISK_STATUS="warn" DISK="unknown" elif [ "$DISK_PCT" -ge 90 ]; then DISK_STATUS="down" elif [ "$DISK_PCT" -ge 75 ]; then DISK_STATUS="warn" else DISK_STATUS="ok" fi json_line "hardware" "zroot usage" "$DISK" "$DISK_STATUS" for pool in $(zpool list -H -o name 2>/dev/null); do HEALTH=$(zpool list -H -o health "$pool" 2>/dev/null) CAP=$(zpool list -H -o capacity "$pool" 2>/dev/null) case "$HEALTH" in ONLINE) POOL_STATUS="ok" ;; DEGRADED) POOL_STATUS="warn" ;; *) POOL_STATUS="down" ;; esac json_line "hardware" "zpool $pool" "$HEALTH, ${CAP} used" "$POOL_STATUS" STATUS_OUT=$(zpool status "$pool" 2>/dev/null) || STATUS_OUT="" SCAN=$(printf '%s\n' "$STATUS_OUT" | sed -n 's/^[[:space:]]*scan: //p') ERRORS=$(printf '%s\n' "$STATUS_OUT" | sed -n 's/^[[:space:]]*errors: //p') case "$ERRORS" in "No known data errors") ERR_STATUS="ok" ;; "") ERR_STATUS="warn" ;; *) ERR_STATUS="down" ;; esac json_line "hardware" "zpool $pool scrub" "${SCAN:-unknown}" "$ERR_STATUS" done