Skip to content

Redis — alert on a high mem-fragmentation ratio

Redis is up but `mem_fragmentation_ratio > 1.5` — real RAM use is much bigger than what `used_memory_human` reports. OOM will hit suddenly.

Recipe

bash
#!/usr/bin/env bash
# /etc/cron.d/redis-frag
# */5 * * * * root /opt/redis-frag.sh

HOST=${REDIS_HOST:-127.0.0.1}
PORT=${REDIS_PORT:-6379}
THRESH=${THRESH:-1.5}                # alert above 1.5

INFO=$(redis-cli -h "$HOST" -p "$PORT" INFO memory)
RATIO=$(echo "$INFO" | awk -F: '/mem_fragmentation_ratio/{gsub(/\r/,"",$2); print $2}')
USED=$(echo "$INFO"  | awk -F: '/used_memory_human/{gsub(/\r/,"",$2); print $2}')

# Bash floats: use bc
if (( $(echo "$RATIO > $THRESH" | bc -l) )); then
  echo "redis-frag: ratio=$RATIO used=$USED (threshold $THRESH)"
  curl -fsS "$HEARTBEAT_URL" --data "ratio=$RATIO,used=$USED"
  exit 2
fi
echo "OK (ratio=$RATIO used=$USED)"

Same thing in Enterno.io

Wrap in an Enterno heartbeat — 30-day fragmentation trend lets you plan MEMORY PURGE or a restart a week before OOM.

Set up HTTP monitor → ← All recipes

Related recipes

long_query_time = 1, slow_query_log enabled. You need to know when the slow-query rate per minute suddenly jumps (a deploy broke an index, ORM went N+1).