Created
May 21, 2012 22:53
-
-
Save arsenio/2765209 to your computer and use it in GitHub Desktop.
My plugin for checking a redis server (free mem, frag ratio, last save)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# 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; version 2 of the License only. | |
# | |
# 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` | |
AUTHOR="Arsenio Santos" | |
usage(){ | |
echo $PROGNAME $VERSION | |
echo | |
echo Check the local Redis instance | |
echo | |
echo OPTIONS: | |
echo -h Shows this help | |
echo -w sets the memory utilization warning level | |
echo -c sets the memory utilization critical level | |
echo -r sets the memory fragmentation ratio critical level | |
echo -l sets the last save time critical limit in seconds | |
} | |
MEMWARN=50 # 50% system memory | |
MEMCRIT=75 # 75% system memory | |
MEMRATIO=2.0 # 2.0 ratio | |
LASTSAVE=604800 # 1 week | |
while getopts "hvw:c:r:l:" opt; do | |
case $opt in | |
h) | |
usage | |
exit | |
;; | |
w) | |
MEMWARN=$OPTARG | |
;; | |
c) | |
MEMCRIT=$OPTARG | |
;; | |
r) | |
MEMRATIO=$OPTARG | |
;; | |
l) | |
LASTSAVE=$OPTARG | |
;; | |
\?) | |
usage | |
exit | |
;; | |
:) | |
echo "Option -$OPTARG requires an argument." >&2 | |
exit 1 | |
;; | |
esac | |
done | |
if [ ${MEMWARN} -gt ${MEMCRIT} ] | |
then | |
echo "Your warning level cannot be higher than your critical level." | |
exit 1 | |
fi | |
if [ "$MEMWARN" -lt 0 -o "$MEMWARN" -gt 100 -o "$MEMCRIT" -lt 0 -o "$MEMCRIT" -gt 100 ] | |
then | |
echo "Warning and critical levels must be integers between 0 and 100." | |
exit 2 | |
fi | |
STAT_MEM_FRAG_RATIO="null" | |
STAT_USED_MEMORY="null" | |
STAT_LAST_SAVE_TIME="null" | |
DATA=`redis-cli info` | |
for stat in $DATA | |
do | |
IFS=":" read -ra DATUM <<< "$stat" | |
case "${DATUM[0]}" in | |
mem_fragmentation_ratio) | |
STAT_MEM_FRAG_RATIO=`echo ${DATUM[1]} | sed -e 's/[^0-9\.]//g'` | |
;; | |
used_memory) | |
STAT_USED_MEMORY=`echo ${DATUM[1]} | sed -e 's/[^0-9\.]//g'` | |
;; | |
last_save_time) | |
STAT_LAST_SAVE_TIME=`echo ${DATUM[1]} | sed -e 's/[^0-9\.]//g'` | |
;; | |
esac | |
done | |
TOTAL_MEMORY=`free -b | grep Mem: | sed -e 's/^Mem: *\([0-9]*\) *.*$/\1/'` | |
STAT_MEM_PCT=$(echo "$STAT_USED_MEMORY*100.0/$TOTAL_MEMORY" | bc) | |
RATIO_EXCEEDED=$(echo "${STAT_MEM_FRAG_RATIO} > ${MEMRATIO}" | bc) | |
CURRENT_TIMESTAMP=`date +"%s"` | |
LAST_SAVE_AGE=$(echo "$CURRENT_TIMESTAMP - $STAT_LAST_SAVE_TIME" | bc) | |
if [ ${STAT_MEM_PCT} -ge ${MEMWARN} -a ${STAT_MEM_PCT} -lt ${MEMCRIT} ] | |
then | |
echo "WARNING - Used: $STAT_USED_MEMORY (${STAT_MEM_PCT}%) | Frag Ratio: $STAT_MEM_FRAG_RATIO, Last Save: $LAST_SAVE_AGE secs ago" | |
exit $ST_WR | |
elif [ ${STAT_MEM_PCT} -ge ${MEMCRIT} ] | |
then | |
echo "CRITICAL - Used: $STAT_USED_MEMORY (${STAT_MEM_PCT}%) | Frag Ratio: $STAT_MEM_FRAG_RATIO, Last Save: $LAST_SAVE_AGE secs ago" | |
exit $ST_CR | |
elif [ ${RATIO_EXCEEDED} -eq 1 ] | |
then | |
echo "CRITICAL - Frag Ratio: $STAT_MEM_FRAG_RATIO | Used: $STAT_USED_MEMORY (${STAT_MEM_PCT}%), Last Save: $LAST_SAVE_AGE secs ago" | |
exit $ST_CR | |
elif [ ${LAST_SAVE_AGE} -ge ${LASTSAVE} ] | |
then | |
echo "CRITICAL - Last Save: $LAST_SAVE_AGE secs ago | Used: $STAT_USED_MEMORY (${STAT_MEM_PCT}%), Frag Ratio: $STAT_MEM_FRAG_RATIO" | |
exit $ST_CR | |
else | |
echo "OK - Used: $STAT_USED_MEMORY (${STAT_MEM_PCT}%), Frag Ratio: $STAT_MEM_FRAG_RATIO, Last Save: $LAST_SAVE_AGE secs ago" | |
exit $ST_OK | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment