Last active
December 26, 2015 10:19
-
-
Save kesor/7136091 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Description: | |
# Allows Hubot to search a Graphite server for saved graphs | |
# | |
# Dependencies: | |
# A HuBot robot brain of some kind. MongoDB/Redis/... | |
# | |
# Configuration | |
# HUBOT_HIPCHAT_TOKEN | |
# GRAPHITE_URL (e.g. https://graphite.example.com) | |
# GRAPHITE_AUTH (e.g. user:password for Basic Auth) | |
# | |
# Commands: | |
# hubot graphite add <name> <target> - remember a new graph | |
# hubot graphite list - show a list of known graphs | |
# hubot graphite delete #<number> - remove a numbered graph | |
# hubot graphite show #<number> - show a numbered graph | |
# hubot graphite show <name> - show a named graph | |
# | |
# Author: | |
# Evgeny Zislis <[email protected]> | |
HipChatClient = require "node-hipchat" | |
class Hipchat | |
constructor: (@robot)-> | |
@client = new HipChatClient process.env.HUBOT_HIPCHAT_TOKEN | |
@client.listRooms (data) => | |
@robot.brain.data.hipchat_rooms = data["rooms"] | |
refreshRooms: -> | |
@client.listRooms (data) => | |
@robot.brain.data.hipchat_rooms = data["rooms"] | |
send: (destination, message) -> | |
room = @translateJID(destination) | |
if room | |
@client.postMessage | |
room: room | |
from: "Graphite" | |
message: message | |
translateJID: (jid) -> | |
index = @robot.brain.data.hipchat_rooms.map((r) -> r.xmpp_jid).indexOf(jid) | |
room = @robot.brain.data.hipchat_rooms[index] | |
room?.room_id | |
class Graphite | |
constructor: (@robot) -> | |
@cache = [] | |
@robot.brain.on 'loaded', => | |
if @robot.brain.data.graphite | |
@cache = @robot.brain.data.graphite | |
nextTargetNum: -> | |
maxTargetNum = if @cache.length then Math.max.apply(Math, @cache.map (n) -> n.num) else 0 | |
maxTargetNum++ | |
maxTargetNum | |
add: (name, parameters) -> | |
target = { num: @nextTargetNum(), name: name, target: parameters } | |
@cache.push target | |
@robot.brain.data.graphite = @cache | |
target | |
showByNum: (num) -> | |
index = @cache.map((n) -> n.num).indexOf( parseInt(num) ) | |
@cache[index] | |
showByName: (name) -> | |
index = @cache.map((n) -> n.name).indexOf(name) | |
@cache[index] | |
all: -> @cache | |
deleteByNumber: (num) -> | |
index = @cache.map((n) -> n.num).indexOf(parseInt(num)) | |
target = @cache.splice(index, 1)[0] | |
@robot.brain.data.target = @cache | |
target | |
module.exports = (robot) -> | |
targets = new Graphite robot | |
hipchat_client = new Hipchat robot | |
robot.hear /graphite refresh rooms/i, (msg) -> | |
hipchat_client.refreshRooms() | |
msg.send "Hipchat rooms list refreshed" | |
robot.hear /graph(ite)? add (.+?) ([^ ]+?)$/i, (msg) -> | |
target = targets.add msg.match[2], msg.match[3] | |
msg.send "Graphite target added: ##{target.num} - #{target.name}" | |
robot.hear /graph(ite)? list/i, (msg) -> | |
if targets.all().length > 0 | |
response = "" | |
for target, num in targets.all() | |
response += "##{target.num} - #{target.name}\n" | |
msg.send response | |
else | |
msg.send "There are no graphite targets" | |
robot.hear /graph(ite)? delete #?(\d+)$/i, (msg) -> | |
targetNum = msg.match[2] | |
target = targets.deleteByNumber targetNum | |
msg.send "Target deleted: ##{target.num} #{target.name} #{target.target}" | |
robot.hear /graph(ite)? show #?(\d+)$/i, (msg) -> | |
target = targets.showByNum(msg.match[2]) | |
if target | |
hipchat_client.send msg.message.user.reply_to, "<img alt='#{target.name}' src='http://#{process.env.GRAPHITE_URL}/render/?#{target.target}&width=480&bgcolor=fffae9&fgcolor=000000&lineWidth=2&format=svg'/>" | |
else | |
msg.send "Could not find a matching target" | |
robot.hear /graph(ite)? show code( for)? #?(\d+)$/i, (msg) -> | |
target = targets.showByNum(msg.match[3]) | |
if target | |
msg.send "Graphite code for #{target.num} #{target.name}: #{target.target}" | |
else | |
msg.send "Could not find a matching target" | |
robot.hear /graph(ite)? show ([^#0-9].+?)$/i, (msg) -> | |
return if msg.match[2].indexOf("code ") == 0 | |
target = targets.showByName(msg.match[2]) | |
if target | |
hipchat_client.send msg.message.user.reply_to, "<img alt='#{target.name}' src='http://#{process.env.GRAPHITE_URL}/render/?#{target.target}&width=480&bgcolor=fffae9&fgcolor=000000&lineWidth=2&format=svg'/>" | |
else | |
msg.send "Could not find a matching target" | |
robot.hear /graph(ite)? show code( for)? ([^#0-9].+?)$/i, (msg) -> | |
target = targets.showByName(msg.match[3]) | |
if target | |
msg.send "Graphite code for #{target.num} #{target.name}: #{target.target}" | |
else | |
msg.send "Could not find a matching target" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment