sensu-plugins / sensu-plugins-slack

Sensu Slack chat handlers
http://sensu-plugins.io
MIT License
30 stars 54 forks source link

Adding payload_template stops messages sending to Slack #75

Closed michaelpporter closed 5 years ago

michaelpporter commented 5 years ago

I am trying to update the message to Slack to use the client name in place of the address in slack messages. I thought using a payload_template would allow me to customize it, but when I add it I no longer get any slack notices. I looked over the noted posted in #24 and #16 The message template is working and has been for a while. What, am I missing?

handler-sklack.json

{
  "handlers": {
    "slack": {
      "type": "pipe",
      "command": "handler-slack.rb",
      "severities": [
        "critical",
        "unknown"
      ],
      "template": "/etc/sensu/templates/slack.erb",
      "filters": [
        "recurrences"
      ]
    },
    "slack-test": {
      "type": "pipe",
      "command": "handler-slack.rb",
      "severities": [
        "critical",
        "unknown"
      ],
      "message_template": "/etc/sensu/templates/slack.erb",
      "payload_template": "/etc/sensu/templates/slack_payload.erb"
    },
  "slack": {
    "webhook_url": "https://hooks.slack.com/services/...",
    "channel": "#dev-alerts-test",
    "bot_name": "sensu",
    "icon_emoji": ":siren:",
    "dashboard": "http://sensu.domain.com:3000/#/events",
    "message_template": "/etc/sensu/templates/slack.erb"
  }
}

slack_payload.erb

{
      icon_url: slack_icon_url ? slack_icon_url : 'http://sensuapp.org/img/sensu_logo_large-c92d73db.png',
      attachments: [{
        title: "#{@event['client']['name']} - #{translate_status}",
        text: [slack_message_prefix, notice].compact.join(' '),
        color: color,
        fields: client_fields
      }]
    }
}

slack.erb

<%= @event["check"]["output"].gsub('"', '\\"') %>
Alert-Time: <%= Time.at(@event["check"]["issued"])  %>
zeninfinity commented 5 years ago

I am having similar problems. Were you able to figure out a solution @michaelpporter ?

zeninfinity commented 5 years ago

Sooo I ended up getting it working by creating the line with this syntax:

"payload_template": "/etc/sensu/conf.d/payload.erb"

and putting in the following:


  "attachments": [
    {
      "title": "Uchiwa Dashboard",
      "title_link": "<insert dashboard link here>",
      "color": "<%= color %>",
      "fields": [
          {
            "title": "Status",
            "value": "<%= translate_status %>",
            "short": false
          },
          {
            "title": "Client Name",
            "value": "<%= @event['client']['name'] %>",
            "short": true
          },
          {
            "title": "IP address",
            "value": "<%= @event['client']['address'] %>",
            "short": true
          },
          {
             "title": "Check Name",
             "value": "<%= @event['check']['name'] %>",
             "short": true
          },
          {
             "title": "Action",
             "value": "<%= @event['action'] %>",
             "short": true
          },
          {
             "title": "Output",
             "value": "<%= @event['check']['output'] %>",
             "short": false
          }
      ]
    }
  ]
}```