bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/docs/notifications.md (about)

     1  ---
     2  layout: default
     3  title: Customizing Notifications
     4  ---
     5  {% raw %}
     6  
     7  # Customizing notifications
     8  
     9  Bosun supports full customization over notification contents, and provides a templating engine to generate whatever
    10  content you need from an alert. Multiple components affect how your notifications look:
    11  
    12  1. **Alerts** reference exactly one *template* and any number of *notifications*.
    13  2. **Templates** define *what content* to generate for an alert.
    14  3. **Notifications** define *which* content to send, and *where* to send it.
    15  
    16  We support sending notifications via:
    17  
    18  - Email
    19  - HTTP Request
    20  - Printing to console
    21  
    22  There are also various different types of notification events, each having different kinds of underlying data and semantics:
    23  
    24  - **Alert Notifications** are sent whenever an alert triggers (or gets worse). The content is rendered at the time the alert triggers, and stored for future re-use.
    25  - **Action Notifications** are sent when a user performs an action in bosun (Ack, Close, etc..). For these, the content is rendered on-demand as actions occur.
    26  - **Unknown Notifications** occur as alerts go "unknown" because data is not available. These get batched up and sent periodically.
    27  
    28  # Alert
    29  
    30  Alerts mostly define the *rules* that we alert on. We can control notifications with the `template`, `critNotification`, and `warnNotification` keys.
    31  
    32  ```
    33  alert high_cpu {
    34      crit = 1
    35      template = high_cpu
    36      critNotification = email,slack 
    37  }
    38  ```
    39  
    40  defines an alert linked to the `high_cpu` template, and the `email` and `slack` notifications.
    41  
    42  # Templates
    43  
    44  Templates define specific templates for rendering your content. For guidance on constructing templates, see the [relevant documentation](definitions#templates). A template is essentially a set
    45  of key/value pairs. It may define as many keys as it wishes with whatever keys it likes. There are a few special keys however:
    46  
    47  - `body` and `subject` are the only required template keys. These are what show up on the bosun dashboard, as well as the default for most other notifications.
    48  - `emailBody` and `emailSubject` are not required, but if present, they will be used as the body and subject for email notifications.
    49  
    50  Any other template key will may be defined and will be used by any notifications that select it.  
    51  
    52  ## Template inheritance
    53  
    54  A template may `inherit` another template, which copies all of the key/value pairs into the child template. This is useful if you have some set of common formatting templates that may be shared among multiple templates. An example:
    55  
    56  ~~~
    57  # base template for all slack notifications. Creates json message using alert subject.
    58  template slack {
    59    slackBody = `{
    60    "text": "{{.Subject}} <{{.Incident}}|view in bosun>",
    61    "username": "bosun",
    62    "icon_url": "https://i.imgur.com/ogj0wkj.png",
    63  }`
    64  }
    65  
    66  template high_cpu {
    67      body = {{.Subject}}
    68      subject = `High CPU on {{.Group.host}}`
    69      # inherit slack template
    70      inherit = slack
    71  }
    72  
    73  notification slack {
    74    post = ${sys.SLACK_URL}
    75    #select slack body template
    76    bodyTemplate = slackBody
    77  }
    78  
    79  alert high_cpu {
    80    crit = avg(series("host=server01", epoch(), 1))
    81    template = high_cpu
    82    critNotification = slack
    83  }
    84  ~~~
    85  
    86  ## Text vs HTML templates
    87  
    88  There are a few situations where it matters if we use *plain text* templates, or *html* templates. Html templates perform some extra sanitization for when we expect to display the content, and they also perform css-inlining to be more compatible with email clients. The rules are simple:
    89  
    90  1. `body` and `emailBody` are always rendered as html templates.
    91  1. Any custom template key ending with `HTML` (like `myCustomHTML`) will be rendered as html.
    92  1. Anything else (including `subject`) will be rendered as plain-text.
    93  
    94  # Notifications
    95  
    96  A notification's job is to choose what content gets sent, and where to send it. It is common to make a unique notification for each unique email address or list that bosun sends to, and for each url/api it calls. For alerts, the notification choses which of the pre-rendered templates to send. For actions and unknowns, it will pick the right template, and render it on the fly. Rules for template selection are as follows:
    97  
    98  ## Email Alerts
    99  
   100  For alert emails, the subject and body are chosen according to the following priorities:
   101  
   102  ### Subject
   103  
   104  1. If the notification sets `emailSubjectTemplate`, use that key from the template.
   105  1. If the associated template has an `emailSubject` key defined, use that.
   106  1. Otherwise, the `subject` template will be used. It will be rendered with the `.IsEmail` flag set, if you would like to customize part of it for email. 
   107  
   108  ### Body
   109  
   110  1. If the notification sets the `bodyTemplate`,  use that key.
   111  1. If the associated template has an `emailBody` key defined, use that.
   112  1. Otherwise use the `body` template, rendering with `.IsEmail` set to `true`.
   113  
   114  ## HTTP Alerts
   115  
   116  Bosun can send http notifications using the following precedence rules:
   117  
   118  ### URL
   119  
   120  1. If the notification sets `postTemplate` or `getTemplate`, those rendered templates will be used as the notification url.
   121  1. Otherwise the plain `post` or `get` values are used.
   122  
   123  ### Post Body
   124  
   125  1. If the notification sets `bodyTemplate`, use that rendered template as the post body.
   126  1. Otherwise use the rendered `subject` template.
   127  
   128  ## Action Notifications
   129  
   130  Action notifications are a little different than alert notifications. They are rendered as actions happen, and they use a different context than the alert templates, and has the following data available:
   131  
   132  - `{{.States}}` is a list of all incidents affected.
   133  - `{{.User}}` is the user who performed the action.
   134  - `{{.Message}}` is the message they entered.
   135  - `{{.ActionType}}` is the type of action.
   136  
   137  If multiple actions are performed at once, they are grouped together by default. You can disable this, and send a notification for each individual alert key by setting `groupActions = false` in the notification. You can get the first incident from `States` with `{{$first := index .States 0}}` if this is the case.
   138  
   139  You can choose whether a notification sends action notifications or not on a per-action basis using the `runOnActions` key. You may set it to `all` or `none`, or to any comma separated list of action types from `Ack`, `Close`, `Forget`, `ForceClose`, `Purge`, `Note`, `DelayedClose`, or `CancelClose`.
   140  
   141  If you do not override anything in the notification, bosun will use its' own built in action template for action notifications. You can otherwise specify a template to use for all actions, or to override only spcific actions. You may customize a number of fields individually as well. The general form for these keys is:
   142  
   143  `action{TemplateType}{ActionType?}`
   144  
   145  Where "templateType" is one of `Body`, `Get`, `Post`, or `EmailSubject`, and "ActionType" if present, is one of `Ack`, `Close`, `Forget`, `ForceClose`, `Purge`, `Note`, `DelayedClose`, or `CancelClose`. If Action Type is not specified, it will apply to all actions types, unless specifically overridden.
   146  
   147  For example, setting `actionBody = keyX`, will use the `keyX` template for all action notification bodies for all action types, but `actionBodyAck = keyY`, will use the `keyY` template only for acknowledge actions.
   148  
   149  Example Slack Action notification:
   150  
   151  ```
   152  template slack {
   153    #format action like "steve_brown Acknowledged incident 45 (High CPU on server01): "I was running a load test"
   154    slackActionBody = `{{$first := index .States 0}}{
   155      "text": "{{.User}} {{.ActionType}} <{{.IncidentLink $first.Id}}|incident {{$first.Id}}> ({{$first.Subject}}): \"{{.Message}}\"",
   156      "username": "bosun",
   157      "icon_url": "https://i.imgur.com/ogj0wkj.png",
   158    }`
   159  }
   160  
   161  notification slack {
   162    post = ${sys.SLACK_URL}
   163    bodyTemplate = slackBody
   164    runOnActions = Ack
   165    groupActions = false
   166    actionBody = slackActionBody
   167  }
   168  ```
   169  
   170  ## Unknown Notifications
   171  
   172  When an alert goes "unknown", it will send a special notification through `critNotification` to let you know. Similar to actions, these notifications are rendered on-demand, with a special context. Bosun attempts to group these appropriately to reduce spam. The context has:
   173  
   174  - `{{.Time}}`, a timestamp of when the unknown event occurred.
   175  - `{{.Name}}`, bosun's description of the tags or alert name for the grouping.
   176  - `{{.Group}}`, list of alert keys that are unknown.
   177  
   178  If you want to control the way bosun groups unknowns, you can set the `unknownMinGroupSize` value in a notification to override bosun's global default value. This controls the number of alert keys that need to share a common tag in order for us to consider them "similar". You can set this to 0 to never group unknowns together into groups, and send a notification for each alert key.
   179  
   180  Bosun also has a threshold for how many unknown notifications it will send at all in a single batch of notifications. This can be overridden in a notification with the `unknownThreshold` value. Setting to 0 will again remove the limit. Once the limit is reached, the final batch will switch over to using the "multiple unknown groups" templates, which use a similar context, except `{{.Groups}}` is now a lookup of group names to a list of alert keys.
   181  
   182  You can define which template keys a notification will use for unknown notifications by setting
   183  
   184  `unknownBody`, `unknownPost`, `unknownGet`, and `unknownEmailSubject`
   185  
   186  or
   187  
   188  `unknownMultiBody`, `unknownMultiPost`, `unknownMultiGet`, and `unknownMultiEmailSubject`.
   189  
   190  If a template is not set as needed by the notification type, a built-in default template will be used.
   191  
   192  {% endraw %}