Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate email addresses #22

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# monitor zfs from nagios/NRPE or cron on FreeBSD
# monitor zfs from Cron/Zabbix/NRPE/Nagios

[![Codacy Badge](https://app.codacy.com/project/badge/Grade/af682a2e5ff34d13b4fba76798eb37a8)](https://app.codacy.com/gh/Klintrup/check_zpool/dashboard)
[![License Apache 2.0](https://img.shields.io/github/license/Klintrup/check_zpool)](https://github.com/Klintrup/check_zpool/blob/main/LICENSE)
Expand All @@ -9,7 +9,7 @@

## Synopsis

Simple check-script for NRPE/nagios to get the status of various zpool volumes
Simple check-script for Cron/Zabbix/NRPE/Nagios to get the status of all zpool volumes
in a box, and output the failed volumes if any such exist.

## Syntax
Expand All @@ -31,15 +31,19 @@ diagnose the problem when receiving the output via pager/sms.

## Output examples

| output | description |
| ------------- | ------------------------------------------------------------------------------------------------------------------------------- |
| ok | The device is reported as ok by zpool |
| DEGRADED | The RAID volume is degraded, it's still working but without the safety of RAID, and in some cases with severe performance loss. |
| rebuilding | The RAID is rebuilding, will return to OK when done |
| unknown state | Volume is in an unknown state. Please report this as an issue on [GitHub](https://github.com/Klintrup/check_zpool/issues) |
| output | exit code | description |
| ------------- | --------- | ------------------------------------------------------------------------------------------------------------------------- |
| ONLINE | 0 | The device is online and functioning normally. |
| DEGRADED | 1 | The device is experiencing a non-fatal fault, which may be causing degraded performance. |
| FAULTED | 2 | An unrecoverable error has occurred. The device cannot be opened. |
| OFFLINE | 2 | The device has been taken offline by the administrator. |
| REMOVED | 2 | The device was physically removed while the system was running. |
| UNAVAIL | 2 | The device cannot be opened because the system is currently running a resilvering or scrubbing operation. |
| SUSPENDED | 2 | The device is inaccessible, possibly because the system is in the process of resilvering or scrubbing the device. |
| UNKNOWN STATE | 3 | Volume is in an unknown state. Please report this as an issue on [GitHub](https://github.com/Klintrup/check_zpool/issues) |

## Compatibility

Should work on all versions of FreeBSD with zfs.
Compatible with all versions of FreeBSD and Linux that support ZFS.

Tested on FreeBSD 8.0-10.1
Specifically tested on FreeBSD 8.0+ and Ubuntu 22.04 LTS
84 changes: 73 additions & 11 deletions check_zpool.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,64 @@
# Get your copy from https://github.com/Klintrup/check_zpool

PATH="/sbin:/bin:/usr/sbin:/usr/bin"
ERR="0"
unset ERRORSTRING
unset OKSTRING
unset ERR

validate_email() {
if [ -n "$_validate_email__input" ]; then
echo "_validate_email__input variable is already set" >&2
exit 1
fi
for _validate_email__input in "$@"; do
if ! echo "$_validate_email__input" | grep -qE '^[a-zA-Z0-9._%+-]{1,}@[a-zA-Z0-9.-]{1,}\.[a-zA-Z]{2,}$'; then
echo "$_validate_email__input is not a valid email address" >&2
else
echo "$_validate_email__input"
fi
done
}
Klintrup marked this conversation as resolved.
Show resolved Hide resolved

set_error_code() {
current_error_code="${1}"
new_error_code="${2}"
if [ -z "${current_error_code}" ] || [ -z "${new_error_code}" ]; then
echo "No error code or new error code given" >&2
exit 1
fi
case "${new_error_code}" in
ok)
new_error_code=0
;;
warning)
new_error_code=1
;;
error)
new_error_code=2
;;
unknown)
new_error_code=3
;;
[0-3])
;;
*)
echo "Invalid error code: ${new_error_code}" >&2
exit 1
;;
esac

if [ "${new_error_code}" -eq 3 ]; then
if [ "${current_error_code}" -eq 0 ]; then
echo "${new_error_code}"
else
echo "${current_error_code}"
fi
elif [ "${current_error_code}" -lt "${new_error_code}" ]; then
echo "${new_error_code}"
else
echo "${current_error_code}"
fi
}

if [ -x "/sbin/zpool" ]; then
DEVICES="$(zpool list -H -o name)"
Expand All @@ -17,45 +72,52 @@ fi

for DEVICE in ${DEVICES}; do
DEVICESTRING="$(zpool list -H -o health "${DEVICE}")"
if [ "$(echo "${DEVICESTRING}" | tr '[:upper:]' '[:lower:]' | sed -Ee 's/.*(degraded|faulted|offline|online|removed|unavail).*/\1/')" = "" ]; then
if [ "$(echo "${DEVICESTRING}" | tr '[:upper:]' '[:lower:]' | sed -Ee 's/.*(degraded|faulted|suspended|offline|online|removed|unavail).*/\1/')" = "" ]; then
ERRORSTRING="${ERRORSTRING} / ${DEVICE}: unknown state"
if ! [ "${ERR}" = 2 ]; then ERR=3; fi
ERR=$(set_error_code "${ERR}" "unknown")
else
case $(echo "${DEVICESTRING}" | tr '[:upper:]' '[:lower:]' | sed -Ee 's/.*(degraded|faulted|offline|online|removed|unavail).*/\1/') in
case $(echo "${DEVICESTRING}" | tr '[:upper:]' '[:lower:]' | sed -Ee 's/.*(degraded|faulted|suspended|offline|online|removed|unavail).*/\1/') in
degraded)
ERR=2
ERR=$(set_error_code "${ERR}" "warning")
ERRORSTRING="${ERRORSTRING} / ${DEVICE}: DEGRADED"
;;
faulted)
ERR=2
ERR=$(set_error_code "${ERR}" "error")
ERRORSTRING="${ERRORSTRING} / ${DEVICE}: FAULTED"
;;
offline)
ERR=2
ERR=$(set_error_code "${ERR}" "error")
ERRORSTRING="${ERRORSTRING} / ${DEVICE}: OFFLINE"
;;
suspended)
ERR=$(set_error_code "${ERR}" "error")
ERRORSTRING="${ERRORSTRING} / ${DEVICE}: SUSPENDED"
;;
removed)
ERR=2
ERR=$(set_error_code "${ERR}" "error")
ERRORSTRING="${ERRORSTRING} / ${DEVICE}: REMOVED"
;;
unavail)
ERR=2
ERR=$(set_error_code "${ERR}" "error")
ERRORSTRING="${ERRORSTRING} / ${DEVICE}: UNAVAIL"
;;
online)
ERR=$(set_error_code "${ERR}" "ok")
OKSTRING="${OKSTRING} / ${DEVICE}: online"
;;
esac
fi
done

if [ "${1}" ]; then
if [ "${ERRORSTRING}" ]; then
echo "${ERRORSTRING} ${OKSTRING}" | sed s/"^\/ "// | mail -s "$(hostname -s): ${0} reports errors" -E "${*}"
recipients=$(validate_email "${@}")
echo "${ERRORSTRING} ${OKSTRING}" | sed s/"^\/ "// | mail -s "$(hostname -s): ${0} reports errors" -E "${recipients}"
fi
else
if [ "${ERRORSTRING}" ] || [ "${OKSTRING}" ]; then
echo "${ERRORSTRING} ${OKSTRING}" | sed -E s/"^[[:blank:]]{1,}\/ "//
exit ${ERR}
exit "${ERR}"
else
echo no zpool volumes found
exit 3
Expand Down
Loading