Os tools for golang
Example1: https://github.com/ahuigo/goos-tools/blob/main/nets/stats_linux_test.go import ( "testing" "github.com/ahuigo/goos-tools/helper" "github.com/stretchr/testify/assert" )
func TestGetStats(t *testing.T) {
stats, err := GetStats("")
assert.NoError(t, err)
assert.NotEqual(t, "", stats.InterfaceName)
helper.PrintPretty(stats)
}
Example2: https://github.com/ahuigo/goos-tools/blob/master/examples
import (
"testing"
"github.com/ahuigo/goos-tools/cli/netstat"
"github.com/stretchr/testify/assert"
)
func TestGetAllTcpConnections(t *testing.T) {
tcps, err := netstat.GetAllTcpConnections()
assert.NoError(t, err)
for _, tcp := range tcps {
t.Logf("local:%s, remote:%s, state:%s", tcp.LocalAddr, tcp.ForeignAddr, tcp.State)
}
}
For more examples, refer to examples
import (
"testing"
"github.com/ahuigo/goos-tools/cli/lsof"
"github.com/stretchr/testify/assert"
)
func TestGetLsofTcps(t *testing.T) {
tcps, err := lsof.GetLsofTcps()
assert.NoError(t, err)
for _, tcp := range tcps {
t.Logf("%s, %s, %s, %s, %s\n", tcp.Pid, tcp.Command, tcp.LocalAddr, tcp.ForeignAddr, tcp.TcpState)
}
}
Tools for gonic
Add routes:
import (
"github.com/ahuigo/goos-tools/gonic"
)
r := gin.New()
r.GET("/stat/os", gonic.OsStat)
r.GET("/stat/net", gonic.StatNet)
The code above has functions:
- Show network stat(all tcp connects): http://your.domain/stat/net
- Show os stat(memory and cpu usage): http://your.domain/stat/os