LoginSignup
4
4

More than 5 years have passed since last update.

sensu からメールhandler

Last updated at Posted at 2014-04-02

背景

  • sensu は基本 json で値を返す。
  • そのままメールコマンド実行すると結構ウケる内容で来る。
  • 内容を見やすくして送信したいよね。
  • ってなわけで自作

Code

/etc/sensu/conf.d/handler_email.json
{
        "handlers": {
        "email": {
                "type": "pipe",
                "command": "/etc/sensu/plugins/mail_send_sensu.rb"
                }
        }
}

でコマンド指定する。

/etc/sensu/plugins/mail_send_sensu.rb
#! /usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'json'
require 'nkf'
require 'mail'

from_addr = "alert@"+`hostname`.chomp
to_addr = %w{mail@address}

while line = STDIN.gets
        json_data = JSON.parse(line)

        client_data = json_data["client"]
        check_data = json_data["check"]

        status = check_data["output"].split(" ")

        case status[1]
        when "OK:"
                status_flg = "Recovery"
        when "CRITICAL:"
                status_flg = "Critical"
        when "WARNING:"
                status_flg = "Warning"
        else
                status_flg = "Unknown"
        end

        send_subject = "ALERT ON #{status_flg}"
        mail_body = "##### Notifficate #{status_flg} ####\n"

        client_data.merge(check_data).each do |issue,data|
                mail_body << "#{issue}".ljust(20)+": #{data}".ljust(100)+"\n"
        end

        mail = Mail.new do
                to "#{to_addr.to_a.join(",")}"
                from "#{from_addr}"
                subject "#{send_subject}"
                body "#{NKF.nkf("-Wwm0", mail_body)}"
        end

        mail.deliver!

end
__END__

アラートをパイプで受け取り、ステータスチェック後mailする

とりあえず、晒すまでもないことだけど備忘録として置いてく。

4
4
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4