愛と勇気と缶ビール

ふしぎとぼくらはなにをしたらよいか

Google Apps Scriptで個人サービスの超絶ゆるふわ監視

さくらのVPSにも微妙に置きたくないし、かといって自宅のPCとか論外。そういう場合には便利かもしれない。環境整えなくてもメール飛ばせるし。

以下のscriptをHour timerのtrigger(cronみたいなもの)で一時間おきに回すだけ。

function doCheck() {
  var url = "http://example.com";
  var res = UrlFetchApp.fetch(url);
  
  if ( res.getResponseCode() !== 200 ) {
    MailApp.sendEmail("example+fatal@gmail.com", "Example not returns 200", "responseCode = " + res.getResponseCode() + ", body = " + res.getContentText());
  }
  else {
    MailApp.sendEmail("example+check@gmail.com", "Example response", "", {
      htmlBody: res.getContentText()
    });
  }
}