Add wireless statistics (airtime)

This commit is contained in:
Julian Kornberger 2016-12-22 03:14:51 +01:00
parent ed6e67fa13
commit ab01eb0d9e
5 changed files with 56 additions and 41 deletions

View File

@ -11,10 +11,7 @@ type Wireless struct {
Channel5 uint32 `json:"channel5,omitempty"`
}
type WirelessStatistics struct {
Airtime24 *WirelessAirtime `json:"airtime24,omitempty"`
Airtime5 *WirelessAirtime `json:"airtime5,omitempty"`
}
type WirelessStatistics []*WirelessAirtime
type WirelessAirtime struct {
ChanUtil float32 // Channel utilization
@ -29,19 +26,28 @@ type WirelessAirtime struct {
Frequency uint32 `json:"frequency"`
}
// Calculates the utilization values in regard to the previous values
func (cur *WirelessStatistics) SetUtilization(prev *WirelessStatistics) {
if cur.Airtime24 != nil {
cur.Airtime24.SetUtilization(prev.Airtime24)
func (airtime WirelessAirtime) FrequencyName() string {
if airtime.Frequency < 5000 {
return "11g"
} else {
return "11a"
}
}
// Calculates the utilization values in regard to the previous values
func (current WirelessStatistics) SetUtilization(previous WirelessStatistics) {
for _, c := range current {
for _, p := range previous {
if c.Frequency == p.Frequency {
c.SetUtilization(p)
}
}
if cur.Airtime5 != nil {
cur.Airtime5.SetUtilization(prev.Airtime5)
}
}
// Calculates the utilization values in regard to the previous values
func (cur *WirelessAirtime) SetUtilization(prev *WirelessAirtime) {
if prev == nil || cur.Active_time <= prev.Active_time {
if cur.Active_time <= prev.Active_time {
return
}

View File

@ -6,6 +6,14 @@ import (
"github.com/stretchr/testify/assert"
)
func TestFrequencyName(t *testing.T) {
assert := assert.New(t)
assert.Equal("11a", WirelessAirtime{Frequency: 5000}.FrequencyName())
assert.Equal("11g", WirelessAirtime{Frequency: 4999}.FrequencyName())
assert.Equal("11g", WirelessAirtime{Frequency: 2412}.FrequencyName())
}
func TestUtilization(t *testing.T) {
assert := assert.New(t)
@ -45,18 +53,28 @@ func TestUtilization(t *testing.T) {
assert.EqualValues(0, t3.TxUtil)
}
func TestUtilizationStatistics(t *testing.T) {
func TestWirelessStatistics(t *testing.T) {
assert := assert.New(t)
stats := WirelessStatistics{
Airtime24: &WirelessAirtime{Active_time: 20},
Airtime5: &WirelessAirtime{Active_time: 20},
}
stats.SetUtilization(&WirelessStatistics{
Airtime24: &WirelessAirtime{},
Airtime5: &WirelessAirtime{},
})
stats := WirelessStatistics([]*WirelessAirtime{{
Frequency: 2400,
Active_time: 20,
Tx_time: 10,
}})
assert.Equal(20, int(stats.Airtime24.Active_time))
assert.Equal(20, int(stats.Airtime5.Active_time))
// Different Frequency, should not change anything
stats.SetUtilization([]*WirelessAirtime{{
Frequency: 5000,
Active_time: 15,
Tx_time: 1,
}})
assert.EqualValues(0, stats[0].ChanUtil)
// Same Frequency, should set the utilization
stats.SetUtilization([]*WirelessAirtime{{
Frequency: 2400,
Active_time: 10,
Tx_time: 5,
}})
assert.EqualValues(50, stats[0].ChanUtil)
}

View File

@ -27,7 +27,7 @@ type Statistics struct {
MgmtRx *Traffic `json:"mgmt_rx"`
} `json:"traffic,omitempty"`
Switch map[string]*SwitchPort `json:"switch,omitempty"`
Wireless *WirelessStatistics `json:"wireless,omitempty"`
Wireless WirelessStatistics `json:"wireless,omitempty"`
}
type MeshVPNPeerLink struct {

View File

@ -1,12 +1,11 @@
package models
import (
"strconv"
"github.com/FreifunkBremen/respond-collector/data"
"github.com/FreifunkBremen/respond-collector/jsontime"
"github.com/FreifunkBremen/respond-collector/meshviewer"
imodels "github.com/influxdata/influxdb/models"
"strconv"
)
// Node struct
@ -73,22 +72,15 @@ func (node *Node) ToInflux() (tags imodels.Tags, fields imodels.Fields) {
fields["traffic.mgmt_tx.bytes"] = int64(t.Bytes)
fields["traffic.mgmt_tx.packets"] = t.Packets
}
if w := stats.Wireless; w != nil {
addAirtime := func(suffix string, time *data.WirelessAirtime) {
fields["airtime"+suffix+".chan_util"] = time.ChanUtil
fields["airtime"+suffix+".rx_util"] = time.RxUtil
fields["airtime"+suffix+".tx_util"] = time.TxUtil
fields["airtime"+suffix+".noise"] = time.Noise
fields["airtime"+suffix+".frequency"] = time.Frequency
tags.SetString("frequency"+suffix, strconv.Itoa(int(time.Frequency)))
}
if time := w.Airtime24; time != nil {
addAirtime("24", w.Airtime24)
}
if time := w.Airtime5; time != nil {
addAirtime("5", w.Airtime5)
}
for _, airtime := range stats.Wireless {
suffix := airtime.FrequencyName()
fields["airtime"+suffix+".chan_util"] = airtime.ChanUtil
fields["airtime"+suffix+".rx_util"] = airtime.RxUtil
fields["airtime"+suffix+".tx_util"] = airtime.TxUtil
fields["airtime"+suffix+".noise"] = airtime.Noise
fields["airtime"+suffix+".frequency"] = airtime.Frequency
tags.SetString("frequency"+suffix, strconv.Itoa(int(airtime.Frequency)))
}
return

View File

@ -4,7 +4,6 @@ import (
"bytes"
"compress/flate"
"encoding/json"
"io/ioutil"
"log"
"net"
"reflect"