From 9b948b082080a7e1d04463927087423215cc22b5 Mon Sep 17 00:00:00 2001 From: localhorst Date: Mon, 23 May 2022 22:55:43 +0200 Subject: [PATCH] inital version --- README.md | 36 ++++++++++++++- pyLCDinfo.py | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 pyLCDinfo.py diff --git a/README.md b/README.md index 68c61fd..bdd4346 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,37 @@ # pyLCDInfo -display current linux/network info on a 16x2 LCD via I2C \ No newline at end of file +display current linux/network info on a 16x2 LCD via I2C + + +## Install + +`apt-get install i2c-tools pip python3-dev` + +`pip3 install rpi_lcd` + +search I2C LCD Controller on Bus X (0, 1, 2, 3, ...) and note address of device +`i2cdetect -y X` + +`nano /lib/systemd/system/LCDinfo.service` + +``` +[Unit] +Description=LCD info +After=syslog.target +After=network.target + +[Service] +Type=simple +User=root +Group=root +Restart=on-failure +RestartSec=5s +ExecStart=sudo /usr/bin/nice -n 19 sudo -u root /usr/bin/python3 /root/pyLCDinfo.py eth0 wg0 /mnt/hdd/backups + +[Install] +WantedBy=multi-user.target +``` + +`systemctl daemon-reload` + +`systemctl enable /lib/systemd/system/LCDinfo.service` \ No newline at end of file diff --git a/pyLCDinfo.py b/pyLCDinfo.py new file mode 100644 index 0000000..ba5d001 --- /dev/null +++ b/pyLCDinfo.py @@ -0,0 +1,126 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" Author: Hendrik Schutter, mail@hendrikschutter.com + Date of creation: 2022/05/23 + Date of last modification: 2022/05/23 +""" + +from rpi_lcd import LCD +import netifaces as ni +import subprocess +import time +import shutil +import sys + +lcd = LCD(address=0x3f, bus=2, width=16, rows=2, backlight=True) + +def get_ip_addr(interfce_name): + return { + "ipv4": ni.ifaddresses(interfce_name)[ni.AF_INET][0]['addr'], + "ipv6": ni.ifaddresses(interfce_name)[ni.AF_INET6][0]['addr'] , + } + +def get_wireguard_state(config_name): + try: + last_handshake_unix_timestamp = subprocess.check_output(f"wg show {config_name} latest-handshakes", + shell=True, stderr=subprocess.STDOUT) + except subprocess.CalledProcessError: + return False + return True + +def get_disk_usage(disk_path): + total, used, free = shutil.disk_usage(disk_path) + return str(format((free // (2**30)) / (total // (2**30)) * 100.0,'.2f')) + '%' + +def get_uptime(): + with open('/proc/uptime', 'r') as f: + uptime_seconds = float(f.readline().split()[0]) + if (uptime_seconds >= (60.0*60.0*24.0)): + return str(format(uptime_seconds/(60.0*60.0*24.0),'.2f')) + ' days' + else: + return str(format((uptime_seconds/(60.0*60.0)),'.2f')) + ' hours' + + + return strftime("%H:%M:%S", gmtime(60*60*24)) + +def main(): + + if len(sys.argv) != 4: + print("exiting due to few arguments") + exit(-1) + + local_interface = sys.argv[1] + wireguard_interface = sys.argv[2] + disk_path = sys.argv[3] + + #print(get_ip_addr(local_interface)) + #print(get_wireguard_state(wireguard_interface)) + #print(get_disk_usage(disk_path)) + #print(get_uptime()) + + lcd.clear() + lcd.text(" Offsite Backup", 1) + lcd.text(" fckaf.de/FHA", 2) + time.sleep(2.0) + + state_ok = False + + while(True): + if state_ok == False: + lcd.backlight(turn_on=True) + + lcd.clear() + lcd.text("Local IPv4:", 1) + lcd.text(get_ip_addr(local_interface)["ipv4"], 2, align='right') + time.sleep(2.4) + lcd.clear() + lcd.text("Local IPv6:", 1) + lcd.text(get_ip_addr(local_interface)["ipv6"][14:-1], 2, align='right') + time.sleep(2.4) + + lcd.clear() + lcd.text("Waiting for VPN", 1) + + for loops in range(4): + for fill in range(14): + if loops % 2 == 0: + lcd.text("<>".rjust(fill), 2, align='left') + else: + lcd.text("<>".ljust(fill), 2, align='right') + time.sleep(0.1) + + + if get_wireguard_state(wireguard_interface): + state_ok = True + lcd.clear() + lcd.text(" VPN connected ", 1) + time.sleep(10.0) + else: + lcd.backlight(turn_on=False) + + lcd.clear() + lcd.text("Local IPv4:", 1) + lcd.text(get_ip_addr(local_interface)["ipv4"], 2, align='right') + time.sleep(5.0) + lcd.clear() + lcd.text("Local IPv6:", 1) + lcd.text(get_ip_addr(local_interface)["ipv6"][14:-1], 2, align='right') + time.sleep(5.0) + lcd.clear() + lcd.text("Free Disk:", 1) + lcd.text(get_disk_usage(disk_path), 2, align='right') + time.sleep(5.0) + lcd.clear() + lcd.text("System Uptime:", 1) + lcd.text(get_uptime(), 2, align='right') + time.sleep(5.0) + + lcd.clear() + lcd.text(" VPN Connected ", 1) + time.sleep(40.0) #sleep a lot to idle cpu + + #check if wireguard is still connected + state_ok = get_wireguard_state(wireguard_interface) + +if __name__ == "__main__": + main()