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

View File

@ -6,6 +6,14 @@ import (
"github.com/stretchr/testify/assert" "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) { func TestUtilization(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
@ -45,18 +53,28 @@ func TestUtilization(t *testing.T) {
assert.EqualValues(0, t3.TxUtil) assert.EqualValues(0, t3.TxUtil)
} }
func TestUtilizationStatistics(t *testing.T) { func TestWirelessStatistics(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
stats := WirelessStatistics{
Airtime24: &WirelessAirtime{Active_time: 20},
Airtime5: &WirelessAirtime{Active_time: 20},
}
stats.SetUtilization(&WirelessStatistics{ stats := WirelessStatistics([]*WirelessAirtime{{
Airtime24: &WirelessAirtime{}, Frequency: 2400,
Airtime5: &WirelessAirtime{}, Active_time: 20,
}) Tx_time: 10,
}})
assert.Equal(20, int(stats.Airtime24.Active_time)) // Different Frequency, should not change anything
assert.Equal(20, int(stats.Airtime5.Active_time)) 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"` MgmtRx *Traffic `json:"mgmt_rx"`
} `json:"traffic,omitempty"` } `json:"traffic,omitempty"`
Switch map[string]*SwitchPort `json:"switch,omitempty"` Switch map[string]*SwitchPort `json:"switch,omitempty"`
Wireless *WirelessStatistics `json:"wireless,omitempty"` Wireless WirelessStatistics `json:"wireless,omitempty"`
} }
type MeshVPNPeerLink struct { type MeshVPNPeerLink struct {

View File

@ -1,12 +1,11 @@
package models package models
import ( import (
"strconv"
"github.com/FreifunkBremen/respond-collector/data" "github.com/FreifunkBremen/respond-collector/data"
"github.com/FreifunkBremen/respond-collector/jsontime" "github.com/FreifunkBremen/respond-collector/jsontime"
"github.com/FreifunkBremen/respond-collector/meshviewer" "github.com/FreifunkBremen/respond-collector/meshviewer"
imodels "github.com/influxdata/influxdb/models" imodels "github.com/influxdata/influxdb/models"
"strconv"
) )
// Node struct // 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.bytes"] = int64(t.Bytes)
fields["traffic.mgmt_tx.packets"] = t.Packets 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 { for _, airtime := range stats.Wireless {
addAirtime("24", w.Airtime24) suffix := airtime.FrequencyName()
} fields["airtime"+suffix+".chan_util"] = airtime.ChanUtil
if time := w.Airtime5; time != nil { fields["airtime"+suffix+".rx_util"] = airtime.RxUtil
addAirtime("5", w.Airtime5) 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 return

View File

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