10
17

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.

サイトの死活監視用Google Apps Script

Last updated at Posted at 2014-09-03

サイト死活監視用に
無料でCRONが出来るサービス無いかと探していたら、
Google Apps Scriptのトリガーが便利そうなので書いてみた。
一定時間置きにURLをチェックして、検証文字列が無ければメールを送信。
トリガーの設定方法は下記を参考。
http://www.infoscoop.org/blogjp/2014/05/05/google-apps-script-sample/
http://dotinstall.com/lessons/basic_google_apps_script/24313

○秒応答が無かったらメール送信としたかったけど、timeoutの設定方法はわからず。
※timeoutの指定は出来ないようだ。GoogleAppScriptのタイムアウトは5minらしい。
http://stackoverflow.com/questions/19135818/how-to-cancel-an-urlfetchapp-fetch-after-specified-time
http://hima.okitsunesama.com/2014/07/gmail-googleappscript.html

function myFunction() {
  try{
    var url="検証URL"; //検証URL
    var mailto="送信先メール"; // 送信先メール(カンマ区切りで複数指定)
    var search_string="検証文字列"; //検証文字列

    var response = UrlFetchApp.fetch(url,{
      muteHttpExceptions : true 
      //trueの場合、HTTPエラーの時に例外をスローしない
    });

    if(response.getContentText().indexOf(search_string)<=0){
      MailApp.sendEmail(
        mailto,
        "[GAS] 検証文字列が見つかりません",// メール件名
        "",
        {
          htmlBody: url + "<br/>" + response.getContentText()
        }
      );
    }
  }catch(e){
      // 接続エラー
      //Logger.log(e);
      MailApp.sendEmail(
        mailto,// 送信先メール
        "[GAS] 検証URLに接続できません",// メール件名
        "",
        {
          htmlBody: url + "<br />" + e.message
        }
      );    
  }

}

探してみたら、newrelicのsyntheticsを使えばページ内の「検証文字列」の監視ができるので、実際はsyntheticsを使ったほうがよさそう。
http://newrelic.com/synthetics
http://qiita.com/kumatronik/items/c542feac77b08c3ca646

10
17
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
10
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?