server icon indicating copy to clipboard operation
server copied to clipboard

Gotify Notification for unattended-upgrades

Open ImFilippo opened this issue 3 years ago • 6 comments

Have you read the documentation?

  • [X] Yes, but it does not include related information regarding my question.
  • [ ] Yes, but the steps described in the documentation do not work on my machine.
  • [ ] Yes, but I am having difficulty understanding it and wants clarification.

You are setting up gotify in

  • [X] Docker
  • [ ] Linux native platform
  • [ ] Windows native platform

Describe your problem Hi, I am struggling with my first self-hosted server and I have set up automatic system updates by installing and configuring unattended-upgrades; I would however like to get a notification on gotify with the updates it has installed. Is this possible? By default it is possible to send an email with unattended-upgrades but I would prefer to use gotify to have all notifications with one service. Thank you.

ImFilippo avatar Sep 15 '22 15:09 ImFilippo

Look at DPkg::Post-Invoke and APT::Update::Post-Invoke

schoerg avatar Sep 15 '22 15:09 schoerg

Look at DPkg::Post-Invoke and APT::Update::Post-Invoke

And what script should i run after that two commands?

ImFilippo avatar Sep 15 '22 15:09 ImFilippo

Something that sends you a notification.

schoerg avatar Sep 15 '22 20:09 schoerg

Something that sends you a notification.

Thanks, up to this point I was there... I can't write a script and was wondering if there was something reusable with some slight tweaks.

ImFilippo avatar Sep 16 '22 13:09 ImFilippo

Example: https://gotify.net/docs/pushmsg

https://gotify.net/docs/more-pushmsg

schoerg avatar Sep 16 '22 13:09 schoerg

Example: https://gotify.net/docs/pushmsg

https://gotify.net/docs/more-pushmsg

I could use a bash script with DPkg::Post-Invoke, but how can i list all upgraded programs?

#!/bin/bash
TITLE="unattended-upgrades"
MESSAGE="Upgraded Programs: ![](https://gotify.net/img/logo.png)"
PRIORITY=5
URL="http://localhost:8008/message?token=<apptoken>"

curl -s -S --data '{"message": "'"${MESSAGE}"'", "title": "'"${TITLE}"'", "priority":'"${PRIORITY}"', "extras": {"client::display": {"contentType": "text/markdown"}}}' -H 'Content-Type: application/json' "$URL"

ImFilippo avatar Sep 16 '22 13:09 ImFilippo

For a simple message using gotify-cli:

cat /var/log/unattended-upgrades/unattended-upgrades.log | grep Packages | gotify push

If you also want to be notified when no packages were upgraded, substitute

grep -i packages

For a markdown message, edit your above script with

MESSAGE=$(cat /var/log/unattended-upgrades/unattended-upgrades.log | grep Packages)

pdfrg avatar Oct 08 '22 14:10 pdfrg

For a markdown message, edit your above script with

Something like this? DPkg::Post-Invoke {"/home/script/gotify-update.sh";};

Script (gotify-update.sh)

#!/bin/bash
TITLE="unattended-upgrades"
PRIORITY=5
URL="http://localhost:8008/message?token=<apptoken>"

curl -s -S --data '{"MESSAGE=$(cat /var/log/unattended-upgrades/unattended-upgrades.log | grep Packages)}' -H 'Content-Type: application/json' "$URL"

ImFilippo avatar Oct 08 '22 19:10 ImFilippo

Sure, but this curl command will not include the TITLE or PRIORITY you assigned. I think they are optional, however. You also need to replace <apptoken> with your actual token.

Edit: sorry, looking at it again, I think your curl command is wrong. I think you need the syntax from your prior comment curl -s -S --data '{"message": "'"${MESSAGE}"'", "title": "'"${TITLE}"'", "priority":'"${PRIORITY}"', "extras": {"client::display": {"contentType": "text/markdown"}}}' -H 'Content-Type: application/json' "$URL"

Try them and see what happens. Just run the gotify-update.sh script without waiting for it to be invoked by dpkg.

pdfrg avatar Oct 09 '22 04:10 pdfrg

Sure, but this curl command will not include the TITLE or PRIORITY you assigned. I think they are optional, however. You also need to replace <apptoken> with your actual token.

Edit: sorry, looking at it again, I think your curl command is wrong. I think you need the syntax from your prior comment curl -s -S --data '{"message": "'"${MESSAGE}"'", "title": "'"${TITLE}"'", "priority":'"${PRIORITY}"', "extras": {"client::display": {"contentType": "text/markdown"}}}' -H 'Content-Type: application/json' "$URL"

