Skip to content

Instantly share code, notes, and snippets.

@waleedahmad
Last active May 31, 2017 10:34
Show Gist options
  • Save waleedahmad/272fe3442d3afec3fe0c9d4c546fdd25 to your computer and use it in GitHub Desktop.
Save waleedahmad/272fe3442d3afec3fe0c9d4c546fdd25 to your computer and use it in GitHub Desktop.
Python script to monitor your internet down time
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import socket, pymysql.cursors, time, sys
def internet(host="8.8.8.8", port=53, timeout=3):
try:
socket.setdefaulttimeout(timeout)
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host,port))
return True
except Exception as ex:
return False
def last_record_status(connection):
with connection.cursor() as cursor:
sql = "SELECT `status` FROM `record` ORDER BY id DESC"
cursor.execute(sql)
result = cursor.fetchone()
return None if result is None else result['status']
def get_records(connection):
with connection.cursor() as cursor:
sql = "SELECT `*` FROM `record`"
cursor.execute(sql)
return cursor
def write_record(connection, cursor, status):
sql = "INSERT INTO `record` (`status`) VALUES (%s)"
cursor.execute(sql, (status))
connection.commit()
def get_total_downtime(connection):
seconds = 0
cursor = get_records(connection)
for record in cursor:
if(record['status'] is 0):
up = cursor.fetchone()
seconds += (up['time'] - record['time']).total_seconds()
return round(seconds / 60);
def monitor_connection(connection, sleep_time):
print('Monitoring your connection')
while True:
try:
with connection.cursor() as cursor:
last_record = last_record_status(connection)
if not internet():
if last_record is 1 or last_record is None:
write_record(connection, cursor, 0)
else:
if last_record is 0 or last_record is None:
write_record(connection, cursor, 1)
except Exception as ex:
print(ex)
time.sleep(sleep_time)
def args_error():
print('Please provide an argument\nOptions\n./internet.py monitor\n./internet.py downtime');
connection = pymysql.connect(host='localhost',
user='root',
password='binarystar',
db='internet',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
args = sys.argv;
if not len(args) > 1:
args_error()
elif args[1] == 'monitor':
monitor_connection(connection, 10)
elif args[1] == 'downtime':
print('Remained down for : ' , get_total_downtime(connection) , 'mintues')
-- phpMyAdmin SQL Dump
-- version 4.5.4.1deb2ubuntu2
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: May 30, 2017 at 06:35 AM
-- Server version: 5.7.18-0ubuntu0.16.04.1
-- PHP Version: 7.0.18-0ubuntu0.16.04.1
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `internet`
--
-- --------------------------------------------------------
--
-- Table structure for table `record`
--
CREATE TABLE `record` (
`id` int(11) NOT NULL,
`status` tinyint(1) NOT NULL,
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `record`
--
ALTER TABLE `record`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `record`
--
ALTER TABLE `record`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment