Skip to content

Commit

Permalink
respondd statistics traffic
Browse files Browse the repository at this point in the history
  • Loading branch information
genofire committed Apr 11, 2019
1 parent 5588fd2 commit dc0fdbd
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 19 deletions.
2 changes: 1 addition & 1 deletion config-respondd_example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down
13 changes: 7 additions & 6 deletions respond/daemon/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
50 changes: 38 additions & 12 deletions respond/daemon/statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

0 comments on commit dc0fdbd

Please sign in to comment.