Skip to content

Instantly share code, notes, and snippets.

@cscorley
Last active August 29, 2015 14:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cscorley/a26d1771b7cbf46d9431 to your computer and use it in GitHub Desktop.
Save cscorley/a26d1771b7cbf46d9431 to your computer and use it in GitHub Desktop.
internet uptime script
#!/usr/bin/python3
import sqlite3
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from dateutil.parser import parse
conn = sqlite3.connect('iup.db')
c = conn.cursor()
c.execute("create table if not exists log (time text primary key, status integer)")
with open('iup.log') as log:
for line in log:
time, status = line.split(',')
status = int(status)
try:
c.execute("insert into log values (?,?)", (time, status))
except sqlite3.IntegrityError:
pass
conn.commit()
c.execute("select * from log")
rows = c.fetchall()
dates = [parse(r[0]) for r in rows]
status = [r[1] == 0 for r in rows]
y2 = [0] * len(status)
y3 = [1] * len(status)
#plt.ylim([-0.1, 1.1])
p, = plt.plot_date(x=dates, y=status, xdate=True, ydate=False, fmt='r',
label="Disconnected", antialiased=False)
plt.fill_between(dates, status, y2, color='g')
plt.fill_between(dates, status, y3, color='r')
plt.gca().xaxis.set_major_formatter( mdates.DateFormatter('%d.%H') )
l1 = plt.legend(handles=[p])
plt.gca().add_artist(l1)
plt.savefig('downtime.png', format='png')
#!/bin/bash
while true; do
sleep 60
ping -c 1 google.com &> /dev/null
echo $(date "+%Y-%m-%d %H:%M:%S"),$? >> /jffs/iup.log
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment