Redis Sentinel — alert when master switched
Sentinel performed a failover at 3 AM and nobody noticed. We want a notification the moment the master flips, not next week.
Recipe
#!/usr/bin/env bash
# Tracks the current Sentinel-elected master and screams when it changes.
SENTINEL="${SENTINEL:-127.0.0.1:26379}"
NAME="${SENTINEL_MASTER_NAME:-mymaster}"
STATE_FILE="${STATE_FILE:-/var/lib/enterno/redis-master.txt}"
mkdir -p "$(dirname "$STATE_FILE")"
CUR=$(redis-cli -h "${SENTINEL%:*}" -p "${SENTINEL#*:}" \
sentinel get-master-addr-by-name "$NAME" | tr '\n' ':' | sed 's/:$//')
[ -z "$CUR" ] && { echo "no-sentinel"; exit 1; }
PREV=$(cat "$STATE_FILE" 2>/dev/null || echo "")
echo "$CUR" > "$STATE_FILE"
if [ -n "$PREV" ] && [ "$PREV" != "$CUR" ]; then
echo "failover $PREV -> $CUR"
exit 1
fi
echo "ok $CUR"
Same thing in Enterno.io
Cron every minute + an Enterno monitor with the "ok" keyword turns Sentinel failovers into a real-time page. Pair with a heartbeat on Sentinel itself — so you know Sentinel is up, not just Redis.
Related recipes
Redis slave is behind master — read-after-write returns stale data. No native alert, you need an external one.
Detect the moment a replica falls behind the primary by more than 10 seconds.
A replica-set secondary falls behind the primary; the app will read stale data within a minute. Want an HTTP endpoint that says "ok" or "lag".