add alarm feature

This commit is contained in:
s_jqzhang 2019-01-09 14:48:15 +08:00
parent 16f758c98d
commit 200c0b5fc2

View File

@ -3,6 +3,7 @@ package main
import (
"crypto/md5"
"crypto/rand"
"encoding/base64"
"encoding/json"
"errors"
@ -99,7 +100,8 @@ const (
"password":"abc",
"host":"smtp.163.com:25"
},
"alram_receivers":[]
"alram_receivers":[],
"alarm_url":""
}
`
@ -185,6 +187,7 @@ type GloablConfig struct {
AlramReceivers []string `json:"alram_receivers"`
DefaultScene string `json:"default_scene"`
Mail Mail `json:"mail"`
AlarmUrl string `json:"alarm_url"`
}
type CommonMap struct {
@ -1488,23 +1491,39 @@ func (this *Server) Check() {
err error
subject string
body string
req *httplib.BeegoHTTPRequest
)
for _, peer := range Config().Peers {
req := httplib.Get(peer + "/status")
req = httplib.Get(peer + "/status")
req.SetTimeout(time.Second*5, time.Second*5)
err = req.ToJSON(&status)
if status.Status != "ok" {
for _, to := range Config().AlramReceivers {
subject = fmt.Sprintf("fastdfs server %s error", peer)
subject = "fastdfs server error"
if err != nil {
body = fmt.Sprintf("%s\nerror:\n%s", subject, err.Error())
body = fmt.Sprintf("%s\nserver:%s\nerror:\n%s", subject, peer, err.Error())
} else {
body = fmt.Sprintf("%s\nserver:%s\n", subject, peer)
}
this.SendToMail(to, subject, body, "text")
if err = this.SendToMail(to, subject, body, "text"); err != nil {
log.Error(err)
}
}
if Config().AlarmUrl != "" {
req = httplib.Post(Config().AlarmUrl)
req.SetTimeout(time.Second*10, time.Second*10)
req.Param("message", body)
req.Param("subject", subject)
if _, err = req.String(); err != nil {
log.Error(err)
}
}
}
@ -1514,7 +1533,7 @@ func (this *Server) Check() {
go func() {
for {
time.Sleep(time.Minute * 10)
time.Sleep(time.Second * 10)
check()
}
}()