feat: init pc-monitor project
- Client: Go-based Windows hardware monitoring (CPU, GPU, memory, disk, network, power) - Server: Go + Gin + SQLite backend with REST API - Frontend: Vue 3 + Element Plus dashboard - Docker deployment support - Windows service installation script
This commit is contained in:
68
client/collector/network.go
Normal file
68
client/collector/network.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package collector
|
||||
|
||||
import (
|
||||
"github.com/shirou/gopsutil/v3/net"
|
||||
)
|
||||
|
||||
type NetInterface struct {
|
||||
Name string `json:"name"`
|
||||
MACAddress string `json:"mac_address"`
|
||||
IPAddress string `json:"ip_address"`
|
||||
BytesSent uint64 `json:"bytes_sent"`
|
||||
BytesRecv uint64 `json:"bytes_recv"`
|
||||
Speed uint64 `json:"speed"`
|
||||
IsUp bool `json:"is_up"`
|
||||
}
|
||||
|
||||
func CollectNetwork() ([]NetInterface, error) {
|
||||
interfaces, err := net.Interfaces()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
counters, err := net.IOCounters(true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
counterMap := make(map[string]net.IOCountersStat)
|
||||
for _, c := range counters {
|
||||
counterMap[c.Name] = c
|
||||
}
|
||||
|
||||
var nets []NetInterface
|
||||
for _, iface := range interfaces {
|
||||
if iface.Name == "lo" || iface.Name == "Loopback Pseudo-Interface 1" {
|
||||
continue
|
||||
}
|
||||
|
||||
ni := NetInterface{
|
||||
Name: iface.Name,
|
||||
MACAddress: iface.HardwareAddr,
|
||||
IsUp: false,
|
||||
}
|
||||
|
||||
for _, addr := range iface.Addrs {
|
||||
if addr.Addr != "" && addr.Addr != "0.0.0.0" {
|
||||
ni.IPAddress = addr.Addr
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
for _, flag := range iface.Flags {
|
||||
if flag == "up" {
|
||||
ni.IsUp = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if c, ok := counterMap[iface.Name]; ok {
|
||||
ni.BytesSent = c.BytesSent
|
||||
ni.BytesRecv = c.BytesRecv
|
||||
}
|
||||
|
||||
nets = append(nets, ni)
|
||||
}
|
||||
|
||||
return nets, nil
|
||||
}
|
||||
Reference in New Issue
Block a user