Skip to content

Instantly share code, notes, and snippets.

@viertelb
Created October 25, 2020 12:08
Show Gist options
  • Save viertelb/a5562847d5e06341acbe12408da9882b to your computer and use it in GitHub Desktop.
Save viertelb/a5562847d5e06341acbe12408da9882b to your computer and use it in GitHub Desktop.
## Download Monitor v0.2 - June 2017
# Set the interface you wish to monitor, eg: eth0, wlan0, usb0
# enp4s0 ist die andere Buchse
INTERFACE = "enp3s0"
# Set the minimum download speed in KB/s that must be achieved.
MINIMUM_SPEED = 2
# Set the number of retries to test for the average minimum speed.
RETRIES = 60
# Set the interval (in seconds), between retries to calculate average speed.
INTERVAL = 120
# Set the interval (in seconds), between recalculating average speed
LONG_INTERVAL = 3600
# Initial uptime after boot (Ben Okt 2020) 3 hours
INIT_INTERVAL = 10800
import os, time
from commands import getoutput # deprecated, geht nur mit python2 nicht 3
def worker ():
RETRIES_COUNT = 1
SPEED = 0
# initialer sleep - Ben Feb 2020
time.sleep(INIT_INTERVAL)
while True:
# Sum downstream and upstream and add with previous speed value
# {print $1} use just downstream
# {print $2} use just upstream
# {print $1+$2} use sum of downstream and upstream
SPEED += int(float(getoutput("ifstat -i %s 1 1 | awk '{print $1+$2}' | sed -n '3p'" % INTERFACE)))
if RETRIES_COUNT > RETRIES:
# Calculate average speed from all retries
AVG_SPEED = int(float(SPEED) / float(RETRIES_COUNT))
# If average speed is below minimum speed - suspend
if AVG_SPEED < MINIMUM_SPEED:
os.system("shutdown now")
# Else reset calculations and wait for longer to retry calculation
else:
RETRIES_COUNT = 1
SPEED = 0
time.sleep(LONG_INTERVAL)
else:
RETRIES_COUNT += 1
time.sleep(INTERVAL)
worker()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment