Webhook integrations in Zabbix 4.4. Pushover integration for the warm up?

Vitaly Zhuravlev
5 min readOct 28, 2019

--

Ok, new functionality introduced, shall we start using? Zabbix webhook feature promises ability to build powerful two-way integrations but to make learning curve not that steep, let’s start with something easy, yet useful — Pushover.

With Pushover, you can be notified the most convenient way — with pushes straight to your mobile phone. Note though, the service isn’t free, yet, one time purchase is affordable.

Pushover setup

Start by creating an account on https://pushover.net/ if you don’t have it yet.

Once done, proceed by installing Pushover app on your phone.

At this point, we have an Application API Token (token) of Zabbix application and User Key.

You would need both in Zabbix pushover webhook we are about to write.

Zabbix setup

Create webhook

To create a simple webhook let’s just follow the documentation of both Zabbix here and Pushover.

Let’s create new media of type Webhook:

Script field is the most important one here, it should include few lines of code that will do HTTP POST to the Pushover endpoint:

try {params = JSON.parse(value),req = new CurlHttpRequest(),fields = {};req.AddHeader('Content-Type: application/json');fields.token= params.token;fields.user = params.user;fields.title =  params.title;fields.message =  params.message;fields.url = params.url;fields.url_title = params.url_title;fields.priority = params.priority;var resp = req.Post('https://api.pushover.net/1/messages.json',JSON.stringify(fields));if (req.Status() != 200) {throw JSON.parse(resp).errors[0];}resp = JSON.parse(resp);return "OK";} catch (error) {Zabbix.Log(3, 'Pushover notification failed : '+error);throw 'Pushover notification failed : '+error;}

The script here is dead simple it’s just Zabbix JS wrapper around Pushover API.

No magic really, just HTTP POST — and then if 200 OK is returned then your notification is sent successfully, otherwise an error is thrown, showing problem description returned by Pushover in both Zabbix frontend and in Zabbix server log (thanks to Zabbix.Log()).

Create global macro

All API tokens and passwords should be stored outside of the webhook, so it can be easily shared with Zabbix community. So let’s set global macro {$PUSHOVER.TOKEN} with your Application token you generated in Pushover.

Token example. Don’t worry, I changed it before taking the screenshot :)

To do that, go to Administration → General (Macros):

Testing

Thanks to media type test feature, you can test and troubleshoot your Pushover webhook immediately.

But note, that when testing here, provide actual ‘Application Token’ and ‘User Key’ instead of macros, as macros will not expand here.

Create user media

Has test been successful? Cool, finally, create media for the specific user. Here, media for user Zabbix Administrator is created.

Check trigger actions

Make sure proper trigger actions are set. For starters, you can enable default “Report problems to Zabbix administrators” rule.

Wait for some problem to happen

And here is long awaited result.

XML Import/Export of media type.

One more thing. Once all is working, the media type in Zabbix 4.4 can be exported to a file, just like a template! Use it to share your work with others.

Just make sure that all sensitive data like API tokens is outside of the XML.

<?xml version="1.0" encoding="UTF-8"?><zabbix_export><version>4.4</version><date>2019-10-17T11:49:12Z</date><media_types><media_type><name>Pushover</name><type>WEBHOOK</type><parameters><parameter><name>token</name><value>{$PUSHOVER.TOKEN}</value></parameter><parameter><name>user</name><value>{ALERT.SENDTO}</value></parameter><parameter><name>message</name><value>{ALERT.MESSAGE}</value></parameter><parameter><name>title</name><value>{ALERT.SUBJECT}</value></parameter><parameter><name>url</name><value>{$ZABBIX.URL}</value></parameter><parameter><name>url_title</name><value>Zabbix</value></parameter><parameter><name>priority</name><value>0</value></parameter></parameters><max_sessions>0</max_sessions><script>try {//  Zabbix.Log(127, 'Pushover webhook script value='+value);params = JSON.parse(value),req = new CurlHttpRequest(),fields = {};req.AddHeader('Content-Type: application/json');fields.token= params.token;fields.user = params.user;/*If a device name is not specified for a user, or the specified device name is no longer enabled/valid,notifications will be sent to all active devices for that user to avoid losing messages.Messages may be addressed to multiple specific devices by joining them with a comma (such as device=iphone,nexus5).fields.device = params.device;*/fields.title =  params.title;fields.message =  params.message;fields.url = params.url;fields.url_title = params.url_title;fields.priority = params.priority;var resp = req.Post('https://api.pushover.net/1/messages.json',JSON.stringify(fields));if (req.Status() != 200) {throw JSON.parse(resp).errors[0];}resp = JSON.parse(resp);return "OK";} catch (error) {Zabbix.Log(3, 'Pushover notification failed : '+error);throw 'Pushover notification failed : '+error;}</script><show_event_menu>NO</show_event_menu><description>Please refer to https://pushover.net/api and https://www.zabbix.com/documentation/current/manual/config/notifications/media/webhook#example_scripts.Set global macro {$PUSHOVER.TOKEN} with your Application key.When assigning Pushover media to the Zabbix user - add user key into send to field.</description></media_type></media_types></zabbix_export>

Conclusion

Thanks to Zabbix webhooks as well as media types export functionality it is becoming easier to build easy to share notifications. And just the way we like it — no external scripts and dependencies required. Import XML and you are good to go.

--

--