#!/bin/bash # Nagios monitor script to check current apache vitals # Liam - 12/04/07 UTILS=/usr/lib/nagios/plugins/utils.sh # utils.sh if [ -f $UTILS ]; then . $UTILS; else echo "Error: utils.sh not found" && exit 3; fi VERSION=1.0.8 # -------------------------------------- # Functions # requests function requests() { check # Obtain data from /server-status REQUESTS=`curl -s $HOST/server-status | grep 'processed' | head -n 1 | awk -F \> '{ print $2 }' | awk '{ print $1 }'` if [[ $REQUESTS =~ ^[-+]?[0-9]+\(\.[0-9]+\)?$ || $REQUESTS = "" ]]; then echo "Error: Unreadable output from $HOST/server-status - check that it is accessible" exit 3 fi if [[ $REQUESTS -gt $CRITICAL ]] then echo "CRITICAL: Current requests ($REQUESTS) exceeds threshold of $CRITICAL" exit $STATE_CRITICAL elif [[ $REQUESTS -gt $WARNING ]] then echo "WARNING: Current requests ($REQUESTS) exceeds threshold of $WARNING" exit $STATE_WARNING else echo "Current requests: $REQUESTS" exit $STATE_OK fi } # idle worker function idle() { check # Obtain data from /server-status IDLE=`curl -s $HOST/server-status | grep 'idle' | head -n 1 | awk '{ print $6 }'` if [[ $IDLE =~ ^[-+]?[0-9]+\(\.[0-9]+\)?$ || $IDLE = "" ]]; then echo "Error: Unreadable output from $HOST/server-status - check that it is accessible" exit 3 fi if [[ $IDLE -lt $CRITICAL ]] then echo "CRITICAL: Current idle workers ($IDLE) falls below threshold of $CRITICAL" exit $STATE_CRITICAL elif [[ $IDLE -lt $WARNING ]] then echo "WARNING: Current idle workers ($IDLE) falls below threshold of $WARNING" exit $STATE_WARNING else echo "Current idle workers: $IDLE" exit $STATE_OK fi } # requests per second function rps() { check # Obtain data from /server-status RPS=`curl -s $HOST/server-status | grep 'kB' | head -n 1 | awk -F \> '{ print $2 }' | awk '{ print $1 }'` if [[ $RPS =~ ^[-+]?[0-9]+\(\.[0-9]+\)?$ || $IDLE = "" ]]; then : ; else echo $RPS - invalid echo "Error: Unreadable output from $HOST/server-status - check that it is accessible" exit 3 fi if [[ $RPS -gt $CRITICAL ]] then echo "CRITICAL: Requests per second ($RPS) exceeds threshold of $CRITICAL" exit $STATE_CRITICAL elif [[ $RPS -gt $WARNING ]] then echo "WARNING: Requests per second ($RPS) exceeds threshold of $WARNING" exit $STATE_WARNING else echo "Requests per second: $RPS" exit $STATE_OK fi } # data throughput function data() { check # Obtain data from /server-status DATA=`curl -s $HOST/server-status | grep 'kB' | head -n 1 | awk -F \> '{ print $2 }' | awk '{ print $4 }'` if [[ $DATA =~ ^[-+]?[0-9]+\(\.[0-9]+\)?$ || $DATA = "" ]]; then echo "Error: Unreadable output from $HOST/server-status - check that it is accessible" exit 3 fi if [[ $DATA -gt $CRITICAL ]] then echo "CRITICAL: Data throughput ($DATA MB/s) exceeds threshold of $CRITICAL" exit $STATE_CRITICAL elif [[ $DATA -gt $WARNING ]] then echo "WARNING: Data throughput ($DATA MB/s) exceeds threshold of $WARNING" exit $STATE_WARNING else echo "Data throughput: $DATA MB/s" exit $STATE_OK fi } usage() { print_revision cat << EOF `basename $0` version $VERSION Usage: `basename $0` -H host [-W warn] [-C crit] [-r -i -s -d] POSIX options: -H hostname Required: Hostname or IP of server -W [warning] Required: Warning level - numerical -C [critical] Required: Critical level - numerical Function toggles: (use only one) -r Requests the server is currently processing -i Idle worker threads -s Requests served per second -d Data throughput of server Example: `basename $0` -H httpd.apache.org -W 500 -C 750 -r EOF exit 3 } check() { # Check to see the vitals have been provided if [[ ! $HOST || ! $WARNING || ! $CRITICAL ]] then echo "Error: Vital information omitted" echo usage fi # Test that the host is reachable ping -c 1 $HOST &> /dev/null if [ $? -gt 0 ] then echo "Error: Host $HOST is unreachable" exit 3 fi } # -------------------------------------- if [[ $@ = "" ]]; then usage fi x=1 while getopts "H:W:C:risd" opt; do case "$opt" in H) # Host to check HOST="$OPTARG" ;; W) # Warning level WARNING="$OPTARG" ;; C) # Critical level CRITICAL="$OPTARG" ;; r) # Requests currently being processed requests ;; i) # Idle worker processes idle ;; s) # Requests per second rps ;; d) # Data throughput data ;; *) # Usage usage ;; esac x=$OPTIND done shift $((x-1))