Simple shell script to check APC (Alternative PHP Cache) status and alert in case of fragmentation

Here's a super simple shell script to check for APC cache fragmentation and send an alert email root. Depends on apc_info.php from http://code.google.com/p/munin-php-apc/.

#!/bin/bash
 
# ------------------
#  WHAT
# ------------------
# This script checks APC cache status. At the moment, just looks for
# fragmentation, if above 0, sends an alert email to root.
#
# This script depends on apc_info.php from
# http://code.google.com/p/munin-php-apc/
#
# ------------------
#  WHY
# ------------------
# APC cache fragmentation can cause a significant performance hit.
#
# ------------------
#  WHEN
# ------------------
# Can run this via crontab as often as desired. 30 min should be often
# enough for my needs.
#
# */30 *  * * *   root	/usr/local/bin/apc.check
 
apc_status=$(curl -s http://localhost/apc_info.php | sed 's/\ [a-z]/~&/g;s/~ /\
/g;')
 
fragmented=$(echo "$apc_status" | awk '/fragmented:/ {print$NF}')
 
if ( echo "$fragmented" | grep -q [1-9] ); then
  echo "$apc_status" | mail -s "APC cache fragmentation detected on $(hostname -s)" root
fi

Leave a comment

NOTE: Enclose quotes in <blockquote></blockquote>. Enclose code in <pre lang="LANG"></pre> (where LANG is one of these).