簡體   English   中英

如何配置sensu keepalive引發警報松弛

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

我想發送一個松弛警報。 當客戶端未通過保持活動檢查時。

流程是什么? 我能知道怎么做嗎? 我正在使用hiroakis/docker-sensu-server鏡像。

在松弛方面:

在松弛側,您必須為所需的頻道創建一個新的傳入網絡掛鈎。

在sensu方面:

您創建一個使用webhook的新處理程序。

那么您必須在其檢查配置文件中分配此處理程序以用於所需的檢查。

如果您需要代理來連接到Internet,請記住將其也放置在處理程序中,或者以更優雅的方式通過配置文件將其傳遞。

例如。 您可以使用以下處理程序:

#!/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

然后將這樣的配置文件傳遞給它

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

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

然后,這還將包括該處理程序要處理的嚴重程度

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM