codepot/sbin/codepot-mon-httpd

70 lines
1.2 KiB
Plaintext
Raw Normal View History

2016-01-20 04:06:51 +00:00
#!/bin/sh
#
# This is just a sample script for httpd resource monitoring
# While this is inside the codepot distro, it's not supposed
# to be used without consideration of the actual environment.
#
DAEMON=0
THRESHOLD=1024
if [ "$1" = "-daemon" ]
then
2016-01-22 07:51:02 +00:00
shift
($0 --daemon $@ </dev/null >/dev/null 2>&1 &) &
2016-01-20 04:06:51 +00:00
#($0 --daemon &) &
exit 0
elif [ "$1" = "--daemon" ]
then
DAEMON=1
fi
2016-01-22 07:51:02 +00:00
[ -n "$2" ] && THRESHOLD="$2"
exit_requested=no
2016-01-20 04:06:51 +00:00
trap 'exit_requested=yes' SIGINT SIGHUP SIGTERM
2016-01-22 07:51:02 +00:00
[ "${DAEMON}" = "1" ] && logger -t "$0" "Started (THRESHOLD=$THRESHOLD)"
while [ "${exit_requested}" = "no" ]
2016-01-20 04:06:51 +00:00
do
sleep 5
ps -ylC httpd | awk -vdaemon=$DAEMON -v"self=$0" -vthreshold=$THRESHOLD '
2016-01-22 07:51:02 +00:00
/httpd/ {
2016-01-20 04:06:51 +00:00
x += $8;
y += 1;
}
END {
2016-01-22 07:51:02 +00:00
if (y > 0)
2016-01-20 04:06:51 +00:00
{
2016-01-22 07:51:02 +00:00
MB = x / 1024;
AVGMB = x / (y * 1024);
2016-01-20 04:06:51 +00:00
2016-01-22 07:51:02 +00:00
if (daemon <= 0)
2016-01-20 04:06:51 +00:00
{
2016-01-22 07:51:02 +00:00
print "Apache Memory Usage (MB): " MB;
print "Average Proccess Size (MB): " AVGMB;
if (MB > int(threshold))
{
system ("/etc/init.d/httpd restart");
}
2016-01-20 04:06:51 +00:00
}
2016-01-22 07:51:02 +00:00
else
2016-01-20 04:06:51 +00:00
{
2016-01-22 07:51:02 +00:00
if (MB > int(threshold))
{
system (sprintf ("logger -t \"%s\" \"Restarting httpd for excess memory usage [%s/%d]\"", self, MB, threshold));
system ("/etc/init.d/httpd restart >/dev/null 2>&1 </dev/null");
}
2016-01-20 04:06:51 +00:00
}
}
}
'
done
[ "${DAEMON}" = "1" ] && logger -t "$0" "Exited"