Try them and see what happens. Just run the gotify-update.sh script without waiting for it to be invoked by dpkg.

I've tried to create this gotify-unattended-upgrades.sh script:

#!/bin/bash
TITLE="unattended-upgrades"
URL="http://192.168.1.30:8900/message?token=REf4geDXfe5Pn8O>"

curl -s -S --data '{"message": "'"${MESSAGE=$(cat /var/log/unattended-upgrades/unattended-upgrades.log | grep Packages)}"'", "title": "'"${TITLE}"'", "priority":'"${PRIORITY}"', "extras": {"client::display": {"contentType": "text/markdown"}}}' -H 'Content-Type: application/json' "$URL"

but when i run it (sh gotify-unattended-upgrades.sh), I got this error: {"error":"Bad Request","errorCode":400,"errorDescription":"invalid character ',' looking for beginning of value"}

The syntax is not correct, where should i put the MESSAGE command? Thank you!

ImFilippo avatar Oct 09 '22 14:10 ImFilippo

Everything is right, you just didn't define PRIORITY.

When I run your script, with just the addition of

PRIORITY=5

It works as desired.

pdfrg avatar Oct 10 '22 12:10 pdfrg

Everything is right, you just didn't define PRIORITY.

When I run your script, with just the addition of

PRIORITY=5

It works as desired.

Thank you, with the priority now the script works. However, what should i do with the DPkg::Post-Invoke and APT::Update::Post-Invoke? I've tried to create a file (/etc/apt/apt.conf.d/100update) with this:

DPkg::Post-Invoke {"/home/filippo/script/gotify-unattended-upgrades.sh";};
APT::Update::Post-Invoke {"/home/filippo/script/gotify-unattended-upgrades.sh";};

but this is only what i get in gotify:

gotify

while with email notification I get all correct info:

Unattended upgrade result: All upgrades installed

Warning: A reboot is required to complete this upgrade, or a previous one.

Packages that were upgraded:
cloud-init linux-generic linux-headers-generic linux-image-generic
linux-libc-dev

Packages that were auto-removed:
linux-image-5.15.0-47-generic linux-modules-extra-5.15.0-47-generic
linux-modules-5.15.0-47-generic linux-headers-5.15.0-47-generic
linux-headers-5.15.0-47

Package installation log:
Log started: 2022-10-16 02:40:10
Preparing to unpack .../linux-libc-dev_5.15.0-50.56_amd64.deb ...
Unpacking linux-libc-dev:amd64 (5.15.0-50.56) over (5.15.0-48.54) ...
Setting up linux-libc-dev:amd64 (5.15.0-50.56) ...
{"error":"Bad Request","errorCode":400,"errorDescription":"invalid character '\\n' in string literal"}NEEDRESTART-VER: 3.5
NEEDRESTART-KCUR: 5.15.0-48-generic
NEEDRESTART-KEXP: 5.15.0-48-generic
NEEDRESTART-KSTA: 1
NEEDRESTART-UCSTA: 1
NEEDRESTART-UCCUR: 0x00f0
NEEDRESTART-UCEXP: 0x00f0
{"error":"Bad Request","errorCode":400,"errorDescription":"invalid character '\\n' in string literal"}Log ended: 2022-10-16 02:40:13

