Merge pull request #162 from AkkiaS7/enhance-notification
feat: 添加更多的占位符以支持基于服务器状态指标构造自定义的HTTP请求 Co-authored-by: AkkiaS7 <68485070+AkkiaS7@users.noreply.github.com>
This commit is contained in:
commit
4091752096
@ -4,7 +4,7 @@
|
|||||||
<br>
|
<br>
|
||||||
<small><i>LOGO designed by <a href="https://xio.ng" target="_blank">熊大</a> .</i></small>
|
<small><i>LOGO designed by <a href="https://xio.ng" target="_blank">熊大</a> .</i></small>
|
||||||
<br><br>
|
<br><br>
|
||||||
<img src="https://img.shields.io/github/workflow/status/naiba/nezha/Dashboard%20image?label=Dash%20v0.12.20&logo=github&style=for-the-badge"> <img src="https://img.shields.io/github/v/release/naiba/nezha?color=brightgreen&label=Agent&style=for-the-badge&logo=github"> <img src="https://img.shields.io/github/workflow/status/naiba/nezha/Agent%20release?label=Agent%20CI&logo=github&style=for-the-badge"> <img src="https://img.shields.io/badge/Installer-v0.8.2-brightgreen?style=for-the-badge&logo=linux">
|
<img src="https://img.shields.io/github/workflow/status/naiba/nezha/Dashboard%20image?label=Dash%20v0.12.21&logo=github&style=for-the-badge"> <img src="https://img.shields.io/github/v/release/naiba/nezha?color=brightgreen&label=Agent&style=for-the-badge&logo=github"> <img src="https://img.shields.io/github/workflow/status/naiba/nezha/Agent%20release?label=Agent%20CI&logo=github&style=for-the-badge"> <img src="https://img.shields.io/badge/Installer-v0.8.2-brightgreen?style=for-the-badge&logo=linux">
|
||||||
<br>
|
<br>
|
||||||
<br>
|
<br>
|
||||||
<p>:trollface: <b>哪吒监控</b> 一站式轻监控轻运维系统。支持系统状态、HTTP(SSL 证书变更、即将到期、到期)、TCP、Ping 监控报警,计划任务和在线终端。</p>
|
<p>:trollface: <b>哪吒监控</b> 一站式轻监控轻运维系统。支持系统状态、HTTP(SSL 证书变更、即将到期、到期)、TCP、Ping 监控报警,计划任务和在线终端。</p>
|
||||||
|
@ -412,7 +412,11 @@ func (ma *memberAPI) addOrEditNotification(c *gin.Context) {
|
|||||||
verifySSL := nf.VerifySSL == "on"
|
verifySSL := nf.VerifySSL == "on"
|
||||||
n.VerifySSL = &verifySSL
|
n.VerifySSL = &verifySSL
|
||||||
n.ID = nf.ID
|
n.ID = nf.ID
|
||||||
err = n.Send("这是测试消息")
|
ns := model.NotificationServerBundle{
|
||||||
|
Notification: &n,
|
||||||
|
Server: nil,
|
||||||
|
}
|
||||||
|
err = ns.Send("这是测试消息")
|
||||||
}
|
}
|
||||||
if err == nil {
|
if err == nil {
|
||||||
// 保证Tag不为空
|
// 保证Tag不为空
|
||||||
|
@ -25,6 +25,11 @@ const (
|
|||||||
NotificationRequestMethodPOST
|
NotificationRequestMethodPOST
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type NotificationServerBundle struct {
|
||||||
|
Notification *Notification
|
||||||
|
Server *Server
|
||||||
|
}
|
||||||
|
|
||||||
type Notification struct {
|
type Notification struct {
|
||||||
Common
|
Common
|
||||||
Name string
|
Name string
|
||||||
@ -37,8 +42,9 @@ type Notification struct {
|
|||||||
VerifySSL *bool
|
VerifySSL *bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Notification) reqURL(message string) string {
|
func (ns *NotificationServerBundle) reqURL(message string) string {
|
||||||
return replaceParamsInString(n.URL, message, func(msg string) string {
|
n := ns.Notification
|
||||||
|
return replaceParamsInString(ns.Server, n.URL, message, func(msg string) string {
|
||||||
return url.QueryEscape(msg)
|
return url.QueryEscape(msg)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -53,13 +59,14 @@ func (n *Notification) reqMethod() (string, error) {
|
|||||||
return "", errors.New("不支持的请求方式")
|
return "", errors.New("不支持的请求方式")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Notification) reqBody(message string) (string, error) {
|
func (ns *NotificationServerBundle) reqBody(message string) (string, error) {
|
||||||
|
n := ns.Notification
|
||||||
if n.RequestMethod == NotificationRequestMethodGET || message == "" {
|
if n.RequestMethod == NotificationRequestMethodGET || message == "" {
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
switch n.RequestType {
|
switch n.RequestType {
|
||||||
case NotificationRequestTypeJSON:
|
case NotificationRequestTypeJSON:
|
||||||
return replaceParamsInString(n.RequestBody, message, func(msg string) string {
|
return replaceParamsInString(ns.Server, n.RequestBody, message, func(msg string) string {
|
||||||
msgBytes, _ := utils.Json.Marshal(msg)
|
msgBytes, _ := utils.Json.Marshal(msg)
|
||||||
return string(msgBytes)[1 : len(msgBytes)-1]
|
return string(msgBytes)[1 : len(msgBytes)-1]
|
||||||
}), nil
|
}), nil
|
||||||
@ -70,7 +77,7 @@ func (n *Notification) reqBody(message string) (string, error) {
|
|||||||
}
|
}
|
||||||
params := url.Values{}
|
params := url.Values{}
|
||||||
for k, v := range data {
|
for k, v := range data {
|
||||||
params.Add(k, replaceParamsInString(v, message, nil))
|
params.Add(k, replaceParamsInString(ns.Server, v, message, nil))
|
||||||
}
|
}
|
||||||
return params.Encode(), nil
|
return params.Encode(), nil
|
||||||
}
|
}
|
||||||
@ -102,9 +109,9 @@ func (n *Notification) setRequestHeader(req *http.Request) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Notification) Send(message string) error {
|
func (ns *NotificationServerBundle) Send(message string) error {
|
||||||
var verifySSL bool
|
var verifySSL bool
|
||||||
|
n := ns.Notification
|
||||||
if n.VerifySSL != nil && *n.VerifySSL {
|
if n.VerifySSL != nil && *n.VerifySSL {
|
||||||
verifySSL = true
|
verifySSL = true
|
||||||
}
|
}
|
||||||
@ -115,7 +122,7 @@ func (n *Notification) Send(message string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
client := &http.Client{Transport: transCfg, Timeout: time.Minute * 10}
|
client := &http.Client{Transport: transCfg, Timeout: time.Minute * 10}
|
||||||
reqBody, err := n.reqBody(message)
|
reqBody, err := ns.reqBody(message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -125,7 +132,7 @@ func (n *Notification) Send(message string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
req, err := http.NewRequest(reqMethod, n.reqURL(message), strings.NewReader(reqBody))
|
req, err := http.NewRequest(reqMethod, ns.reqURL(message), strings.NewReader(reqBody))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -150,11 +157,42 @@ func (n *Notification) Send(message string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func replaceParamsInString(str string, message string, mod func(string) string) string {
|
// replaceParamInString 替换字符串中的占位符
|
||||||
|
func replaceParamsInString(s *Server, str string, message string, mod func(string) string) string {
|
||||||
if mod != nil {
|
if mod != nil {
|
||||||
str = strings.ReplaceAll(str, "#NEZHA#", mod(message))
|
str = strings.ReplaceAll(str, "#NEZHA#", mod(message))
|
||||||
|
if s != nil {
|
||||||
|
str = strings.ReplaceAll(str, "#SERVER.NAME#", mod(s.Name))
|
||||||
|
str = strings.ReplaceAll(str, "#SERVER.IP#", mod(s.Host.IP))
|
||||||
|
str = strings.ReplaceAll(str, "#SERVER.CPU#", mod(fmt.Sprintf("%f", s.State.CPU)))
|
||||||
|
str = strings.ReplaceAll(str, "#SERVER.MEM#", mod(fmt.Sprintf("%d", s.State.MemUsed)))
|
||||||
|
str = strings.ReplaceAll(str, "#SERVER.SWAP#", mod(fmt.Sprintf("%d", s.State.SwapUsed)))
|
||||||
|
str = strings.ReplaceAll(str, "#SERVER.DISK#", mod(fmt.Sprintf("%d", s.State.DiskUsed)))
|
||||||
|
str = strings.ReplaceAll(str, "#SERVER.NETINSPEED#", mod(fmt.Sprintf("%d", s.State.NetInSpeed)))
|
||||||
|
str = strings.ReplaceAll(str, "#SERVER.NETOUTSPEED#", mod(fmt.Sprintf("%d", s.State.NetOutSpeed)))
|
||||||
|
str = strings.ReplaceAll(str, "#SERVER.TRANSFERIN#", mod(fmt.Sprintf("%d", s.State.NetInTransfer)))
|
||||||
|
str = strings.ReplaceAll(str, "#SERVER.TRANSFEROUT#", mod(fmt.Sprintf("%d", s.State.NetOutTransfer)))
|
||||||
|
str = strings.ReplaceAll(str, "#SERVER.LOAD1#", mod(fmt.Sprintf("%f", s.State.Load1)))
|
||||||
|
str = strings.ReplaceAll(str, "#SERVER.LOAD5#", mod(fmt.Sprintf("%f", s.State.Load5)))
|
||||||
|
str = strings.ReplaceAll(str, "#SERVER.LOAD15#", mod(fmt.Sprintf("%f", s.State.Load15)))
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
str = strings.ReplaceAll(str, "#NEZHA#", message)
|
str = strings.ReplaceAll(str, "#NEZHA#", message)
|
||||||
|
if s != nil {
|
||||||
|
str = strings.ReplaceAll(str, "#SERVER.NAME#", s.Name)
|
||||||
|
str = strings.ReplaceAll(str, "#SERVER.IP#", s.Host.IP)
|
||||||
|
str = strings.ReplaceAll(str, "#SERVER.CPU#", fmt.Sprintf("%f", s.State.CPU))
|
||||||
|
str = strings.ReplaceAll(str, "#SERVER.MEM#", fmt.Sprintf("%d", s.State.MemUsed))
|
||||||
|
str = strings.ReplaceAll(str, "#SERVER.SWAP#", fmt.Sprintf("%d", s.State.SwapUsed))
|
||||||
|
str = strings.ReplaceAll(str, "#SERVER.DISK#", fmt.Sprintf("%d", s.State.DiskUsed))
|
||||||
|
str = strings.ReplaceAll(str, "#SERVER.NETINSPEED#", fmt.Sprintf("%d", s.State.NetInSpeed))
|
||||||
|
str = strings.ReplaceAll(str, "#SERVER.NETOUTSPEED#", fmt.Sprintf("%d", s.State.NetOutSpeed))
|
||||||
|
str = strings.ReplaceAll(str, "#SERVER.TRANSFERIN#", fmt.Sprintf("%d", s.State.NetInTransfer))
|
||||||
|
str = strings.ReplaceAll(str, "#SERVER.TRANSFEROUT#", fmt.Sprintf("%d", s.State.NetOutTransfer))
|
||||||
|
str = strings.ReplaceAll(str, "#SERVER.LOAD1#", fmt.Sprintf("%f", s.State.Load1))
|
||||||
|
str = strings.ReplaceAll(str, "#SERVER.LOAD5#", fmt.Sprintf("%f", s.State.Load5))
|
||||||
|
str = strings.ReplaceAll(str, "#SERVER.LOAD15#", fmt.Sprintf("%f", s.State.Load15))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return str
|
return str
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
@ -35,8 +36,56 @@ func execCase(t *testing.T, item testSt) {
|
|||||||
RequestBody: item.body,
|
RequestBody: item.body,
|
||||||
RequestHeader: item.header,
|
RequestHeader: item.header,
|
||||||
}
|
}
|
||||||
assert.Equal(t, item.expectURL, n.reqURL(msg))
|
server := Server{
|
||||||
reqBody, err := n.reqBody(msg)
|
Common: Common{},
|
||||||
|
Name: "ServerName",
|
||||||
|
Tag: "",
|
||||||
|
Secret: "",
|
||||||
|
Note: "",
|
||||||
|
DisplayIndex: 0,
|
||||||
|
Host: &Host{
|
||||||
|
Platform: "",
|
||||||
|
PlatformVersion: "",
|
||||||
|
CPU: nil,
|
||||||
|
MemTotal: 0,
|
||||||
|
DiskTotal: 0,
|
||||||
|
SwapTotal: 0,
|
||||||
|
Arch: "",
|
||||||
|
Virtualization: "",
|
||||||
|
BootTime: 0,
|
||||||
|
IP: "1.1.1.1",
|
||||||
|
CountryCode: "",
|
||||||
|
Version: "",
|
||||||
|
},
|
||||||
|
State: &HostState{
|
||||||
|
CPU: 0,
|
||||||
|
MemUsed: 0,
|
||||||
|
SwapUsed: 8888,
|
||||||
|
DiskUsed: 0,
|
||||||
|
NetInTransfer: 0,
|
||||||
|
NetOutTransfer: 0,
|
||||||
|
NetInSpeed: 0,
|
||||||
|
NetOutSpeed: 0,
|
||||||
|
Uptime: 0,
|
||||||
|
Load1: 0,
|
||||||
|
Load5: 0,
|
||||||
|
Load15: 0,
|
||||||
|
TcpConnCount: 0,
|
||||||
|
UdpConnCount: 0,
|
||||||
|
ProcessCount: 0,
|
||||||
|
},
|
||||||
|
LastActive: time.Time{},
|
||||||
|
TaskClose: nil,
|
||||||
|
TaskStream: nil,
|
||||||
|
PrevHourlyTransferIn: 0,
|
||||||
|
PrevHourlyTransferOut: 0,
|
||||||
|
}
|
||||||
|
ns := NotificationServerBundle{
|
||||||
|
Notification: &n,
|
||||||
|
Server: &server,
|
||||||
|
}
|
||||||
|
assert.Equal(t, item.expectURL, ns.reqURL(msg))
|
||||||
|
reqBody, err := ns.reqBody(msg)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, item.expectBody, reqBody)
|
assert.Equal(t, item.expectBody, reqBody)
|
||||||
reqMethod, err := n.reqMethod()
|
reqMethod, err := n.reqMethod()
|
||||||
@ -117,6 +166,28 @@ func TestNotification(t *testing.T) {
|
|||||||
expectBody: `{"msg":"msg"}`,
|
expectBody: `{"msg":"msg"}`,
|
||||||
expectHeader: map[string]string{"asd": "dsa11"},
|
expectHeader: map[string]string{"asd": "dsa11"},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
url: "https://example.com/?m=#NEZHA#",
|
||||||
|
body: `{"Server":"#SERVER.NAME#","ServerIP":"#SERVER.IP#","ServerSWAP":#SERVER.SWAP#}`,
|
||||||
|
reqMethod: NotificationRequestMethodPOST,
|
||||||
|
header: `{"asd":"dsa11"}`,
|
||||||
|
reqType: NotificationRequestTypeJSON,
|
||||||
|
expectURL: "https://example.com/?m=" + msg,
|
||||||
|
expectMethod: http.MethodPost,
|
||||||
|
expectContentType: reqTypeJSON,
|
||||||
|
expectBody: `{"Server":"ServerName","ServerIP":"1.1.1.1","ServerSWAP":8888}`,
|
||||||
|
expectHeader: map[string]string{"asd": "dsa11"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
url: "https://example.com/?m=#NEZHA#",
|
||||||
|
body: `{"#NEZHA#":"#NEZHA#","Server":"#SERVER.NAME#","ServerIP":"#SERVER.IP#","ServerSWAP":"#SERVER.SWAP#"}`,
|
||||||
|
reqMethod: NotificationRequestMethodPOST,
|
||||||
|
reqType: NotificationRequestTypeForm,
|
||||||
|
expectURL: "https://example.com/?m=" + msg,
|
||||||
|
expectMethod: http.MethodPost,
|
||||||
|
expectContentType: reqTypeForm,
|
||||||
|
expectBody: "%23NEZHA%23=" + msg + "&Server=ServerName&ServerIP=1.1.1.1&ServerSWAP=8888",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
|
@ -36,7 +36,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="secret field">
|
<div class="secret field">
|
||||||
<label>Body</label>
|
<label>Body</label>
|
||||||
<textarea name="RequestBody" placeholder='{"content":"#NEZHA#"}'></textarea>
|
<textarea name="RequestBody" placeholder='{ "content":"#NEZHA#", "ServerName":"#SERVER.NAME#", "ServerIP":"#SERVER.IP#", "CPU":"#SERVER.CPU#", "MEM":"#SERVER.MEM#", "SWAP":"#SERVER.SWAP#", "DISK":"#SERVER.DISK#", "NetInSpeed":"#SERVER.NETINSPEED#", "NetOutSpeed":"#SERVER.NETOUTSPEED#", "TransferIn":"#SERVER.TRANSFERIN#", "TranferOut":"#SERVER.TRANSFEROUT#", "Load1":"#SERVER.LOAD1#", "Load5":"#SERVER.LOAD5#", "Load15":"#SERVER.LOAD15#" }'></textarea>
|
||||||
</div>
|
</div>
|
||||||
<div class="field">
|
<div class="field">
|
||||||
<div class="ui nf-ssl checkbox">
|
<div class="ui nf-ssl checkbox">
|
||||||
|
@ -3,6 +3,7 @@ package rpc
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/jinzhu/copier"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/naiba/nezha/model"
|
"github.com/naiba/nezha/model"
|
||||||
@ -28,11 +29,14 @@ func (s *NezhaHandler) ReportTask(c context.Context, r *pb.TaskResult) (*pb.Rece
|
|||||||
if cr != nil {
|
if cr != nil {
|
||||||
singleton.ServerLock.RLock()
|
singleton.ServerLock.RLock()
|
||||||
defer singleton.ServerLock.RUnlock()
|
defer singleton.ServerLock.RUnlock()
|
||||||
|
// 保存当前服务器状态信息
|
||||||
|
curServer := model.Server{}
|
||||||
|
copier.Copy(&curServer, singleton.ServerList[clientID])
|
||||||
if cr.PushSuccessful && r.GetSuccessful() {
|
if cr.PushSuccessful && r.GetSuccessful() {
|
||||||
singleton.SendNotification(cr.NotificationTag, fmt.Sprintf("[任务成功] %s ,服务器:%s,日志:\n%s", cr.Name, singleton.ServerList[clientID].Name, r.GetData()), false)
|
singleton.SendNotification(cr.NotificationTag, fmt.Sprintf("[任务成功] %s ,服务器:%s,日志:\n%s", cr.Name, singleton.ServerList[clientID].Name, r.GetData()), false, &curServer)
|
||||||
}
|
}
|
||||||
if !r.GetSuccessful() {
|
if !r.GetSuccessful() {
|
||||||
singleton.SendNotification(cr.NotificationTag, fmt.Sprintf("[任务失败] %s ,服务器:%s,日志:\n%s", cr.Name, singleton.ServerList[clientID].Name, r.GetData()), false)
|
singleton.SendNotification(cr.NotificationTag, fmt.Sprintf("[任务失败] %s ,服务器:%s,日志:\n%s", cr.Name, singleton.ServerList[clientID].Name, r.GetData()), false, &curServer)
|
||||||
}
|
}
|
||||||
singleton.DB.Model(cr).Updates(model.Cron{
|
singleton.DB.Model(cr).Updates(model.Cron{
|
||||||
LastExecutedAt: time.Now().Add(time.Second * -1 * time.Duration(r.GetDelay())),
|
LastExecutedAt: time.Now().Add(time.Second * -1 * time.Duration(r.GetDelay())),
|
||||||
|
@ -2,6 +2,7 @@ package singleton
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/jinzhu/copier"
|
||||||
"log"
|
"log"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@ -147,14 +148,17 @@ func checkStatus() {
|
|||||||
ID][server.ID], alert.Snapshot(AlertsCycleTransferStatsStore[alert.ID], server, DB))
|
ID][server.ID], alert.Snapshot(AlertsCycleTransferStatsStore[alert.ID], server, DB))
|
||||||
// 发送通知,分为触发报警和恢复通知
|
// 发送通知,分为触发报警和恢复通知
|
||||||
max, passed := alert.Check(alertsStore[alert.ID][server.ID])
|
max, passed := alert.Check(alertsStore[alert.ID][server.ID])
|
||||||
|
// 保存当前服务器状态信息
|
||||||
|
curServer := model.Server{}
|
||||||
|
copier.Copy(&curServer, server)
|
||||||
if !passed {
|
if !passed {
|
||||||
alertsPrevState[alert.ID][server.ID] = _RuleCheckFail
|
alertsPrevState[alert.ID][server.ID] = _RuleCheckFail
|
||||||
message := fmt.Sprintf("[主机故障] %s(%s) 规则:%s", server.Name, IPDesensitize(server.Host.IP), alert.Name)
|
message := fmt.Sprintf("[主机故障] %s(%s) 规则:%s", server.Name, IPDesensitize(server.Host.IP), alert.Name)
|
||||||
go SendNotification(alert.NotificationTag, message, true)
|
go SendNotification(alert.NotificationTag, message, true, &curServer)
|
||||||
} else {
|
} else {
|
||||||
if alertsPrevState[alert.ID][server.ID] == _RuleCheckFail {
|
if alertsPrevState[alert.ID][server.ID] == _RuleCheckFail {
|
||||||
message := fmt.Sprintf("[主机恢复] %s(%s) 规则:%s", server.Name, IPDesensitize(server.Host.IP), alert.Name)
|
message := fmt.Sprintf("[主机恢复] %s(%s) 规则:%s", server.Name, IPDesensitize(server.Host.IP), alert.Name)
|
||||||
go SendNotification(alert.NotificationTag, message, true)
|
go SendNotification(alert.NotificationTag, message, true, &curServer)
|
||||||
}
|
}
|
||||||
alertsPrevState[alert.ID][server.ID] = _RuleCheckPass
|
alertsPrevState[alert.ID][server.ID] = _RuleCheckPass
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package singleton
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/jinzhu/copier"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/robfig/cron/v3"
|
"github.com/robfig/cron/v3"
|
||||||
@ -84,7 +85,10 @@ func CronTrigger(cr model.Cron) func() {
|
|||||||
Type: model.TaskTypeCommand,
|
Type: model.TaskTypeCommand,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
SendNotification(cr.NotificationTag, fmt.Sprintf("[任务失败] %s,服务器 %s 离线,无法执行。", cr.Name, s.Name), false)
|
// 保存当前服务器状态信息
|
||||||
|
curServer := model.Server{}
|
||||||
|
copier.Copy(&curServer, s)
|
||||||
|
SendNotification(cr.NotificationTag, fmt.Sprintf("[任务失败] %s,服务器 %s 离线,无法执行。", cr.Name, s.Name), false, &curServer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -102,7 +102,7 @@ func OnDeleteNotification(id uint64) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SendNotification 向指定的通知方式组的所有通知方式发送通知
|
// SendNotification 向指定的通知方式组的所有通知方式发送通知
|
||||||
func SendNotification(notificationTag string, desc string, mutable bool) {
|
func SendNotification(notificationTag string, desc string, mutable bool, ext ...*model.Server) {
|
||||||
if mutable {
|
if mutable {
|
||||||
// 通知防骚扰策略
|
// 通知防骚扰策略
|
||||||
nID := hex.EncodeToString(md5.New().Sum([]byte(desc))) // #nosec
|
nID := hex.EncodeToString(md5.New().Sum([]byte(desc))) // #nosec
|
||||||
@ -143,7 +143,14 @@ func SendNotification(notificationTag string, desc string, mutable bool) {
|
|||||||
log.Println("尝试通知", n.Name)
|
log.Println("尝试通知", n.Name)
|
||||||
}
|
}
|
||||||
for _, n := range NotificationList[notificationTag] {
|
for _, n := range NotificationList[notificationTag] {
|
||||||
if err := n.Send(desc); err != nil {
|
ns := model.NotificationServerBundle{
|
||||||
|
Notification: n,
|
||||||
|
Server: nil,
|
||||||
|
}
|
||||||
|
if len(ext) > 0 {
|
||||||
|
ns.Server = ext[0]
|
||||||
|
}
|
||||||
|
if err := ns.Send(desc); err != nil {
|
||||||
log.Println("NEZHA>> 向 ", n.Name, " 发送通知失败:", err)
|
log.Println("NEZHA>> 向 ", n.Name, " 发送通知失败:", err)
|
||||||
} else {
|
} else {
|
||||||
log.Println("NEZHA>> 向 ", n.Name, " 发送通知成功:")
|
log.Println("NEZHA>> 向 ", n.Name, " 发送通知成功:")
|
||||||
|
@ -12,7 +12,7 @@ import (
|
|||||||
"github.com/naiba/nezha/pkg/utils"
|
"github.com/naiba/nezha/pkg/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Version = "v0.12.20" // !!记得修改 README 中的 badge 版本!!
|
var Version = "v0.12.21" // !!记得修改 README 中的 badge 版本!!
|
||||||
|
|
||||||
var (
|
var (
|
||||||
Conf *model.Config
|
Conf *model.Config
|
||||||
|
Loading…
x
Reference in New Issue
Block a user