Long time no see. Sorry for the long downtime, but I’ve been on vacation for quite some time and spent my time out of reach of any electronical device except my phone. Anyway, strength and motivation for work during spare time is back again! Expect a few updates of various plugins I’ve written in the past within the next weeks.
For the beginning here’s the revised version of the Apache check plugin for Nagios. It now doesn’t write temporary files anymore, the code is cleaner and it should be a bit faster. Note that I haven’t done much testing for now (will be done tomorrow and on Wednesday). Therefore it might won’t work out of the box for you. At least in my environments it works well. Let me know if you have any problems or if you stumble upon something which is completely nonsense.
Copy’n'paste below or svn co via svn.matejunkie.com/nagios-plugins/trunk/check_apache/.
#!/bin/sh
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
PROGNAME=`basename $0`
VERSION="Version 1.3,"
AUTHOR="2009, Mike Adolphs (http://www.matejunkie.com/)"
print_version() {
echo "$VERSION $AUTHOR"
}
print_help() {
print_version $PROGNAME $VERSION
echo ""
echo "Description:"
echo "$PROGNAME is a Nagios plugin to check the Apache's server status."
echo "It monitors requests per second, bytes per second/request, "
echo "amount of busy/idle workers and its CPU load."
echo ""
echo "Example call:"
echo "./$PROGNAME -H localhost -P 80 -t 3 -b /usr/sbin -p /var/run \\"
echo "-n apache2.pid -s status_page [-S] [-R] [-wr] 100 [-cr] 250"
echo ""
echo "Options:"
echo " -H|--hostname)"
echo " Sets the hostname. Default is: localhost"
echo " -P|--port)"
echo " Sets the port. Default is: 80"
echo " -t|--timeout)"
echo " Sets a timeout within the server's status page must've been"
echo " accessed. Otherwise the check will go into an error state."
echo " Default is: 3"
echo " -b|--binary-path)"
echo " Sets the path to the apache binary. Used for getting Apache's"
echo " CPU load. Default is: /usr/sbin"
echo " -p|--pid-path)"
echo " Path to Apache's pid file. Default is: /var/run"
echo " -n|--pid-name)"
echo " Name of Apache's pid file. Default is: apache2.pid"
echo " -s|--status-page)"
echo " Defines the name of the status page. Default is: server-status"
echo " -R|--remote-server)"
echo " Disabled the pid check so that remote Apaches can be queried."
echo " Default is: off"
echo " -S|--secure)"
echo " Enables HTTPS (no certificate check though). Default is: off"
echo " -wr|--warning-req)"
echo " Sets a warning level for requests per second. Default is: off"
echo " -cr|--critical-req)"
echo " Sets a critical level for requests per second. Default is: off"
exit $ST_UK
}
ST_OK=0
ST_WR=1
ST_CR=2
ST_UK=3
hostname="localhost"
port=80
remote_srv=0
path_binary="/usr/sbin"
path_pid="/var/run"
name_pid="apache2.pid"
status_page="server-status"
timeout=3
secure=0
running=0
wcdiff_req=0
wclvls_req=0
while test -n "$1"; do
case "$1" in
--help|-h)
print_help
exit $ST_UK
;;
--version|-v)
print_version $PROGNAME $VERSION
exit $ST_UK
;;
--hostname|-H)
hostname=$2
shift
;;
--port|-P)
port=$2
shift
;;
--timeout|-t)
timeout=$2
shift
;;
--remote-server|-R)
remote_srv=1
;;
--binary_path|-b)
path_binary=$2
shift
;;
--pid_path|-p)
path_pid=$2
shift
;;
--pid_name|-n)
name_pid=$2
shift
;;
--status-page|-s)
status_page=$2
shift
;;
--secure|-S)
secure=1
;;
--warning-req|-wr)
warn_req=$2
shift
;;
--critical-req|-cr)
crit_req=$2
shift
;;
*)
echo "Unknown argument: $1"
print_help
exit $ST_UK
;;
esac
shift
done
# check functions
val_wcdiff_req() {
if [ ! -z "$warn_req" -a ! -z "$crit_req" ]
then
wclvls_req=1
if [ ${warn_req} -gt ${crit_req} ]
then
wcdiff_req=1
fi
elif [ ! -z "$warn_req" -a -z "$crit_req" ]
then
wcdiff_req=2
elif [ -z "$warn_req" -a ! -z "$crit_req" ]
then
wcdiff_req=3
fi
}
check_pid() {
if [ -f "$path_pid/$name_pid" ]
then
retval=0
else
retval=1
fi
}
check_processes() {
if [ $1 -lt 1 ]
then
echo "UNKNOWN - Your Apache server seems not to run. Is your Nagios \
privileged to run 'ps ax' and is the Apache2 binary really located in \
$path_binary?"
exit $ST_UK
fi
}
check_output() {
stat_output=`stat -c %s ${output_dir}/server-status`
if [ "$stat_output" = 0 ]
then
echo "UNKNOWN - Local copy of server-status is empty. Are we \
allowed to access http://${hostname}:${port}/server-status?"
exit $ST_UK
fi
}
# get functions
get_status() {
if [ "$secure" = 1 ]
then
server_status1=`wget -qO- --no-check-certificate -t 3 \
-T ${timeout} https://${hostname}:${port}/${status_page}?auto`
sleep 1
server_status2=`wget -qO- --no-check-certificate -t 3 \
-T ${timeout} https://${hostname}:${port}/${status_page}?auto`
else
server_status1=`wget -qO- -t 3 -T ${timeout} \
http://${hostname}:${port}/${status_page}?auto`
sleep 1
server_status2=`wget -qO- -t 3 -T ${timeout} \
http://${hostname}:${port}/${status_page}?auto`
fi
}
get_vals() {
cpu_load="$(cpu_load=0; ps -Ao pcpu,args | grep "$path_binary/apache2" \
| awk '{print $1}' | while read line
do
cpu_load=`echo "scale=3; $cpu_load + $line" | bc -l`
echo $cpu_load
done)"
cpu_load=`echo $cpu_load | awk '{print $NF}' | sed 's/^\./0./'`
tmp1_req_psec=`echo ${server_status1} | awk '{print $3}'`
tmp2_req_psec=`echo ${server_status2} | awk '{print $3}'`
req_psec=`echo "scale=2; ${tmp2_req_psec} - ${tmp1_req_psec}" | bc -l \
| sed 's/^\./0./'`
bytes_psec=`echo ${server_status1} | awk '{print $14}' | sed 's/^\./0./'`
bytes_preq=`echo ${server_status1} | awk '{print $16}' | sed 's/^\./0./'`
wkrs_busy=`echo ${server_status1} | awk '{print $18}' | sed 's/^\./0./'`
wkrs_idle=`echo ${server_status1} | awk '{print $20}' | sed 's/^\./0./'`
}
do_output() {
output="Apache serves $req_psec Requests per second with an average CPU \
utilization of $cpu_load%. Busy workers: $wkrs_busy, idle: $wkrs_idle"
}
do_perfdata() {
perfdata="'cpu_load'=$cpu_load 'req_psec'=$req_psec \
'bytes_psec'=$bytes_psec 'bytes_preq'=$bytes_preq 'workers_busy'=$wkrs_busy \
'workers_idle'=$wkrs_idle"
}
# Let's go
val_wcdiff_req
if [ "$wcdiff_req" = 1 ]
then
echo "Please adjust your warning/critical thresholds. The warning must \
be lower than the critical level!"
exit $ST_UK
elif [ "$wcdiff_req" = 2 ]
then
echo "Please also set a critical value when you want to use \
warning/critical thresholds!"
exit $ST_UK
elif [ "$wcdiff_req" = 3 ]
then
echo "Please also set a warning value when you want to use \
warning/critical thresholds!"
exit $ST_UK
else
if [ "$remote_srv" = 0 ]
then
running=`check_pid`
check_pid $running
fi
get_status
get_vals
do_output
do_perfdata
if [ ${wclvls_req} = 1 ]
then
if [ ${req_psec} -ge ${warn_req} -a ${req_psec} -lt ${crit_req} ]
then
echo "WARNING - ${output} | ${perfdata}"
exit $ST_WR
elif [ ${req_psec} -ge ${crit_req} ]
then
echo "CRITICAL - ${output} | ${perfdata}"
exit $ST_CR
else
echo "OK - ${output} | ${perfdata}"
exit $ST_OK
fi
else
echo "OK - ${output} | ${perfdata}"
exit $ST_OK
fi
fi
On a Slackware System it should be $path_binary/httpd instead of $path_binary/apache2
Correct or incorrect path to pid file makes no difference somehow for me. ;)
Hi!
have a roblem with your plugin : (
have changed parameers insite, they are rigt,get illegaƶ charter errors
[root@localhost libexec]# ./check_apache2.sh
(standard_in) 1: illegal character: I
(standard_in) 1: illegal character: W
(standard_in) 1: parse error
(standard_in) 1: illegal character: :
(standard_in) 1: illegal character: I
(standard_in) 1: illegal character: W
(standard_in) 1: parse error
(standard_in) 1: illegal character: :
OK – Apache serves Requests per second with an average CPU utilization of 0%. Busy workers: , idle: | ‘cpu_load’=0 ‘req_psec’= ‘bytes_psec’= ‘bytes_preq’= ‘workers_busy’= ‘workers_idle’=
Thank you in advance!
Hi,
thank-you first for your plugin.
I have the same error (“(standard_in) 1: illegal character: I …”) without any change into your code.
FYI the System is an Ubuntu 9.10 Workstation 32 bits on VirtualBox.
Any idea ? Thank-you in advance, Christophe.
Please check whether bc is installed. Simply “sudo aptitude install bc” and everything should be fine.
If not, please do an “sh -x check_apache2.sh” and mail me its output. Thanks in advance and sorry for the late response Anton.
I’m getting the same error with regards to the illegal character. It’s a CentOS5 box. I have bc installed.
Here is the output of the script.
[root@FDT-CLEMGT031 libexec]# sh -x ./check_apache2.sh -H 10.1.10.33 -P 80 -t 10 -b /usr/local/apache2/bin -p /usr/local/apache2/logs -n httpd.pid -s server-status
++ basename ./check_apache2.sh
+ PROGNAME=check_apache2.sh
+ VERSION=’Version 1.3,’
+ AUTHOR=’2009, Mike Adolphs (http://www.matejunkie.com/)‘
+ ST_OK=0
+ ST_WR=1
+ ST_CR=2
+ ST_UK=3
+ hostname=localhost
+ port=80
+ remote_srv=0
+ path_binary=/usr/sbin
+ path_pid=/var/run
+ name_pid=apache2.pid
+ status_page=server-status
+ timeout=3
+ secure=0
+ running=0
+ wcdiff_req=0
+ wclvls_req=0
+ test -n -H
+ case “$1″ in
+ hostname=10.1.10.33
+ shift
+ shift
+ test -n -P
+ case “$1″ in
+ port=80
+ shift
+ shift
+ test -n -t
+ case “$1″ in
+ timeout=10
+ shift
+ shift
+ test -n -b
+ case “$1″ in
+ path_binary=/usr/local/apache2/bin
+ shift
+ shift
+ test -n -p
+ case “$1″ in
+ path_pid=/usr/local/apache2/logs
+ shift
+ shift
+ test -n -n
+ case “$1″ in
+ name_pid=httpd.pid
+ shift
+ shift
+ test -n -s
+ case “$1″ in
+ status_page=server-status
+ shift
+ shift
+ test -n ”
+ val_wcdiff_req
+ ‘[' '!' -z '' -a '!' -z '' ']‘
+ ‘[' '!' -z '' -a -z '' ']‘
+ ‘[' -z '' -a '!' -z '' ']‘
+ ‘[' 0 = 1 ']‘
+ ‘[' 0 = 2 ']‘
+ ‘[' 0 = 3 ']‘
+ ‘[' 0 = 0 ']‘
++ check_pid
++ ‘[' -f /usr/local/apache2/logs/httpd.pid ']‘
++ retval=0
+ running=
+ check_pid
+ ‘[' -f /usr/local/apache2/logs/httpd.pid ']‘
+ retval=0
+ get_status
+ ‘[' 0 = 1 ']‘
++ wget -qO- -t 3 -T 10 ‘http://10.1.10.33:80/server-status?auto’
+ server_status1=’BusyWorkers: 1
IdleWorkers: 7
Scoreboard: _______W…………………………………………………………………………………………………………………………………………………………………………………………………………………………..’
+ sleep 1
++ wget -qO- -t 3 -T 10 ‘http://10.1.10.33:80/server-status?auto’
+ server_status2=’BusyWorkers: 1
IdleWorkers: 7
Scoreboard: _W______…………………………………………………………………………………………………………………………………………………………………………………………………………………………..’
+ get_vals
++ cpu_load=0
++ ps -Ao pcpu,args
++ grep /usr/local/apache2/bin/httpd
++ awk ‘{print $1}’
++ read line
+ cpu_load=
++ echo
++ awk ‘{print $NF}’
++ sed ‘s/^\./0./’
+ cpu_load=
++ echo BusyWorkers: 1 IdleWorkers: 7 Scoreboard: _______W…………………………………………………………………………………………………………………………………………………………………………………………………………………………..
++ awk ‘{print $3}’
+ tmp1_req_psec=IdleWorkers:
++ echo BusyWorkers: 1 IdleWorkers: 7 Scoreboard: _W______…………………………………………………………………………………………………………………………………………………………………………………………………………………………..
++ awk ‘{print $3}’
+ tmp2_req_psec=IdleWorkers:
++ echo ‘scale=2; IdleWorkers: – IdleWorkers:’
++ bc -l
(standard_in) 1: illegal character: I
(standard_in) 1: illegal character: W
(standard_in) 1: parse error
(standard_in) 1: illegal character: :
(standard_in) 1: illegal character: I
(standard_in) 1: illegal character: W
(standard_in) 1: parse error
(standard_in) 1: illegal character: :
++ sed ‘s/^\./0./’
+ req_psec=
++ echo BusyWorkers: 1 IdleWorkers: 7 Scoreboard: _______W…………………………………………………………………………………………………………………………………………………………………………………………………………………………..
++ awk ‘{print $14}’
++ sed ‘s/^\./0./’
+ bytes_psec=
++ awk ‘{print $16}’
++ sed ‘s/^\./0./’
++ echo BusyWorkers: 1 IdleWorkers: 7 Scoreboard: _______W…………………………………………………………………………………………………………………………………………………………………………………………………………………………..
+ bytes_preq=
++ echo BusyWorkers: 1 IdleWorkers: 7 Scoreboard: _______W…………………………………………………………………………………………………………………………………………………………………………………………………………………………..
++ awk ‘{print $18}’
++ sed ‘s/^\./0./’
+ wkrs_busy=
++ echo BusyWorkers: 1 IdleWorkers: 7 Scoreboard: _______W…………………………………………………………………………………………………………………………………………………………………………………………………………………………..
++ awk ‘{print $20}’
++ sed ‘s/^\./0./’
+ wkrs_idle=
+ do_output
+ output=’Apache serves Requests per second with an average CPU utilization of %. Busy workers: , idle: ‘
+ do_perfdata
+ perfdata=”\”cpu_load’\”= ‘\”req_psec’\”= ‘\”bytes_psec’\”= ‘\”bytes_preq’\”= ‘\”workers_busy’\”= ‘\”workers_idle’\”=’
+ ‘[' 0 = 1 ']‘
+ echo ‘OK – Apache serves Requests per second with an average CPU utilization of %. Busy workers: , idle: | ‘\”cpu_load’\”= ‘\”req_psec’\”= ‘\”bytes_psec’\”= ‘\”bytes_preq’\”= ‘\”workers_busy’\”= ‘\”workers_idle’\”=’
OK – Apache serves Requests per second with an average CPU utilization of %. Busy workers: , idle: | ‘cpu_load’= ‘req_psec’= ‘bytes_psec’= ‘bytes_preq’= ‘workers_busy’= ‘workers_idle’=
+ exit 0
[root@FDT-CLEMGT031 libexec]#
Nevermind, I figured it out. I had to put ExtendedStatus On in my httpd.conf. This should help the others with this issue.
Thanks,
Jason
Hi I’m getting something of a similar problem. I get the following error when running : ./check_apache2.sh -H xx.xx.xx -P 80 -t 3 -b /usr/sbin -p /var/run -n httpd.pid -s server-status -S -R -wr 100 -cr 250
(standard_in) 2: parse error
./check_apache2.sh: line 277: [: too many arguments
./check_apache2.sh: line 281: [: -ge: unary operator expected
OK - Apache serves Requests per second with an average CPU utilization of 0%. Busy workers: , idle: | 'cpu_load'=0 'req_psec'= 'bytes_psec'= 'bytes_preq'= 'workers_busy'= 'workers_idle'=
The output from an sh -x is as follows:
++ basename ./check_apache2.sh
+ PROGNAME=check_apache2.sh
+ VERSION='Version 1.3,'
+ AUTHOR='2009, Mike Adolphs (http://www.matejunkie.com/)'
+ ST_OK=0
+ ST_WR=1
+ ST_CR=2
+ ST_UK=3
+ hostname=localhost
+ port=80
+ remote_srv=0
+ path_binary=/usr/sbin
+ path_pid=/var/run
+ name_pid=apache2.pid
+ status_page=server-status
+ timeout=3
+ secure=0
+ running=0
+ wcdiff_req=0
+ wclvls_req=0
+ test -n -H
+ case "$1" in
+ hostname=xx.xx.xx
+ shift
+ shift
+ test -n -P
+ case "$1" in
+ port=80
+ shift
+ shift
+ test -n -t
+ case "$1" in
+ timeout=3
+ shift
+ shift
+ test -n -b
+ case "$1" in
+ path_binary=/usr/sbin
+ shift
+ shift
+ test -n -p
+ case "$1" in
+ path_pid=/var/run
+ shift
+ shift
+ test -n -n
+ case "$1" in
+ name_pid=httpd.pid
+ shift
+ shift
+ test -n -s
+ case "$1" in
+ status_page=server-status
+ shift
+ shift
+ test -n -S
+ case "$1" in
+ secure=1
+ shift
+ test -n -R
+ case "$1" in
+ remote_srv=1
+ shift
+ test -n -wr
+ case "$1" in
+ warn_req=100
+ shift
+ shift
+ test -n -cr
+ case "$1" in
+ crit_req=250
+ shift
+ shift
+ test -n ''
+ val_wcdiff_req
+ '[' '!' -z 100 -a '!' -z 250 ']‘
+ wclvls_req=1
+ ‘[' 100 -gt 250 ']‘
+ ‘[' 0 = 1 ']‘
+ ‘[' 0 = 2 ']‘
+ ‘[' 0 = 3 ']‘
+ ‘[' 1 = 0 ']‘
+ get_status
+ ‘[' 1 = 1 ']‘
++ wget -qO- –no-check-certificate -t 3 -T 3 ‘https://xx.xx.xx:80/server-status?auto’
+ server_status1=
+ sleep 1
++ wget -qO- –no-check-certificate -t 3 -T 3 ‘https://xx.xx.xx:80/server-status?auto’
+ server_status2=
+ get_vals
++ cpu_load=0
++ ps -Ao pcpu,args
++ grep /usr/sbin/apache2
++ awk ‘{print $1}’
++ read line
+++ echo ‘scale=3; 0 + 0.0′
+++ bc -l
++ cpu_load=0
++ echo 0
++ read line
+ cpu_load=0
++ echo 0
++ awk ‘{print $NF}’
++ sed ‘s/^\./0./’
+ cpu_load=0
++ echo
++ awk ‘{print $3}’
+ tmp1_req_psec=
++ echo
++ awk ‘{print $3}’
+ tmp2_req_psec=
++ echo ‘scale=2; – ‘
++ bc -l
++ sed ‘s/^\./0./’
(standard_in) 2: parse error
+ req_psec=
++ echo
++ sed ‘s/^\./0./’
++ awk ‘{print $14}’
+ bytes_psec=
++ echo
++ awk ‘{print $16}’
++ sed ‘s/^\./0./’
+ bytes_preq=
++ echo
++ awk ‘{print $18}’
++ sed ‘s/^\./0./’
+ wkrs_busy=
++ echo
++ awk ‘{print $20}’
++ sed ‘s/^\./0./’
+ wkrs_idle=
+ do_output
+ output=’Apache serves Requests per second with an average CPU utilization of 0%. Busy workers: , idle: ‘
+ do_perfdata
+ perfdata=”\”cpu_load’\”=0 ‘\”req_psec’\”= ‘\”bytes_psec’\”= ‘\”bytes_preq’\”= ‘\”workers_busy’\”= ‘\”workers_idle’\”=’
+ ‘[' 1 = 1 ']‘
+ ‘[' -ge 100 -a -lt 250 ']‘
./check_apache2.sh: line 277: [: too many arguments
+ '[' -ge 250 ']‘
./check_apache2.sh: line 281: [: -ge: unary operator expected
+ echo ‘OK – Apache serves Requests per second with an average CPU utilization of 0%. Busy workers: , idle: | ‘\”cpu_load’\”=0 ‘\”req_psec’\”= ‘\”bytes_psec’\”= ‘\”bytes_preq’\”= ‘\”workers_busy’\”= ‘\”workers_idle’\”=’
+ exit 0
following should set for httpd.conf:
ExtendedStatus On
SetHandler server-status
Order deny,allow
Deny from all
Allow from 127.0.0.1