alert.go 1.6 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3
package utils

import (
黄孟柱 已提交
4
	log "github.com/eolinker/goku-api-gateway/goku-log"
E
eoLinker API Management 已提交
5 6 7 8 9 10
	"io/ioutil"
	"net"
	"strings"
	"time"
)

11 12 13 14 15 16
var (
	currentAlertBody string
)

func init() {
	body, err := ioutil.ReadFile("html/currentAlert.html")
E
eoLinker API Management 已提交
17
	if err != nil {
18
		log.Panic(err)
E
eoLinker API Management 已提交
19
	}
20 21 22
	currentAlertBody = string(body)
}

Y
Your Name 已提交
23
//SendAlertMail 发送告警邮件
24 25 26 27
func SendAlertMail(sender, senderPassword, smtpAddress, smtpPort, smtpProtocol, receiverMail, requestURL, alertLogPath, alertPeriod, alertCount, apiName, apiID, targetServer, proxyURL string) (bool, error) {
	alertTime := time.Now().Format("2006-01-02 15:04:05")

	bodyStr := currentAlertBody
E
eoLinker API Management 已提交
28 29 30 31 32 33 34 35 36 37 38
	bodyStr = strings.Replace(bodyStr, "$requestURL", requestURL, -1)
	bodyStr = strings.Replace(bodyStr, "$alertTime", alertTime, -1)
	bodyStr = strings.Replace(bodyStr, "$alertLogPath", alertLogPath, -1)
	bodyStr = strings.Replace(bodyStr, "$alertPeriod", period[alertPeriod], -1)
	bodyStr = strings.Replace(bodyStr, "$alertCount", alertCount, -1)
	bodyStr = strings.Replace(bodyStr, "$apiID", apiID, -1)
	bodyStr = strings.Replace(bodyStr, "$targetServer", targetServer, -1)
	bodyStr = strings.Replace(bodyStr, "$proxyURL", proxyURL, -1)
	bodyStr = strings.Replace(bodyStr, "$apiName", apiName, -1)
	host := net.JoinHostPort(smtpAddress, smtpPort)
	subject := "GoKu告警:" + requestURL + "接口在" + period[alertPeriod] + "分钟内转发失败" + alertCount + "次"
Y
Your Name 已提交
39
	err := SendToMail(sender, senderPassword, host, receiverMail, subject, bodyStr, "html", smtpProtocol)
E
eoLinker API Management 已提交
40
	if err != nil {
Y
Your Name 已提交
41 42
		log.Warn("SendAlertMail:", err)
		return false, err
E
eoLinker API Management 已提交
43 44 45
	}
	return true, nil
}