Skip to content

Commit

Permalink
优化 agent 信息上报与 ARM 安装面板的错误提示
Browse files Browse the repository at this point in the history
  • Loading branch information
naiba committed Jun 30, 2021
1 parent 92ecb33 commit 8d0e269
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 15 deletions.
5 changes: 4 additions & 1 deletion cmd/agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -283,6 +285,7 @@ func reportState() {
client.ReportSystemInfo(context.Background(), monitor.GetHost().PB())
}
}
time.Sleep(time.Until(now.Add(time.Second)))
}
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/agent/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
Expand Down
2 changes: 1 addition & 1 deletion model/alertrule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 4 additions & 1 deletion resource/static/main.js
Original file line number Diff line number Diff line change
@@ -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");
Expand Down
2 changes: 1 addition & 1 deletion resource/template/common/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/semantic.min.js"></script>
<script src="/static/semantic-ui-alerts.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.min.js"></script>
<script src="/static/main.js?v202106212144"></script>
<script src="/static/main.js?v202106302144"></script>
</body>

</html>
Expand Down
8 changes: 2 additions & 6 deletions resource/template/theme-hotaru/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ <h1>
<td>@#server.Host?server.Host.Platform:'-'#@</td>
<td>@#server.Host?server.Host.CountryCode:'-'#@</td>
<td>@#server.State?secondToDate(server.State.Uptime):'-'#@</td>
<td>@#server.State?formatByteSize(server.State.NetInSpeed)+'/s|'+formatByteSize(server.State.NetOutSpeed)+'/s':'-'#@
<td>@#server.State?readableBytes(server.State.NetInSpeed)+'/s|'+readableBytes(server.State.NetOutSpeed)+'/s':'-'#@
</td>
<td>@#server.State?formatByteSize(server.State.NetInTransfer)+'|'+formatByteSize(server.State.NetOutTransfer):'-'#@
<td>@#server.State?readableBytes(server.State.NetInTransfer)+'|'+readableBytes(server.State.NetOutTransfer):'-'#@
</td>
<td>
<div v-if="server.State" :class="formatPercent(server.live,server.State.CPU, 100).class">
Expand Down Expand Up @@ -226,10 +226,6 @@ <h3 class="h4"><img
},
formatTimestamp(t) {
return new Date(t * 1000).toLocaleString()
},
formatByteSize(bs) {
const x = this.readableBytes(bs)
return x != "NaN undefined" ? x : '0 KB'
}
}
})
Expand Down
7 changes: 6 additions & 1 deletion script/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion service/dao/alertsentinel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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秒钟检查一次
}
}

Expand Down
3 changes: 2 additions & 1 deletion service/rpc/nezha.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8d0e269

Please sign in to comment.