3
\$\begingroup\$

Next script returns NRPE friendly output. It checks percentage usage of all local filesystems.

Desired output if everything is ok:

OK | /=56;;;; /boot=20;;;; /opt=40;;;; /var=60;;;; (and exit_code0 = ok)

Desired output if something is wrong:

CHECK /var | /=56;;;; /boot=20;;;; /opt=40;;;; /var=93;;;; (and exit_code1 = warning, 2 = error)

#!/usr/bin/env bash

set -o errexit -o nounset -o pipefail

warning=80
critical=95

declare -a output_text
declare -a output_data
exit_code="0"


while read -r filesystem _ _ _ used mountpoint; do
  if [[ ${filesystem:0:4} = "/dev" ]] && [[ ${filesystem:0:8} != "/dev/sr0" ]]; then
    code="$(( ( ${used%"%"} > warning ) + ( ${used%"%"} > critical ) ))"

    # Never decrease the exit_code value!
    if [[ $code -gt $exit_code ]]; then
      exit_code="$code"
      output_text+=("$mountpoint")
    fi

    output_data+=("${mountpoint}=${used%"%"};;;;")
  fi
done < <( df -P )


if [[ exit_code -gt 0 ]]; then
  echo "CHECK" "${output_text[@]}" "|" "${output_data[@]}"
else
  echo "OK |" "${output_data[@]}"
fi

exit $exit_code

Could I do something better?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.