Added xmit bytes to interface report

This commit is contained in:
Martin Eskdale Moen 2020-05-28 15:50:19 +01:00
parent fb6ccee193
commit ce1b1dd9ff
3 changed files with 44 additions and 23 deletions

View File

@ -223,6 +223,10 @@ An example report file, generated by `dsnet report` to
"DNS": "", "DNS": "",
"PeersOnline": 4, "PeersOnline": 4,
"PeersTotal": 13, "PeersTotal": 13,
"ReceiveBytes": 32517164,
"TransmitBytes": 85384984,
"ReceiveBytesSI": "32.5 MB",
"TransmitBytesSI": "85.4 MB",
"Peers": [ "Peers": [
{ {
"Hostname": "test", "Hostname": "test",

View File

@ -8,6 +8,7 @@ import (
"time" "time"
"github.com/go-playground/validator/v10" "github.com/go-playground/validator/v10"
"github.com/vishvananda/netlink"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes" "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
) )
@ -21,11 +22,15 @@ type DsnetReport struct {
IP net.IP IP net.IP
// IP network from which to allocate automatic sequential addresses // IP network from which to allocate automatic sequential addresses
// Network is chosen randomly when not specified // Network is chosen randomly when not specified
Network JSONIPNet Network JSONIPNet
DNS net.IP DNS net.IP
PeersOnline int PeersOnline int
PeersTotal int PeersTotal int
Peers []PeerReport Peers []PeerReport
ReceiveBytes uint64
TransmitBytes uint64
ReceiveBytesSI string
TransmitBytesSI string
} }
func GenerateReport(dev *wgtypes.Device, conf *DsnetConfig, oldReport *DsnetReport) DsnetReport { func GenerateReport(dev *wgtypes.Device, conf *DsnetConfig, oldReport *DsnetReport) DsnetReport {
@ -34,6 +39,11 @@ func GenerateReport(dev *wgtypes.Device, conf *DsnetConfig, oldReport *DsnetRepo
oldPeerReportIndex := make(map[string]PeerReport) oldPeerReportIndex := make(map[string]PeerReport)
peersOnline := 0 peersOnline := 0
linkDev, err := netlink.LinkByName(conf.InterfaceName)
check(err)
stats := linkDev.Attrs().Statistics
for _, peer := range dev.Peers { for _, peer := range dev.Peers {
wgPeerIndex[peer.PublicKey] = peer wgPeerIndex[peer.PublicKey] = peer
} }
@ -57,7 +67,7 @@ func GenerateReport(dev *wgtypes.Device, conf *DsnetConfig, oldReport *DsnetRepo
dormant := !wgPeer.LastHandshakeTime.IsZero() && time.Since(wgPeer.LastHandshakeTime) > EXPIRY dormant := !wgPeer.LastHandshakeTime.IsZero() && time.Since(wgPeer.LastHandshakeTime) > EXPIRY
if online { if online {
peersOnline += 1 peersOnline++
} }
externalIP := net.IP{} externalIP := net.IP{}
@ -65,6 +75,9 @@ func GenerateReport(dev *wgtypes.Device, conf *DsnetConfig, oldReport *DsnetRepo
externalIP = wgPeer.Endpoint.IP externalIP = wgPeer.Endpoint.IP
} }
uReceiveBytes := uint64(wgPeer.ReceiveBytes)
uTransmitBytes := uint64(wgPeer.TransmitBytes)
peerReports[i] = PeerReport{ peerReports[i] = PeerReport{
Hostname: peer.Hostname, Hostname: peer.Hostname,
Online: online, Online: online,
@ -76,24 +89,28 @@ func GenerateReport(dev *wgtypes.Device, conf *DsnetConfig, oldReport *DsnetRepo
ExternalIP: externalIP, ExternalIP: externalIP,
Networks: peer.Networks, Networks: peer.Networks,
LastHandshakeTime: wgPeer.LastHandshakeTime, LastHandshakeTime: wgPeer.LastHandshakeTime,
ReceiveBytes: wgPeer.ReceiveBytes, ReceiveBytes: uReceiveBytes,
TransmitBytes: wgPeer.TransmitBytes, TransmitBytes: uTransmitBytes,
ReceiveBytesSI: BytesToSI(wgPeer.ReceiveBytes), ReceiveBytesSI: BytesToSI(uReceiveBytes),
TransmitBytesSI: BytesToSI(wgPeer.TransmitBytes), TransmitBytesSI: BytesToSI(uTransmitBytes),
} }
} }
return DsnetReport{ return DsnetReport{
ExternalIP: conf.ExternalIP, ExternalIP: conf.ExternalIP,
InterfaceName: conf.InterfaceName, InterfaceName: conf.InterfaceName,
ListenPort: conf.ListenPort, ListenPort: conf.ListenPort,
Domain: conf.Domain, Domain: conf.Domain,
IP: conf.IP, IP: conf.IP,
Network: conf.Network, Network: conf.Network,
DNS: conf.DNS, DNS: conf.DNS,
Peers: peerReports, Peers: peerReports,
PeersOnline: peersOnline, PeersOnline: peersOnline,
PeersTotal: len(peerReports), PeersTotal: len(peerReports),
ReceiveBytes: stats.RxBytes,
TransmitBytes: stats.TxBytes,
ReceiveBytesSI: BytesToSI(stats.RxBytes),
TransmitBytesSI: BytesToSI(stats.TxBytes),
} }
} }
@ -146,8 +163,8 @@ type PeerReport struct {
// TODO support routing additional networks (AllowedIPs) // TODO support routing additional networks (AllowedIPs)
Networks []JSONIPNet Networks []JSONIPNet
LastHandshakeTime time.Time LastHandshakeTime time.Time
ReceiveBytes int64 ReceiveBytes uint64
TransmitBytes int64 TransmitBytes uint64
ReceiveBytesSI string ReceiveBytesSI string
TransmitBytesSI string TransmitBytesSI string
} }

View File

@ -49,7 +49,7 @@ func ConfirmOrAbort(format string, a ...interface{}) {
} }
} }
func BytesToSI(b int64) string { func BytesToSI(b uint64) string {
const unit = 1000 const unit = 1000
if b < unit { if b < unit {
return fmt.Sprintf("%d B", b) return fmt.Sprintf("%d B", b)