提交 dc5082a8 编写于 作者: Y Your Name

#console 修复告警部分文件读取后未关闭的问题

上级 e679a098
......@@ -9,17 +9,22 @@ import (
"time"
)
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")
f, err := os.Open("html/currentAlert.html")
if err != nil {
log.Warn(err)
}
body, err := ioutil.ReadAll(f)
var (
currentAlertBody string
)
func init() {
body, err := ioutil.ReadFile("html/currentAlert.html")
if err != nil {
log.Warn(err)
log.Panic(err)
}
bodyStr := string(body)
currentAlertBody = string(body)
}
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
bodyStr = strings.Replace(bodyStr, "$requestURL", requestURL, -1)
bodyStr = strings.Replace(bodyStr, "$alertTime", alertTime, -1)
bodyStr = strings.Replace(bodyStr, "$alertLogPath", alertLogPath, -1)
......@@ -38,33 +43,3 @@ func SendAlertMail(sender, senderPassword, smtpAddress, smtpPort, smtpProtocol,
return true, nil
}
func SendMonitorAlertMail(sender, senderPassword, smtpAddress, smtpPort, smtpProtocol, receiverMail, bodyStr string) (bool, error) {
host := net.JoinHostPort(smtpAddress, smtpPort)
subject := "EOLINKER AGW节点自动重启失败告警"
err := SendToMail(sender, senderPassword, host, receiverMail, subject, bodyStr, "html", smtpProtocol)
if err != nil {
log.Warn("SendMonitorAlertMail:",err)
}
return true, nil
}
func ReplaceMonitorBody(nodeList []map[string]string) (bool, string, error) {
f, err := os.Open("html/monitorAlert.html")
if err != nil {
log.Warn(err)
}
body, err := ioutil.ReadAll(f)
if err != nil {
log.Warn(err)
}
bodyStr := string(body)
nodeStr := ""
for _, nodeInfo := range nodeList {
nodeStr += "<p>节点名称:" + nodeInfo["nodeName"] + "</p>"
nodeStr += "<p>节点IP:" + nodeInfo["nodeIP"] + ":" + nodeInfo["nodePort"] + "</p>"
nodeStr += "<p></p>"
}
bodyStr = strings.Replace(bodyStr, "$alertTime", time.Now().Format("2006-01-02 15:04:05"), -1)
bodyStr = strings.Replace(bodyStr, "$nodeList", nodeStr, -1)
return true, bodyStr, nil
}
......@@ -2,9 +2,7 @@ package utils
import (
"crypto/md5"
"crypto/sha1"
"encoding/hex"
"errors"
"fmt"
"math/rand"
"net"
......@@ -14,8 +12,6 @@ import (
"strconv"
"strings"
"time"
"golang.org/x/crypto/bcrypt"
)
// 将string转为int类型
......@@ -48,17 +44,6 @@ func InterceptIP(str, substr string) string {
return rs
}
func GetHashKey(first_sail string, args ...string) string {
hashKey := ""
hashKey = hashKey + strconv.Itoa(int(time.Now().Unix())) + first_sail
for i := 0; i < len(args); i++ {
hashKey += args[i]
}
h := sha1.New()
h.Write([]byte(hashKey))
return hex.EncodeToString(h.Sum(nil))
}
func Md5(encodeString string) string {
h := md5.New()
h.Write([]byte(encodeString))
......@@ -99,29 +84,6 @@ func Stop() bool {
}
}
// 启动网关服务
func StartGateway() bool {
cmd := exec.Command("/bin/bash", "-c", "go run gateway.go")
if _, err := cmd.Output(); err != nil {
return false
} else {
return true
}
}
// 将[]string转为[]int
func ConvertArray(arr []string) (bool, []int) {
result := make([]int, 0)
for _, i := range arr {
res, err := strconv.Atoi(i)
if err != nil {
return false, result
}
result = append(result, res)
}
return true, result
}
// 获取MAC地址
func GetMac() (bool, string) {
interfaces, err := net.Interfaces()
......@@ -141,41 +103,3 @@ func GetMac() (bool, string) {
}
return false, ""
}
// 匹配机器码和授权码是否一致
func MatchVerifyCode(verifyCode, mac string) bool {
err := bcrypt.CompareHashAndPassword([]byte(verifyCode), []byte(mac))
if err != nil {
return false
} else {
return true
}
}
// 将机器码加密
func BcryptMAC() string {
_, mac := GetMac()
verifyCode, err := bcrypt.GenerateFromPassword([]byte(Md5(mac)), bcrypt.DefaultCost)
if err != nil {
return ""
}
return string(verifyCode)
}
// 将数组的值赋给每一个变量
func ConvertArrayToVariable(arr []interface{}, variable ...interface{}) error {
if len(arr) != len(variable) {
return errors.New("[ERROR]Fail to convert")
}
for i, v := range arr {
tmp, _ := variable[i].(*int)
tmpvstr, _ := arr[i].(string)
tmpv, _ := strconv.Atoi(tmpvstr)
if v != nil {
*tmp = tmpv
} else {
*tmp = 0
}
}
return nil
}
......@@ -16,14 +16,6 @@ var period map[string]string = map[string]string{
"4": "60",
}
func SendMail(sender, subject, senderPassword, smtpAddress, smtpPort, smtpProtocol, receiverMail, content string) {
host := net.JoinHostPort(smtpAddress, smtpPort)
err := SendToMail(sender, senderPassword, host, receiverMail, subject, content, "html", smtpProtocol)
if err != nil {
log.Warn("SendMail:",err)
}
}
func SendToMail(user, password, host, to, subject, body, mailtype, smtpProtocol string) error {
hp := strings.Split(host, ":")
auth := smtp.PlainAuth("", user, password, hp[0])
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册