From 8d0e269b73cd1ebabd7f7ab458582b35ca3930f6 Mon Sep 17 00:00:00 2001 From: naiba Date: Wed, 30 Jun 2021 18:15:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20agent=20=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E4=B8=8A=E6=8A=A5=E4=B8=8E=20ARM=20=E5=AE=89=E8=A3=85=E9=9D=A2?= =?UTF-8?q?=E6=9D=BF=E7=9A=84=E9=94=99=E8=AF=AF=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/agent/main.go | 5 ++++- cmd/agent/monitor/monitor.go | 4 ++-- model/alertrule.go | 2 +- resource/static/main.js | 5 ++++- resource/template/common/footer.html | 2 +- resource/template/theme-hotaru/home.html | 8 ++------ script/install.sh | 7 ++++++- service/dao/alertsentinel.go | 2 +- service/rpc/nezha.go | 3 ++- 9 files changed, 23 insertions(+), 15 deletions(-) diff --git a/cmd/agent/main.go b/cmd/agent/main.go index 9424ed6..486ee84 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -267,12 +267,14 @@ func doTask(task *pb.Task) { func reportState() { var lastReportHostInfo time.Time var err error + var now time.Time defer println("reportState exit", time.Now(), "=>", err) for { + now = time.Now() if client != nil { monitor.TrackNetworkSpeed() timeOutCtx, cancel := context.WithTimeout(context.Background(), networkTimeOut) - _, err = client.ReportSystemState(timeOutCtx, monitor.GetState(dao.ReportDelay).PB()) + _, err = client.ReportSystemState(timeOutCtx, monitor.GetState().PB()) cancel() if err != nil { println("reportState error", err) @@ -283,6 +285,7 @@ func reportState() { client.ReportSystemInfo(context.Background(), monitor.GetHost().PB()) } } + time.Sleep(time.Until(now.Add(time.Second))) } } diff --git a/cmd/agent/monitor/monitor.go b/cmd/agent/monitor/monitor.go index 3d4dc88..74832e0 100644 --- a/cmd/agent/monitor/monitor.go +++ b/cmd/agent/monitor/monitor.go @@ -63,12 +63,12 @@ func GetHost() *model.Host { } } -func GetState(delay int64) *model.HostState { +func GetState() *model.HostState { hi, _ := host.Info() mv, _ := mem.VirtualMemory() ms, _ := mem.SwapMemory() var cpuPercent float64 - cp, err := cpu.Percent(time.Second*time.Duration(delay), false) + cp, err := cpu.Percent(0, false) if err == nil { cpuPercent = cp[0] } diff --git a/model/alertrule.go b/model/alertrule.go index 61e0919..b752df6 100644 --- a/model/alertrule.go +++ b/model/alertrule.go @@ -41,7 +41,7 @@ func (r *AlertRule) Check(points [][]interface{}) (int, bool) { for i := 0; i < len(r.Rules); i++ { total := 0.0 fail := 0.0 - num := int(r.Rules[i].Duration / 2) // SnapshotDelay + num := int(r.Rules[i].Duration) if num > max { max = num } diff --git a/resource/static/main.js b/resource/static/main.js index 356142c..6f286b6 100644 --- a/resource/static/main.js +++ b/resource/static/main.js @@ -1,7 +1,10 @@ function readableBytes(bytes) { + if (!bytes) { + return '0B' + } var i = Math.floor(Math.log(bytes) / Math.log(1024)), sizes = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; - return (bytes / Math.pow(1024, i)).toFixed(0) + " " + sizes[i]; + return parseFloat((bytes / Math.pow(1024, i)).toFixed(2)) + sizes[i]; } const confirmBtn = $(".mini.confirm.modal .positive.button"); diff --git a/resource/template/common/footer.html b/resource/template/common/footer.html index c91610e..4376a45 100644 --- a/resource/template/common/footer.html +++ b/resource/template/common/footer.html @@ -9,7 +9,7 @@ - + diff --git a/resource/template/theme-hotaru/home.html b/resource/template/theme-hotaru/home.html index be5f9d4..133fa04 100644 --- a/resource/template/theme-hotaru/home.html +++ b/resource/template/theme-hotaru/home.html @@ -69,9 +69,9 @@ @#server.Host?server.Host.Platform:'-'#@ @#server.Host?server.Host.CountryCode:'-'#@ @#server.State?secondToDate(server.State.Uptime):'-'#@ - @#server.State?formatByteSize(server.State.NetInSpeed)+'/s|'+formatByteSize(server.State.NetOutSpeed)+'/s':'-'#@ + @#server.State?readableBytes(server.State.NetInSpeed)+'/s|'+readableBytes(server.State.NetOutSpeed)+'/s':'-'#@ - @#server.State?formatByteSize(server.State.NetInTransfer)+'|'+formatByteSize(server.State.NetOutTransfer):'-'#@ + @#server.State?readableBytes(server.State.NetInTransfer)+'|'+readableBytes(server.State.NetOutTransfer):'-'#@
@@ -226,10 +226,6 @@ }, formatTimestamp(t) { return new Date(t * 1000).toLocaleString() - }, - formatByteSize(bs) { - const x = this.readableBytes(bs) - return x != "NaN undefined" ? x : '0 KB' } } }) diff --git a/script/install.sh b/script/install.sh index 63202b0..b7cfb78 100755 --- a/script/install.sh +++ b/script/install.sh @@ -11,7 +11,7 @@ NZ_BASE_PATH="/opt/nezha" NZ_DASHBOARD_PATH="${NZ_BASE_PATH}/dashboard" NZ_AGENT_PATH="${NZ_BASE_PATH}/agent" NZ_AGENT_SERVICE="/etc/systemd/system/nezha-agent.service" -NZ_VERSION="v0.6.1" +NZ_VERSION="v0.6.2" red='\033[0;31m' green='\033[0;32m' @@ -134,6 +134,11 @@ install_dashboard() { echo -e "> 安装面板" + if [[ $(uname -m | grep 'arm') != "" ]]; then + echo "面板目前不支持在 arm 环境下安装" + exit 1 + fi + # 哪吒监控文件夹 mkdir -p $NZ_DASHBOARD_PATH chmod 777 -R $NZ_DASHBOARD_PATH diff --git a/service/dao/alertsentinel.go b/service/dao/alertsentinel.go index 866fff0..d788166 100644 --- a/service/dao/alertsentinel.go +++ b/service/dao/alertsentinel.go @@ -59,7 +59,7 @@ func AlertSentinelStart() { checkCount = 0 lastPrint = startedAt } - time.Sleep(time.Until(startedAt.Add(time.Second * SnapshotDelay))) + time.Sleep(time.Until(startedAt.Add(time.Second * 3))) // 3秒钟检查一次 } } diff --git a/service/rpc/nezha.go b/service/rpc/nezha.go index 7436463..ce4eef4 100644 --- a/service/rpc/nezha.go +++ b/service/rpc/nezha.go @@ -6,6 +6,7 @@ import ( "time" "github.com/naiba/nezha/model" + "github.com/naiba/nezha/pkg/utils" pb "github.com/naiba/nezha/proto" "github.com/naiba/nezha/service/dao" ) @@ -94,7 +95,7 @@ func (s *NezhaHandler) ReportSystemInfo(c context.Context, r *pb.Host) (*pb.Rece dao.ServerList[clientID].Host.IP != host.IP { dao.SendNotification(fmt.Sprintf( "IP变更提醒 服务器:%s ,旧IP:%s,新IP:%s。", - dao.ServerList[clientID].Name, dao.ServerList[clientID].Host.IP, host.IP), true) + dao.ServerList[clientID].Name, utils.IPDesensitize(dao.ServerList[clientID].Host.IP), utils.IPDesensitize(host.IP)), true) } dao.ServerList[clientID].Host = &host return &pb.Receipt{Proced: true}, nil