Purpose of the script
I wanted to see the reliability of my internet connection and to log in file that the internet is not working in case the ping stops working. The Ubuntu Internet monitoring script records every lost ping and in the event that there are three lost pings, it records that the limit has been reached.
At the beginning of the script there are variables that can be changed in order to use this script as best and as easily as possible. There is also define where the log file will be recorded.
The MAX_LOST_PINGS variable represents the number of lost pings, after which the following message will only be printed in the log:”We reach threshold of losing $MAX_LOST_PINGS ping in a row”
After the Internet is restored, the script writes the message “Ping to 8.8.8.8 is OK” in the log. The exact time of execution of the command is entered in the log.
Example of the output of internet moniroting script
Bash Script
nano /opt/scripts/check_internet.sh
To make it executable:
chmod +x /opt/scripts/check_internet.sh
Content of the file:
#!/bin/bash
PING_IP="8.8.8.8" # IP to ping
MAX_LOST_PINGS=3 # Number of lost pings before restarting service
LOG_FILE="/opt/scripts/log/check_internet_$(date +%Y-%m-%d).log" # Location for log file
echo "Check connectivity script is executed on $(date)" >> $LOG_FILE
lost_pings=0 # Define variable to enable tracking lost pings
# Loop indefinitely
while :
do
# Ping the IP once, wait for up to 1 second
if ping -c 1 -W 1 $PING_IP > /dev/null; then
echo "Ping to $PING_IP OK"
if [ $lost_pings -ge $MAX_LOST_PINGS ]; then
echo "$(date): Ping to $PING_IP OK" >> $LOG_FILE
fi
lost_pings=0
else
# Ping failed, log the date, time, and IP
if [ $lost_pings -lt $MAX_LOST_PINGS ]; then
echo "$(date): Ping to $PING_IP failed and it is $lost_pings" >> $LOG_FILE
fi
# Count the number of consecutive lost pings
lost_pings=$((lost_pings + 1))
# If the number of lost pings has reached the limit, restart the service
if [ $lost_pings -eq $MAX_LOST_PINGS ]; then
echo "$(date): We reach treshold of loosing $MAX_LOST_PINGS ping in a row" >> $LOG_FILE
fi
fi
# Wait for 3 seconds before pinging again
sleep 3
# Check if the date has changed
if [ "$(date +%Y-%m-%d)" != "$(date -r "$LOG_FILE" +%Y-%m-%d)" ]; then
# Create a new log file for the new day
LOG_FILE="/opt/scripts/log/check_internet_$(date +%Y-%m-%d).log"
echo "Script is executed on $(date)" >> "$LOG_FILE"
fi
done
Install script in the Crontab
If you like to run this script every time after reboot the server, add the following lines in the crontab:
# Check if internet is working
@reboot sleep 180 && sudo bash /opt/scripts/check_internet.sh