Skip to content

Instantly share code, notes, and snippets.

@mwender
Last active January 17, 2025 15:35
Show Gist options
  • Save mwender/4a980ede589864ac10ee77ea2e81f353 to your computer and use it in GitHub Desktop.
Save mwender/4a980ede589864ac10ee77ea2e81f353 to your computer and use it in GitHub Desktop.
[Restart REDIS] Checks if REDIS is running and restarts if it isn't. #bash #REDIS #servers
#!/bin/bash
# Script to monitor and manage a Redis server.
# This script checks if Redis is running, executes a FLUSHALL command if it's down,
# sends email notifications, and rotates log files based on a specified age.
# Required Software:
# - redis-cli: Command-line interface for Redis.
# - sendmail: For sending email notifications.
# - awk: For processing log files.
# Ensure these are installed and accessible on the server.
# Usage:
# - This script should be run by a user with sudo access to execute Redis commands.
# - Configure the script by setting the appropriate variables below.
# Cron Setup:
# To run this script periodically, add a cron job. For example, to run every hour:
# 0 * * * * /path/to/restart-redis.sh
# Configuration
REDIS_CLI="/usr/bin/redis-cli" # Path to redis-cli
EMAIL="[email protected]" # Recipient email address
SERVER="web1.example.com" # Name of the server
FLUSH_COMMAND="FLUSHALL"
SENDER="[email protected]" # Sender's email address
SEND_NOTIFICATION=true # Boolean for sending notifications
LOG_FILE="/var/log/redis_check.log" # Log file path
LOG_AGE=2 # Number of days to keep log entries
MEMORY_USAGE_THRESHOLD=75 # Percentage of maxmemory to trigger FLUSHALL
# Function to rotate logs
rotate_logs() {
# Use awk to filter out log entries older than LOG_AGE days
awk -v date="$(date --date="$LOG_AGE days ago" '+%Y-%m-%d')" '$1 >= date' $LOG_FILE > "${LOG_FILE}.tmp" && mv "${LOG_FILE}.tmp" $LOG_FILE
}
# Function to check memory usage
check_memory_usage() {
local used_memory max_memory memory_usage_percentage
used_memory=$($REDIS_CLI INFO memory | grep used_memory: | awk -F: '{print $2}')
max_memory=$($REDIS_CLI INFO memory | grep maxmemory: | awk -F: '{print $2}')
# If maxmemory is set to 0, it means no limit is set, so we skip the check
if [ "$max_memory" -eq 0 ]; then
echo "No maxmemory limit set. Skipping memory usage check."
return 0
fi
# Calculate the percentage of used memory
memory_usage_percentage=$((used_memory * 100 / max_memory))
if [ "$memory_usage_percentage" -ge "$MEMORY_USAGE_THRESHOLD" ]; then
echo "Memory usage is high: $memory_usage_percentage% of maxmemory. Running FLUSHALL command..."
sudo $REDIS_CLI $FLUSH_COMMAND
SUBJECT="REDIS Memory Cleared [$SERVER]"
MESSAGE="Memory usage was high ($memory_usage_percentage% of maxmemory), and the FLUSHALL command has been executed on $(date '+%Y-%m-%d %H:%M:%S')."
return 1
fi
return 0
}
# Check if Redis is running
if ! $REDIS_CLI PING | grep -q "PONG"; then
echo "REDIS is down. Running FLUSHALL command..."
sudo $REDIS_CLI $FLUSH_COMMAND
SUBJECT="REDIS Restarted [$SERVER]"
MESSAGE="REDIS was down, and the FLUSHALL command has been executed on $(date '+%Y-%m-%d %H:%M:%S')."
if [ "$SEND_NOTIFICATION" = false ]; then
SEND_NOTIFICATION=true # Ensure we send a notification when restarting REDIS
fi
else
# Check memory usage
if ! check_memory_usage; then
SEND_NOTIFICATION=true
else
SUBJECT="REDIS is Running [$SERVER]"
MESSAGE="REDIS was found to be running on $(date '+%Y-%m-%d %H:%M:%S')."
echo "Redis is running."
fi
fi
if [ "$SEND_NOTIFICATION" = true ]; then
{
echo "Subject: $SUBJECT"
echo "From: $SENDER"
echo "To: $EMAIL"
echo ""
echo "$MESSAGE"
} | /usr/sbin/sendmail -t
fi
# Rotate logs
rotate_logs
echo "$(date '+%Y-%m-%d %H:%M:%S') - Redis check executed." >> $LOG_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment