[agent-go] [status] add status module
This commit is contained in:
103
agent-go/status/Network.go
Normal file
103
agent-go/status/Network.go
Normal file
@@ -0,0 +1,103 @@
|
||||
package status
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/shirou/gopsutil/v3/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"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
info, err := GetNetworkInfo()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("%+v\n", info)
|
||||
}
|
||||
|
||||
func GetNetworkInfo() (*NetworkInfo, error) {
|
||||
interfaces, err := net.Interfaces()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var mainInterface net.InterfaceStat
|
||||
for _, intf := range interfaces {
|
||||
if strings.HasPrefix(intf.Name, "ens") || strings.HasPrefix(intf.Name, "eth") || strings.HasPrefix(intf.Name, "eno") {
|
||||
mainInterface = intf
|
||||
break
|
||||
}
|
||||
}
|
||||
var internalIP 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
|
||||
}
|
||||
}
|
||||
counters, err := net.IOCounters(true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var sent uint64
|
||||
var recv uint64
|
||||
for _, counter := range counters {
|
||||
if counter.Name == mainInterface.Name {
|
||||
sent = counter.BytesSent
|
||||
recv = counter.BytesRecv
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// 休眠3秒
|
||||
|
||||
time.Sleep(3 * time.Second)
|
||||
|
||||
var sentAfter uint64
|
||||
var recvAfter uint64
|
||||
for _, counter := range counters {
|
||||
if counter.Name == mainInterface.Name {
|
||||
sentAfter = counter.BytesSent
|
||||
recvAfter = counter.BytesRecv
|
||||
break
|
||||
}
|
||||
}
|
||||
sendRate := fmt.Sprintf(formatBytes((sentAfter - sent) / 3))
|
||||
recvRate := fmt.Sprintf(formatBytes((recvAfter - recv) / 3))
|
||||
|
||||
info := &NetworkInfo{
|
||||
Name: mainInterface.Name,
|
||||
InternalIP: internalIP,
|
||||
ExternalIP: "",
|
||||
Mac: mainInterface.HardwareAddr,
|
||||
Sent: sent,
|
||||
Recv: recv,
|
||||
SentRate: sendRate,
|
||||
RecvRate: recvRate,
|
||||
}
|
||||
return info, nil
|
||||
}
|
||||
|
||||
func formatBytes(bytes uint64) string {
|
||||
const unit = 1024
|
||||
if bytes < unit {
|
||||
return fmt.Sprintf("%d B", bytes)
|
||||
}
|
||||
div, exp := int64(unit), 0
|
||||
for n := bytes / unit; n >= unit; n /= unit {
|
||||
div *= unit
|
||||
exp++
|
||||
}
|
||||
return fmt.Sprintf("%.1f %cB", float64(bytes)/float64(div), "KMGTPE"[exp])
|
||||
}
|
||||
Reference in New Issue
Block a user