Coder Social home page Coder Social logo

monitor-internet-connection's Introduction

monitor internet connection

A Python module to monitor the uptime of the Internet connection in real-time and record the time and duration of any downtime.

Sample output

--------------------------------------------------------------
--------------------------------------------------------------
Monitoring Internet Connection commencing : 2020-01-10 10:34:45 polling every 1 second(s)
-------Internet Connection unavailable at : 2020-01-11 07:35:01
-------Internet Connection restored at    : 2020-01-11 07:35:10
-------The duration of the downtime was   :             0:00:09
-------Internet Connection unavailable at : 2020-01-12 08:35:20
-------Internet Connection restored at    : 2020-01-12 08:36:27
-------The duration of the downtime was   :             0:01:07
Monitoring Internet Connection stopped at : 2020-01-13 12:42:38

Overview

Name: monitor_internet_connection

Version: 1.2.1

Date: 27th January 2020

Author: Martin F. O'Connor

YouTube Channel: Martin O'Connor

What is monitor_internet_connection?

It is a Python module to monitor the uptime of the Internet connection - that is to say to monitor that an external IP address is always reachable.

Why should I use monitor_internet_connection?

If you have automated long-running processes/programs/activities on your computer that requires Internet connectivity, there is nothing worse than coming back the next hour/day/week/whenever to review the logs/progress and find out that the program(s) failed or data is missing because of lost Internet connectivity. What is worse - you may not know exactly when Internet connectivity was lost. Thus, will you need to rerun the entire program/process? Or just a part of it? and so on.

The Python module monitor_internet_connection is a solution to this problem in that it monitors Internet connectivity in real-time, displaying on the console/terminal and recording to a log file: the start time, the end time and the duration of any Internet connectivity downtime. You may simply run this module in a console/terminal and leave it running for days/weeks on end.

What does monitor_internet_connection do?

Every X seconds (default polling frequency is 1 second), the program monitors whether the Internet connection is alive and an external IP address is reachable.

If the Internet is unreachable:

  1. The first observed time of failure is logged.

  2. Every one-minute interval of subsequent unavailability is logged. The one-minute logs can be useful as a proxy indicator of whether the computer lost power or just the Internet connection was unavailable.

  3. When Internet connectivity is restored, the first observed time of restoration is logged.

  4. Finally, the total time duration of the Internet unavailability is logged.

Log File

  • The log file is called internet_monitor.log.

  • The log file is written to the current working folder.

  • The log file is always appended to, never overwritten.

  • The information written to the log file is also displayed on the console/terminal.

  • The log file may be disabled via a command line parameter. See Usage below.

Prerequisites

You must have Python 3.6 or higher installed.

Installation

pip install monitor_internet_connection

QuickStart

python -m monitor_internet_connection

How do I exit the program

To exit the program, simply press Ctrl-C inside the console/terminal. This will cause the program to exit gracefully.

Usage

usage: python -m monitor_internet_connection [-h] [-n] [-f N]

Monitor the uptime of the Internet connection and record any downtime

optional arguments:
  -h, --help        show this help message and exit
  -n, --no-logfile  do not create a log file (default: False)
  -f N, --freq N    specify polling frequency in seconds (default: 1)

Acknowledgments

The is_internet-alive method was inspired by 7h3rAm's answer on Stackoverflow

Licence

This project is licensed under the MIT License - see the LICENSE.md file for details

monitor-internet-connection's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

monitor-internet-connection's Issues

crashes when the network connection is lost.

monitor-internet-connection 1.2.1 running on Raspbian 10 dies when the network becomes unreachable. The daemon seems to run until the network connection goes down.

Monitoring Internet Connection commencing : 2020-06-04 20:40:34 polling every 20 second(s)
Traceback (most recent call last):
File "/usr/lib/python2.7/runpy.py", line 174, in _run_module_as_main
"main", fname, loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "build/bdist.linux-armv7l/egg/monitor_internet_connection/main.py", line 250, in
File "build/bdist.linux-armv7l/egg/monitor_internet_connection/main.py", line 200, in monitor_inet_connection
File "build/bdist.linux-armv7l/egg/monitor_internet_connection/main.py", line 113, in is_internet_alive
File "/usr/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 101] Network is unreachable

The duration of the downtime was 0:00:00

I am getting few duration downtime of 0:00:00 by day.
I am surprise since the polling time is 1 second.

The code is running on a Rasperry 3B+ that is ethernet wired on a 5GHz bridge
The only use for this RPI is to host a VPN Server

There is a part of my log;

-------Internet Connection unavailable at : 2021-03-22 16:01:44
-------Internet Connection restored at : 2021-03-22 16:01:44
-------The duration of the downtime was : 0:00:00
-------Internet Connection unavailable at : 2021-03-22 17:20:23
-------Internet Connection restored at : 2021-03-22 17:20:23
-------The duration of the downtime was : 0:00:00
-------Internet Connection unavailable at : 2021-03-22 22:21:25
-------Internet Connection restored at : 2021-03-22 22:21:25
-------The duration of the downtime was : 0:00:00
-------Internet Connection unavailable at : 2021-03-22 22:46:17
-------Internet Connection restored at : 2021-03-22 22:46:17
-------The duration of the downtime was : 0:00:00
-------Internet Connection unavailable at : 2021-03-23 08:16:16
-------Internet Connection restored at : 2021-03-23 08:16:16
-------The duration of the downtime was : 0:00:00
-------Internet Connection unavailable at : 2021-03-23 15:25:15
-------Internet Connection restored at : 2021-03-23 15:25:15
-------The duration of the downtime was : 0:00:00
-------Internet Connection unavailable at : 2021-03-23 16:18:42
-------Internet Connection restored at : 2021-03-23 16:18:42
-------The duration of the downtime was : 0:00:00
-------Internet Connection unavailable at : 2021-03-23 21:16:37
-------Internet Connection restored at : 2021-03-23 21:16:37
-------The duration of the downtime was : 0:00:00
-------Internet Connection unavailable at : 2021-03-23 22:54:04
-------Internet Connection restored at : 2021-03-23 22:54:04
-------The duration of the downtime was : 0:00:00
-------Internet Connection unavailable at : 2021-03-24 04:54:46
-------Internet Connection restored at : 2021-03-24 04:54:47
-------The duration of the downtime was : 0:00:00
-------Internet Connection unavailable at : 2021-03-24 07:25:39
-------Internet Connection restored at : 2021-03-24 07:25:43
-------The duration of the downtime was : 0:00:04
-------Internet Connection unavailable at : 2021-03-25 00:39:21
-------Internet Connection restored at : 2021-03-25 00:39:21
-------The duration of the downtime was : 0:00:00
-------Internet Connection unavailable at : 2021-03-25 00:45:37
-------Internet Connection restored at : 2021-03-25 00:45:37
-------The duration of the downtime was : 0:00:00
-------Internet Connection unavailable at : 2021-03-25 09:43:40
-------Internet Connection restored at : 2021-03-25 09:43:40
-------The duration of the downtime was : 0:00:00
-------Internet Connection unavailable at : 2021-03-25 16:10:33
-------Internet Connection restored at : 2021-03-25 16:10:33
-------The duration of the downtime was : 0:00:00
-------Internet Connection unavailable at : 2021-03-26 07:00:48
-------Internet Connection restored at : 2021-03-26 07:00:48
-------The duration of the downtime was : 0:00:00

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.