respondd statistics traffic

This commit is contained in:
Martin/Geno 2019-04-11 12:59:55 +02:00 committed by genofire
parent c104fd01a7
commit 7934682ffb
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
3 changed files with 46 additions and 19 deletions

View File

@ -14,12 +14,12 @@ hostname = ""
site_code = "ffhb"
domain_code = ""
vpn = false
traffic_interfaces = [ "eth0" ]
[defaults.location]
latitude = 53.112446246
longitude = 8.734087944
# if divergent configuration of defaults are wanted by respond on interface/zones example with bat0
[zones.bat0]
node_id = ""

View File

@ -30,12 +30,13 @@ type Daemon struct {
}
type AnswerConfig struct {
NodeID string `toml:"node_id"`
Hostname string `toml:"hostname"`
SiteCode string `toml:"site_code"`
DomainCode string `toml:"domain_code"`
Location *data.Location `json:"location,omitempty"`
VPN bool `toml:"vpn"`
NodeID string `toml:"node_id"`
Hostname string `toml:"hostname"`
SiteCode string `toml:"site_code"`
DomainCode string `toml:"domain_code"`
Location *data.Location `json:"location,omitempty"`
VPN bool `toml:"vpn"`
TrafficInterfaces []string `toml:"traffic_interfaces"`
}
func (d *Daemon) getAnswer(iface string) (*AnswerConfig, string) {

View File

@ -4,29 +4,55 @@ import (
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/load"
"github.com/shirou/gopsutil/mem"
"github.com/shirou/gopsutil/net"
"github.com/FreifunkBremen/yanic/data"
)
func (d *Daemon) updateStatistics(iface string, data *data.ResponseData) {
_, nodeID := d.getAnswer(iface)
data.Statistics.NodeID = nodeID
func (d *Daemon) updateStatistics(iface string, resp *data.ResponseData) {
config, nodeID := d.getAnswer(iface)
resp.Statistics.NodeID = nodeID
if uptime, err := host.Uptime(); err == nil {
data.Statistics.Uptime = float64(uptime)
resp.Statistics.Uptime = float64(uptime)
}
if m, err := mem.VirtualMemory(); err == nil {
data.Statistics.Memory.Cached = int64(m.Cached)
data.Statistics.Memory.Total = int64(m.Total)
data.Statistics.Memory.Buffers = int64(m.Buffers)
data.Statistics.Memory.Free = int64(m.Free)
data.Statistics.Memory.Available = int64(m.Available)
resp.Statistics.Memory.Cached = int64(m.Cached)
resp.Statistics.Memory.Total = int64(m.Total)
resp.Statistics.Memory.Buffers = int64(m.Buffers)
resp.Statistics.Memory.Free = int64(m.Free)
resp.Statistics.Memory.Available = int64(m.Available)
}
if v, err := load.Avg(); err == nil {
data.Statistics.LoadAverage = v.Load1
resp.Statistics.LoadAverage = v.Load1
}
if v, err := load.Misc(); err == nil {
data.Statistics.Processes.Running = uint32(v.ProcsRunning)
resp.Statistics.Processes.Running = uint32(v.ProcsRunning)
//TODO fix after upstream
data.Statistics.Processes.Total = uint32(v.Ctxt)
resp.Statistics.Processes.Total = uint32(v.Ctxt)
}
if ls, err := net.IOCounters(true); err == nil {
resp.Statistics.Traffic.Tx = &data.Traffic{}
resp.Statistics.Traffic.Rx = &data.Traffic{}
allowedInterfaces := make(map[string]bool)
for _, iface := range config.TrafficInterfaces {
allowedInterfaces[iface] = true
}
for _, s := range ls {
if i, ok := allowedInterfaces[s.Name]; !ok || !i {
continue
}
resp.Statistics.Traffic.Tx.Bytes = float64(s.BytesSent)
resp.Statistics.Traffic.Tx.Packets = float64(s.PacketsSent)
resp.Statistics.Traffic.Tx.Dropped = float64(s.Dropout)
resp.Statistics.Traffic.Rx.Bytes = float64(s.BytesRecv)
resp.Statistics.Traffic.Rx.Packets = float64(s.PacketsRecv)
resp.Statistics.Traffic.Rx.Dropped = float64(s.Dropin)
}
}
}