warthog618 / dunnart

Lightweight remote system monitoring over MQTT for Home Assistant
MIT License
11 stars 2 forks source link

Feature request: Is update/upgrade avaiable #4

Open Mimosinito opened 3 months ago

Mimosinito commented 3 months ago

Feature request: Is update/upgrade avaiable

warthog618 commented 3 months ago

You need to define what you mean by update/upgrade and why that needs to be a feature of dunnart itself.

My immediate reaction is that a general solution would require a huge amount of work, and I don't see it being worth the effort, but perhaps what you have in mind is very different.

warthog618 commented 3 months ago

Do you mean add a system check that the packaging system recognizes that some packages could be updated? In that case you need to specify which packaging systems you want to support.

Mimosinito commented 3 months ago

Thanx for your quick reply. Yes, i do mean "is an update avaiable". Like is a new firmware avaiable in Home Assistant. For my purpose Debian/Ubuntu apt is the only platform i use.

warthog618 commented 3 months ago

Ok, so what command can be used to determine if there are updates available? I would rather not use anything that requires root access, as dunnart doesn't run as root - at least not how I use it.

Mimosinito commented 3 months ago

apt list --upgradeable will run as non root. But I have to wait for some upgradeable packages to see what it will list, when some are available.

warthog618 commented 3 months ago
pi@devpi:~ $ apt list --upgradable | grep upgradable

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

less/stable 590-2.1~deb12u2 armhf [upgradable from: 590-2]
libdav1d6/stable 1.0.0-2+rpi1+deb12u1 armhf [upgradable from: 1.0.0-2+rpi1]
libglib2.0-0/stable 2.74.6-2+deb12u2 armhf [upgradable from: 2.74.6-1]
pi@devpi:~ $

So you get a warning that you shouldn't rely on apt output.

And the return code is always 0 as far as I can tell, independent of whether updates are available or not.

Mimosinito commented 3 months ago

Strange

I put it into t.sh (for testing in Debian Bullseye german version) and get following output: user@server:~ ./t.sh Auflistung… Fertig containerd.io/bullseye 1.6.33-1 amd64 [aktualisierbar von: 1.6.32-1] docker-buildx-plugin/bullseye 0.14.1-1~debian.11~bullseye amd64 [aktualisierbar von: 0.14.0-1~debian.11~bullseye] docker-ce-cli/bullseye 5:26.1.4-1~debian.11~bullseye amd64 [aktualisierbar von: 5:26.1.3-1~debian.11~bullseye] docker-ce/bullseye 5:26.1.4-1~debian.11~bullseye amd64 [aktualisierbar von: 5:26.1.3-1~debian.11~bullseye] docker-compose-plugin/bullseye 2.27.1-1~debian.11~bullseye amd64 [aktualisierbar von: 2.27.0-1~debian.11~bullseye]

Maybe you will count the lines…

warthog618 commented 3 months ago

Not strange, given that apt does not have a stable CLI interface.

What is in your t.sh? Cos I get the same thing if I run it from within a t.sh.

I am trying to count the lines - the ones containing "upgradable", and that is when I the warning - if you pipe the apt output then you get the warning not to rely on that.

warthog618 commented 3 months ago

If we can't find a suitable command, how about a generic hook where dunnart executes a command and uses the exit status to control the value of an input for HA? Then what you put in your command is up to you and can be customised to suit your application.

Mimosinito commented 3 months ago

That's a good idea.

warthog618 commented 3 months ago

Ok, I'll look into doing that.

warthog618 commented 3 months ago

I've committed a cmd module in 67759a7 that can run arbitrary commands and set a binary sensor in HA based on their exit code. It is preliminary, and not documented yet, so I'm not guaranteeing it wont change, but you might want to try it to see if it does what you need.

I add this to my config to get it to report the apt status:

cmd:
    binary_sensors: [apt_status]
    apt_status:
        name: "apt status"
        cmd: /opt/dunnart/apt_status.sh
        device_class: update

with cmd added to the list of modules to be loaded.

The apt_status.sh script is:

#!/bin/sh
apt list --upgradable 2>&1 | grep -q upgradable 

You would change that grep to "aktualisierbar" for your German apt, and change the path to the script as required.

All that gives me an apt update sensor in HA.

Mimosinito commented 3 months ago

Thanx, i will try it. And report!

warthog618 commented 3 months ago

After some more digging around, I've discovered the apt -qq option. With that, apt -qq list --upgradable will only return the packages to be updated, one per line, which is predicable enough to base a sensor off. So I've added a couple of sensors to the sys_info module; _aptstatus which gives a boolean upgrade available indicator, and _aptupgradable that provides a count of how many packages can be upgraded.

To use them you just need to add them to your config, e.g.

sys_info:
  entities: [apt_status, apt_upgradable, kernel_release, os_release]

which is a bit simpler than the cmd approach.

Mimosinito commented 3 months ago

I will try it the next week.