14
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

hubotで毎分サイトのステータスを監視する

Last updated at Posted at 2014-11-24

グローバルからwebサイトの状態を監視するのにhubotが使えそうだったので使ってみた
http responseのstatusが200以外だったらslackに通知する様にした
サンプルはモバイルhttpsのサイト用

scripts/monitor.coffee

# Description:
#   Monitoring
https = require 'https'
cron = require('cron').CronJob

module.exports = (robot) ->
  https_health_check = (host, path) ->
    https.get {host: host, path: path, port: 443, headers: "User-Agent":"iPhone xxx Bot", method: 'GET'}, (res) ->
      msg = "https://#{host}#{path} is #{res.statusCode}"
      console.log msg
      if res.statusCode != 200
        console.log res.headers
        robot.send {room: "notifications"}, msg

  new cron('0 * * * * *', () =>
    https_health_check('サイトドメイン', 'パス(/から記述)')
  ).start()

改良版 - 2015/06

  • httpsでなくrequestモジュールを使ってみた
  • cronをやめてagendaを使ってみた
  • EventEmitterを使ってみた

監視用関数

request = require('request')
module.exports = (robot) ->
  robot.on 'healthcheck:url', (url) ->
    options = {
      url: url
      headers: "User-Agent":"iPhone"
    }
    request.get options, (err, res, body) ->
      msg = "#{url} is #{res.statusCode}"
      robot.logger.info msg
      if err
        robot.logger.error err
        robot.send {room: "notifications"}, err
      else if not res.statusCode == 200
        robot.logger.error res.headers
        robot.send {room: "notifications"}, msg

呼び出し側

Agenda = require('agenda')
agenda = new Agenda({db: { address: process.env.MONGOLAB_URI ? 'localhost:27017/agenda'}})

module.exports = (robot) ->
  agenda.define 'agenda:monitoring', (job, done) ->
    # 複数URL監視
    robot.emit 'healthcheck:url', 'https://xxx.yyy/zzz'
    robot.emit 'healthcheck:url', 'https://xxx.yyy/aaa'
    robot.emit 'healthcheck:url', 'https://zzz.xxx.yyy/aaa'
    done()

  agenda.every('1 minutes', 'agenda:monitoring')
  agenda.start()

npm package

hubot-healthcheck 公開した(2015/08/24)

14
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?