From 3fae42305c79fa021cf20aee73938cd89cb5a451 Mon Sep 17 00:00:00 2001 From: SpudGunMan Date: Mon, 23 Dec 2024 18:56:05 -0800 Subject: [PATCH] sysEnv enhance --- mesh_bot.py | 2 +- modules/filemon.py | 8 ++++++-- runShell.sh | 25 +------------------------ sysEnv.sh | 30 ++++++++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 27 deletions(-) create mode 100644 sysEnv.sh diff --git a/mesh_bot.py b/mesh_bot.py index 7eadf01..89a7b3d 100755 --- a/mesh_bot.py +++ b/mesh_bot.py @@ -770,7 +770,7 @@ def sysinfo(message, message_from_id, deviceID): return "sysinfo command returns system information." else: if enable_runShellCmd and file_monitor_enabled: - shellData = call_external_script(None).rstrip() + shellData = call_external_script(None, "sysEnv.sh").rstrip() return get_sysinfo(message_from_id, deviceID) + "\n" + shellData else: return get_sysinfo(message_from_id, deviceID) diff --git a/modules/filemon.py b/modules/filemon.py index 4bec6fd..d309b96 100644 --- a/modules/filemon.py +++ b/modules/filemon.py @@ -70,8 +70,12 @@ def call_external_script(message, script="runShell.sh"): script_path = os.path.join(current_working_directory, script) if not os.path.exists(script_path): - logger.warning(f"FileMon: Script not found: {script_path}") - return "sorry I can't do that" + # try the raw script name + script_path = script + if not os.path.exists(script_path): + logger.warning(f"FileMon: Script not found: {script_path}") + return "sorry I can't do that" + output = os.popen(f"bash {script_path} {message}").read() return output except Exception as e: diff --git a/runShell.sh b/runShell.sh index 5dfc5a7..3e67e7e 100644 --- a/runShell.sh +++ b/runShell.sh @@ -4,27 +4,4 @@ cd "$(dirname "$0")" program_path=$(pwd) -# get basic telemetry data. Free space, CPU, RAM, and temperature for a raspberry pi -free_space=$(df -h | grep ' /$' | awk '{print $4}') -cpu_usage=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1}') -ram_usage=$(free | grep Mem | awk '{print $3/$2 * 100.0}') -ram_free=$(echo "scale=2; 100 - $ram_usage" | bc) - -# if command vcgencmd is found, part of raspberrypi tools, use it to get temperature -if command -v vcgencmd &> /dev/null -then - # get temperature - temp=$(vcgencmd measure_temp | sed "s/temp=//" | sed "s/'C//") - # temp in fahrenheit - tempf=$(echo "scale=2; $temp * 9 / 5 + 32" | bc) -else - # get temperature from thermal zone - temp=$(paste <(cat /sys/class/thermal/thermal_zone*/type) <(cat /sys/class/thermal/thermal_zone*/temp) | grep "temp" | awk '{print $2/1000}' | awk '{s+=$1} END {print s/NR}') - tempf=$(echo "scale=2; $temp * 9 / 5 + 32" | bc) -fi - -# print telemetry data -printf "Disk:%s" "$free_space" -printf " RAM:%.2f%%" "$ram_usage" -printf " CPU:%.1f%%" "$cpu_usage" -printf " CPU-T:%.1f°C (%.1f°F)" "$temp" "$tempf" \ No newline at end of file +printf "Running meshing-around demo script for shell scripting\n" \ No newline at end of file diff --git a/sysEnv.sh b/sysEnv.sh new file mode 100644 index 0000000..69e18f4 --- /dev/null +++ b/sysEnv.sh @@ -0,0 +1,30 @@ +#!/bin/bash +# meshing-around shell script for sysinfo +# runShell.sh +cd "$(dirname "$0")" +program_path=$(pwd) + +# get basic telemetry data. Free space, CPU, RAM, and temperature for a raspberry pi +free_space=$(df -h | grep ' /$' | awk '{print $4}') +cpu_usage=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1}') +ram_usage=$(free | grep Mem | awk '{print $3/$2 * 100.0}') +ram_free=$(echo "scale=2; 100 - $ram_usage" | bc) + +# if command vcgencmd is found, part of raspberrypi tools, use it to get temperature +if command -v vcgencmd &> /dev/null +then + # get temperature + temp=$(vcgencmd measure_temp | sed "s/temp=//" | sed "s/'C//") + # temp in fahrenheit + tempf=$(echo "scale=2; $temp * 9 / 5 + 32" | bc) +else + # get temperature from thermal zone + temp=$(paste <(cat /sys/class/thermal/thermal_zone*/type) <(cat /sys/class/thermal/thermal_zone*/temp) | grep "temp" | awk '{print $2/1000}' | awk '{s+=$1} END {print s/NR}') + tempf=$(echo "scale=2; $temp * 9 / 5 + 32" | bc) +fi + +# print telemetry data +printf "Disk:%s" "$free_space" +printf " RAM:%.2f%%" "$ram_usage" +printf " CPU:%.1f%%" "$cpu_usage" +printf " CPU-T:%.1f°C (%.1f°F)" "$temp" "$tempf" \ No newline at end of file