[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>
<summary>如何禁用连接数/进程数等占用资源的监控?</summary>
编辑 `/etc/systemd/system/nezha-agent.service`,在 `ExecStart=` 这一行的末尾加上 `-kconn` 就是不监控连接数,加上 `-kprocess` 就是不监控进程数
</details>
<details>
<summary>Agent 不断重启/无法启动 </summary>

View File

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

View File

@ -6,6 +6,7 @@ import (
"runtime"
"strings"
"sync/atomic"
"syscall"
"time"
"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()
mv, _ := mem.VirtualMemory()
@ -94,21 +100,36 @@ func GetState() *model.HostState {
_, diskUsed := getDiskTotalAndUsed()
loadStat, _ := load.Avg()
tcpConns, _ := net.Connections("tcp")
udpConns, _ := net.Connections("udp")
var tcpConnCount, udpConnCount uint64
ps, _ := process.Pids()
// log.Println("pids", len(ps), err)
// var threads uint64
// for i := 0; i < len(ps); i++ {
// p, err := process.NewProcess(ps[i])
// if err != nil {
// continue
// }
// c, _ := p.NumThreads()
// threads += uint64(c)
// }
// log.Println("threads", threads)
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()
processCount = uint64(len(ps))
// log.Println("pids", len(ps), err)
// var threads uint64
// for i := 0; i < len(ps); i++ {
// p, err := process.NewProcess(ps[i])
// if err != nil {
// continue
// }
// c, _ := p.NumThreads()
// threads += uint64(c)
// }
// log.Println("threads", threads)
}
return &model.HostState{
CPU: cpuPercent,
@ -123,9 +144,9 @@ func GetState() *model.HostState {
Load1: loadStat.Load1,
Load5: loadStat.Load5,
Load15: loadStat.Load15,
TcpConnCount: uint64(len(tcpConns)),
UdpConnCount: uint64(len(udpConns)),
ProcessCount: uint64(len(ps)),
TcpConnCount: tcpConnCount,
UdpConnCount: udpConnCount,
ProcessCount: processCount,
}
}