Mustang65
Posts: 10
Joined: Fri Dec 06, 2013 2:20 am

Ping website and log date-time when unavailable

Fri Dec 06, 2013 2:43 am

Hi,
I searched a few sites, but was unable to locate any code for what I need to accomplish. I just received my Raspberry PI and I am trying to come up with a program that will run 24/7 and monitor my internet connection. I have been having issues with my internet connection and when I ping a site I get a message indicating that the site could not be found. I get the same msg if I bypass my WIFI router and plug directly into the cable modem. I believe the best way to monitor my connection is to ping a website or the DNS server, ignore a valid connection, but log the Date/Time and a non-connect message. Pinging once every 30 seconds, I believe should do the trick. Does anyone know where there may be some code, using Python?
Process:
1- Ping Site
2- Determine connection status
3- If connection is made, igonore and wait 30 seconds and try again
4- If no connection is made, create a record with Date/Time of no connection, and error message
5- Wait 30 seconds and ping again......
It should be interesting to see how many "Site can't be found" messages I will have and see how long the connection was down.
Thanks in advance for any assistance you can offer.
Don

gmc
Posts: 123
Joined: Fri Mar 09, 2012 11:31 am
Location: Cheshire, UK
Contact: Website

Re: Ping website and log date-time when unavailable

Fri Dec 06, 2013 2:58 pm

I'm using this code - if it failed to ping a site it restarts the nic, then the wlan, then reboot. I modded it to ping my local router.

I can't take credit for the code - I found it somewhere on the net. Forget the location.

It should help you out

Code: Select all

#!/usr/bin/python
#sudo ifdown --force wlan0
#sudo ifup wlan0

import commands
import re       #Import regular expressions
from datetime import datetime
today = datetime.today()
broken_after_pass = False

def log_write(log_text):
        with open("/home/pi/log_connection.txt", "a") as myfile:
                myfile.write("%s - %s\n" % (today, log_text))
                print log_text
                myfile.close()
                return

def can_ping_google():          #if contains 'bytes from' pass
        ping_once = "ping 192.168.1.1 -c 1"
        output = commands.getstatusoutput(ping_once)
        if re.search('bytes from', output[1]):
                log_write('Ping Successful - %s' %output[1])
                return True
        else:
                log_write('Ping failed - %s' %output[1])
                return False

def connection_test():
        if can_ping_google():
                log_write("Pinged Fine")
                return
        else:
                if can_ping_google():
                        print "Pinged fine second time, first was a fail"
                        return
                else:
                        log_write('Restarting networking service')
                        restart_output = commands.getstatusoutput("sudo service networking restart")
                        print restart_output
                        if can_ping_google():
                                log_write('Restarting worked, can ping now')
                                print "Can ping after network restart"
                                return
                        else:
                                print "Trying to force wlan0 down and up"
                                log_write("Restarting didn't work. Will force wlan0 down and up, and restart networking after")
                                updownout1 = commands.getstatusoutput("sudo ifdown --force wlan0")
                                updownout2 = commands.getstatusoutput("sudo ifup wlan0")
                                updownout3 = commands.getstatusoutput("sudo service networking restart")
                                log_write("%s - %s \n %s \n %s \n" %(today, updownout1, updownout2, updownout3))
                                if can_ping_google():
                                        log_write("%s - Down, up and restart worked. Can ping.\n" % today)
                                        return
                                else:
                                        log_write("Broken still after restart and up/down. Rebooting...")
                                        restartout = commands.getstatusoutput("sudo reboot -n")

connection_test()


Mustang65
Posts: 10
Joined: Fri Dec 06, 2013 2:20 am

Re: Ping website and log date-time when unavailable

Fri Dec 06, 2013 4:30 pm

Thanks gmc,
This should do the trick.

Return to “Beginners”