Advertisement
boostn

UptimeRobot.Coffee

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