[agent] 可关闭的进程数连接数监控

This commit is contained in:
naiba 2021-08-16 23:26:38 +08:00
parent d64106333f
commit 960266bf71
3 changed files with 52 additions and 23 deletions

View File

@ -221,6 +221,13 @@ URL 里面也可放置占位符,请求时会进行简单的字符串替换。
</details> </details>
<details>
<summary>如何禁用连接数/进程数等占用资源的监控?</summary>
编辑 `/etc/systemd/system/nezha-agent.service`,在 `ExecStart=` 这一行的末尾加上 `-kconn` 就是不监控连接数,加上 `-kprocess` 就是不监控进程数
</details>
<details> <details>
<summary>Agent 不断重启/无法启动 </summary> <summary>Agent 不断重启/无法启动 </summary>

View File

@ -33,10 +33,9 @@ func init() {
} }
var ( var (
server string server, clientSecret, version string
clientSecret string
version string
debug bool debug bool
stateConf monitor.GetStateConfig
) )
var ( var (
@ -67,6 +66,8 @@ func main() {
flag.BoolVar(&debug, "d", false, "开启调试信息") flag.BoolVar(&debug, "d", false, "开启调试信息")
flag.StringVar(&server, "s", "localhost:5555", "管理面板RPC端口") flag.StringVar(&server, "s", "localhost:5555", "管理面板RPC端口")
flag.StringVar(&clientSecret, "p", "", "Agent连接Secret") flag.StringVar(&clientSecret, "p", "", "Agent连接Secret")
flag.BoolVar(&stateConf.SkipConnectionCount, "kconn", false, "不监控连接数")
flag.BoolVar(&stateConf.SkipProcessCount, "kprocess", false, "不监控进程数")
flag.Parse() flag.Parse()
if server == "" || clientSecret == "" { if server == "" || clientSecret == "" {
@ -271,7 +272,7 @@ func reportState() {
if client != nil && inited { if client != nil && inited {
monitor.TrackNetworkSpeed() monitor.TrackNetworkSpeed()
timeOutCtx, cancel := context.WithTimeout(context.Background(), networkTimeOut) timeOutCtx, cancel := context.WithTimeout(context.Background(), networkTimeOut)
_, err = client.ReportSystemState(timeOutCtx, monitor.GetState().PB()) _, err = client.ReportSystemState(timeOutCtx, monitor.GetState(stateConf).PB())
cancel() cancel()
if err != nil { if err != nil {
println("reportState error", err) println("reportState error", err)

View File

@ -6,6 +6,7 @@ import (
"runtime" "runtime"
"strings" "strings"
"sync/atomic" "sync/atomic"
"syscall"
"time" "time"
"github.com/shirou/gopsutil/v3/cpu" "github.com/shirou/gopsutil/v3/cpu"
@ -73,7 +74,12 @@ func GetHost() *model.Host {
} }
} }
func GetState() *model.HostState { type GetStateConfig struct {
SkipConnectionCount bool
SkipProcessCount bool
}
func GetState(conf GetStateConfig) *model.HostState {
hi, _ := host.Info() hi, _ := host.Info()
mv, _ := mem.VirtualMemory() mv, _ := mem.VirtualMemory()
@ -94,10 +100,24 @@ func GetState() *model.HostState {
_, diskUsed := getDiskTotalAndUsed() _, diskUsed := getDiskTotalAndUsed()
loadStat, _ := load.Avg() loadStat, _ := load.Avg()
tcpConns, _ := net.Connections("tcp") var tcpConnCount, udpConnCount uint64
udpConns, _ := net.Connections("udp")
if !conf.SkipConnectionCount {
conns, _ := net.Connections("all")
for i := 0; i < len(conns); i++ {
switch conns[i].Type {
case syscall.SOCK_STREAM:
tcpConnCount++
case syscall.SOCK_DGRAM:
udpConnCount++
}
}
}
var processCount uint64
if !conf.SkipProcessCount {
ps, _ := process.Pids() ps, _ := process.Pids()
processCount = uint64(len(ps))
// log.Println("pids", len(ps), err) // log.Println("pids", len(ps), err)
// var threads uint64 // var threads uint64
// for i := 0; i < len(ps); i++ { // for i := 0; i < len(ps); i++ {
@ -109,6 +129,7 @@ func GetState() *model.HostState {
// threads += uint64(c) // threads += uint64(c)
// } // }
// log.Println("threads", threads) // log.Println("threads", threads)
}
return &model.HostState{ return &model.HostState{
CPU: cpuPercent, CPU: cpuPercent,
@ -123,9 +144,9 @@ func GetState() *model.HostState {
Load1: loadStat.Load1, Load1: loadStat.Load1,
Load5: loadStat.Load5, Load5: loadStat.Load5,
Load15: loadStat.Load15, Load15: loadStat.Load15,
TcpConnCount: uint64(len(tcpConns)), TcpConnCount: tcpConnCount,
UdpConnCount: uint64(len(udpConns)), UdpConnCount: udpConnCount,
ProcessCount: uint64(len(ps)), ProcessCount: processCount,
} }
} }