简体   繁体   English

如何配置sensu keepalive引发警报松弛

[英]how to configure sensu keepalive to throw an alert to slack

I want to send out an slack alert. 我想发送一个松弛警报。 when a client fails a keep alive check. 当客户端未通过保持活动检查时。

What is the process to do it? 流程是什么? can I know how to do it? 我能知道怎么做吗? I am using hiroakis/docker-sensu-server docker image. 我正在使用hiroakis/docker-sensu-server镜像。

On the slack side: 在松弛方面:

on the slack side you have to create a new incoming webhook to your desired channel. 在松弛侧,您必须为所需的频道创建一个新的传入网络挂钩。

On the sensu side: 在sensu方面:

you create a new handler that uses the webhook. 您创建一个使用webhook的新处理程序。

then you have to assign this handler to be used for the checks you desire in their check configuration file. 那么您必须在其检查配置文件中分配此处理程序以用于所需的检查。

If you need a proxy to connect to the internet keep in mind to put that one in the handler as well or in a more elegant way pass it on via the config file. 如果您需要代理来连接到Internet,请记住将其也放置在处理程序中,或者以更优雅的方式通过配置文件将其传递。

eg. 例如。 you can use this handler: 您可以使用以下处理程序:

#!/usr/bin/env ruby

# Copyright 2014 Dan Shultz and contributors.
#
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.
#
# In order to use this plugin, you must first configure an incoming webhook
# integration in slack. You can create the required webhook by visiting
# https://{your team}.slack.com/services/new/incoming-webhook
#
# After you configure your webhook, you'll need the webhook URL from the integration.

require 'rubygems' if RUBY_VERSION < '1.9.0'
require 'sensu-handler'
require 'json'

class Slack < Sensu::Handler
  option :json_config,
         description: 'Configuration name',
         short: '-j JSONCONFIG',
         long: '--json JSONCONFIG',
         default: 'slack'

  def slack_webhook_url
    get_setting('webhook_url')
  end

  def slack_channel
    get_setting('channel')
  end

  def slack_proxy_addr
    get_setting('proxy_addr')
  end

  def slack_proxy_port
    get_setting('proxy_port')
  end

  def slack_message_prefix
    get_setting('message_prefix')
  end

  def slack_bot_name
    get_setting('bot_name')
  end

  def slack_surround
    get_setting('surround')
  end
  def markdown_enabled
    get_setting('markdown_enabled') || true
  end

  def incident_key
    @event['client']['name'] + '/' + @event['check']['name']
  end

  def get_setting(name)
    settings[config[:json_config]][name]
  end

  def handle
    description = @event['check']['notification'] || build_description
    post_data("*Check*\n#{incident_key}\n\n*Description*\n#{description}")
  end

  def build_description
    [
      @event['check']['output'].strip,
      @event['client']['address'],
      @event['client']['subscriptions'].join(',')
    ].join(' : ')
  end

  def post_data(notice)
    uri = URI(slack_webhook_url)

    if (defined?(slack_proxy_addr)).nil?
      http = Net::HTTP.new(uri.host, uri.port)
    else
      http = Net::HTTP::Proxy(slack_proxy_addr, slack_proxy_port).new(uri.host, uri.port)
    end

    http.use_ssl = true

    begin
      req = Net::HTTP::Post.new("#{uri.path}?#{uri.query}")
      text = slack_surround ? slack_surround + notice + slack_surround : notice
      req.body = payload(text).to_json

      response = http.request(req)
      verify_response(response)
    rescue Exception => e
     puts "An error has ocurred when posting to slack: #{e.message}"
    end
  end

  def verify_response(response)
    case response
    when Net::HTTPSuccess
      true
    else
      fail response.error!
    end
  end

  def payload(notice)
    {
      icon_url: 'http://sensuapp.org/img/sensu_logo_large-c92d73db.png',
      attachments: [{
        text: [slack_message_prefix, notice].compact.join(' '),
        color: color
      }]
    }.tap do |payload|
      payload[:channel] = slack_channel if slack_channel
      payload[:username] = slack_bot_name if slack_bot_name
      payload[:attachments][0][:mrkdwn_in] = %w(text) if markdown_enabled
    end
  end

  def color
    color = {
      0 => '#36a64f',
      1 => '#FFCC00',
      2 => '#FF0000',
      3 => '#6600CC'
    }
    color.fetch(check_status.to_i)
  end

  def check_status
    @event['check']['status']
  end
end

and then pass a config file like this on to it 然后将这样的配置文件传递给它

{
  "handlers": {
    "slack": {
      "command": "/etc/sensu/handlers/slack.rb",
      "type": "pipe",
      "filters": [

      ],
      "severities": [
        "ok",
        "critical"
      ]
    }
  }
}

which then would also include which severities to be handled by that handler 然后,这还将包括该处理程序要处理的严重程度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM