nezhahq-agent/pkg/monitor/conn/conn_linux.go
UUBulb af41e4d843
modularize monitor, reduce init usage (#81)
* kill process tree using syscall on windows & cleanup (#80)

* kill process tree using syscall on windows & cleanup

* use job api

* add error check for cmd.Start

* modularize monitor, reduce init usage

* replace slices with sort

* update gopsutil & other dependencies
2024-11-03 21:53:09 +08:00

51 lines
1.1 KiB
Go

//go:build linux
package conn
import (
"context"
"syscall"
"github.com/dean2021/goss"
"github.com/shirou/gopsutil/v4/net"
)
func GetState(_ context.Context) ([]uint64, error) {
var tcpConnCount, udpConnCount uint64
tcpStat, err := goss.ConnectionsWithProtocol(goss.AF_INET, syscall.IPPROTO_TCP)
if err == nil {
tcpConnCount = uint64(len(tcpStat))
}
udpStat, err := goss.ConnectionsWithProtocol(goss.AF_INET, syscall.IPPROTO_UDP)
if err == nil {
udpConnCount = uint64(len(udpStat))
}
tcpStat6, err := goss.ConnectionsWithProtocol(goss.AF_INET6, syscall.IPPROTO_TCP)
if err == nil {
tcpConnCount += uint64(len(tcpStat6))
}
udpStat6, err := goss.ConnectionsWithProtocol(goss.AF_INET6, syscall.IPPROTO_UDP)
if err == nil {
udpConnCount += uint64(len(udpStat6))
}
if tcpConnCount < 1 && udpConnCount < 1 {
// fallback to parsing files
conns, _ := net.Connections("all")
for _, conn := range conns {
switch conn.Type {
case syscall.SOCK_STREAM:
tcpConnCount++
case syscall.SOCK_DGRAM:
udpConnCount++
}
}
}
return []uint64{tcpConnCount, udpConnCount}, nil
}