From af5f9f6c58b12b5711c5d93b9bc1bce687cb315f Mon Sep 17 00:00:00 2001 From: Tim Dykes Date: Fri, 8 Nov 2013 13:22:12 +1100 Subject: [PATCH 1/5] Added hostname, cpu load, and free disk. Move temp to second page. Changed update time to 10 seconds to benefit multiple pages. --- examples/sysinfo.py | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/examples/sysinfo.py b/examples/sysinfo.py index e12c9eb..05ffaf4 100644 --- a/examples/sysinfo.py +++ b/examples/sysinfo.py @@ -4,27 +4,37 @@ from time import sleep import pifacecad - -UPDATE_INTERVAL = 60 * 5 # 5 mins +SHUTDOWN_HOLD = 0 +UPDATE_INTERVAL = 10 # 10 sec GET_IP_CMD = "hostname --all-ip-addresses" GET_TEMP_CMD = "/opt/vc/bin/vcgencmd measure_temp" TOTAL_MEM_CMD = "free | grep 'Mem' | awk '{print $2}'" USED_MEM_CMD = "free | grep '\-\/+' | awk '{print $3}'" +CPU_LOAD_CMD = "uptime | awk '{print $9}' | sed s/,/""/g" +HOSTNAME_CMD = "hostname" +FREE_DISK_CMD = "df -h | grep rootfs | awk '{print $5}'" -temperature_symbol = pifacecad.LCDBitmap( - [0x4, 0x4, 0x4, 0x4, 0xe, 0xe, 0xe, 0x0]) -memory_symbol = pifacecad.LCDBitmap( - [0xe, 0x1f, 0xe, 0x1f, 0xe, 0x1f, 0xe, 0x0]) -temp_symbol_index, memory_symbol_index = 0, 1 +cpu_symbol = pifacecad.LCDBitmap( [0x0, 0x1f, 0x11, 0x15, 0x11, 0x1f, 0x0, 0x0] ) +disk_symbol = pifacecad.LCDBitmap( [0x0, 0x1c, 0x1e, 0x1e, 0x1e, 0x1e, 0x0, 0x0] ) +temperature_symbol = pifacecad.LCDBitmap( [0x4, 0x4, 0x4, 0x4, 0xe, 0xe, 0xe, 0x0] ) +memory_symbol = pifacecad.LCDBitmap( [0xe, 0x1f, 0xe, 0x1f, 0xe, 0x1f, 0xe, 0x0] ) +temp_symbol_index, memory_symbol_index, cpu_symbol_index, disk_symbol_index = 0, 1, 2, 3 def run_cmd(cmd): return subprocess.check_output(cmd, shell=True).decode('utf-8') +def get_cpu_load(): + return run_cmd(CPU_LOAD_CMD)[:-1] + +def get_disk_usage(): + return run_cmd(FREE_DISK_CMD)[:-1] def get_my_ip(): return run_cmd(GET_IP_CMD)[:-1] +def get_hostname(): + return run_cmd(HOSTNAME_CMD)[:-1] def get_my_temp(): return run_cmd(GET_TEMP_CMD)[5:9] @@ -48,12 +58,20 @@ def show_sysinfo(): while True: cad.lcd.clear() cad.lcd.write("IP:{}\n".format(get_my_ip())) + cad.lcd.write_custom_bitmap(cpu_symbol_index) + cad.lcd.write(":{} ".format(get_cpu_load())) + cad.lcd.write_custom_bitmap(memory_symbol_index) + cad.lcd.write(":{}".format(get_my_free_mem())) + sleep(UPDATE_INTERVAL) + + cad.lcd.clear() + cad.lcd.write(format(get_hostname())+"\n") cad.lcd.write_custom_bitmap(temp_symbol_index) - cad.lcd.write(":{}C ".format(get_my_temp())) + cad.lcd.write(":{}C ".format(get_my_temp())) + cad.lcd.write_custom_bitmap(disk_symbol_index) + cad.lcd.write(":{}".format(get_disk_usage())) - cad.lcd.write_custom_bitmap(memory_symbol_index) - cad.lcd.write(":{}".format(get_my_free_mem())) sleep(UPDATE_INTERVAL) @@ -69,7 +87,9 @@ def show_sysinfo(): else: cad.lcd.store_custom_bitmap(temp_symbol_index, temperature_symbol) cad.lcd.store_custom_bitmap(memory_symbol_index, memory_symbol) + cad.lcd.store_custom_bitmap(cpu_symbol_index, cpu_symbol) + cad.lcd.store_custom_bitmap(disk_symbol_index, disk_symbol) cad.lcd.backlight_on() cad.lcd.write("Waiting for IP..") wait_for_ip() - show_sysinfo() + show_sysinfo() \ No newline at end of file From b524ef93f1aa8b1f5484b84f0478985a5e14990c Mon Sep 17 00:00:00 2001 From: Tim Dykes Date: Fri, 8 Nov 2013 13:26:34 +1100 Subject: [PATCH 2/5] cleanup up test code snippets. --- examples/sysinfo.py | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/sysinfo.py b/examples/sysinfo.py index 05ffaf4..03fbcea 100644 --- a/examples/sysinfo.py +++ b/examples/sysinfo.py @@ -4,7 +4,6 @@ from time import sleep import pifacecad -SHUTDOWN_HOLD = 0 UPDATE_INTERVAL = 10 # 10 sec GET_IP_CMD = "hostname --all-ip-addresses" GET_TEMP_CMD = "/opt/vc/bin/vcgencmd measure_temp" From 5a7883afbee07cfd95c4762b328dd0fe58d56af0 Mon Sep 17 00:00:00 2001 From: Tim Dykes Date: Thu, 14 Nov 2013 18:10:38 +1100 Subject: [PATCH 3/5] Changed GET_IP_CMD to only get IPv4 address. otherwise will return both ipv4 and ipv6 addresses which are way to long of the screen to display. --- examples/sysinfo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/sysinfo.py b/examples/sysinfo.py index 03fbcea..14bfc5c 100644 --- a/examples/sysinfo.py +++ b/examples/sysinfo.py @@ -5,7 +5,7 @@ import pifacecad UPDATE_INTERVAL = 10 # 10 sec -GET_IP_CMD = "hostname --all-ip-addresses" +GET_IP_CMD = "hostname --all-ip-addresses | awk '{print $1}'" GET_TEMP_CMD = "/opt/vc/bin/vcgencmd measure_temp" TOTAL_MEM_CMD = "free | grep 'Mem' | awk '{print $2}'" USED_MEM_CMD = "free | grep '\-\/+' | awk '{print $3}'" From d4c6e1fb0af5a6583e8e6c75952402d00ae4a399 Mon Sep 17 00:00:00 2001 From: Tim Dykes Date: Fri, 15 Nov 2013 10:23:14 +1100 Subject: [PATCH 4/5] First build of lldp.py --- examples/lldp.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 examples/lldp.py diff --git a/examples/lldp.py b/examples/lldp.py new file mode 100644 index 0000000..c130d08 --- /dev/null +++ b/examples/lldp.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python3 +# requires `lldpctl` to be installed + +import sys +import subprocess +from time import sleep +import pifacecad + +UPDATE_INTERVAL = 30 # 10 sec + +GET_LLDP_HOSTNAME_CMD = "lldpctl | grep SysName | awk '{print $2}'" +GET_LLDP_PORT_CMD = "lldpctl | grep PortDescr | awk '{print $2}'" + + +def run_cmd(cmd): + return subprocess.check_output(cmd, shell=True).decode('utf-8') + +def get_lldp_hostname(): + return run_cmd(GET_LLDP_HOSTNAME_CMD)[:-1] + +def get_lldp_port(): + return run_cmd(GET_LLDP_PORT_CMD)[:-1] + + +def wait_for_neighbour(): + hostname = "" + while len(hostname) <= 0: + sleep(1) + hostname = get_lldp_hostname() + + +def show_lldpinfo(): + while True: + cad.lcd.clear() + cad.lcd.write(get_lldp_hostname()+"\n"+get_lldp_port()) + sleep(UPDATE_INTERVAL) + + +if __name__ == "__main__": + # test for lldpd + try: + subprocess.call(["lldpctl"], stdout=open('/dev/null')) + except OSError as e: + if e.errno == os.errno.ENOENT: + print( + "lldpctl was not found, install with " + "`sudo apt-get install lldpd`") + sys.exit(1) + else: + raise # Something else went wrong while trying to run `lldpd` + + cad = pifacecad.PiFaceCAD() + cad.lcd.blink_off() + cad.lcd.cursor_off() + + if "clear" in sys.argv: + cad.lcd.clear() + cad.lcd.display_off() + cad.lcd.backlight_off() + else: + cad.lcd.backlight_on() + cad.lcd.write("Waiting for LLDP..") + wait_for_neighbour() + show_lldpinfo() \ No newline at end of file From f6a474ff5c05677b4ab5c5048ace3c3c63cb264a Mon Sep 17 00:00:00 2001 From: Tim Dykes Date: Fri, 15 Nov 2013 12:02:49 +1100 Subject: [PATCH 5/5] Fixed 'import OS' for python2 --- examples/lldp.py | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/lldp.py b/examples/lldp.py index c130d08..536de22 100644 --- a/examples/lldp.py +++ b/examples/lldp.py @@ -3,6 +3,7 @@ import sys import subprocess +import os from time import sleep import pifacecad