Merge pull request #148 from Erope/master

Add SS Func to accelerate connections count in some Linux

Co-authored-by: Erope <44471469+Erope@users.noreply.github.com>
This commit is contained in:
naiba 2021-12-30 12:56:36 +02:00 committed by GitHub
commit bdce9baa78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 10 deletions

View File

@ -2,14 +2,15 @@ package monitor
import (
"fmt"
"os/exec"
"regexp"
"runtime"
"strconv"
"strings"
"syscall"
"time"
"strconv"
"os/exec"
"github.com/Erope/goss"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/host"
@ -114,6 +115,17 @@ func GetState(skipConnectionCount bool, skipProcsCount bool) *model.HostState {
var tcpConnCount, udpConnCount uint64
if !skipConnectionCount {
ss_err := true
if runtime.GOOS == "linux" {
tcpStat, err_tcp := goss.ConnectionsWithProtocol(syscall.IPPROTO_TCP)
udpStat, err_udp := goss.ConnectionsWithProtocol(syscall.IPPROTO_UDP)
if err_tcp == nil && err_udp == nil {
ss_err = false
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 {
@ -124,6 +136,7 @@ func GetState(skipConnectionCount bool, skipProcsCount bool) *model.HostState {
}
}
}
}
return &model.HostState{
CPU: cpuPercent,

1
go.mod
View File

@ -4,6 +4,7 @@ go 1.13
require (
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/blang/semver v3.5.1+incompatible
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=
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/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/StackExchange/wmi v1.2.1 h1:VIkavFPXSjcnS+O8yTq7NI32k0R5Aj+v39y29VYDOSA=
github.com/StackExchange/wmi v1.2.1/go.mod h1:rcmrprowKIVzvc+NUiLncP2uuArMWLCbu9SBzvHz7e8=