Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 67 lines (55 sloc) 2.377 kb
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
# Description:
# Show uptime status of sites monitored by UptimeRobot.
#
# Dependencies:
# None
#
# Configuration:
# HUBOT_UPTIMEROBOT_APIKEY
#
# Commands:
# hubot uptimerobot - Show the status of the monitored sites
# hubot what is the uptime of the monitored sites? - Show the status of the monitored sites
# hubot uptimerobot add <url>[ as <friendlyname>] - Instructs uptime robot to monitor the <url>, optionally with a name
# hubot start monitoring the http uptime of <url> - Instructs uptime robot to monitor the <url>
#
# Author:
# lukesmith

module.exports = (robot) ->
  apikey = process.env.HUBOT_UPTIMEROBOT_APIKEY

  getMonitors = (msg) ->
    msg.http("http://api.uptimerobot.com/getMonitors")
      .query({ apiKey: apikey, logs: 0, format: "json", noJsonCallback: 1 })
      .get() (err, res, body) ->
        if err
          msg.send "Uptime robot says: #{err}"
          return

        response = JSON.parse(body)

        if response.stat is "ok"
          for monitor in response.monitors.monitor
            status = ""
            switch monitor.status
              when "1" then status = "paused"
              when "2" then status = "up"
              when "8" then status = "seems down"
              when "9" then status = "down"
              else status = "unknown"

            msg.send "#{monitor.friendlyname}: has an uptime of #{monitor.alltimeuptimeratio}% and current status of #{status}"
        else if response.stat is "fail"
          msg.send "Uhoh, #{response.message}";

  robot.respond /what is the uptime of the monitored sites?/i, getMonitors
  robot.respond /uptimerobot/i, getMonitors

  newMonitor = (monitorUrl, monitorFriendlyName) ->
    monitorFriendlyName ?= monitorUrl
    msg.http("http://api.uptimerobot.com/newMonitor")
      .query({ apiKey: apikey, monitorFriendlyName: monitorFriendlyName, monitorURL: monitorUrl, monitorType: 1, format: "json", noJsonCallback: 1 })
      .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}"

  robot.respond /start monitoring the http uptime of (.*)$/i, (msg) ->
    newMonitor(msg.match[1], null)
  robot.respond /uptimerobot add (http(s?)\:\/\/\S+)( as (.+))?/i, (msg) ->
    newMonitor(msg.match[1], msg.match[4])

Something went wrong with that request. Please try again.