3

When using Bosun you can send a HTTP POST request using an alert notification with a specified json body like so:

post = myurl
body = {"foo": "bar"}

I have an external application which listens to myurl and sends and email based on the context of the post body. Is there a way to generically template the post body to represent the triggered alert details.

Ideally something like this (syntax is just for example purposes):

 post = myurl
 body = {"body": "Alert.name, Alert.host, ..."}
Chris Edwards
  • 1,518
  • 2
  • 13
  • 23

1 Answers1

3

What you want is is to use a post action within the definition of the notification. You can also override the default post body by using the body directive within the notification. You can then use the json template function and the contentType to set it to JSON.

Two examples follow, from the notification documentation. In particular, I think the second example is what you are after.

# post to a slack.com chatroom 
notification slack{
    post = https://company.slack.com/services/hooks/incoming-webhook?token=TOKEN
    body = payload={"username": "bosun", "text": {{.|json}}, "icon_url": "http://stackexchange.github.io/bosun/public/bosun-logo-mark.svg"} 
}

#post json
notification json{
    post = https://someurl.com/submit
    body = {"text": {{.|json}}, apiKey="2847abc23"}
    contentType = application/json
}
captncraig
  • 22,118
  • 17
  • 108
  • 151
Kyle Brandt
  • 26,938
  • 37
  • 124
  • 165
  • 2
    When it comes to the generic information in the body. I believe the same template variables you see exposed at the bottom of the config test page should be available to the body, for example `{{.Alert.Name}}` – Kyle Brandt Aug 18 '15 at 14:01
  • 2
    In a notification, the context of `{{.}}` is the rendered subject from the alert's template. You could certainly make a template with `subject = {{.|json}}`, but that would be a little odd. This may be the best case I have seen yet for merging [#1102](https://github.com/bosun-monitor/bosun/pull/1101) – captncraig Aug 18 '15 at 14:03