From f52687642e30c6f9c64a60c480e635cda9a864a3 Mon Sep 17 00:00:00 2001 From: spees Date: Sat, 19 Oct 2019 21:32:55 +0200 Subject: [PATCH] refactored so it actually works Signed-off-by: spees --- pwnagotchi/defaults.yml | 5 +- pwnagotchi/plugins/default/memtemp.py | 109 ++++++++++++++------------ 2 files changed, 62 insertions(+), 52 deletions(-) diff --git a/pwnagotchi/defaults.yml b/pwnagotchi/defaults.yml index 986d2fc2..952dcfba 100644 --- a/pwnagotchi/defaults.yml +++ b/pwnagotchi/defaults.yml @@ -70,8 +70,9 @@ main: netmask: 24 interval: 1 # check every x minutes for device share_internet: false - memtemp: # Display memory usage and cpu temperature on screen - enabled: false + memtemp: # Display memory usage, cpu load and cpu temperature on screen + enabled: false + orientation: horizontal # horizontal/vertical # monitor interface to use iface: mon0 # command to run to bring the mon interface up in case it's not up already diff --git a/pwnagotchi/plugins/default/memtemp.py b/pwnagotchi/plugins/default/memtemp.py index b17225a3..74732d6d 100644 --- a/pwnagotchi/plugins/default/memtemp.py +++ b/pwnagotchi/plugins/default/memtemp.py @@ -1,6 +1,6 @@ -# tempmem shows memory infos and cpu temperature +# memtemp shows memory infos and cpu temperature # -# totalmem usedmem freemem cputemp +# mem usage, cpu load, cpu temp # ############################################################### # @@ -10,71 +10,80 @@ # - removed the label so we wont waste screen space # - Updated version to 1.0.1 # +# 20-10-2019 by spees +# - Refactored to use the already existing functions +# - Now only shows memory usage in percentage +# - Added CPU load +# - Added horizontal and vertical orientation +# ############################################################### - __author__ = 'https://github.com/xenDE' __version__ = '1.0.1' __name__ = 'memtemp' __license__ = 'GPL3' -__description__ = 'A plugin that will add a memory and temperature indicator' +__description__ = 'A plugin that will display memory/cpu usage and temperature' -import struct from pwnagotchi.ui.components import LabeledValue from pwnagotchi.ui.view import BLACK import pwnagotchi.ui.fonts as fonts +import subprocess +import logging -import time - - -class MEMTEMP: - - # set the minimum seconds before refresh the values - refresh_wait = 30 - - refresh_ts_last = time.time() - refresh_wait - - def __init__(self): - # only import when the module is loaded and enabled - import os - - def get_temp(self): - try: - temp = os.popen('/opt/vc/bin/vcgencmd measure_temp').readlines()[0].split('=')[1].replace("\n", '').replace("'","") - return 't:' + temp - except: - return 't:-' - # cpu:37.4C - - def get_mem_info(self): - try: - # includes RAM + Swap Memory: -# total, used, free = map(int, os.popen('free -t -m').readlines()[-1].split()[1:]) - # without Swap, only real memory: - total, used, free = map(int, os.popen('free -t -m').readlines()[-3].split()[1:4]) - return "\nT:"+str(total)+"M U:"+str(used)+"M\nF:"+str(free)+"M" - except: - return "\nT:- U:-\nF:- " - # tm:532 um:82 fm:353 - - -memtemp = None +OPTIONS = dict() def on_loaded(): - global memtemp - memtemp = MEMTEMP() + logging.info("memtemp plugin loaded.") +def mem_usage(): + out = subprocess.getoutput("free -m") + for line in out.split("\n"): + line = line.strip() + if line.startswith("Mem:"): + parts = list(map(int, line.split()[1:])) + tot = parts[0] + used = parts[1] + free = parts[2] + return int((used / tot) * 100) + + return 0 + + +def cpu_load(): + with open('/proc/stat', 'rt') as fp: + for line in fp: + line = line.strip() + if line.startswith('cpu '): + parts = list(map(int, line.split()[1:])) + user_n = parts[0] + sys_n = parts[2] + idle_n = parts[3] + tot = user_n + sys_n + idle_n + return int(((user_n + sys_n) / tot) * 100) + return 0 + + +def temperature(celsius=True): + with open('/sys/class/thermal/thermal_zone0/temp', 'rt') as fp: + temp = int(fp.read().strip()) + c = int(temp / 1000) + return c if celsius else ((c * (9 / 5)) + 32) + + def on_ui_setup(ui): - ui.add_element('memtemp', LabeledValue(color=BLACK, label='', value='\nT:- U:-\nF:- -', position=(ui.width() / 2 + 17, ui.height() / 2), - label_font=fonts.Bold, text_font=fonts.Medium)) - + if OPTIONS['orientation'] == "horizontal": + ui.add_element('memtemp', LabeledValue(color=BLACK, label='', value='mem cpu temp\n - - -', position=(ui.width() / 2 + 30, ui.height() /2 + 15), + label_font=fonts.Small, text_font=fonts.Small)) + elif OPTIONS['orientation'] == "vertical": + ui.add_element('memtemp', LabeledValue(color=BLACK, label='', value=' mem:-\n cpu:-\ntemp:-', position=(ui.width() / 2 + 55, ui.height() /2), + label_font=fonts.Small, text_font=fonts.Small)) def on_ui_update(ui): - if time.time() > memtemp.refresh_ts_last + memtemp.refresh_wait: - ui.set('memtemp', "%s %s" % (memtemp.get_mem_info(), memtemp.get_temp())) - memtemp.refresh_ts_last = time.time() - - + if OPTIONS['orientation'] == "horizontal": + ui.set('memtemp', " mem cpu temp\n %s%% %s%% %sc" % (mem_usage(), cpu_load(), temperature())) + + elif OPTIONS['orientation'] == "vertical": + ui.set('memtemp', " mem:%s%%\n cpu:%s%%\ntemp:%sc" % (mem_usage(), cpu_load(), temperature()))