Skip to content

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

bash
#!/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.

Set up HTTP monitor → ← All recipes

Related recipes