diff --git a/Bandwidth-Monitor/README.md b/Bandwidth-Monitor/README.md new file mode 100644 index 0000000000..1f85b9ebfd --- /dev/null +++ b/Bandwidth-Monitor/README.md @@ -0,0 +1,28 @@ +# Network-Usage-Tracker +A python script to keep a track of network usage and notify you if it exceeds a specified limit +(only support for wifi right now) + +### Requirements: + +#####**This script needs Python 3+** + +You may also need to install vnstat if you don't have it already installed +```bash +$ sudo apt-get install vnstat +``` + +### Usage: + +```bash +$ python bandwidth_py3.py xxx MiB/GiB & +``` +The '&' has been added to run the process in background. If you want to stop the process at any time use : + +```bash +$ ps -ef +``` +to get the list of running processes and then get the pid of the process you want to kill and do : + +```bash +$ sudo kill pid +``` diff --git a/Bandwidth-Monitor/bandwidth_py3.py b/Bandwidth-Monitor/bandwidth_py3.py new file mode 100644 index 0000000000..5c2ad7bc1c --- /dev/null +++ b/Bandwidth-Monitor/bandwidth_py3.py @@ -0,0 +1,55 @@ +#Contributed via : https://github.com/adarshkushwah/Network-Usage-Tracker + +import os +import sys +import time +import threading +import subprocess +import tkinter +import tkinter.messagebox + + +def monitor(limit, unit): + check = "vnstat" + proc = subprocess.Popen(check, shell=True, stdout=subprocess.PIPE) + output = proc.communicate() + output = str(output) + #print output + l = [] + for t in output.split(): + try: + if t == "MiB" or t == "GiB": + l.append(t) + else: + l.append(float(t)) + except ValueError: + pass + + if unit == l[5] and limit < l[4]: + print("\nnetwork usage limit exceeded!\n") + top = tkinter.Tk() + + def hello(): + tkinter.messagebox.showinfo("Warning!", "Network usage limit exceeded!!!!") + B1 = tkinter.Button(top, text="Warning", command=hello) + B1.pack() + top.mainloop() + arg = [limit, unit] + threading.Timer(60.0, monitor, arg).start() + + +def main(): + if len(sys.argv) > 3 or len(sys.argv) < 3: + print('command usage: python3 bandwidth_py3.py ') + print('example: python3 bandwidth_py3.py 500 MiB') + print('or python3 bandwidth_py3.py 2 GiB') + exit(1) + else: + limit = float(sys.argv[1]) + unit = str(sys.argv[2]) + #callMonitor(limit, unit) + monitor(limit, unit) + + +if __name__ == "__main__": + main()