#!/bin/bash # $Header: $ # vim:nu:ai:sw=4:ts=4: # # Nagios check script for services and processes # Created by Wolfram Schlich # Distributed under the terms of the GNU General Public License v3 # # External dependencies: # - nice # - pgrep (for process check) # ## ## 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##*/} " echo } function checkargs() { if [[ -z "${service}" ]]; then usage exit ${state_unknown} fi } ## ## main() ## service="${1}"; shift checkargs case "${service}" in ## check ALL enabled init scrpits rc:all) declare -a rc_started declare -a rc_stopped declare -i check_time=0 declare -i check_start=${SECONDS} if [[ -e /lib/librc.so ]]; then ## baselayout-2/OpenRC current_softlevel=$(rc-status -r) elif [[ -f /var/lib/init.d/softlevel ]]; then ## baselayout-1 current_softlevel=$(/dev/null; then rc_stopped=( ${rc_stopped[@]} ${rc_service} ) else rc_started=( ${rc_started[@]} ${rc_service} ) fi done declare -i check_finish=${SECONDS} let 'check_time = check_finish - check_start' if [ ${#rc_stopped[@]} -gt 0 ]; then echo "CRITICAL - rc scripts [${rc_stopped[@]}] STOPPED" exit ${state_critical} else echo "OK - all enabled rc scripts started (check took ${check_time}s)" exit ${state_ok} fi ;; ## check a single init script rc:*) rc_service=${service#rc:} rc_script=/etc/init.d/${rc_service} if [[ ! -x ${rc_script} ]]; then echo "rc script '${rc_service}' not executable" exit ${state_unknown} elif ! nice -n 19 ${rc_script} --quiet status &>/dev/null; then echo "CRITICAL - rc script '${rc_service}' STOPPED" exit ${state_critical} else echo "OK - rc script '${rc_service}' started" exit ${state_ok} fi ;; ## check for processes proc:*) proc=${service#proc:} if [[ ! -x "$(type -p pgrep 2>/dev/null)" ]]; then echo "ERROR - pgrep not executable" exit ${state_unknown} elif ! pgrep -fx "${proc}" &>/dev/null; then echo "ERROR - no process(es) running" exit ${state_critical} else echo "OK - '${proc}' running" exit ${state_ok} fi ;; ## unknown services *) echo "service '${service}' unknown/unsupported" exit ${state_unknown} ;; esac ## should never reach this echo "ERROR - there's an error in the matrix!" exit ${state_unknown}