[Agent] [Status] network info accomplish
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/shirou/gopsutil/v3/net"
|
||||
net2 "net"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@@ -19,6 +20,14 @@ type NetworkMetric struct {
|
||||
}
|
||||
|
||||
type NetworkInfo struct {
|
||||
Name string `json:"name"` // e.g., "en0", "lo0", "eth0.100"
|
||||
MTU int `json:"mtu"` // maximum transmission unit
|
||||
HardwareAddr string `json:"hardwareAddr"` // IEEE MAC-48, EUI-48 and EUI-64 form
|
||||
Flags []string `json:"flags"` // e.g., FlagUp, FlagLoopback, FlagMulticast
|
||||
InternalIPv4 []string `json:"internal_ip_v4,omitempty"`
|
||||
InternalIPv6 []string `json:"internal_ip_v6,omitempty"`
|
||||
ExternalIPv4 []string `json:"external_ip_v4,omitempty"`
|
||||
ExternalIPv6 []string `json:"external_ip_v6,omitempty"`
|
||||
}
|
||||
|
||||
type NetworkStatus struct {
|
||||
@@ -89,9 +98,11 @@ func GetNetworkMetric() ([]NetworkMetric, error) {
|
||||
RecvSpeed: 0,
|
||||
}
|
||||
|
||||
// calculate speed
|
||||
networkMetric.SendSpeed = (float64(after.BytesSent - before.BytesSent)) / duration.Seconds()
|
||||
networkMetric.RecvSpeed = (float64(after.BytesRecv - before.BytesRecv)) / duration.Seconds()
|
||||
|
||||
// append result
|
||||
result = append(result, networkMetric)
|
||||
}
|
||||
}
|
||||
@@ -104,50 +115,92 @@ func GetNetworkMetric() ([]NetworkMetric, error) {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func GetNetworkInfo() {
|
||||
func GetNetworkInfo() ([]NetworkInfo, error) {
|
||||
|
||||
interfaceStatList, err := net.Interfaces()
|
||||
if err != nil {
|
||||
log.ErrorF("[GetNetworkInfo] - get net interface list error %s", err.Error())
|
||||
}
|
||||
|
||||
var result []NetworkInfo
|
||||
for _, interfaceStat := range interfaceStatList {
|
||||
fmt.Println()
|
||||
marshal, _ := json.Marshal(interfaceStat)
|
||||
fmt.Println(string(marshal))
|
||||
fmt.Println()
|
||||
if MatchNetInterfaceRight(interfaceStat.Name) {
|
||||
// just collect useful net interface
|
||||
|
||||
// new result
|
||||
networkInfo := NetworkInfo{
|
||||
Name: interfaceStat.Name,
|
||||
MTU: interfaceStat.MTU,
|
||||
HardwareAddr: interfaceStat.HardwareAddr,
|
||||
Flags: interfaceStat.Flags,
|
||||
InternalIPv4: []string{},
|
||||
InternalIPv6: []string{},
|
||||
ExternalIPv4: nil,
|
||||
ExternalIPv6: nil,
|
||||
}
|
||||
|
||||
connections, _ := net.Connections("tcp4")
|
||||
// calculate real ip addr
|
||||
for _, interfaceAddr := range interfaceStat.Addrs {
|
||||
|
||||
ip, cidr, err := net2.ParseCIDR(interfaceAddr.Addr)
|
||||
if err != nil {
|
||||
log.ErrorF("[GetNetworkInfo] - parse interface addr error %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
// get the mask size
|
||||
maskLength, _ := cidr.Mask.Size()
|
||||
if ip.To4() != nil {
|
||||
networkInfo.InternalIPv4 = append(networkInfo.InternalIPv4, ip.String()+"/"+strconv.Itoa(maskLength))
|
||||
} else if ip.To16() != nil {
|
||||
networkInfo.InternalIPv6 = append(networkInfo.InternalIPv6, ip.String()+"/"+strconv.Itoa(maskLength))
|
||||
}
|
||||
}
|
||||
|
||||
// assign
|
||||
result = append(result, networkInfo)
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func GetNetworkConnectionALl() []net.ConnectionStat {
|
||||
|
||||
connections, err := net.Connections("all")
|
||||
if err != nil {
|
||||
log.ErrorF("[GetNetworkConnectionALl] - get network connections error ! %v", err)
|
||||
return nil
|
||||
}
|
||||
for _, con := range connections {
|
||||
fmt.Println()
|
||||
marshal, _ := json.Marshal(con)
|
||||
fmt.Println(string(marshal))
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
protoCounters, err := net.ProtoCounters([]string{})
|
||||
if err != nil {
|
||||
log.ErrorF("[GetNetworkInfo] - get proto counter list error %s", err.Error())
|
||||
return connections
|
||||
}
|
||||
for _, proto := range protoCounters {
|
||||
|
||||
func GetNetworkConnectionTCP4() {
|
||||
|
||||
}
|
||||
|
||||
func GetNetworkConnectionTCP6() {
|
||||
connections, _ := net.Connections("tcp6")
|
||||
for _, con := range connections {
|
||||
fmt.Println()
|
||||
marshal, _ := json.Marshal(proto)
|
||||
marshal, _ := json.Marshal(con)
|
||||
fmt.Println(string(marshal))
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
|
||||
func GetNetworkConnectionUDP4() {
|
||||
|
||||
filterCounters, err := net.FilterCounters()
|
||||
if err != nil {
|
||||
log.ErrorF("[GetNetworkInfo] - get filter counter list error %s", err.Error())
|
||||
}
|
||||
for _, filter := range filterCounters {
|
||||
fmt.Println()
|
||||
marshal, _ := json.Marshal(filter)
|
||||
fmt.Println(string(marshal))
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
func GetNetworkConnectionUDP6() {
|
||||
|
||||
}
|
||||
|
||||
func GetNetworkStatus() (*NetworkStatus, error) {
|
||||
@@ -167,7 +220,7 @@ func GetNetworkStatus() (*NetworkStatus, error) {
|
||||
for _, addr := range mainInterface.Addrs {
|
||||
allAddrs = append(allAddrs, addr.Addr)
|
||||
}
|
||||
ipv4List, ipv6List := GetInternelIpAddrs(allAddrs)
|
||||
ipv4List, ipv6List := GetInternalIpAdders(allAddrs)
|
||||
log.DebugF("ipv4 list are => %v, ipv6 list are => %v", ipv4List, ipv6List)
|
||||
|
||||
counters, err := net.IOCounters(true)
|
||||
@@ -231,9 +284,8 @@ func formatBytes(bytes uint64) string {
|
||||
return fmt.Sprintf("%.1f %cB", float64(bytes)/float64(div), "KMGTPE"[exp])
|
||||
}
|
||||
|
||||
func GetInternelIpAddrs(addresses []string) ([]string, []string) {
|
||||
var ipv4 []string
|
||||
var ipv6 []string
|
||||
func GetInternalIpAdders(addresses []string) (ipv4List []string, ipv6List []string) {
|
||||
|
||||
for _, addr := range addresses {
|
||||
// it parse (0.0.0.0) not cidr
|
||||
ip, _, err := net2.ParseCIDR(addr)
|
||||
@@ -241,12 +293,12 @@ func GetInternelIpAddrs(addresses []string) ([]string, []string) {
|
||||
continue
|
||||
}
|
||||
if ip.To4() != nil {
|
||||
ipv4 = append(ipv4, addr)
|
||||
ipv4List = append(ipv4List, addr)
|
||||
} else if ip.To16() != nil {
|
||||
ipv6 = append(ipv6, addr)
|
||||
ipv6List = append(ipv6List, addr)
|
||||
}
|
||||
}
|
||||
return ipv4, ipv6
|
||||
return ipv4List, ipv6List
|
||||
}
|
||||
|
||||
func MatchNetInterfaceRight(interfaceName string) bool {
|
||||
|
||||
@@ -31,6 +31,16 @@ func TestGetNetworkMetric(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetNetworkInfo(t *testing.T) {
|
||||
GetNetworkInfo()
|
||||
networkInfos, err := GetNetworkInfo()
|
||||
if err != nil {
|
||||
t.Errorf("GetNetworkInfo error of %s", err)
|
||||
}
|
||||
|
||||
for _, networkInfo := range networkInfos {
|
||||
fmt.Println()
|
||||
marshal, _ := json.MarshalIndent(networkInfo, "", " ")
|
||||
fmt.Println(string(marshal))
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
22
agent-go/utils/PrintUtils.go
Normal file
22
agent-go/utils/PrintUtils.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"agent-go/logger"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var log = logger.Log
|
||||
|
||||
func BeautifulPrint(object interface{}) {
|
||||
|
||||
bytes, err := json.MarshalIndent(object, "", " ")
|
||||
if err != nil {
|
||||
log.ErrorF("[BeautifulPrint] - json marshal error ! => %v", object)
|
||||
}
|
||||
|
||||
fmt.Println()
|
||||
fmt.Println(string(bytes))
|
||||
fmt.Println()
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user