[agent-go] [status] add status module
This commit is contained in:
@@ -3,14 +3,17 @@ package status
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/shirou/gopsutil/v3/net"
|
"github.com/shirou/gopsutil/v3/net"
|
||||||
|
net2 "net"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type NetworkInfo struct {
|
type NetworkInfo struct {
|
||||||
Name string `json:"name,omitempty"`
|
Name string `json:"name,omitempty"`
|
||||||
InternalIP string `json:"internal_ip,omitempty"`
|
InternalIPv4 []string `json:"internal_ip_v4,omitempty"`
|
||||||
ExternalIP string `json:"external_ip,omitempty"`
|
InternalIPv6 []string `json:"internal_ip_v6,omitempty"`
|
||||||
|
ExternalIPv4 []string `json:"external_ip_v4,omitempty"`
|
||||||
|
ExternalIPv6 []string `json:"external_ip_v6,omitempty"`
|
||||||
Mac string `json:"mac,omitempty"`
|
Mac string `json:"mac,omitempty"`
|
||||||
Sent uint64 `json:"sent,omitempty"`
|
Sent uint64 `json:"sent,omitempty"`
|
||||||
Recv uint64 `json:"recv,omitempty"`
|
Recv uint64 `json:"recv,omitempty"`
|
||||||
@@ -38,14 +41,13 @@ func GetNetworkInfo() (*NetworkInfo, error) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var internalIP string
|
var allAddrs []string
|
||||||
log.DebugF("all main interface address are %v", mainInterface.Addrs)
|
log.DebugF("all main interface address are %v", mainInterface.Addrs)
|
||||||
for _, addr := range mainInterface.Addrs {
|
for _, addr := range mainInterface.Addrs {
|
||||||
if strings.Contains(addr.Addr, ".") {
|
allAddrs = append(allAddrs, addr.Addr)
|
||||||
internalIP = addr.Addr
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
ipv4List, ipv6List := GetIPAddresses(allAddrs)
|
||||||
|
|
||||||
counters, err := net.IOCounters(true)
|
counters, err := net.IOCounters(true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -62,7 +64,7 @@ func GetNetworkInfo() (*NetworkInfo, error) {
|
|||||||
|
|
||||||
// 休眠3秒
|
// 休眠3秒
|
||||||
|
|
||||||
time.Sleep(3 * time.Second)
|
time.Sleep(10 * time.Second)
|
||||||
|
|
||||||
var sentAfter uint64
|
var sentAfter uint64
|
||||||
var recvAfter uint64
|
var recvAfter uint64
|
||||||
@@ -73,13 +75,15 @@ func GetNetworkInfo() (*NetworkInfo, error) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sendRate := fmt.Sprintf(formatBytes((sentAfter - sent) / 3))
|
sendRate := fmt.Sprintf(formatBytes((sentAfter-sent)/10) + "/s")
|
||||||
recvRate := fmt.Sprintf(formatBytes((recvAfter - recv) / 3))
|
recvRate := fmt.Sprintf(formatBytes((recvAfter-recv)/10) + "/s")
|
||||||
|
|
||||||
info := &NetworkInfo{
|
info := &NetworkInfo{
|
||||||
Name: mainInterface.Name,
|
Name: mainInterface.Name,
|
||||||
InternalIP: internalIP,
|
InternalIPv4: ipv4List,
|
||||||
ExternalIP: "",
|
InternalIPv6: ipv6List,
|
||||||
|
ExternalIPv4: nil,
|
||||||
|
ExternalIPv6: nil,
|
||||||
Mac: mainInterface.HardwareAddr,
|
Mac: mainInterface.HardwareAddr,
|
||||||
Sent: sent,
|
Sent: sent,
|
||||||
Recv: recv,
|
Recv: recv,
|
||||||
@@ -101,3 +105,17 @@ func formatBytes(bytes uint64) string {
|
|||||||
}
|
}
|
||||||
return fmt.Sprintf("%.1f %cB", float64(bytes)/float64(div), "KMGTPE"[exp])
|
return fmt.Sprintf("%.1f %cB", float64(bytes)/float64(div), "KMGTPE"[exp])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetIPAddresses(addresses []string) ([]string, []string) {
|
||||||
|
var ipv4 []string
|
||||||
|
var ipv6 []string
|
||||||
|
for _, addr := range addresses {
|
||||||
|
ip := net2.ParseIP(addr)
|
||||||
|
if ip.To4() != nil {
|
||||||
|
ipv4 = append(ipv4, addr)
|
||||||
|
} else if ip.To16() != nil {
|
||||||
|
ipv6 = append(ipv6, addr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ipv4, ipv6
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user