LoginSignup
8
8

More than 5 years have passed since last update.

Google Apps Scriptで簡単にレスポンスタイムを監視する

Last updated at Posted at 2013-06-17

管理しているサービスのレスポンスタイムを計測し,設定時間以上に遅い場合には,アラートメールを飛ばすスクリプト

下記コードの必要な部分を書き換えて,スクリプトの実行トリガーを1時間毎とかにすると自動的にチェックしてくれます。

ResponseTimeChecker.js
var BASE_URL     = "http://hogefuga.com";
var DEFAULT_LIMIT = 200;
var MAIL_ADDRESS  = "hogehoge@hogefuga.com";
var checkApiMap = {
  '/api1' : DEFAULT_LIMIT,
  '/api2' : DEFAULT_LIMIT,
  '/api3' : 300,
}

function sendMail(overApiMap) {
  var title = "#alert some api exceed the limit response time"
  var msg = ""
  for (var api in overApiMap) {
    msg += api + "\t" + overApiMap[api] + "\n"
  }
  if (msg != "") {
    MailApp.sendEmail(MAIL_ADDRESS, title, msg);
  }
}

function doCheck() {
  var overApiMap = {};
  for (var api in checkApiMap) {
    var url    = BASE_URL + api
    var before = new Date();
    var res    = UrlFetchApp.fetch(url);
    var after  = new Date();

    var responseTime = after - before;
    if (res.getResponseCode() !== 200) {
      overApiMap[api] = -1;
    } else if (responseTime > checkApiMap[api]){
      overApiMap[api] = responseTime;
    }
  }
  sendMail(overApiMap);
}

8
8
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
8
8