Advertisement
smeech

Monitor working status of two Pi-Hole

Jun 8th, 2024 (edited)
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.40 KB | Source Code | 0 0
  1. #!/bin/sh
  2.  
  3. # Monitors working status of two Pi-Hole devices
  4. # Triggers a popup window on host if one or both aren't working
  5. # Intended to run every 30 minutes via crontab
  6. # Add something like this to crontab: 1,31 * * * * /home/stephen/.local/bin/pihole_check.sh
  7.  
  8. # Define the IP addresses or hostnames of the devices you want to ping
  9. PIHOLE1="10.0.0.7"
  10. PIHOLE2="10.0.0.8"
  11. LOG_FILE="/home/stephen/ping_log.txt"
  12. MAX_LOG_SIZE=12288  # 12KB
  13. TRUNCATE_SIZE=10240  # 10KB
  14.  
  15. # Function to ping a device, check if it's reachable, and log the status
  16. ping_and_log() {
  17.     device_ip="$1"
  18.     if ping -c 1 "$device_ip" > /dev/null 2>&1; then
  19.         echo "$(date): $device_ip is reachable." >> "$LOG_FILE"
  20.     else
  21.         echo "$(date): $device_ip is not reachable." >> "$LOG_FILE"
  22.         return 1
  23.     fi
  24. }
  25.  
  26. # Ping both Pi-Hole devices and log their status
  27. if ! ping_and_log "$PIHOLE1" || ! ping_and_log "$PIHOLE2"; then
  28.     # If any of the devices don't respond, display a warning window
  29.     zenity --warning --text="At least one of the devices ($PIHOLE1 or $PIHOLE2) is not responding." --display=:0.0
  30. fi
  31.  
  32. # Check and truncate the log file if its size exceeds 12KB
  33. log_size=$(stat -c %s "$LOG_FILE" 2>/dev/null || echo 0)
  34. if [ "$log_size" -gt "$MAX_LOG_SIZE" ]; then
  35.     echo "Truncating log file..."
  36.     tail -c "$TRUNCATE_SIZE" "$LOG_FILE" > "${LOG_FILE}.tmp" && mv "${LOG_FILE}.tmp" "$LOG_FILE"
  37. fi
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement