Home / Platform management / Operations center / Notifications / Notification Template

Notification Template

A notification template is a standardized construct that consists of custom content, content variables, and content format parameters. It is used to standardize and customize the content and format of alarm notification messages for notification policies.

Platform administrators or O&M personnel can customize the content and format of notification messages based on different alarm notification methods by setting notification templates, helping users quickly obtain key alarm information and improve O&M efficiency.

The platform supports a variety of notification servers, please refer to the Notification Server document to add the server, after adding, the template of the corresponding notification type will be displayed, if the notification server is not configured, the corresponding notification template will not be displayed by default.

create a notification template

Based on the actual monitoring and alarm operation and maintenance scenarios, the platform provides a variety of variables for setting the content of notification messages. When you create a notification template, you can add or remove content variables and set the format of message content based on the actual monitoring and alarm O&M scenarios. A notification template can be used by one or more notification policies of the same notification type, and all notification policies that use the template can standardize the output of notification messages sent to notification recipients based on the template.

Procedure

  1. In the navigation pane on the left, click Operation Center > Notifications.

  2. Switch to the Notification Templates tab.

  3. Click Create Notification Template.

  4. In the Basic Information area, configure the following parameters.

    Parameter Description
    Message Type Select the type of message based on the purpose of the notification.
    Alarm Message: Combined with the alarm function of the platform, send an alarm message triggered by an alarm rule;
    Component Exception Message: Component Exception Message Reminder to send notifications triggered by certain component exceptions
  5. In the Template Configuration area, refer to the configuration variables and content format parameters for different types of templates.

Prompt:

This article uses the webhook template as an example.

Notification content: The content of the notification will be displayed by default in the preset notification template, and you can add or remove variables or set format labels according to your needs.

Prompt:

  1. Click Create.

How do I reference a variable > <span id=“10”?

A variable is a key to the labels or annotations of a NotificationMessage, in the format ‘{{.tag key}}’. In general, in order to facilitate users to quickly obtain key information, you can customize the display name for the variable, for example: ‘Alarm Level: {{ .externalLabels.severity }}’.

