script.go 2.6 KB
Newer Older
Y
Your Name 已提交
1
package consolemysql
E
eoLinker API Management 已提交
2 3 4

import (
	"encoding/json"
黄孟柱 已提交
5
	database2 "github.com/eolinker/goku-api-gateway/common/database"
Y
Your Name 已提交
6
	log "github.com/eolinker/goku-api-gateway/goku-log"
E
eoLinker API Management 已提交
7 8 9 10 11 12 13 14 15 16 17
)

type balanceConfig struct {
	LoadBalancingServer []server `json:"loadBalancingServer"`
}

type server struct {
	Server string `json:"server"`
	Weight int    `json:"weight"`
}

Y
Your Name 已提交
18
// RefreshGatewayAlertConfig 新建项目
E
eoLinker API Management 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
func RefreshGatewayAlertConfig() bool {
	db := database2.GetConnection()
	var (
		alertAddr       string
		alertLogPath    string
		alertPeriodType int
		receiverList    string
	)
	// 获取网关告警配置
	sqlCode := `SELECT alertAddress,alertLogPath,alertPeriodType,receiverList FROM goku_gateway;`
	err := db.QueryRow(sqlCode).Scan(&alertAddr, &alertLogPath, &alertPeriodType, &receiverList)
	if err != nil {
		log.Error(err)
		return false
	}
	if alertLogPath == "" {
		alertLogPath = "./log/apiAlert"
	}
	// 构造告警需要的信息
	apiAlertInfo := map[string]interface{}{
		"alertPeriodType": alertPeriodType,
		"receiverList":    receiverList,
		"alertAddr":       alertAddr,
	}
	nodeAlertInfo := map[string]interface{}{
		"receiverList": receiverList,
		"alertAddr":    alertAddr,
	}
	redisAlertInfo := map[string]interface{}{
		"receiverList": receiverList,
		"alertAddr":    alertAddr,
	}
	apiAlertInfoByte, err := json.Marshal(apiAlertInfo)
	if err != nil {
		log.Error(err)
		return false
	}
	nodeAlertInfoByte, err := json.Marshal(nodeAlertInfo)
	if err != nil {
		log.Error(err)
		return false
	}
	redisAlertInfoByte, err := json.Marshal(redisAlertInfo)
	if err != nil {
		log.Error(err)
		return false
	}
	Tx, _ := db.Begin()

	_, err = Tx.Exec("UPDATE goku_gateway SET apiAlertInfo = ?,nodeAlertInfo = ?,redisAlertInfo = ?;", string(apiAlertInfoByte), string(nodeAlertInfoByte), string(redisAlertInfoByte))
	if err != nil {
		Tx.Rollback()
		log.Error(err)
		return false
	}
	dropColomn := []string{"alertPeriodType", "alertAddress", "alertLogPath", "receiverList"}
	for _, colomn := range dropColomn {
		sql := "ALTER TABLE goku_gateway DROP COLUMN " + colomn + ";"
Y
Your Name 已提交
77
		log.Debug("RefreshGatewayAlertConfig-sql:", sql)
E
eoLinker API Management 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
		_, err = Tx.Exec(sql)
		if err != nil {
			Tx.Rollback()
			log.Error(err)
			return false
		}
	}
	Tx.Commit()
	return true
}

type monitorRecord struct {
	gatewayRequestCount   int
	gatewaySuccessCount   int
	gatewayStatus2xxCount int
	gatewayStatus4xxCount int
	gatewayStatus5xxCount int
	proxyRequestCount     int
	proxySuccessCount     int
	proxyStatus2xxCount   int
	proxyStatus4xxCount   int
	proxyStatus5xxCount   int
	proxyTimeoutCount     int
	updateTime            string
	hour                  string
	strategyID            string
	apiID                 int
}