🚸 使用服务器端时间判断机器离线

This commit is contained in:
naiba 2021-07-16 11:14:07 +08:00
parent 321ccce931
commit 8f06d90c30
9 changed files with 34 additions and 14 deletions

View File

@ -1,7 +1,7 @@
<div align="center">
<img width="500" style="max-width:100%" src="resource/static/brand.png" title="哪吒监控">
<br><br>
<img src="https://img.shields.io/github/workflow/status/naiba/nezha/Dashboard%20image?label=Dash%20v0.9.1&logo=github&style=for-the-badge">&nbsp;<img src="https://img.shields.io/github/v/release/naiba/nezha?color=brightgreen&label=Agent&style=for-the-badge&logo=github">&nbsp;<img src="https://img.shields.io/github/workflow/status/naiba/nezha/Agent%20release?label=Agent%20CI&logo=github&style=for-the-badge">&nbsp;<img src="https://img.shields.io/badge/Installer-v0.6.4-brightgreen?style=for-the-badge&logo=linux">
<img src="https://img.shields.io/github/workflow/status/naiba/nezha/Dashboard%20image?label=Dash%20v0.9.2&logo=github&style=for-the-badge">&nbsp;<img src="https://img.shields.io/github/v/release/naiba/nezha?color=brightgreen&label=Agent&style=for-the-badge&logo=github">&nbsp;<img src="https://img.shields.io/github/workflow/status/naiba/nezha/Agent%20release?label=Agent%20CI&logo=github&style=for-the-badge">&nbsp;<img src="https://img.shields.io/badge/Installer-v0.6.4-brightgreen?style=for-the-badge&logo=linux">
<br>
<br>
<p>:trollface: <b>哪吒监控</b> 一站式轻监控轻运维系统。支持系统状态、HTTP(SSL 证书变更、即将到期、到期)、TCP、Ping 监控报警,命令批量执行和计划任务。</p>

View File

@ -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

View File

@ -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,

View File

@ -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++

View File

@ -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 {

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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