#!/bin/sh
# -*- sh -*-
#
# Plugin to monitor the number of PHP processes on the machine.
#
# Copyright Khalid Baheyeldin 2009 http://2bits.com
#
# Parameters:
#
# config (required)
# autoconf (optional - used by munin-config)
#
# Magick markers (optional - used by munin-config and som installation
# scripts):
#%# family=manual
#%# capabilities=autoconf

if [ "$1" = "autoconf" ]; then
echo yes
exit 0
fi

if [ "$1" = "config" ]; then
  echo "graph_title PHP CGI Memory";
  echo "graph_vlabel PHP CGI Memory usage in GB";
  echo "graph_category processes";
  echo "graph_args -l 0";
  echo "php_cgi_ram.label PHP CGI Used RAM";
  echo "php_cgi_ram.draw LINE2";
  echo "php_cgi_processes.info Number of PHP CGI processes";
  echo "php_cgi_processes.label processes";
exit 0
fi

CMD_GREP=`which grep`
if [ ! -e ${CMD_GREP} ]; then
  echo "Command grep not found!"
  exit 1
fi
CMD_SED=`which sed`
if [ ! -e ${CMD_SED} ]; then
  echo "Command sed not found!"
  exit 1
fi
CMD_WC=`which wc`
if [ ! -e ${CMD_WC} ]; then
  echo "Command wc not found!"
  exit 1
fi
CMD_AWK=`which awk`
if [ ! -e ${CMD_AWK} ]; then
  echo "Command awk not found!"
  exit 1
fi
CMD_BC=`which bc`
if [ ! -e ${CMD_BC} ]; then
  echo "Command bc not found!"
  exit 1
fi

echo -n "php_cgi_processes.value "
ps ax | grep -i php-cgi | grep -v grep | wc -l | sed 's/\t +//' | sed 's/ *//'

MEMORY=0
for mem in `ps avx | grep -i php-cgi | grep -v grep | grep "Ss" | awk '{ print $7 }'`; do
  MEMORY=$(($MEMORY + $mem))
done
echo -n "php_cgi_ram.value "
echo "scale=4;${MEMORY}/1024/1024" | bc