Log started: 2022-10-16 02:40:14
Selecting previously unselected package linux-modules-5.15.0-50-generic.
Preparing to unpack .../0-linux-modules-5.15.0-50-generic_5.15.0-50.56_amd64.deb ...
Unpacking linux-modules-5.15.0-50-generic (5.15.0-50.56) ...
Selecting previously unselected package linux-image-5.15.0-50-generic.
Preparing to unpack .../1-linux-image-5.15.0-50-generic_5.15.0-50.56_amd64.deb ...
Unpacking linux-image-5.15.0-50-generic (5.15.0-50.56) ...
Selecting previously unselected package linux-modules-extra-5.15.0-50-generic.
Preparing to unpack .../2-linux-modules-extra-5.15.0-50-generic_5.15.0-50.56_amd64.deb ...
Unpacking linux-modules-extra-5.15.0-50-generic (5.15.0-50.56) ...
Preparing to unpack .../3-linux-generic_5.15.0.50.50_amd64.deb ...
Unpacking linux-generic (5.15.0.50.50) over (5.15.0.48.48) ...
Preparing to unpack .../4-linux-image-generic_5.15.0.50.50_amd64.deb ...
Unpacking linux-image-generic (5.15.0.50.50) over (5.15.0.48.48) ...
Selecting previously unselected package linux-headers-5.15.0-50.
Preparing to unpack .../5-linux-headers-5.15.0-50_5.15.0-50.56_all.deb ...
Unpacking linux-headers-5.15.0-50 (5.15.0-50.56) ...
Selecting previously unselected package linux-headers-5.15.0-50-generic.
Preparing to unpack .../6-linux-headers-5.15.0-50-generic_5.15.0-50.56_amd64.deb ...
Unpacking linux-headers-5.15.0-50-generic (5.15.0-50.56) ...
Preparing to unpack .../7-linux-headers-generic_5.15.0.50.50_amd64.deb ...
Unpacking linux-headers-generic (5.15.0.50.50) over (5.15.0.48.48) ...
Setting up linux-headers-5.15.0-50 (5.15.0-50.56) ...
Setting up linux-headers-5.15.0-50-generic (5.15.0-50.56) ...
Setting up linux-headers-generic (5.15.0.50.50) ...
Setting up linux-image-5.15.0-50-generic (5.15.0-50.56) ...
I: /boot/vmlinuz.old is now a symlink to vmlinuz-5.15.0-48-generic
I: /boot/initrd.img.old is now a symlink to initrd.img-5.15.0-48-generic
I: /boot/vmlinuz is now a symlink to vmlinuz-5.15.0-50-generic
I: /boot/initrd.img is now a symlink to initrd.img-5.15.0-50-generic
Setting up linux-modules-5.15.0-50-generic (5.15.0-50.56) ...
Setting up linux-modules-extra-5.15.0-50-generic (5.15.0-50.56) ...
Setting up linux-image-generic (5.15.0.50.50) ...
Setting up linux-generic (5.15.0.50.50) ...
Processing triggers for linux-image-5.15.0-50-generic (5.15.0-50.56) ...
/etc/kernel/postinst.d/initramfs-tools:
update-initramfs: Generating /boot/initrd.img-5.15.0-50-generic
/etc/kernel/postinst.d/zz-update-grub:
Sourcing file `/etc/default/grub'
Sourcing file `/etc/default/grub.d/init-select.cfg'
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-5.15.0-50-generic
Found initrd image: /boot/initrd.img-5.15.0-50-generic
Found linux image: /boot/vmlinuz-5.15.0-48-generic
Found initrd image: /boot/initrd.img-5.15.0-48-generic
Found linux image: /boot/vmlinuz-5.15.0-47-generic
Found initrd image: /boot/initrd.img-5.15.0-47-generic
Warning: os-prober will not be executed to detect other bootable partitions.
Systems on them will not be added to the GRUB boot configuration.
Check GRUB_DISABLE_OS_PROBER documentation entry.
Adding boot menu entry for UEFI Firmware Settings ...
done
{"error":"Bad Request","errorCode":400,"errorDescription":"invalid character '\\n' in string literal"}NEEDRESTART-VER: 3.5
NEEDRESTART-KCUR: 5.15.0-48-generic
NEEDRESTART-KEXP: 5.15.0-50-generic
NEEDRESTART-KSTA: 3
NEEDRESTART-UCSTA: 1
NEEDRESTART-UCCUR: 0x00f0
NEEDRESTART-UCEXP: 0x00f0
{"error":"Bad Request","errorCode":400,"errorDescription":"invalid character '\\n' in string literal"}Log ended: 2022-10-16 02:41:04

Log started: 2022-10-16 02:41:04
Preconfiguring packages ...
Preconfiguring packages ...
Preparing to unpack .../cloud-init_22.3.4-0ubuntu1~22.04.1_all.deb ...
Unpacking cloud-init (22.3.4-0ubuntu1~22.04.1) over (22.2-0ubuntu1~22.04.3) ...
Setting up cloud-init (22.3.4-0ubuntu1~22.04.1) ...
Installing new version of config file /etc/cloud/cloud.cfg ...
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for rsyslog (8.2112.0-2ubuntu2.2) ...
{"error":"Bad Request","errorCode":400,"errorDescription":"invalid character '\\n' in string literal"}NEEDRESTART-VER: 3.5
NEEDRESTART-KCUR: 5.15.0-48-generic
NEEDRESTART-KEXP: 5.15.0-50-generic
NEEDRESTART-KSTA: 3
NEEDRESTART-UCSTA: 1
NEEDRESTART-UCCUR: 0x00f0
NEEDRESTART-UCEXP: 0x00f0
{"error":"Bad Request","errorCode":400,"errorDescription":"invalid character '\\n' in string literal"}Log ended: 2022-10-16 02:41:09

