-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathuptimerobot.coffee
86 lines (72 loc) · 2.17 KB
/
uptimerobot.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Description
# A hubot script to list/add monitors for the Uptime Robot service.
#
# Configuration:
# HUBOT_UPTIMEROBOT_APIKEY
# HUBOT_UPTIMEROBOT_CONTACT_ID (optional)
#
# Commands:
# hubot uptime <filter> - Returns uptime for sites.
# hubot uptime add-check <http://example.com> [as <friendlyname>]- Adds a new uptime check.
#
# Author:
# patcon@myplanetdigital
apiKey = process.env.HUBOT_UPTIMEROBOT_APIKEY
alertContactId = process.env.HUBOT_UPTIMEROBOT_CONTACT_ID
module.exports = (robot) ->
REGEX = ///
uptime
( # 1)
\s+ # whitespace
(.*) # 2) filter
)?
///i
robot.respond REGEX, (msg) ->
Client = require 'uptime-robot'
client = new Client apiKey
filter = msg.match[2]
data = {}
client.getMonitors data, (err, res) ->
if err
throw err
monitors = res
if filter
query = require 'array-query'
monitors = query('friendlyname')
.regex(new RegExp filter, 'i')
.on res
for monitor, i in monitors
name = monitor.friendlyname
url = monitor.url
uptime = monitor.alltimeuptimeratio
status = switch monitor.status
when "0" then "paused"
when "1" then "not checked yet"
when "2" then "up"
when "8" then "seems down"
when "9" then "down"
msg.send "#{status.toUpperCase()} <- #{url} (#{uptime}% uptime)"
robot.respond /uptime add-check (\S+)( as (.*))?$/i, (msg) ->
url = require('url').parse(msg.match[1])
friendlyName = msg.match[3] or url.href
# Check that url format is correct.
monitorUrl = url.href if url.protocol
# Create monitor
msg.http("http://api.uptimerobot.com/newMonitor")
.query({
apiKey: apiKey
monitorFriendlyName: friendlyName
monitorURL: monitorUrl
monitorType: 1
format: "json"
noJsonCallback: 1
monitorAlertContacts: [
alertContactId
]
})
.get() (err, res, body) ->
response = JSON.parse(body)
if response.stat is "ok"
msg.send "done"
if response.stat is "fail"
msg.send "#{response.message}"