Gotify is a selfhosted, open source notification service. You can set it up with docker and use the API to send notifications from other applications to your phone.
Therfor your application, just needs to call the api of gotify and it will send a notification to the app on your android or ios phone.
This post will show you how to setup gotify and use it to transmit notifications from nagios.
Installation with Docker Compose
The installation with Docker Compose is quite simple.
I copied the docker-compose.yml from the official documentation.
version: "3"
services:
gotify:
image: gotify/server
ports:
- 8080:80
environment:
- GOTIFY_DEFAULTUSER_PASS=custom
volumes:
- "./gotify_data:/app/data"
You should change the environment variable GOTIFY_DEFAULTUSER_PASS
to something else, to make it more secure.
There are far more variables you can set, but you don’t have to. To see all of the have a look at the documentation.
After that we can start the container.
$ docker compose up -d
You can open the webinterface in your browser of choice. Just go to <your-servers-ip:8080
.
It will ask you for a login. The default user is admin
and the password is the one you specified as GOTIFY_DEFAULTUSER_PASS
.
Create an Application
Next we will create an application. To do so click on the Button labled APPS
in the top bar and then click on CREATE APPLICATION
. It will ask you for a name and an optional description. I will call mine Nagios, and leave the description empty for now.
Once that is done, you will see it in a list of all your applications. You can also copy it’s token, which we will need later.
Install the mobile App
To test the notifications, we should install the mobile app next. I can only describe the process on an android phone, but I’m sure the ios app will be similar.
First download the app. Just search for “Gotify” in the Play Store.
Next open the app. It will ask for the Gotify URL
. This is the url of your server, the one you used to open the webinterface. Next click on Check URL
.
If that was successful, it will ask you for a username an password. Either create a new user in the Webinterface or simply use the admin user again. Than Click on Login
.
Next you can choose your client name. This is handy to identify your phone if you have a longer list of clients connected to your gotify server.
Of course you will need to allow notifications from the app.
If all that worked you will see a list of all your notifications, which might be empty, since we didn’t send any yet.
Testing Notifications
To test the notifications, we will use curl first.
$ curl "https://<your-server-url>/message?token=<your-application-token>" -F "title=Curl Test" -F "message=This is a test with curl and gotify" -F "priority=5"
The <your-application-token
is the one we created in the web interface. Just copy it from there.
The field priority
is optional. If you do not provide on, it will take the default priority of your application.
This should trigger a notification on your phone.
Setup Nagios to use Gotify
Now I want to use Gotify to recieve notifications from nagios.
Therefor I need to login to my nagios server and go to my configuration files. Usually in /etc/nagios/
.
I will create a new file named gotify.cfg
in one of the imported directories, like objects
.
First thing is to create a contact, if you do not have one already.
define contact {
contact_name jannik
use gotify-contact ; use the gotify-contact template below
}
define contact {
name gotify-contact
service_notification_period 24x7 ; always recieve notifications
service_notification_options u,w,c,f,r ; recieve unknown, warning, critical, fatal, resolved
service_notfication_command gotify-notification-command ; how to notify
register 0 ; use this only as a template
}
Now we have a contact that we be notified when something happens. Next we need to define the commands, how the contact should get notified.
define command {
command_name gotify-notification-command
command_line /usr/lib64/nagios/plugins/send-gotify-service.sh "$HOSTNAME$" "$SERVICEDESC$" "$SERVICESTATE$" "$SERVICEOUTPUT$" "$TOTALSERVICEPROBLEMS$" "$NOTIFICATIONTYPE$"
}
This tells nagios to call the script send-gotify-service.sh
, which we will create next.
You could also call curl directly, but the script allows us to do some formating.
All the parameters are used to get the necessary fields from nagios into the script.
Lets create the script.
$ vim /usr/lib64/nagios/plugins/send-gotify-service.sh
#!/bin/bash
NAGIOS_HOSTNAME=$1
NAGIOS_SERVICEDESC=$2
NAGIOS_SERVICESTATE=$3
NAGIOS_SERVICEOUTPUT=$4
NAGIOS_TOTALSERVICEPROBLEMS=$5
NAGIOS_NOTIFICATIONTYPE=$6
TITLE=$(echo "$NAGIOS_NOTIFICATIONTYPE $NAGIOS_SERVICESTATE - $NAGIOS_HOSTNAME - $NAGIOS_SERVICEDESC")
MESSAGE=$(printf "$NAGIOS_SERVICEOUTPUT\nTotal Problems: $NAGIOS_TOTALSERVICEPROBLEMS")
curl "https://<your-server-url>/message?token=<your-application-token>" -F "title=${TITLE}" -F "message=${MESSAGE}"
exit $?
Now lets apply all these changes to our nagios configuration.
$ systemctl reload nagios
And that’t it. Now you will recieve notifications with gotify.