Created
April 27, 2015 08:06
-
-
Save aibou/9892c4defcc08b573ec9 to your computer and use it in GitHub Desktop.
sensu + grafana で、hipchatにグラフ画像投下するやつ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"handlers": { | |
"hipchat": { | |
"type": "pipe", | |
"command": "/etc/sensu/handlers/hipchat.rb" | |
}, | |
"grafana-image": { | |
"type": "pipe", | |
"command": "/etc/sensu/handlers/grafana-image.rb" | |
} | |
}, | |
"hipchat": { | |
"apikey": "HipchatのAPIキー", | |
"apiversion": "v2", | |
"room": "ルームID", | |
"from": "Sensu(ここは適当に)" | |
}, | |
"grafana": { | |
"grafana_url": "GrafanaのURL(最後/で終えてください・・・)", | |
"db_name": "DBの名前", | |
"panel_id": "パネルID", | |
"from_time": "now-1h", | |
"to_time": "now" | |
}, | |
"aws-s3": { | |
"region": "ap-northeast-1", | |
"bucket": "バケット名", | |
"access_key_id": "↑のバケットにputできるユーザのアクセスキーID", | |
"secret_access_key": "シークレットアクセスキー" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'aws-sdk' | |
require 'open-uri' | |
require 'hipchat' | |
require 'sensu-handler' | |
class GrafanaImage < Sensu::Handler | |
def handle | |
json_config = config[:json_config] || 'hipchat' | |
server_url = settings[json_config]["server_url"] || 'https://api.hipchat.com' | |
apiversion = settings[json_config]["apiversion"] || 'v1' | |
proxy_url = settings[json_config]["proxy_url"] | |
hipchat = HipChat::Client.new(settings[json_config]["apikey"], :api_version => apiversion, :http_proxy => proxy_url, :server_url => server_url) | |
room = settings[json_config]["room"] | |
from = settings[json_config]["from"] || 'Sensu' | |
grafana_url = settings['grafana']['grafana_url'] | |
db_name = settings['grafana']['db_name'] | |
panel_id = settings['grafana']['panel_id'] | |
from_time = settings['grafana']['from_time'] | |
to_time = settings['grafana']['to_time'] | |
region = settings['aws-s3']['region'] | |
bucket = settings['aws-s3']['bucket'] | |
access_key_id = settings['aws-s3']['access_key_id'] | |
secret_access_key = settings['aws-s3']['secret_access_key'] | |
png = open("#{grafana_url}render/dashboard/solo/db/#{db_name}?panelId=#{panel_id}&fullscreen&from=#{from_time}&to=#{to_time}") | |
Aws.config.update( | |
region: region, | |
credentials: Aws::Credentials.new(access_key_id, secret_access_key) | |
) | |
s3 = Aws::S3::Client.new | |
key = "loadavg-#{Time.now.strftime('%Y%m%d-%H%M%S')}.png" # TODO: fix name | |
s3.put_object( | |
bucket: bucket, | |
key: key, | |
body: png.read, | |
content_type: 'image/png' | |
) | |
url = "https://s3-#{region}.amazonaws.com/#{bucket}/#{key}" | |
hipchat[room].send(from, url, message_format: 'text') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment