alert.go 4.4 KB
Newer Older
Y
Your Name 已提交
1
package consolemysql
E
eoLinker API Management 已提交
2 3 4 5 6 7

import (
	"encoding/json"
	"strconv"
	"time"

黄孟柱 已提交
8
	database2 "github.com/eolinker/goku-api-gateway/common/database"
E
eoLinker API Management 已提交
9 10
)

Y
Your Name 已提交
11
// GetAlertMsgList 获取告警信息列表
E
eoLinker API Management 已提交
12 13 14 15 16 17 18 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
func GetAlertMsgList(page, pageSize int) (bool, []map[string]interface{}, int, error) {
	db := database2.GetConnection()
	var count int
	sql := `SELECT COUNT(*) FROM goku_gateway_alert;`
	err := db.QueryRow(sql).Scan(&count)
	if err != nil {
		return false, make([]map[string]interface{}, 0), 0, err
	}
	sql = "SELECT A.alertID,A.requestURL,A.targetServer,A.targetURL,A.alertPeriodType,A.alertCount,A.updateTime,IFNULL(A.clusterName,''),IFNULL(A.nodeIP,''),IFNULL(B.title,'') FROM goku_gateway_alert A LEFT JOIN goku_cluster B ON B.`name` = A.clusterName ORDER BY updateTime DESC LIMIT ?,?;"
	rows, err := db.Query(sql, (page-1)*pageSize, pageSize)
	if err != nil {
		return false, make([]map[string]interface{}, 0), 0, err
	}
	defer rows.Close()
	alertList := make([]map[string]interface{}, 0)
	for rows.Next() {
		var requestURL, targetServer, targetURL, updateTime, nodeIP, clusterName, title string
		var alertID, alertPeriodType, alertCount int
		err = rows.Scan(&alertID, &requestURL, &targetServer, &targetURL, &alertPeriodType, &alertCount, &updateTime, &clusterName, &nodeIP, &title)
		if err != nil {
			return false, make([]map[string]interface{}, 0), 0, err
		}
		period := "1"
		if alertPeriodType == 1 {
			period = "5"
		} else if alertPeriodType == 2 {
			period = "15"
		} else if alertPeriodType == 3 {
			period = "30"
		} else if alertPeriodType == 4 {
			period = "60"
		}
Y
Your Name 已提交
44
		var msg  = "网关转发失败" + period + "分钟达到" + strconv.Itoa(alertCount) + "次"
E
eoLinker API Management 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
		alertInfo := map[string]interface{}{
			"alertID":      alertID,
			"requestURL":   requestURL,
			"targetServer": targetServer,
			"addr":         nodeIP,
			"name":         clusterName,
			"title":        title,
			"msg":          msg,
			"updateTime":   updateTime,
			"targetURL":    targetURL,
		}
		alertList = append(alertList, alertInfo)
	}
	return true, alertList, count, nil
}

Y
Your Name 已提交
61
// ClearAlertMsg 清空告警信息列表
E
eoLinker API Management 已提交
62 63 64 65 66 67 68 69 70 71
func ClearAlertMsg() (bool, string, error) {
	db := database2.GetConnection()
	sql := "DELETE FROM goku_gateway_alert;"
	_, err := db.Exec(sql)
	if err != nil {
		return false, "[ERROR]Illegal SQL Statement!", err
	}
	return true, "", nil
}

Y
Your Name 已提交
72
// DeleteAlertMsg 删除告警信息
E
eoLinker API Management 已提交
73 74 75 76 77 78 79 80 81 82
func DeleteAlertMsg(alertID int) (bool, string, error) {
	db := database2.GetConnection()
	sql := "DELETE FROM goku_gateway_alert WHERE alertID = ?;"
	_, err := db.Exec(sql, alertID)
	if err != nil {
		return false, "[ERROR]Illegal SQL Statement!", err
	}
	return true, "", nil
}

Y
Your Name 已提交
83
// AddAlertMsg 新增告警信息
E
eoLinker API Management 已提交
84 85 86 87 88 89 90 91 92 93 94
func AddAlertMsg(requestURL, targetServer, targetURL, ip, clusterName string, alertPeriodType, alertCount int) (bool, string, error) {
	db := database2.GetConnection()
	now := time.Now().Format("2006-01-02 15:04:05")
	sql := "INSERT INTO goku_gateway_alert (requestURL,targetServer,targetURL,alertPeriodType,alertCount,nodeIP,clusterName,updateTime) VALUES (?,?,?,?,?,?,?,?);"
	_, err := db.Exec(sql, requestURL, targetServer, targetURL, alertPeriodType, alertCount, ip, clusterName, now)
	if err != nil {
		return false, "[ERROR]Illegal SQL Statement!", err
	}
	return true, "", nil
}

Y
Your Name 已提交
95
//GetAlertConfig 获取告警配置
E
eoLinker API Management 已提交
96 97 98 99 100 101 102 103 104 105
func GetAlertConfig() (bool, map[string]interface{}, error) {
	db := database2.GetConnection()
	var apiAlertInfo, sender, senderPassword, smtpAddress string
	var alertStatus, smtpPort, smtpProtocol int
	sql := `SELECT alertStatus,IFNULL(apiAlertInfo,"{}"),IFNULL(sender,""),IFNULL(senderPassword,""),IFNULL(smtpAddress,""),IFNULL(smtpPort,25),IFNULL(smtpProtocol,0) FROM goku_gateway WHERE id = 1;`
	err := db.QueryRow(sql).Scan(&alertStatus, &apiAlertInfo, &sender, &senderPassword, &smtpAddress, &smtpPort, &smtpProtocol)
	if err != nil {
		return false, nil, err
	}

Y
Your Name 已提交
106
	apiAlertInfoJSON := map[string]interface{}{}
E
eoLinker API Management 已提交
107 108

	if apiAlertInfo == "" || apiAlertInfo == "{}" {
Y
Your Name 已提交
109 110 111
		apiAlertInfoJSON["alertPeriodType"] = 0
		apiAlertInfoJSON["alertAddr"] = ""
		apiAlertInfoJSON["receiverList"] = ""
E
eoLinker API Management 已提交
112
	} else {
Y
Your Name 已提交
113
		err = json.Unmarshal([]byte(apiAlertInfo), &apiAlertInfoJSON)
E
eoLinker API Management 已提交
114 115 116 117 118 119 120 121 122 123 124 125
		if err != nil {
			return false, nil, err
		}
	}

	gatewayConfig := map[string]interface{}{
		"alertStatus":    alertStatus,
		"sender":         sender,
		"senderPassword": senderPassword,
		"smtpAddress":    smtpAddress,
		"smtpPort":       smtpPort,
		"smtpProtocol":   smtpProtocol,
Y
Your Name 已提交
126
		"apiAlertInfo":   apiAlertInfoJSON,
E
eoLinker API Management 已提交
127 128 129
	}
	return true, gatewayConfig, nil
}