apt updates sensor (#150)

This commit is contained in:
Masoko
2024-10-25 17:55:34 +03:00
committed by GitHub
parent 2958227b91
commit 3103d2da47
2 changed files with 34 additions and 6 deletions

View File

@@ -75,3 +75,6 @@ rpi5_fan_speed = False
# this works only on raspbery pi # this works only on raspbery pi
rpi_power_status = False rpi_power_status = False
# Check for apt updates - experimental feature, disabled by default, updateed with update_check_interval
apt_updates = False

View File

@@ -218,6 +218,20 @@ def get_mac_address():
return mac return mac
def get_apt_updates():
try:
subprocess.run(['sudo', 'apt', 'update'], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
full_cmd = "apt-get -q -y --ignore-hold --allow-change-held-packages --allow-unauthenticated -s dist-upgrade | /bin/grep ^Inst | wc -l"
result = subprocess.run(full_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
updates_count = int(result.stdout.strip())
except Exception as e:
print(f"Error checking for updates: {e}")
updates_count = 0
return updates_count
def get_hwmon_device_name(hwmon_path): def get_hwmon_device_name(hwmon_path):
try: try:
with open(os.path.join(hwmon_path, 'name'), 'r') as f: with open(os.path.join(hwmon_path, 'name'), 'r') as f:
@@ -254,8 +268,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, rpi_power_status=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
Version: {} Version: {}
:: Device Information :: Device Information
@@ -479,6 +492,10 @@ def config_json(what_config, device="0"):
elif what_config == "rpi_power_status": elif what_config == "rpi_power_status":
data["icon"] = "mdi:flash" data["icon"] = "mdi:flash"
data["name"] = "RPi Power Status" data["name"] = "RPi Power Status"
elif what_config == "apt_updates":
data["icon"] = "mdi:update"
data["name"] = "APT Updates"
else: else:
return "" return ""
# Return our built discovery config # Return our built discovery config
@@ -509,7 +526,7 @@ def create_mqtt_client():
return client return client
def publish_update_status_to_mqtt(git_update): def publish_update_status_to_mqtt(git_update, apt_updates):
client = create_mqtt_client() client = create_mqtt_client()
if client is None: if client is None:
@@ -528,6 +545,13 @@ def publish_update_status_to_mqtt(git_update):
client.publish(config.mqtt_discovery_prefix + "/update/" + hostname + "/config", client.publish(config.mqtt_discovery_prefix + "/update/" + hostname + "/config",
config_json('update'), qos=1) config_json('update'), qos=1)
if config.apt_updates:
if config.discovery_messages:
client.publish(config.mqtt_discovery_prefix + "/sensor/" + config.mqtt_topic_prefix + "/" + hostname + "_apt_updates/config",
config_json('apt_updates'), qos=config.qos)
client.publish(config.mqtt_topic_prefix + "/" + hostname + "/apt_updates", apt_updates, qos=config.qos, retain=config.retain)
# Wait for all messages to be delivered # Wait for all messages to be delivered
while len(client._out_messages) > 0: while len(client._out_messages) > 0:
time.sleep(0.1) time.sleep(0.1)
@@ -781,7 +805,8 @@ def gather_and_send_info():
def update_status(): def update_status():
while not stop_event.is_set(): while not stop_event.is_set():
git_update = check_git_update(script_dir) git_update = check_git_update(script_dir)
publish_update_status_to_mqtt(git_update) apt_updates = get_apt_updates()
publish_update_status_to_mqtt(git_update, apt_updates)
stop_event.wait(config.update_check_interval) stop_event.wait(config.update_check_interval)
if stop_event.is_set(): if stop_event.is_set():
break break