[agent-go] [status] add status module
This commit is contained in:
@@ -3,19 +3,22 @@ package status
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/shirou/gopsutil/v3/net"
|
||||
net2 "net"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type NetworkInfo struct {
|
||||
Name string `json:"name,omitempty"`
|
||||
InternalIP string `json:"internal_ip,omitempty"`
|
||||
ExternalIP string `json:"external_ip,omitempty"`
|
||||
Mac string `json:"mac,omitempty"`
|
||||
Sent uint64 `json:"sent,omitempty"`
|
||||
Recv uint64 `json:"recv,omitempty"`
|
||||
SentRate string `json:"sent_rate,omitempty"`
|
||||
RecvRate string `json:"recv_rate,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
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"`
|
||||
Mac string `json:"mac,omitempty"`
|
||||
Sent uint64 `json:"sent,omitempty"`
|
||||
Recv uint64 `json:"recv,omitempty"`
|
||||
SentRate string `json:"sent_rate,omitempty"`
|
||||
RecvRate string `json:"recv_rate,omitempty"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
@@ -38,14 +41,13 @@ func GetNetworkInfo() (*NetworkInfo, error) {
|
||||
break
|
||||
}
|
||||
}
|
||||
var internalIP string
|
||||
var allAddrs []string
|
||||
log.DebugF("all main interface address are %v", mainInterface.Addrs)
|
||||
for _, addr := range mainInterface.Addrs {
|
||||
if strings.Contains(addr.Addr, ".") {
|
||||
internalIP = addr.Addr
|
||||
break
|
||||
}
|
||||
allAddrs = append(allAddrs, addr.Addr)
|
||||
}
|
||||
ipv4List, ipv6List := GetIPAddresses(allAddrs)
|
||||
|
||||
counters, err := net.IOCounters(true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -62,7 +64,7 @@ func GetNetworkInfo() (*NetworkInfo, error) {
|
||||
|
||||
// 休眠3秒
|
||||
|
||||
time.Sleep(3 * time.Second)
|
||||
time.Sleep(10 * time.Second)
|
||||
|
||||
var sentAfter uint64
|
||||
var recvAfter uint64
|
||||
@@ -73,18 +75,20 @@ func GetNetworkInfo() (*NetworkInfo, error) {
|
||||
break
|
||||
}
|
||||
}
|
||||
sendRate := fmt.Sprintf(formatBytes((sentAfter - sent) / 3))
|
||||
recvRate := fmt.Sprintf(formatBytes((recvAfter - recv) / 3))
|
||||
sendRate := fmt.Sprintf(formatBytes((sentAfter-sent)/10) + "/s")
|
||||
recvRate := fmt.Sprintf(formatBytes((recvAfter-recv)/10) + "/s")
|
||||
|
||||
info := &NetworkInfo{
|
||||
Name: mainInterface.Name,
|
||||
InternalIP: internalIP,
|
||||
ExternalIP: "",
|
||||
Mac: mainInterface.HardwareAddr,
|
||||
Sent: sent,
|
||||
Recv: recv,
|
||||
SentRate: sendRate,
|
||||
RecvRate: recvRate,
|
||||
Name: mainInterface.Name,
|
||||
InternalIPv4: ipv4List,
|
||||
InternalIPv6: ipv6List,
|
||||
ExternalIPv4: nil,
|
||||
ExternalIPv6: nil,
|
||||
Mac: mainInterface.HardwareAddr,
|
||||
Sent: sent,
|
||||
Recv: recv,
|
||||
SentRate: sendRate,
|
||||
RecvRate: recvRate,
|
||||
}
|
||||
return info, nil
|
||||
}
|
||||
@@ -101,3 +105,17 @@ func formatBytes(bytes uint64) string {
|
||||
}
|
||||
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