Add SS Func to accelerate connections count in some Linux

This commit is contained in:
Erope 2021-12-30 18:01:32 +08:00
parent 6e4d4de2f7
commit 830bec4a42
3 changed files with 26 additions and 10 deletions

View File

@ -2,14 +2,15 @@ package monitor
import ( import (
"fmt" "fmt"
"os/exec"
"regexp" "regexp"
"runtime" "runtime"
"strconv"
"strings" "strings"
"syscall" "syscall"
"time" "time"
"strconv"
"os/exec"
"github.com/Erope/goss"
"github.com/shirou/gopsutil/v3/cpu" "github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/disk" "github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/host" "github.com/shirou/gopsutil/v3/host"
@ -114,13 +115,25 @@ func GetState(skipConnectionCount bool, skipProcsCount bool) *model.HostState {
var tcpConnCount, udpConnCount uint64 var tcpConnCount, udpConnCount uint64
if !skipConnectionCount { if !skipConnectionCount {
conns, _ := net.Connections("all") ss_err := true
for i := 0; i < len(conns); i++ { if runtime.GOOS == "linux" {
switch conns[i].Type { tcpStat, err_tcp := goss.ConnectionsWithProtocol(syscall.IPPROTO_TCP)
case syscall.SOCK_STREAM: udpStat, err_udp := goss.ConnectionsWithProtocol(syscall.IPPROTO_UDP)
tcpConnCount++ if err_tcp == nil && err_udp == nil {
case syscall.SOCK_DGRAM: ss_err = false
udpConnCount++ tcpConnCount = uint64(len(tcpStat))
udpConnCount = uint64(len(udpStat))
}
}
if ss_err {
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++
}
} }
} }
} }
@ -192,7 +205,7 @@ func getDiskTotalAndUsed() (total uint64, used uint64) {
} }
used += diskUsageOf.Used used += diskUsageOf.Used
} }
// Fallback 到这个方法,仅统计根路径,适用于OpenVZ之类的. // Fallback 到这个方法,仅统计根路径,适用于OpenVZ之类的.
if runtime.GOOS == "linux" { if runtime.GOOS == "linux" {
if total == 0 && used == 0 { if total == 0 && used == 0 {

1
go.mod
View File

@ -4,6 +4,7 @@ go 1.13
require ( require (
code.cloudfoundry.org/bytefmt v0.0.0-20211005130812-5bb3c17173e5 code.cloudfoundry.org/bytefmt v0.0.0-20211005130812-5bb3c17173e5
github.com/Erope/goss v0.0.0-20211230093305-df3c03fd1ed4 // indirect
github.com/artdarek/go-unzip v1.0.0 github.com/artdarek/go-unzip v1.0.0
github.com/blang/semver v3.5.1+incompatible github.com/blang/semver v3.5.1+incompatible
github.com/creack/pty v1.1.17 github.com/creack/pty v1.1.17

2
go.sum
View File

@ -46,6 +46,8 @@ code.cloudfoundry.org/bytefmt v0.0.0-20211005130812-5bb3c17173e5/go.mod h1:v4VVB
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/Erope/goss v0.0.0-20211230093305-df3c03fd1ed4 h1:X35U3bryt+j2a9RZyXyBPISNIYQsfNKWC/d/B8J861I=
github.com/Erope/goss v0.0.0-20211230093305-df3c03fd1ed4/go.mod h1:gl55GUYOV6rvsL/V23GdfRNdnreh+McD5Yo1sNc+Qe0=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/StackExchange/wmi v1.2.1 h1:VIkavFPXSjcnS+O8yTq7NI32k0R5Aj+v39y29VYDOSA= github.com/StackExchange/wmi v1.2.1 h1:VIkavFPXSjcnS+O8yTq7NI32k0R5Aj+v39y29VYDOSA=
github.com/StackExchange/wmi v1.2.1/go.mod h1:rcmrprowKIVzvc+NUiLncP2uuArMWLCbu9SBzvHz7e8= github.com/StackExchange/wmi v1.2.1/go.mod h1:rcmrprowKIVzvc+NUiLncP2uuArMWLCbu9SBzvHz7e8=