3
3

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.

GoogleAppsScriptでサイトが落ちていないかを確認してSlack通知するスクリプト(ベーシック認証付き)

3
Last updated at Posted at 2018-12-05

検証環境が落ちてると、みんな困る

良かれと思って検証環境を構築し、お客さんとかに教えると
デプロイミスって落ちてるときとかに文句を言われて辛いので
先手を取れるスクリプトを作成しました。

前準備

SlackのWebhookでIncoming Webhooks からURLを発行してね。

GoogleAppsScriptに下記スクリプトを配置する。

postUrl、postUrl2はwebhooksのURLです。

  • 一度エラー通知して、次もエラーだった場合は通知しません。
  • エラーの後エラーが解消されていたら通知します。
var postUrl = 'https://hooks.slack.com/services/*****/****/43Fr******zmr';
var postUrl2 = 'https://hooks.slack.com/services/*****/****/3m6*****cN7r';
var username = '検証環境落ちてるよ君';  // 通知時に表示されるユーザー名
var icon = ':hatching_chick:';  // 通知時に表示されるアイコン
var url = 'https://oneframe.co.jp'; // 調べたいURL
var error_message = url + ' が落ちてるよー確認してー';  // 投稿メッセージ

function myFunction() {
  var user = "basic-auth-user"; // ベーシック認証のユーザ
  var pass = "basic-auth-pass"; // ベーシック認証のパスワード
  var options = {
    "headers" : {
      "Authorization" : " Basic " + Utilities.base64Encode(user + ":" + pass), 
    },
    "muteHttpExceptions" : true,
  };
  var f;
  var code = 500;
  try {
    f = UrlFetchApp.fetch(url, options)
    code = f.getResponseCode();
    Logger.info(url + ":responscode>>>" + response.getResponseCode()); 
  } catch(e) {
    Logger.log("message:" + e.message + "\nfileName:" + e.fileName + "\nlineNumber:" + e.lineNumber + "\nstack:" + e.stack);
  }
  
  // 直前のステータスを取得
  var status = PropertiesService.getScriptProperties().getProperty('status');  
  // エラーの場合で直前のステータスがngの場合は送信しない。
  if (code != 200) {
    if (status != "ng") {
      message = error_message + " ステータスコード:" + code;
      sendMessage(message);
      PropertiesService.getScriptProperties().setProperty("status", "ng");
    }
  } else {
    // 正常の場合で直前のステータスがngの場合は改善した旨を通知する。
    if (status == "ng") {
      message = "エラーは解除されました!よかったよかった。";
      sendMessage(message);
    }
    PropertiesService.getScriptProperties().setProperty("status", "ok");
  }
}

function sendMessage(message) {
  var jsonData =
      {
        "username" : username,
        "icon_emoji": icon,
        "text" : message + " ステータスコード:" + code
      };
  var payload = JSON.stringify(jsonData);
  
  var options =
      {
        "method" : "post",
        "contentType" : "application/json",
        "payload" : payload
      };
  
  UrlFetchApp.fetch(postUrl, options);
  UrlFetchApp.fetch(postUrl2, options);
  
}

GoogleAppsScriptでトリガー設定

5分おきくらいに設定しましょう。
めでたしめでたし。

技術的なポイント

UrlFetchApp.fetch ちゃんは便利なんだけど指定したサイトが500の場合は
例外を返すメンヘラな一面を持つ。
なのでこの子はtry 〜 catch の優しさで包み込んであげましょう。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?