Log started: 2022-10-16 02:41:09
Removing linux-modules-extra-5.15.0-47-generic (5.15.0-47.51) ...
Removing linux-image-5.15.0-47-generic (5.15.0-47.51) ...
/etc/kernel/postrm.d/initramfs-tools:
update-initramfs: Deleting /boot/initrd.img-5.15.0-47-generic
/etc/kernel/postrm.d/zz-update-grub:
Sourcing file `/etc/default/grub'
Sourcing file `/etc/default/grub.d/init-select.cfg'
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-5.15.0-50-generic
Found initrd image: /boot/initrd.img-5.15.0-50-generic
Found linux image: /boot/vmlinuz-5.15.0-48-generic
Found initrd image: /boot/initrd.img-5.15.0-48-generic
Warning: os-prober will not be executed to detect other bootable partitions.
Systems on them will not be added to the GRUB boot configuration.
Check GRUB_DISABLE_OS_PROBER documentation entry.
Adding boot menu entry for UEFI Firmware Settings ...
done
Removing linux-modules-5.15.0-47-generic (5.15.0-47.51) ...
{"error":"Bad Request","errorCode":400,"errorDescription":"invalid character '\\n' in string literal"}{"error":"Bad Request","errorCode":400,"errorDescription":"invalid character '\\n' in string literal"}Log ended: 2022-10-16 02:41:17

Log started: 2022-10-16 02:41:17
Removing linux-headers-5.15.0-47-generic (5.15.0-47.51) ...
dpkg: warning: while removing linux-headers-5.15.0-47-generic, directory '/lib/modules/5.15.0-47-generic' not empty so not removed
{"error":"Bad Request","errorCode":400,"errorDescription":"invalid character '\\n' in string literal"}{"error":"Bad Request","errorCode":400,"errorDescription":"invalid character '\\n' in string literal"}Log ended: 2022-10-16 02:41:19

Log started: 2022-10-16 02:41:20
Removing linux-headers-5.15.0-47 (5.15.0-47.51) ...
{"error":"Bad Request","errorCode":400,"errorDescription":"invalid character '\\n' in string literal"}{"error":"Bad Request","errorCode":400,"errorDescription":"invalid character '\\n' in string literal"}Log ended: 2022-10-16 02:41:23



Unattended-upgrades log:
Starting unattended upgrades script
Allowed origins are: o=Ubuntu,a=jammy, o=Ubuntu,a=jammy-security, o=UbuntuESMApps,a=jammy-apps-security, o=UbuntuESM,a=jammy-infra-security, o=Ubuntu,a=jammy-updates
Initial blacklist:
Initial whitelist (not strict):
Packages that will be upgraded: cloud-init linux-generic linux-headers-generic linux-image-generic linux-libc-dev
Writing dpkg log to /var/log/unattended-upgrades/unattended-upgrades-dpkg.log
All upgrades installed
Packages that were successfully auto-removed: linux-headers-5.15.0-47 linux-headers-5.15.0-47-generic linux-image-5.15.0-47-generic linux-modules-5.15.0-47-generic linux-modules-extra-5.15.0-47-generic
Packages that are kept back:

ImFilippo avatar Oct 16 '22 13:10 ImFilippo

You initially asked "but how can I list all upgraded programs?" Which is why I included the grep filter to list only those packages. However, if you want to send the entire log output through Gotify, you will need to modify the script.

It looks like the email text you have above is basically the two logs found under /var/log/unattended-upgrades, unattended-upgrades-dpkg.log unattended-upgrades.log

The latter file will contain entries for multiple days, so you will need to filter for just today's output.

So...

#!/bin/bash
TITLE="unattended-upgrades"
URL="http://192.168.1.30:8900/message?token=REf4geDXfe5Pn8O>"
PRIORITY=5
TODAY=$(date "+%Y-%m-%d")

cat /var/log/unattended-upgrades/unattended-upgrades-dpkg.log > ~/unattended-upgrades.log
cat /var/log/unattended-upgrades/unattended-upgrades.log | grep $TODAY >> ~/unattended-upgrades.log

MESSAGE=$(cat ~/unattended-upgrades.log)

curl "$URL" -F "title=$TITLE" -F "message=$MESSAGE" -F "priority=$PRIORITY" >/dev/null 2>&1

rm ~/unattended-upgrades.log

pdfrg avatar Oct 16 '22 16:10 pdfrg

Thank you a lot, now everything is working!

ImFilippo avatar Oct 16 '22 18:10 ImFilippo