Log Internet Outages

My neighbor suspected he had daily Internet connectivity outages always taking place at the same time. I wrote this very simple Python script that pings Google.com at regular intervals and logs the ping time and success/failure. He was able to take this log to his ISP and escalate his tech support case with good evidence of technical problems.

You can modify the script to ping your router instead of google.com i.e. 192.168.1.1 if you suspect local network problems.

Usage

From command line run ./pinger.py [seconds] like: ./pinger.py 120 to ping every two minutes. In Windows, with Python 2.x installed, you can also double click the icon and the script will ping once a minute.

The script will save a .csv log in the same directory. You probably don’t want to ping at very short intervals for prolonged periods of time or it may be construed as a DoS attack by Google.

Force quit the script after you have collected enough information. Otherwise it will loop forever.

pinger.py
 
  1. #! /usr/bin/env python
  2. # Generates ISP connectivity log until stopped by user.
  3. # Version 0.2. www.skifatctz.com/wifi
  4. from datetime import datetime
  5. import time
  6. import os
  7. import sys
  8. hostname = "google.com"
  9. logfile = open('pinglog.csv', 'w')
  10. logfile.write('Test of: ' + os.uname()[1])
  11. logfile.close()      
  12. if len(sys.argv) == 1:
  13.       interval = 60
  14. else:
  15.       if sys.argv[1].isdigit() == False:
  16.             sys.exit('Usage: pinger.py [x] where x is an the number of seconds for ping interval.')      
  17.       interval = float(sys.argv[1])
  18. while 1 == 1:
  19.       logfile = open('pinglog.csv', 'a')
  20.       todaysDate = str(datetime.now())[:10]
  21.       currentTime = str(datetime.now())[11:-10]
  22.       response = os.system("ping -c 1 " + hostname)
  23.       if response == 0:
  24.             logfile.write(todaysDate +',' + currentTime + '\n')
  25.       else:
  26.             logfile.write(todaysDate +',' + currentTime + ' lost connection'+ '\n')
  27.             print hostname, 'lost connection'
  28.       logfile.close()
  29.       
  30.       time.sleep(interval)

Leave a Reply

Your email address will not be published. Required fields are marked *