PDA

View Full Version : Example shell script monitoring Apache cpu & memory


tberton
08-12-2008, 15:15
Here's an example script for monitoring Apache processes, output cpu and memory usage.


#!/bin/sh
PROCESSORS=`grep processor /proc/cpuinfo | wc -l`
APACHE_PID=`cat /var/run/apache2.pid`
PS_OUTPUT=`ps ho pcpu,pmem --ppid ${APACHE_PID} --pid ${APACHE_PID} | sed -e 's/[[:space:]]//' -e 's/[[:space:]]\+/,/g'`
CPU=0
MEMORY=0
for LINE in $PS_OUTPUT
do
PROCESS_CPU=`echo "${LINE}" | cut -f 1 -d ','`
PROCESS_MEMORY=`echo "${LINE}" | cut -f 2 -d ','`
CPU=`echo |awk "{print ${PROCESS_CPU} + ${CPU}}"`
MEMORY=`echo |awk "{print ${PROCESS_MEMORY} + ${MEMORY}}"`
done

CPU=`echo | awk "{print ${CPU}/${PROCESSORS}}"`
echo "${CPU},${MEMORY}"


Feel free to comment or make suggestions...

jeenam
20-12-2008, 00:31
This will calculate the number of processes for prefork apache. How about a script to monitor threads for httpd.worker?

dolorres
01-04-2009, 07:45
I made a generic UserParameter to calculate cpu and memory usage

here is for %cpu (the first argument will be the process you need info about)
UserParameter=proc.pcpu ,ps -C $1 -o pcpu= | awk '{cpu+=$$1}; END {print cpu}'

try it with zabbix_get -s mytesthost -k proc.pcpu[httpd]

and for memory, it is in Kb
UserParameter=proc.rssmem ,ps -C $1 -o rss= | awk '{mem+=$$1}; END {print mem}'

as for apache stats, apache status page could be used, i saw a post somewhere about it