diff --git a/README.md b/README.md index feddbbf..54b4f47 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@
:trollface: 哪吒监控 一站式轻监控轻运维系统。支持系统状态、HTTP(SSL 证书变更、即将到期、到期)、TCP、Ping 监控报警,命令批量执行和计划任务。
diff --git a/cmd/dashboard/controller/common_page.go b/cmd/dashboard/controller/common_page.go index 2d9cca3..c83e01f 100644 --- a/cmd/dashboard/controller/common_page.go +++ b/cmd/dashboard/controller/common_page.go @@ -100,6 +100,11 @@ func (cp *commonPage) home(c *gin.Context) { var upgrader = websocket.Upgrader{} +type Data struct { + Now int64 `json:"now,omitempty"` + Servers []*model.Server `json:"servers,omitempty"` +} + func (cp *commonPage) ws(c *gin.Context) { conn, err := upgrader.Upgrade(c.Writer, c.Request, nil) if err != nil { @@ -116,7 +121,10 @@ func (cp *commonPage) ws(c *gin.Context) { count := 0 for { dao.SortedServerLock.RLock() - err = conn.WriteJSON(dao.SortedServerList) + err = conn.WriteJSON(Data{ + Now: time.Now().Unix() * 1000, + Servers: dao.SortedServerList, + }) dao.SortedServerLock.RUnlock() if err != nil { break diff --git a/cmd/dashboard/main.go b/cmd/dashboard/main.go index 39a9dff..79c50d8 100644 --- a/cmd/dashboard/main.go +++ b/cmd/dashboard/main.go @@ -175,7 +175,7 @@ func main() { for _, s := range dao.ServerList { in := s.State.NetInTransfer - uint64(s.PrevHourlyTransferIn) out := s.State.NetOutTransfer - uint64(s.PrevHourlyTransferOut) - if in > 0 && out > 0 { + if in > 0 || out > 0 { tx := model.Transfer{ ServerID: s.ID, In: in, diff --git a/model/alertrule.go b/model/alertrule.go index 053ad0c..c9d50ad 100644 --- a/model/alertrule.go +++ b/model/alertrule.go @@ -40,10 +40,10 @@ func (r *AlertRule) Check(points [][]interface{}) (int, bool) { var count int for i := 0; i < len(r.Rules); i++ { if r.Rules[i].IsTransferDurationRule() { + // 循环区间流量报警 if max < 1 { max = 1 } - // 循环区间流量报警 for j := len(points[i]) - 1; j >= 0; j-- { if points[i][j] != nil { count++ diff --git a/model/rule.go b/model/rule.go index 4e332b3..312ad15 100644 --- a/model/rule.go +++ b/model/rule.go @@ -30,7 +30,8 @@ type Rule struct { Ignore map[uint64]bool `json:"ignore,omitempty"` // 覆盖范围的排除 // 只作为缓存使用,记录下次该检测的时间 - NextTransferAt map[uint64]time.Time `json:"-"` + NextTransferAt map[uint64]time.Time `json:"-"` + LastCycleStatus map[uint64]interface{} `json:"-"` } func percentage(used, total uint64) uint64 { @@ -53,7 +54,7 @@ func (u *Rule) Snapshot(server *Server, db *gorm.DB) interface{} { // 循环区间流量检测 · 短期无需重复检测 if u.IsTransferDurationRule() && u.NextTransferAt[server.ID].After(time.Now()) { - return nil + return u.LastCycleStatus[server.ID] } var src uint64 @@ -96,7 +97,7 @@ func (u *Rule) Snapshot(server *Server, db *gorm.DB) interface{} { src = server.State.NetOutTransfer - uint64(server.PrevHourlyTransferOut) if u.CycleInterval != 1 { var res NResult - db.Model(&Transfer{}).Select("SUM('in') AS n").Where("created_at > ? AND server_id = ?", u.GetTransferDurationStart(), server.ID).Scan(&res) + db.Model(&Transfer{}).Select("SUM('out') AS n").Where("created_at > ? AND server_id = ?", u.GetTransferDurationStart(), server.ID).Scan(&res) src += res.N } case "transfer_all_cycle": @@ -117,7 +118,15 @@ func (u *Rule) Snapshot(server *Server, db *gorm.DB) interface{} { if u.NextTransferAt == nil { u.NextTransferAt = make(map[uint64]time.Time) } + if u.LastCycleStatus == nil { + u.LastCycleStatus = make(map[uint64]interface{}) + } u.NextTransferAt[server.ID] = time.Now().Add(time.Duration(time.Second * seconds)) + if (u.Max > 0 && src > u.Max) || (u.Min > 0 && src < u.Min) { + u.LastCycleStatus[server.ID] = struct{}{} + } else { + u.LastCycleStatus[server.ID] = nil + } } if u.Type == "offline" && uint64(time.Now().Unix())-src > 6 { diff --git a/resource/template/theme-daynight/home.html b/resource/template/theme-daynight/home.html index 0f3df3a..646678c 100644 --- a/resource/template/theme-daynight/home.html +++ b/resource/template/theme-daynight/home.html @@ -263,14 +263,15 @@ }); } ws.onmessage = function (evt) { - statusCards.servers = JSON.parse(evt.data) + const data = JSON.parse(evt.data) + statusCards.servers = data.servers const keys = Object.keys(statusCards.servers) for (let i = 0; i < keys.length; i++) { const ns = statusCards.servers[keys[i]]; if (!ns.Host) ns.live = false else { const lastActive = new Date(ns.LastActive).getTime() - if (Date.now() - lastActive > 20 * 1000) { + if (data.now - lastActive > 10 * 1000) { ns.live = false } else { ns.live = true diff --git a/resource/template/theme-default/home.html b/resource/template/theme-default/home.html index d7261d1..4a5622e 100644 --- a/resource/template/theme-default/home.html +++ b/resource/template/theme-default/home.html @@ -254,13 +254,14 @@ } ws.onmessage = function (evt) { const oldServers = statusCards.servers - statusCards.servers = JSON.parse(evt.data) + const data = JSON.parse(evt.data) + statusCards.servers = data.servers for (let i = 0; i < statusCards.servers.length; i++) { const ns = statusCards.servers[i]; if (!ns.Host) ns.live = false else { const lastActive = new Date(ns.LastActive).getTime() - if (Date.now() - lastActive > 20 * 1000) { + if (data.now - lastActive > 10 * 1000) { ns.live = false } else { ns.live = true diff --git a/resource/template/theme-hotaru/home.html b/resource/template/theme-hotaru/home.html index 65fa4d3..b9a4e30 100644 --- a/resource/template/theme-hotaru/home.html +++ b/resource/template/theme-hotaru/home.html @@ -259,13 +259,14 @@ }); } ws.onmessage = function (evt) { - statusCards.servers = JSON.parse(evt.data) + const data = JSON.parse(evt.data) + statusCards.servers = data.servers for (let i = 0; i < statusCards.servers.length; i++) { const ns = statusCards.servers[i]; if (!ns.Host) ns.live = false else { const lastActive = new Date(ns.LastActive).getTime() - if (Date.now() - lastActive > 20 * 1000) { + if (data.now - lastActive > 10 * 1000) { ns.live = false } else { ns.live = true diff --git a/service/dao/dao.go b/service/dao/dao.go index 0b9096e..34f1b03 100644 --- a/service/dao/dao.go +++ b/service/dao/dao.go @@ -13,7 +13,7 @@ import ( pb "github.com/naiba/nezha/proto" ) -var Version = "v0.9.1" // !!记得修改 README 中的 badge 版本!! +var Version = "v0.9.2" // !!记得修改 README 中的 badge 版本!! var ( Conf *model.Config