之前就有遇過當Google Cloud
有發生狀況而我們使用者都不知道的情況。
所以想透過在本機or其他業者的VM來監控Google Cloud Status
,避免Google整個掛掉後,沒任何通知。
在 Google Cloud Status Dashboard 下方找到 JSON Feed,用golang寫個定時訪問的程式(小爬蟲)
當有任何事故發生搶在第一時間知道!並同時準備災害預演的流程。
resp, err := http.Get("https://status.cloud.google.com/incidents.json")
if err != nil {
fmt.Println("[warn] httpGet:", err)
return nil, err
}
defer resp.Body.Close()
type AutoGenerated struct {
Begin time.Time `json:"begin"`
Created time.Time `json:"created"`
End time.Time `json:"end"`
ExternalDesc string `json:"external_desc"`
Modified time.Time `json:"modified"`
MostRecentUpdate struct {
Created time.Time `json:"created"`
Modified time.Time `json:"modified"`
Text string `json:"text"`
When time.Time `json:"when"`
} `json:"most-recent-update"`
Number int `json:"number"`
Public bool `json:"public"`
ServiceKey string `json:"service_key"`
ServiceName string `json:"service_name"`
Severity string `json:"severity"`
Updates []struct {
Created time.Time `json:"created"`
Modified time.Time `json:"modified"`
Text string `json:"text"`
When time.Time `json:"when"`
} `json:"updates"`
URI string `json:"uri"`
}
body, ioReadAllErr := ioutil.ReadAll(resp.Body)
if ioReadAllErr != nil {
fmt.Println("[warn] ioReadAllErr:", ioReadAllErr)
return ioReadAllErr, nil
}
ret := []*AutoGenerated{}
jerr := json.Unmarshal(body, &ret)
if jerr != nil {
return jerr, nil
}
約每六十秒執行一次
for {
time.Sleep(time.Duration(60) * time.Second)
}
loc, loadLocationErr := time.LoadLocation("Asia/Taipei")
if loadLocationErr != nil {
fmt.Println("[warn] loadLocationErr:", loadLocationErr)
return loadLocationErr, nil
}
// Time zone correction
recentTime := time.Unix(ret[0].Begin.Unix(), 0).In(loc).Format("2006-01-02 15:04:05") + " (" + loc.String() + ")"
now := time.Now().In(loc).Format("2006-01-02 15:04:05") + " (" + loc.String() + ")"
version: "3"
services:
gggcp:
image: rain123473/gggcp:latest
restart: always
command: ["./gggcp", "watch", "${telegram_bot_token}", "${telegram_chat_id}"]
[warn] GGGCP has Error!
Started From: 2020-09-25 22:08:29 (Asia/Taipei)
Related Services: Operations
Severity Level: medium
docker pull rain123473/gggcp:latest