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.

 #! /usr/bin/env python
# Generates ISP connectivity log until stopped by user.
# Version 0.2. www.skifatctz.com/wifi

from datetime import datetime
import time
import os
import sys

hostname = "google.com"
logfile = open('pinglog.csv', 'w')
logfile.write('Test of: ' + os.uname()[1])
logfile.close()      

if len(sys.argv) == 1:
      interval = 60
else:
      if sys.argv[1].isdigit() == False:
            sys.exit('Usage: pinger.py [x] where x is an the number of seconds for ping interval.')      
      interval = float(sys.argv[1])

while 1 == 1:
      logfile = open('pinglog.csv', 'a')
      todaysDate = str(datetime.now())[:10]
      currentTime = str(datetime.now())[11:-10]
      response = os.system("ping -c 1 " + hostname)
      if response == 0:
            logfile.write(todaysDate +',' + currentTime + '\n')
      else:
            logfile.write(todaysDate +',' + currentTime + ' lost connection'+ '\n')
            print hostname, 'lost connection'
      logfile.close()
      
      time.sleep(interval)

Leave a Reply

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