Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # Monitors working status of two Pi-Hole devices
- # Triggers a popup window on host if one or both aren't working
- # Intended to run every 30 minutes via crontab
- # Add something like this to crontab: 1,31 * * * * /home/stephen/.local/bin/pihole_check.sh
- # Define the IP addresses or hostnames of the devices you want to ping
- PIHOLE1="10.0.0.7"
- PIHOLE2="10.0.0.8"
- LOG_FILE="/home/stephen/ping_log.txt"
- MAX_LOG_SIZE=12288 # 12KB
- TRUNCATE_SIZE=10240 # 10KB
- # Function to ping a device, check if it's reachable, and log the status
- ping_and_log() {
- device_ip="$1"
- if ping -c 1 "$device_ip" > /dev/null 2>&1; then
- echo "$(date): $device_ip is reachable." >> "$LOG_FILE"
- else
- echo "$(date): $device_ip is not reachable." >> "$LOG_FILE"
- return 1
- fi
- }
- # Ping both Pi-Hole devices and log their status
- if ! ping_and_log "$PIHOLE1" || ! ping_and_log "$PIHOLE2"; then
- # If any of the devices don't respond, display a warning window
- zenity --warning --text="At least one of the devices ($PIHOLE1 or $PIHOLE2) is not responding." --display=:0.0
- fi
- # Check and truncate the log file if its size exceeds 12KB
- log_size=$(stat -c %s "$LOG_FILE" 2>/dev/null || echo 0)
- if [ "$log_size" -gt "$MAX_LOG_SIZE" ]; then
- echo "Truncating log file..."
- tail -c "$TRUNCATE_SIZE" "$LOG_FILE" > "${LOG_FILE}.tmp" && mv "${LOG_FILE}.tmp" "$LOG_FILE"
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement