rpi power status (#146)
This commit is contained in:
@@ -72,3 +72,6 @@ wifi_signal_dbm = False
|
|||||||
|
|
||||||
# This works only on raspbery pi version 5 with stock fan
|
# This works only on raspbery pi version 5 with stock fan
|
||||||
rpi5_fan_speed = False
|
rpi5_fan_speed = False
|
||||||
|
|
||||||
|
# this works only on raspbery pi
|
||||||
|
rpi_power_status = False
|
||||||
@@ -93,6 +93,21 @@ def check_memory():
|
|||||||
return memory
|
return memory
|
||||||
|
|
||||||
|
|
||||||
|
def check_rpi_power_status():
|
||||||
|
full_cmd = "vcgencmd get_throttled | awk -F= '{print $2}'"
|
||||||
|
power_status = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0]
|
||||||
|
|
||||||
|
if power_status:
|
||||||
|
if power_status == "0x0":
|
||||||
|
power_status = "OK"
|
||||||
|
else:
|
||||||
|
power_status = "KO"
|
||||||
|
else:
|
||||||
|
power_status = "NA"
|
||||||
|
|
||||||
|
return power_status
|
||||||
|
|
||||||
|
|
||||||
def check_cpu_temp():
|
def check_cpu_temp():
|
||||||
full_cmd = "awk '{printf (\"%.2f\\n\", $1/1000); }' $(for zone in /sys/class/thermal/thermal_zone*/; do grep -iq \"cpu\" \"${zone}type\" && echo \"${zone}temp\"; done)"
|
full_cmd = "awk '{printf (\"%.2f\\n\", $1/1000); }' $(for zone in /sys/class/thermal/thermal_zone*/; do grep -iq \"cpu\" \"${zone}type\" && echo \"${zone}temp\"; done)"
|
||||||
try:
|
try:
|
||||||
@@ -238,7 +253,7 @@ def check_all_drive_temps():
|
|||||||
|
|
||||||
|
|
||||||
def print_measured_values(cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_clock_speed=0, swap=0, memory=0,
|
def print_measured_values(cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_clock_speed=0, swap=0, memory=0,
|
||||||
uptime_days=0, uptime_seconds=0, wifi_signal=0, wifi_signal_dbm=0, rpi5_fan_speed=0, drive_temps=0):
|
uptime_days=0, uptime_seconds=0, wifi_signal=0, wifi_signal_dbm=0, rpi5_fan_speed=0, drive_temps=0, rpi_power_status=0):
|
||||||
remote_version = update.check_git_version_remote(script_dir)
|
remote_version = update.check_git_version_remote(script_dir)
|
||||||
output = """
|
output = """
|
||||||
:: rpi-mqtt-monitor
|
:: rpi-mqtt-monitor
|
||||||
@@ -270,8 +285,9 @@ def print_measured_values(cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_c
|
|||||||
Wifi Signal: {} %
|
Wifi Signal: {} %
|
||||||
Wifi Signal dBm: {}
|
Wifi Signal dBm: {}
|
||||||
RPI5 Fan Speed: {} RPM
|
RPI5 Fan Speed: {} RPM
|
||||||
|
RPI Power Status: {}
|
||||||
Update: {}
|
Update: {}
|
||||||
""".format(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, wifi_signal, wifi_signal_dbm, rpi5_fan_speed, check_git_update(script_dir))
|
""".format(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, wifi_signal, wifi_signal_dbm, rpi5_fan_speed, rpi_power_status, check_git_update(script_dir))
|
||||||
|
|
||||||
drive_temps = check_all_drive_temps()
|
drive_temps = check_all_drive_temps()
|
||||||
if len(drive_temps) > 0:
|
if len(drive_temps) > 0:
|
||||||
@@ -317,7 +333,7 @@ def get_release_notes(version):
|
|||||||
return release_notes
|
return release_notes
|
||||||
|
|
||||||
|
|
||||||
def config_json(what_config, device=0):
|
def config_json(what_config, device="0"):
|
||||||
model_name = check_model_name()
|
model_name = check_model_name()
|
||||||
manufacturer = get_manufacturer()
|
manufacturer = get_manufacturer()
|
||||||
os = get_os()
|
os = get_os()
|
||||||
@@ -460,7 +476,9 @@ def config_json(what_config, device=0):
|
|||||||
data["unit_of_measurement"] = "°C"
|
data["unit_of_measurement"] = "°C"
|
||||||
data["device_class"] = "temperature"
|
data["device_class"] = "temperature"
|
||||||
data["state_class"] = "measurement"
|
data["state_class"] = "measurement"
|
||||||
|
elif what_config == "rpi_power_status":
|
||||||
|
data["icon"] = "mdi:flash"
|
||||||
|
data["name"] = "RPi Power Status"
|
||||||
else:
|
else:
|
||||||
return ""
|
return ""
|
||||||
# Return our built discovery config
|
# Return our built discovery config
|
||||||
@@ -520,7 +538,7 @@ def publish_update_status_to_mqtt(git_update):
|
|||||||
|
|
||||||
|
|
||||||
def publish_to_mqtt(cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_clock_speed=0, swap=0, memory=0,
|
def publish_to_mqtt(cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_clock_speed=0, swap=0, memory=0,
|
||||||
uptime_days=0, uptime_seconds=0, wifi_signal=0, wifi_signal_dbm=0, rpi5_fan_speed=0, drive_temps=0):
|
uptime_days=0, uptime_seconds=0, wifi_signal=0, wifi_signal_dbm=0, rpi5_fan_speed=0, drive_temps=0, rpi_power_status=0):
|
||||||
client = create_mqtt_client()
|
client = create_mqtt_client()
|
||||||
if client is None:
|
if client is None:
|
||||||
return
|
return
|
||||||
@@ -608,7 +626,12 @@ def publish_to_mqtt(cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_clock_s
|
|||||||
client.publish(config.mqtt_discovery_prefix + "/sensor/" + config.mqtt_topic_prefix + "/" + hostname + "_" + device + "_temp/config",
|
client.publish(config.mqtt_discovery_prefix + "/sensor/" + config.mqtt_topic_prefix + "/" + hostname + "_" + device + "_temp/config",
|
||||||
config_json(device + "_temp", device), qos=config.qos)
|
config_json(device + "_temp", device), qos=config.qos)
|
||||||
client.publish(config.mqtt_topic_prefix + "/" + hostname + "/" + device + "_temp", temp, qos=config.qos, retain=config.retain)
|
client.publish(config.mqtt_topic_prefix + "/" + hostname + "/" + device + "_temp", temp, qos=config.qos, retain=config.retain)
|
||||||
|
if config.rpi_power_status:
|
||||||
|
if config.discovery_messages:
|
||||||
|
client.publish(
|
||||||
|
config.mqtt_discovery_prefix + "/sensor/" + config.mqtt_topic_prefix + "/" + hostname + "_rpi_power_status/config",
|
||||||
|
config_json('rpi_power_status'), qos=config.qos)
|
||||||
|
client.publish(config.mqtt_topic_prefix + "/" + hostname + "/rpi_power_status", rpi_power_status, qos=config.qos, retain=config.retain)
|
||||||
|
|
||||||
status_sensor_topic = config.mqtt_discovery_prefix + "/sensor/" + config.mqtt_topic_prefix + "/" + hostname + "_status/config"
|
status_sensor_topic = config.mqtt_discovery_prefix + "/sensor/" + config.mqtt_topic_prefix + "/" + hostname + "_status/config"
|
||||||
client.publish(status_sensor_topic, config_json('status'), qos=config.qos)
|
client.publish(status_sensor_topic, config_json('status'), qos=config.qos)
|
||||||
@@ -623,10 +646,10 @@ def publish_to_mqtt(cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_clock_s
|
|||||||
|
|
||||||
|
|
||||||
def bulk_publish_to_mqtt(cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_clock_speed=0, swap=0, memory=0,
|
def bulk_publish_to_mqtt(cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_clock_speed=0, swap=0, memory=0,
|
||||||
uptime_days=0, uptime_seconds=0, wifi_signal=0, wifi_signal_dbm=0, rpi5_fan_speed=0, git_update=0):
|
uptime_days=0, uptime_seconds=0, wifi_signal=0, wifi_signal_dbm=0, rpi5_fan_speed=0, git_update=0, rpi_power_status="0"):
|
||||||
# compose the CSV message containing the measured values
|
# compose the CSV message containing the measured values
|
||||||
|
|
||||||
values = cpu_load, cpu_temp, used_space, voltage, int(sys_clock_speed), swap, memory, uptime_days, uptime_seconds, wifi_signal, wifi_signal_dbm, rpi5_fan_speed, git_update
|
values = cpu_load, cpu_temp, used_space, voltage, int(sys_clock_speed), swap, memory, uptime_days, uptime_seconds, wifi_signal, wifi_signal_dbm, rpi5_fan_speed, git_update, rpi_power_status
|
||||||
values = str(values)[1:-1]
|
values = str(values)[1:-1]
|
||||||
|
|
||||||
client = create_mqtt_client()
|
client = create_mqtt_client()
|
||||||
@@ -697,7 +720,7 @@ def parse_arguments():
|
|||||||
|
|
||||||
|
|
||||||
def collect_monitored_values():
|
def collect_monitored_values():
|
||||||
cpu_load = cpu_temp = used_space = voltage = sys_clock_speed = swap = memory = uptime_seconds = uptime_days = wifi_signal = wifi_signal_dbm = rpi5_fan_speed = drive_temps = False
|
cpu_load = cpu_temp = used_space = voltage = sys_clock_speed = swap = memory = uptime_seconds = uptime_days = wifi_signal = wifi_signal_dbm = rpi5_fan_speed = drive_temps = rpi_power_status = False
|
||||||
|
|
||||||
if config.cpu_load:
|
if config.cpu_load:
|
||||||
cpu_load = check_cpu_load()
|
cpu_load = check_cpu_load()
|
||||||
@@ -725,24 +748,26 @@ def collect_monitored_values():
|
|||||||
rpi5_fan_speed = check_rpi5_fan_speed()
|
rpi5_fan_speed = check_rpi5_fan_speed()
|
||||||
if config.drive_temps:
|
if config.drive_temps:
|
||||||
drive_temps = check_all_drive_temps()
|
drive_temps = check_all_drive_temps()
|
||||||
|
if config.rpi_power_status:
|
||||||
|
rpi_power_status = check_rpi_power_status()
|
||||||
|
|
||||||
return cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, uptime_seconds, wifi_signal, wifi_signal_dbm, rpi5_fan_speed, drive_temps
|
return cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, uptime_seconds, wifi_signal, wifi_signal_dbm, rpi5_fan_speed, drive_temps, rpi_power_status
|
||||||
|
|
||||||
|
|
||||||
def gather_and_send_info():
|
def gather_and_send_info():
|
||||||
while not stop_event.is_set():
|
while not stop_event.is_set():
|
||||||
cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, uptime_seconds, wifi_signal, wifi_signal_dbm, rpi5_fan_speed, drive_temps = collect_monitored_values()
|
cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, uptime_seconds, wifi_signal, wifi_signal_dbm, rpi5_fan_speed, drive_temps, rpi_power_status = collect_monitored_values()
|
||||||
|
|
||||||
if hasattr(config, 'random_delay'):
|
if hasattr(config, 'random_delay'):
|
||||||
time.sleep(config.random_delay)
|
time.sleep(config.random_delay)
|
||||||
|
|
||||||
if args.display:
|
if args.display:
|
||||||
print_measured_values(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, uptime_seconds, wifi_signal, wifi_signal_dbm, rpi5_fan_speed, drive_temps)
|
print_measured_values(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, uptime_seconds, wifi_signal, wifi_signal_dbm, rpi5_fan_speed, drive_temps, rpi_power_status)
|
||||||
|
|
||||||
if hasattr(config, 'group_messages') and config.group_messages:
|
if hasattr(config, 'group_messages') and config.group_messages:
|
||||||
bulk_publish_to_mqtt(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, uptime_seconds, wifi_signal, wifi_signal_dbm, rpi5_fan_speed)
|
bulk_publish_to_mqtt(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, uptime_seconds, wifi_signal, wifi_signal_dbm, rpi5_fan_speed, drive_temps, rpi_power_status)
|
||||||
else:
|
else:
|
||||||
publish_to_mqtt(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, uptime_seconds, wifi_signal, wifi_signal_dbm, rpi5_fan_speed, drive_temps)
|
publish_to_mqtt(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, uptime_seconds, wifi_signal, wifi_signal_dbm, rpi5_fan_speed, drive_temps, rpi_power_status)
|
||||||
|
|
||||||
if not args.service:
|
if not args.service:
|
||||||
break
|
break
|
||||||
|
|||||||
Reference in New Issue
Block a user