[ Agent ] [ Status ] - refresh status part
This commit is contained in:
@@ -2,12 +2,21 @@ package status
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/go-openapi/errors"
|
||||
"github.com/shirou/gopsutil/v3/net"
|
||||
net2 "net"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type NetworkMetric struct {
|
||||
net.IOCountersStat
|
||||
|
||||
SendSpeed float64
|
||||
RecvSpeed float64
|
||||
}
|
||||
|
||||
type NetworkStatus struct {
|
||||
Name string `json:"name,omitempty"`
|
||||
InternalIPv4 []string `json:"internal_ip_v4,omitempty"`
|
||||
@@ -21,6 +30,73 @@ type NetworkStatus struct {
|
||||
RecvRate string `json:"recv_rate,omitempty"`
|
||||
}
|
||||
|
||||
func GetNetworkMetric() ([]NetworkMetric, error) {
|
||||
interfaceStatList, err := net.Interfaces()
|
||||
if err != nil {
|
||||
log.ErrorF("[GetNetworkMetric] - get interface list error %v\n", err)
|
||||
return nil, err
|
||||
}
|
||||
contains := false
|
||||
for _, interfaceStat := range interfaceStatList {
|
||||
if MatchNetInterfaceRight(interfaceStat.Name) {
|
||||
contains = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !contains {
|
||||
msg := "[GetNetworkMetric]- can not find desired net interface !"
|
||||
log.Warn(msg)
|
||||
return nil, errors.New(233, msg)
|
||||
}
|
||||
|
||||
ioCountersStats, err := net.IOCounters(false)
|
||||
if err != nil {
|
||||
log.ErrorF("[GetNetworkMetric] - get io counter list error %v\n", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
duration := time.Second * 2
|
||||
time.Sleep(duration)
|
||||
|
||||
afterIoCounterList, err := net.IOCounters(false)
|
||||
if err != nil {
|
||||
log.ErrorF("[GetNetworkMetric] - get io counter list second error %v\n", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result []NetworkMetric
|
||||
|
||||
for _, interfaceStat := range interfaceStatList {
|
||||
if MatchNetInterfaceRight(interfaceStat.Name) {
|
||||
|
||||
for _, after := range afterIoCounterList {
|
||||
if strings.Contains(after.Name, interfaceStat.Name) {
|
||||
for _, before := range ioCountersStats {
|
||||
if strings.Contains(before.Name, interfaceStat.Name) {
|
||||
// match after and before interface
|
||||
|
||||
// assign
|
||||
networkMetric := NetworkMetric{
|
||||
IOCountersStat: after,
|
||||
SendSpeed: 0,
|
||||
RecvSpeed: 0,
|
||||
}
|
||||
|
||||
networkMetric.SendSpeed = (float64(after.BytesSent - before.BytesSent)) / duration.Seconds()
|
||||
networkMetric.RecvSpeed = (float64(after.BytesRecv - before.BytesRecv)) / duration.Seconds()
|
||||
|
||||
result = append(result, networkMetric)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func GetNetworkStatus() (*NetworkStatus, error) {
|
||||
interfaces, err := net.Interfaces()
|
||||
if err != nil {
|
||||
@@ -119,3 +195,18 @@ func GetInternelIpAddrs(addresses []string) ([]string, []string) {
|
||||
}
|
||||
return ipv4, ipv6
|
||||
}
|
||||
|
||||
func MatchNetInterfaceRight(interfaceName string) bool {
|
||||
|
||||
match, _ := regexp.MatchString(`^(eth|enp|wlan|ens|eno)\d+`, interfaceName)
|
||||
if match {
|
||||
return true
|
||||
}
|
||||
|
||||
match, _ = regexp.MatchString(`^(lo)`, interfaceName)
|
||||
if match {
|
||||
return true
|
||||
}
|
||||
|
||||
return match
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user