[SOLVED] Hook custom script into notifications system

Nov 27, 2023
264
67
33
Netherlands
daniel-doggy.com
Hello everyone,

I have my Proxmox VE host running for a couple of months now and I am really happy with my setup so far.
Now that everything is working as I want, I finally have the time for some fine tuning of some of my processes, scripts and things.

One thing that I would like to do (and like to know if this is possible) is to "plug" my cloud-image update script into the notifications system.
So that after the update script runs it sends out a info notification when it did not need to update (and thus can be filtered out and send a webhook to my monitor) and a warning when it updated the cloud image (to send a "Hey, the cloud-images are updated" to my admin email).

And info on if this is possible and if so, how will be appreciated.
 
Thanks for the info.

Using sendmail would be a possibility for my script.
The only downside (for my use case) is that I cannot filter on severity as it is always unkown.

Ideally I would also be able to filter on severity since I also cannot filter on email subject if it is a system-mail notification trigger.
Since when it exits with anything except 0 it needs to notify my admin email and not notify my monitor (webhook)
And when it is 0 it only needs to notify my monitor (webhook) and only email the admin when it also updated the cloud-images.

But I guess something like this is not possible?
 
But I guess something like this is not possible?
Not really at this time, no. In theory you could write a small Perl script which calls into the notification system's API, but this is not really a supported use case and potentially prone to breakage with future updates. If you're interested I could still give you some hints for that.
 
Thanks for the info.

I am planning to add the notification system to my script without any hacky workarounds as stablity is the most important thing for me.

If this was supported natively then that would be the most ideal but given that is it not, it is just something I have to work around.
Since the script also run on my production servers, it is also important that it does not break whenever it feels like it.
And given that the API is not natively supported and thus may change whenever, it is not suitable for my usecase.
 
Last edited:
WARNING: This is not officially supported and may break at any time if package libpve-notify-perl changes!

For all people finding this thread here is a simple script allowing you to hook into the PVE notifications:

Create some custom notification templates
Bash:
mkdir -p /etc/pve/notification-templates/default

cat >/etc/pve/notification-templates/default/simple-subject.txt.hbs <<'EOF'
{{ title }}
EOF

cat >/etc/pve/notification-templates/default/simple-body.txt.hbs <<'EOF'
{{ message }}
EOF

cat >/etc/pve/notification-templates/default/simple-body.html.hbs <<'EOF'
<pre>{{ message }}</pre>
EOF

and create a script /usr/local/bin/pve-notify with the following content:

Bash:
#!/usr/bin/env bash
set -euo pipefail

# usage: pve-notify <info|notice|warning|error> <title> [message...]
LEVEL="${1:-}"
TITLE="${2:-}"
shift 2 || true
MESSAGE="${*:-}"

case "$LEVEL" in
  info|notice|warning|error) ;;
  *)
    echo "Usage: $0 <info|notice|warning|error> <title> [message...]" >&2
    exit 2
    ;;
esac

if [[ -z "$TITLE" ]]; then
  echo "Usage: $0 <info|notice|warning|error> <title> [message...]" >&2
  exit 2
fi

export LEVEL TITLE MESSAGE

perl -MPVE::Notify -e '
  my $sev = $ENV{LEVEL};

  my $common = PVE::Notify::common_template_data();
  my $data = {
    %$common,
    title   => ($ENV{TITLE} // ""),
    message => ($ENV{MESSAGE} // ""),
  };

  my $fields = { origin => "bash" }; # optional, kann für Matcher/Filter nützlich sein
  PVE::Notify::notify($sev, "simple", $data, $fields);
'

Than you can inject from any shell script (host backups, ceph monitoring, etc) a message over the default notification system.

Example:

Bash:
root@monastery ~ # pve-notify
Usage: /usr/local/bin/pve-notify <info|notice|warning|error> <title> [message...]
root@monastery ~ # pve-notify info "My important info" "All the dirty little details we need to know :)"
 
Last edited:
  • Like
Reactions: Lukas Wagner