Network

Checkmk: Notifications about Mastodon

Notifications from Checkmk can also be displayed on Mastodon with little effort - even privately

Check mk offers all sorts of internal possibilities, to be informed about status changes. There are more in the Checkmk Exchange and the homemade one that we here for Telegram accomplished is also child's play (in the meantime, however unnecessary). Mastodon could be increasingly interesting, after all, you can also do it in-house!

Checkmk + Mastodon + toot

The procedure is very simple: First you need a Script, which reads and processes the data of the notification from Checkmk. In order for the script to be triggered in the event of a notification, you also need one Checkmk rule and the script must be in a specific directory lie.

The script base is the program toot, a simple command line client for Mastodon. We'll show you how to set up toot here.

create script

First, here is the script mastodon_notifier within the Checkmk instance, speak under OMD[mysite]:~/local/share/check_mk/notifications/mastodon_notifier:

#!/bin/bash
# Mastodoner
env | grep NOTIFY_ | sort > $OMD_ROOT/tmp/foobar.out
cat $OMD_ROOT/tmp/foobar.out |\
egrep "NOTIFY_HOSTNAME=|NOTIFY_LASTHOSTSTATE=|NOTIFY_HOSTSTATE=" |\
sed ' s/NOTIFY_HOSTNAME=/Host:/ ; s/NOTIFY_LASTHOSTSTATE=/Alt:/ ; s/NOTIFY_HOSTSTATE=/Neu:/ ' |\
/home/mirco/.nvm/versions/node/v18.14.0/bin/toot --config /home/mirco/.mastodon.json --visibility direct
exit 0

# Mastodon is the title as it is also displayed in Checkmk. The line with env gets the notification, sorts it and saves it to the file foobar.out.

Then foobar.out will be over cat spent, gooseberry reduced to the fields required here: host name, last and current status, thirst replaces the variable names with Host:, Old: and New:, to make it look pretty.

Then he follows toot call: Here you need the absolute path and the manual specification of the configuration file - after all, toot was installed under a user account (here "mirco") and not as an instance user (here "mysite"). The visibility get up here direct, so that the toot is only visible to directly mentioned users - in this case only to the sender himself.

Tip: You might think you could take the intermediate step over the file foobar.out save. But if you put egrep directly on the sort command, the script will be executed multiple times and toots will hail...

Tip 2: A nice overview of all available notification fields can be found in Checkmk at Setup/Events/Notifications/Show analysis. In the list of recent notifications, click one of the scrolls Icons, to see the full context:

fields of a notification.
The fields from the example and all others

Don't forget: Make the script executable!

chmod +x mastodon_notifier

That was it in the terminal.

create rule

Now create one Notifications rule and puts as Notification method Your script – plus any filters.

rule in checkmk.
Notification rule for the mastodon script

And the result on Mastodon:

notifications from checkmk in mastodon.
Of course there is more eye candy ;)

More to Mastodon and Check mk.

Mirco Lang

Freelance journalist, Sauerland exile, (fairly old) skateboarder, graduate computer scientist, retail salesman, open source nerd, Checkmk handbook writer. Ex-Saturn'ler, Ex-Data-Becker'ler, Ex-BSI'ler. First contact with computers: ca. 1982 - a friend's big brother's C64. If you want to read more about open source, Linux and craft stuff and support Tutonaut here: About Coffee sponsorship via Paypal.I'm always happy. In advance: Thank you! Do not miss: cli.help and VoltAmpereWatt.de. New: Mastodon

Leave a Comment

Your e-mail address will not be published. Required fields are marked with * marked

This website uses Akismet to reduce spam. Learn more about how your comment data is processed.

Back to Top button
Close

Oops!

Please deactivate your ad blocker.