hjelev / rpi-mqtt-monitor

Raspberry Pi MQTT Monitor gathers system information and sends it to a MQTT server.
GNU General Public License v3.0
155 stars 41 forks source link

free -t not available on Alpine Linux #91

Closed sunsided closed 3 months ago

sunsided commented 5 months ago

I'm running Alpine on my Raspi 5, it's free command doesn't support the "total" option -t. When using the Memory or Swap reporting, the script fails with the following error

BusyBox v1.36.1 (2023-11-07 18:53:09 UTC) multi-call binary.

Usage: free [-bkmgh]

Display free and used memory
Traceback (most recent call last):
  File "/home/markus/rpi-mqtt-monitor/src/rpi-cpu2mqtt.py", line 712, in <module>
    gather_and_send_info()
  File "/home/markus/rpi-mqtt-monitor/src/rpi-cpu2mqtt.py", line 612, in gather_and_send_info
    cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, uptime_seconds, wifi_signal, wifi_signal_dbm, rpi5_fan_speed = collect_monitored_values()
                                                                                                                                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/markus/rpi-mqtt-monitor/src/rpi-cpu2mqtt.py", line 595, in collect_monitored_values
    memory = check_memory()
             ^^^^^^^^^^^^^^
  File "/home/markus/rpi-mqtt-monitor/src/rpi-cpu2mqtt.py", line 86, in check_memory
    memory = round(float(memory.decode("utf-8").replace(",", ".")))
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: could not convert string to float: ''

Here's an example output of free as-is:

              total        used        free      shared  buff/cache   available
Mem:        8132632     5525592      156444      292000     2450596     2232356
Swap:             0           0           0

I can get it to work when removing the -t and filtering by Mem:

free | grep -i mem | awk 'NR == 1 {print $3/$2*100}'

For Swap, I also get the same issue as #11 (Division by Zero).