iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 28
0
DevOps

遠離 DevOops系列 第 28

[Day 28] Oops!Golang - Google Cloud Status Monitor with Telegram

Oops 出於Ops的原因開發去

之前就有遇過當Google Cloud 有發生狀況而我們使用者都不知道的情況。/images/emoticon/emoticon04.gif
所以想透過在本機or其他業者的VM來監控Google Cloud Status,避免Google整個掛掉後,沒任何通知。

流程

Google Cloud Status Dashboard 下方找到 JSON Feed,用golang寫個定時訪問的程式(小爬蟲)
當有任何事故發生搶在第一時間知道!並同時準備災害預演的流程。

解析JSON Feed的JSON

	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() + ")"

使用docker-compose啟動

version: "3"

services:
  gggcp:
    image: rain123473/gggcp:latest
    restart: always
    command: ["./gggcp", "watch", "${telegram_bot_token}", "${telegram_chat_id}"]

輸出範例

啟動提醒

https://ithelp.ithome.com.tw/upload/images/20201005/20115289TWgHhCwYNt.png

監控到有異動提醒

[warn] GGGCP has Error!
Started From: 2020-09-25 22:08:29 (Asia/Taipei)
Related Services: Operations
Severity Level: medium

想更了解code的人,我有放在github上,開放給讀者們查閱 GGGCP github,也可以直接從Docker Hub下載rain123473/gggcp:latest

docker pull rain123473/gggcp:latest

/images/emoticon/emoticon62.gif

今天是簡單介紹簡易版的,所以關於時間的比例分配以及比對流程想自己改的話,也歡迎大家可以去fork下來改成自己的版本唷!


上一篇
[Day 27] Oops!CI/CD trigger WebHook to Telegram Bot
下一篇
[Day 29] Oops!Block DevOps
系列文
遠離 DevOops30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言