Posting URL alerts to a Slack channel

About this task

Post URL alerts to one or more Slack channels.

Multiple instances of the config_location/slack_posturl.conf file results in multiple instances of the event plugin. The Slack configuration file can have any name provided the file has the .conf file extension, contains the valid event plugin identifier [posturl], and resides in the event-plugins directory location. All configuration files are loaded, so differentiate the file names such as slack_posturl1.conf, slack_posturl2.conf, and so on. Multiple plugin instances facilitate flexibility when setting up alerts to different Slack channels for development and production clusters. All OpsCenter event plugins support multiple configurations files and subsequent plugin instances.

OpsCenter can post alerts via http or https. To enable https, update the URL from http:// to https://.

Prerequisites

Set up an incoming webhook integration using Slack. For instructions, refer to the Slack API.

Procedure

  1. Create a new file slack_posturl.conf in the conf/event-plugins/ directory.

  2. Copy and paste the contents from the following example into the file.

    When updating the posturl.conf file, the post_type must be template and the template for Slack should be uncommented.

    Example slack_posturl.conf:

    [posturl]
    enabled=1
    
    # levels can be comma-delimited list of any of the following:
    # DEBUG,INFO,WARN,ERROR,CRITICAL,ALERT
    # If left empty, will listen for all levels
    levels=
    
    # clusters is a comma-delimited list of cluster names for which
    # this alert config will be eligible to run.
    # If left empty, this alert will be called for events on all clusters
    clusters=
    
    # the URL to send an HTTP POST to
    url=http://host/path/to/script
    
    # Set a username for basic HTTP authorization
    #username=foo
    
    # Set a password for basic HTTP authorization
    #password=bar
    
    # Override any headers with comma seperated, key=value pairs
    # headers=token=abc123, content-type=application/json
    
    # The number of seconds before a connection to url or proxy_url
    # will be aborted due to a timeout. Defaults to 5 seconds.
    connection_timeout=5
    
    # The number of seconds to wait after sending the POST request before
    # the request will be aborted due to a timeout. Defaults to the same
    # value as connection_timeout if not set.
    #request_timeout=5
    
    # Set the type of posted data. Available options are 'json' or 'form'
    post_type=template
    
    # Fields specified here will override the default event data fields.
    #
    # They must be formatted as key-value pair, with key and value separated by
    # an equals (=). Each pair after the first must be on its own line,
    # indented beyond the first line
    #
    # You may use tokens found within the default event data for or in
    # values. For example, some available keys are:
    #   cluster, time, level_str, message, target_node, event_source, success, api_source_ip, user, source_node
    # Keys must be encapsulated in {brackets}.
    #
    #fields=textKey=value
    #    mixedKey=cluster-{cluster}
    #    event-msg={message}
    
    # Customizable templates for http post data. Some available keys are:
    #     node, cluster, datetime, level_str, message, target_node, event_source, success, api_source_ip, user, source_node
    # More advanced formatting options explained here: https://docs.python.org/2/library/string.html#formatspec Each line
    # beyond the first must be indented. Use with post_type=template, and content-type and other headers must be manually
    # assigned.
    # template={{
    #   "messages":[
    #     {{
    #       "type": "link",
    #       "message": "Cluster {cluster} failure - {message}"
    #       "href": "http://opscenter1"
    #     }}
    #   ]}}
    
    # Slack accepts messages in specific format. Following template can be used to send messages to slack webhook.
    # One of the message format accepted by slack:
    #                    {
    #                        "blocks": [
    #                            {
    #                                "type": "section",
    #                                "text": {
    #                                    "type": "mrkdwn",
    #                                    "text": "This is a message which actually sent to webhook and this must be string"
    #                                }
    #                            }
    #                            ]
    #                    }
    # More messaging formats are explained on https://api.slack.com/messaging/composing
    # Some available keys are:
    # node, cluster, datetime, level_str, message, target_node, event_source, success, api_source_ip, user, source_node.
    # Use with post_type=template, and content-type and other headers must be manually assigned.
    # Uncomment below template for sending messages over slack webhook
    template={{
      "blocks": [
                  {{
                      "type": "section",
                      "text": {{
                                 "type": "mrkdwn",
                                 "text" : "cluster: {cluster},
                                           level: {level},
                                           event_source: {event_source},
                                           message: {message},
                                           target_node: {target_node},
                                           api_source_ip: {api_source_ip},
                                           success: {success},
                                           source_node: {source_node},
                                           action: {action},
                                           time: {time},
                                           level_str: {level_str},
                                           user: {user}"
                      }}
                  }}
      ]}}
  3. Ensure that the URL in the example config file url=https://hooks.slack.com/services/your/webhook/url is replaced with your custom-defined incoming webhook URL.

  4. Save slack_posturl.conf and restart the OpsCenter daemon.

    Verify alerts are getting populated in the Slack channel that you defined for your webhook.