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

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

黄孟柱 已提交
8
	database2 "github.com/eolinker/goku-api-gateway/common/database"
E
eoLinker API Management 已提交
9 10 11 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 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 77 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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
)

// 获取告警信息列表
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"
		}
		var msg string = "网关转发失败" + period + "分钟达到" + strconv.Itoa(alertCount) + "次"
		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
}

// 清空告警信息列表
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
}

// 删除告警信息
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
}

// 新增告警信息
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
}

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
	}

	apiAlertInfoJson := map[string]interface{}{}

	if apiAlertInfo == "" || apiAlertInfo == "{}" {
		apiAlertInfoJson["alertPeriodType"] = 0
		apiAlertInfoJson["alertAddr"] = ""
		apiAlertInfoJson["receiverList"] = ""
	} else {
		err = json.Unmarshal([]byte(apiAlertInfo), &apiAlertInfoJson)
		if err != nil {
			return false, nil, err
		}
	}

	gatewayConfig := map[string]interface{}{
		"alertStatus":    alertStatus,
		"sender":         sender,
		"senderPassword": senderPassword,
		"smtpAddress":    smtpAddress,
		"smtpPort":       smtpPort,
		"smtpProtocol":   smtpProtocol,
		"apiAlertInfo":   apiAlertInfoJson,
	}
	return true, gatewayConfig, nil
}