跳到主要內容

Google Apps Script - HTTP Monitor

這邊是一個利用Google的SpreadSheet(試算表)服務,加上Apps Script(指令碼)服務,來做HTTP URL的監控,並且記錄每次Request的回應時間值,應用層面,可以作為監控或是網路回應時間的統計(當然,這邊是以Google的網路來做Request,比較不客觀一點,僅供參考拉 :D),未來可以搭配Mail Service使用或是再發揮更多想像力吧∼ (PS: 不要拿來攻擊阿@@)

Step1: 開立一個Google Spreadsheet
為了美觀,這邊先輸入title,如下:


Step2: 建立Sheet Apps Script,選擇視窗中,可以看到Spreadsheet項目,選這個就對了∼
在上面的視窗中,可以看到網址列有個sheet id,先記錄下這個,之後會用到...

Step3: 撰寫指令碼... 清掉預先產生的內容(因為用不到),貼上下面的code,並改寫sites裡面的內容成為你要監控的HTTP URL,最後儲存起來...
/**
 * 透過UrlFetch的服務來進行網站Response Time的監控
 */
function emitRequestAndRecord() {
  var sites = [ //定義要監控的HTTP點
  ]; 
  
  var sheet = SpreadsheetApp.openById('0Aq-z....RVkE').getActiveSheet(); //id即為開啟sheet時候,網址列的key參數值
  var rows = sheet.getDataRange();
  var numRows = rows.getNumRows();
  var values = rows.getValues();

  sites.forEach(function(v,i){
    var ts = new Date().getTime();
    var response = UrlFetchApp.fetch(v, {method:"GET"});
    var ts2 = new Date().getTime();
    //sheet欄位:HTTP監控點, 觸發時間, 耗時(ms), HTTP Response Code
    sheet.appendRow([v, new Date(), ts2 - ts, response.getResponseCode()]);
  });
};

Step4: 設定排程器
打開排程器後,設定活動為:時間導向+小時計時器(視需要而定)+每小時(也是視需要而定),然後儲存...

順利的話,Google就會幫你每小時去執行request,並且把Request的耗時記錄下來拉,加上Status Code,就可以判斷網頁是否存活,如果需要,可以在Get到response.getResponseCode()之後,判斷如果非正常回應,就做寄信的動作...

最後,感謝Google大神的免費資源拉∼


留言

這個網誌中的熱門文章

透過Google Apps Script結合Google Form做即時郵件通知

體驗過Google Apps Script的功能後,也發現他結合GmailApps的模組 GmailApps的應用可以用在表單填寫完成後,做發信的通知 例如您開立了一個訂購的表單,為了要在第一時間通知商家有訂單進入 就可以直接呼叫Gmail做發信的通知,讓手持Smart Phone的我們可以很快的知道生意上門了! 下面規劃三個function,其中: onCommit():為form commit時候觸發的function,需要掛載於form commit trigger上 jsonArrToTable():目的將json array解析成為一個Table getLastRowTable():目的將整個table的回傳過濾為剩下第一筆(表頭,含有Form的欄位說明)與最後一筆(原則上就是剛剛送出的那一筆表單) 完整程式碼如下: function onCommit(){   var sheet = SpreadsheetApp.getActiveSheet();   var rows = sheet.getDataRange();   var numRows = rows.getNumRows();   var values = rows.getValues();   var content = getLastRowTable(values);   var htmlBody = "Hi Admin: <br/><br/>有訂單拉,檢查一下吧! <br/><br/>" + content + '<br/><br/>Send by Google Apps';   GmailApp.sendEmail(     " your-email-address@gmail.com ",      "Order Confirm Notice",      htmlBody,      {from: ' from-email-...

Share a chrome plugin for manage google cloud platform

好玩意兒報報.... 同事的新作,把Google Project List在Chrome Plugin中! 對一次管理多個專案的人來說,真得超方便的拉! 下載: https://chrome.google.com/webstore/detail/gdclauncher/bicgkglnnilldakpenngnblekooejnpg 使用說明: 1. Use browser url bar to quick search: Type "gdcl" in browser Press "TAB" to start search Type the project id key word then select the search result... 2. Using quick launch bar... You can search by keyword or click project name to go to the project or gae link to go to gae or go to billing page....

Cloud Monitor嚐鮮

GCP上,我們非常想要的一個功能,終於問世.... Cloud Monitor來了!