Tips: In the mailbox, webhook and SMS parameters configured by the template, the platform supports the use of basic variables by default, if the basic variables cannot meet your notification needs, the platform also supports the use of [more webhook advanced variables (Alpha)] (#webhook) in the content of the webhook, you can customize the notification template according to more parameters provided by the platform, please contact the administrator to enable the relevant function.

Base variables

When a notification policy sends a notification message to a user based on a notification template, the variables in the template refer to the value of the corresponding label in the notification message (the actual monitoring data). Finally, the monitoring data is sent to the user in a standardized content form.

The basic variables supported by the platform by default are as follows:

Display name Variable Description
Alarm Status {{ .externalLabels.status }} For example, in an alarm. For more information about alarm status, see Status Description(/en/platform-usermanual/ops/7alarmall/1alarmview/).
Alarm Level {{ .externalLabels.severity }} For example: Critical. For more alarm levels, see Create Alarm Policy .
Alarm Cluster {{ .labels.alert_cluster }} For example, cluster 1
Alarm object {{ .externalLabels.object }} The type and name of the resource for which the alarm is generated, for example, node 192.168.16.53
Policy Name {{ .labels.alert_resource }} Name of the alarm policy. For example: cpaas-node-rules.
Alarm Description {{ .externalLabels.summary }} Description of the alarm policy
Trigger Value {{ .externalLabels.currentValue }} The value of the alert
Alarm time {{ dateFormatWithZone .startsAt “2006-01-02 15:04:05” “Asia/Chongqing” }} The start time of the alarm
Recovery time {{ dateFormatWithZone .endsAt “2006-01-02 15:04:05” “Asia/Chongqing” }} The end time of the alarm
Indicator Name {{ .labels.alert_indicator }} Name of the metric monitored

More webhook advanced variables (Alpha)

If the basic variables do not meet the requirements of the notification template, you can contact the administrator to enable the relevant functions to use more advanced variables.

Tips: The platform supports WeCom group bots, DingTalk group bots, Feishu group bots, or WebHook message types, after this function is enabled, the templates of the content of WeCom group bots, DingTalk group bots, and Feishu group bots remain unchanged, and the basic variables still need to be referenced, you can refer to the interface example to modify. For webhook types, you need to rewrite the template to support more variables by following these steps:

If you set {} in the notification content of the template, the following notification message code will be returned by default: When the notification policy sends a notification message to the user based on the notification template, the variables in the template will reference the value of the corresponding label in the notification message (the actual monitoring data), and finally send the monitoring data to the user in a standardized form.

Notification message code example

# The following is the native message returned by the alertManager after the alarm is triggered, you can use the Golang Template syntax to refer to the variables as needed, and the syntax refers to the https://pkg.go.dev/text/template
# Annotations include the current value of the alarm rule when it is triggered, the notification associated with the alarm rule, the display name of the alarm rule, and the summary that comes with the alarm rule
annotations:
  alert_current_value: '1'
  alert_notifications: '["ait-test"]'
  display_name: deployment_ready
  summary: The cpaas-system namespace has a stateful middleware-exporter service running status that is abnormal
# labels mainly include the fixed labels on the alarm rule and query expressions to find other labels
labels:
  alert_cluster: global # The cluster where the alarm occurs is located, for example, cluster 1.
  alert_indicator: custom # Name of the metric being monitored.
  alert_indicator_aggregate_range: '0' # The duration of collecting metrics
  alert_indicator_comparison: '!=' # Comparison operator in the alarm trigger condition.
  alert_indicator_threshold: '2' # The alarm threshold in the alarm trigger condition.
  alert_involved_object_kind: Cluster # The type of resource for which the alarm occurred.
  alert_involved_object_name: global # Name of the resource where the alarm occurred.
  alert_kind: resource # Name of the alarm policy. For example: cpaas-node-rules.
  alert_name: custom-d5u2l # Name of the alarm rule in which the alarm occurred.
  alert_namespace: cpaas-system # The namespace where the alarm policy is located
  alert_project: cpaas-system # The project where the alarm policy is located
  alert_resource: alauda-rules # # Alarm policy name
  alert_source: Platform # The alarm source comes from the platform or user
  alertgroup: general 
  alertname: custom-d5u2l-388642e14b069a7d662f4752981a441a
  deployment: middleware-exporter
  display_name: deployment_ready
  namespace: cpaas-system
  severity: Medium
# The start and end time of the alarm
startsAt: '2023-08-09T06:29:18.113184463Z'
endsAt: '0001-01-01T00:00:00Z'
# Alarm status, including Alarm Neutral and Resolved
status: firing
# The fingerprint and URL of the alarm
fingerprint: c33ac30fed829472
generatorURL: http://prometheus.server:9090/graph?g0.expr=up%7Binstance%3D~%2210%7C80%7C192.%2A%22%7D+%3D%3D+0&g0.tab=1
# You can also refer to the following fields to aggregate more alarm information
externalLabels:
  status: "Alarming" # Alarm status
  severity: "Critical" # Alarm level
  object: "Cluster global" # Alarm object
  comparison: "Not equal" # The comparison symbol of the alarm rule
  threshold: "2.00" # The threshold of an alarm rule, which is usually kept to two decimal places
  currentValue: "1.00" # The current value of the metric that triggered the alert
  summary: "The stateful middleware-exporter service in the namespace cpaas-system is abnormal" # If the alarm rule sets the annotation of the summary, it will be read directly from the annotation, otherwise it will be read from the summary of the indicator
 
externalNotifications: # Information about the notification policy associated with the alarm rule
- name: nf1 # The name of the notification policy
  users: # Users included in the notification policy
    - apiVersion: auth.alauda.io/v1
      kind: User
      metadata:
        annotations:
          auth.cpaas.io/connector.data: '{"groups":[]}'
          cpaas.io/display-name: admin
        labels:
          auth.cpaas.io/user.connector_id: local
          auth.cpaas.io/user.connector_type: local
          auth.cpaas.io/user.email: 8afe307d054f1e96ce2e3d10aa374162
          auth.cpaas.io/user.state: active
          auth.cpaas.io/user.username: ""
          auth.cpaas.io/user.valid: "true"
        name: 8afe307d054f1e96ce2e3d10aa374162
      spec:
        connector_name: local
        connector_type: local
        email: admin@cpaas.io
        is_admin: true
        last_login_time: "2023-08-15T23:35:23Z"
        state: active
        username: admin # username
        valid: true
        mail: admin@cpaas.io # email address
        mobile: 13312348888 # mobile phone number, which can be empty
        webhookType: "Webhook" # type, Webhook/WeChat/DingTalk
        webhookUrl: https://callback.tk # address
  groups: # Notify the contact groups included in the policy
    - apiVersion: ait.alauda.io/v1beta1
      kind: NotificationGroup
      metadata:
        annotations:
          cpaas.io/creator: admin
          cpaas.io/description: ""
          cpaas.io/operator: admin@cpaas.io
          cpaas.io/updated-at: "2023-08-15T02:57:22Z"
        labels:
          cpaas.io/source: Platform
        name: vx
        namespace: cpaas-system
      spec:
        mail: "ait@da.io" # the email address of the contact group
        webhookType: WeChat # The webhook type and address of the contact group
        webhookUrl: https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=c55f6376-1ed9-485e-8273-4d75449bca7f
        users:
          - 8afe307d054f1e96ce2e3d10aa374162
      users:
        - apiVersion: auth.alauda.io/v1
          kind: User
          metadata:
            annotations:
              auth.cpaas.io/connector.data: '{"groups":[]}'
              cpaas.io/display-name: admin
            labels:
              auth.cpaas.io/user.connector_id: local
              auth.cpaas.io/user.connector_type: local
              auth.cpaas.io/user.email: 8afe307d054f1e96ce2e3d10aa374162
              auth.cpaas.io/user.state: active
              auth.cpaas.io/user.username: ""
              auth.cpaas.io/user.valid: "true"
            name: 8afe307d054f1e96ce2e3d10aa374162
          spec:
            connector_name: local
            connector_type: local
            email: admin@cpaas.io
            is_admin: true
            last_login_time: "2023-08-15T23:35:23Z"
            state: active
            username: admin # username
            valid: true
            mail: admin@cpaas.io # email address
            mobile: 13312348888 # mobile phone number, which can be empty
            webhookType: "Webhook" # type, Webhook/WeChat/DingTalk
            webhookUrl: https://callback.tk # address

Message Extraction as Variables

You can extract and reference the message code returned by the system as a variable to customize the notification template according to your needs. The template is written using the Golang Template syntax, please refer to the [Official Documentation] ( https://pkg.go.dev/text/template ) for more information.

Tip: In addition to supporting all the syntax of the Golang Template, the platform also has the following custom functions built-in, which you can extend according to these custom functions to simplify the writing of templates:

dateFormat: Converts the date of a string to a given layout
"dateFormat": func(date, layout string) string
dateFormatWithZone: Converts the date of a string to a given layout and time zone
"dateFormatWithZone": func(date, layout, zone string)
float: Convert a string of numbers to float64 numbers
"float": func(s string) float64
MarshalIndent: Convert a specific struct to json, you can specify prefixes and indentations
"MarshalIndent": func(i interface{}, prefix string, indent string) string
ToString: Converts a string variable to a valid json string, handles the problem of double quotation marks escaping, and appends a pair of double quotation marks before and after the string
"ToString": func(s string) string

The following is an example of a template that uses custom variables and functions:

Tip: In the notification template, if you use advanced variables, you need to use JSON format.

{
  "status": {{ ToString .externalLabels.status }},
  "serverity": {{ ToString .externalLabels.severity }},
  "cluster": {{  ToString .labels.alert_cluster }},
  "object": {{  ToString .externalLabels.object }},
  "resource": {{  ToString .labels.alert_resource }},
  "summary": {{  ToString .externalLabels.summary }},
  "currentValue": {{  ToString .externalLabels.currentValue }},
  "startsAt": "{{ dateFormatWithZone .startsAt "2006-01-02 15:04:05" "Asia/Chongqing" }}",
  "labels": {{ MarshalIndent .labels "  " "  "}},
  "notifications": {{ MarshalIndent .externalNotifications "  " "  "}}
}

Using the template example above, the JSON rendered by the platform looks like this:

{
  "status": "alarms",
  "serverity": "serious",
  "cluster": "global",
  "object": "cluster/global",
  "resource": "alauda-rules", "summary".
  "summary": "Namespace cpaas-system has stateful middleware-exporter service runtime state exception",
  "currentValue": "1.00",
  "startsAt:"2023-08-09 06:29:18",
  "labels": {
    "alert_cluster": "global",
    "alert_indicator": "custom",
    "alert_indicator_aggregate_range": "0",
    "alert_indicator_comparison": "!=",
    "alert_indicator_threshold": "2",
    "alert_involved_object_kind": "Cluster",
    "alert_involved_object_name": "global",
    "alert_kind": "resource",
    "alert_name": "custom-d5u2l",
    "alert_namespace": "cpaas-system",
    "alert_project": "cpaas-system",
    "alert_resource": "alauda-rules",
    "alert_source": "Platform",
    "alertgroup": "general",
    "alertname": "custom-d5u2l-388642e14b069a7d662f4752981a441a",
    "deployment": "middleware-exporter",
    "display_name": "deployment_ready",
    "namespace": "cpaas-system",
    "severity": "Medium"
  },
  "notifications": [
    {
      "name": "nf1",
      "users": [
        {
          "apiVersion": "auth.alauda.io/v1",
          "kind": "User",
          "metadata": {
            "annotations": {
              "auth.cpaas.io/connector.data": "{\"groups\":[]}",
              "cpaas.io/display-name": "admin"
            },
            "labels": {
              "auth.cpaas.io/user.connector_id": "local",
              "auth.cpaas.io/user.connector_type": "local",
              "auth.cpaas.io/user.email": "8afe307d054f1e96ce2e3d10aa374162",
              "auth.cpaas.io/user.state": "active",
              "auth.cpaas.io/user.username": "",
              "auth.cpaas.io/user.valid": "true"
            },
            "name": "8afe307d054f1e96ce2e3d10aa374162"
          },
          "spec": {
            "connector_name": "local",
            "connector_type": "local",
            "email": "admin@cpaas.io",
            "is_admin": true,
            "last_login_time": "2023-08-15T23:35:23Z",
            "state": "active",
            "username": "admin",
            "valid": true,
            "mail": "admin@cpaas.io",
            "mobile": 13312348888,
            "webhookType": "Webhook",
            "webhookUrl": "https://callback.tk"
          }
        }
      ],
      "groups": [
        {
          "apiVersion": "ait.alauda.io/v1beta1",
          "kind": "NotificationGroup",
          "metadata": {
            "annotations": {
              "cpaas.io/creator": "admin",
              "cpaas.io/description": "",
              "cpaas.io/operator": "admin@cpaas.io",
              "cpaas.io/updated-at": "2023-08-15T02:57:22Z"
            },
            "labels": {
              "cpaas.io/source": "Platform"
            },
            "name": "vx",
            "namespace": "cpaas-system"
          },
          "spec": {
            "mail": "ait@alaa.io",
            "webhookType": "WeChat",
            "webhookUrl": "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=c55f221216376-1ed9-485e-8273-4d75449bca7f",
            "users": [
              "8afe307d054f1e96ce2e3d10aa374162"
            ]
          },
          "users": [
            {
              "apiVersion": "auth.alauda.io/v1",
              "kind": "User",
              "metadata": {
                "annotations": {
                  "auth.cpaas.io/connector.data": "{\"groups\":[]}",
                  "cpaas.io/display-name": "admin"
                },
                "labels": {
                  "auth.cpaas.io/user.connector_id": "local",
                  "auth.cpaas.io/user.connector_type": "local",
                  "auth.cpaas.io/user.email": "8afe307d054f1e96ce2e3d10aa374162",
                  "auth.cpaas.io/user.state": "active",
                  "auth.cpaas.io/user.username": "",
                  "auth.cpaas.io/user.valid": "true"
                },
                "name": "8afe307d054f1e96ce2e3d10aa374162"
              },
              "spec": {
                "connector_name": "local",
                "connector_type": "local",
                "email": "admin@cpaas.io",
                "is_admin": true,
                "last_login_time": "2023-08-15T23:35:23Z",
                "state": "active",
                "username": "admin",
                "valid": true,
                "mail": "admin@cpaas.io",
                "mobile": 13312348888,
                "webhookType": "Webhook",
                "webhookUrl": "https://callback.tk"
              }
            }
          ]
        }
      ]
    }
  ],
  # If Webhook is unable to convert the message to a legal json, we set the reason for this failure in error.
  "error": {
    "kind": "Status",
    "apiVersion": "v1",
    "metadata": {},
    "status": "Failure", # "message": "error when creating", # Details of the error encountered when sending the message.
    "message": "error when creating", # Sent message encountered error details
    "reason": "InvalidJson", # Sending the message encountered an error reason: RenderError/InvalidJson
    "details".
      "causes": [
        {
          "type": "Message", # Native message to send
          "message"; "<unmarshal message>" # Message content after Json serialization
        },
        {
          "type": "Template", # The template to use for sending the message.
          "message"; "<template>" # Content of the template.
        }, { "type".
        {
          "type": "Result", # The result after rendering. 
          "message"; "<render result>" # The content of the rendered message.
        },
      },
    "code": 400
   }
}

special format markup language (SGML)

In email notifications, common HTML format tags and their usage are as follows:

Element Tag Description
Text - Supports input of Chinese/English text content.
Font <font color="#FF0000">Set font color</font>
<b>Bold font</b>
Sets font format.
Heading <h1>Heading 1</h1>, supports up to h6 (Heading 6). Sets heading level.
Paragraph <p>Paragraph</p> Inserts normal paragraph text.
Quote <q>Quote</q> Inserts short quote content.
Hyperlink <a href="//www.example.com">Hyperlink</a> Inserts a hyperlink.