#!/bin/sh # $Header: $ # vim:nu:ai:sw=4:ts=4: # # Nagios check script for UPSes monitored by apcupsd # Created by Wolfram Schlich # Distributed under the terms of the GNU General Public License v3 # # External dependencies: # - apcaccess # - grep # ## ## nagios state specific exit codes ## declare -i state_ok=0 declare -i state_warning=1 declare -i state_critical=2 declare -i state_unknown=3 declare -i state_dependent=4 ## ## functions ## function usage() { echo echo "Usage: ${0##*/} APCUPSD_SERVER WARN_VALUE CRIT_VALUE " echo " ${0##*/} APCUPSD_SERVER status" echo echo " APCUPSD_SERVER: apcupsd server host:port to connect to (e.g. localhost:3551)" echo echo " load (%): WARN when load is > WARN_VALUE, CRIT when load is > CRIT_VALUE" echo " battcharge (%): WARN when charge is < WARN_VALUE, CRIT when charge is < CRIT_VALUE" echo " timeleft (mins): WARN when time is < WARN_VALUE, CRIT when time is < CRIT_VALUE" echo " status: CRIT if not online" echo } function checkargs() { if [[ -z "${apcupsd_server}" || -z "${warn_value}" || -z "${crit_value}" ]]; then usage exit ${state_unknown} fi } function run_apcaccess() { apcaccess status ${apcupsd_server} } function get_load() { line=$(run_apcaccess | grep '^LOADPCT') IFS=': ' set -- ${line} load=${2%%.*} unset IFS set -- } function get_battcharge() { line=$(run_apcaccess | grep '^BCHARGE') IFS=': ' set -- ${line} battcharge=${2%%.*} unset IFS set -- } function get_timeleft() { line=$(run_apcaccess | grep '^TIMELEFT') IFS=': ' set -- ${line} timeleft=${2%%.*} unset IFS set -- } function get_status() { line=$(run_apcaccess | grep '^STATUS') IFS=': ' set -- ${line} status=${2} unset IFS set -- } ## ## main() ## if [[ ! -x "$(type -p apcaccess 2>/dev/null)" ]]; then echo "ERROR: apcaccess not executable" exit ${state_unknown} fi apcupsd_server=${1}; shift check=${1}; shift warn_value=${1}; shift crit_value=${1}; shift case "${check}" in load) checkargs get_load if [ "${load}" -gt "${crit_value}" ]; then echo "UPS CRITICAL - Load: ${load}% > ${crit_value}%" exit ${state_critical} elif [ "${load}" -gt "${warn_value}" ]; then echo "UPS WARNING - Load: ${load}% > ${warn_value}%" exit ${state_warning} else echo "UPS OK - Load: ${load}%" exit ${state_ok} fi ;; battcharge) checkargs get_battcharge if [ "${battcharge}" -lt "${crit_value}" ]; then echo "UPS CRITICAL - Battery charge: ${battcharge}% < ${crit_value}%" exit ${state_critical} elif [ "${battcharge}" -lt "${warn_value}" ]; then echo "UPS WARNING - Battery charge: ${battcharge}% < ${warn_value}%" exit ${state_warning} else echo "UPS OK - Battery charge: ${battcharge}%" exit ${state_ok} fi ;; timeleft) checkargs get_timeleft if [ "${timeleft}" -lt "${crit_value}" ]; then echo "UPS CRITICAL - Time left: ${timeleft} mins < ${crit_value} mins" exit ${state_critical} elif [ "${timeleft}" -lt "${warn_value}" ]; then echo "UPS WARNING - Time left: ${timeleft} mins < ${warn_value} mins" exit ${state_warning} else echo "UPS OK - Time left: ${timeleft} mins" exit ${state_ok} fi ;; status) get_status if [ "${status}" != 'ONLINE' ]; then echo "UPS CRITICAL - ${status}" exit ${state_critical} else echo "UPS OK - ${status}" exit ${state_ok} fi ;; *) usage exit ${state_unknown} ;; esac ## should never reach this echo "ERROR - there's an error in the matrix!" exit ${state_unknown}