diff --git a/config-respondd_example.toml b/config-respondd_example.toml index 4360e3b..df5997f 100644 --- a/config-respondd_example.toml +++ b/config-respondd_example.toml @@ -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 = "" diff --git a/respond/daemon/config.go b/respond/daemon/config.go index 4289209..fa4b7f9 100644 --- a/respond/daemon/config.go +++ b/respond/daemon/config.go @@ -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) { diff --git a/respond/daemon/statistics.go b/respond/daemon/statistics.go index d5ce259..826519a 100644 --- a/respond/daemon/statistics.go +++ b/respond/daemon/statistics.go @@ -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) + } } }