Skip to content

Commit

Permalink
Merge 520ca6f into dcf1b63
Browse files Browse the repository at this point in the history
  • Loading branch information
genofire committed Jan 17, 2018
2 parents dcf1b63 + 520ca6f commit 44ecb0e
Show file tree
Hide file tree
Showing 20 changed files with 231 additions and 109 deletions.
8 changes: 5 additions & 3 deletions cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import (

// importCmd represents the import command
var importCmd = &cobra.Command{
Use: "import <file.rrd> <site>",
Use: "import <file.rrd> <site> <domain>",
Short: "Imports global statistics from the given RRD files, requires InfluxDB",
Example: "yanic import --config /etc/yanic.toml olddata.rrd global",
Args: cobra.ExactArgs(2),
Example: "yanic import --config /etc/yanic.toml olddata.rrd global global",
Args: cobra.ExactArgs(3),
Run: func(cmd *cobra.Command, args []string) {
path := args[0]
site := args[1]
domain := args[2]
config := loadConfig()

err := allDatabase.Start(config.Database)
Expand All @@ -36,6 +37,7 @@ var importCmd = &cobra.Command{
},
ds.Time,
site,
domain,
)
}
},
Expand Down
4 changes: 3 additions & 1 deletion cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ var queryCmd = &cobra.Command{

nodes := runtime.NewNodes(&runtime.NodesConfig{})

collector := respond.NewCollector(nil, nodes, []string{}, []string{iface}, 0)
sitesDomains := make(map[string][]string)

collector := respond.NewCollector(nil, nodes, sitesDomains, []string{iface}, 0)
defer collector.Close()
collector.SendPacket(dstAddress)

Expand Down
2 changes: 1 addition & 1 deletion cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var serveCmd = &cobra.Command{
time.Sleep(delay)
}

collector = respond.NewCollector(connections, nodes, config.Respondd.Sites, config.Respondd.Interfaces, config.Respondd.Port)
collector = respond.NewCollector(connections, nodes, config.Respondd.SitesDomains(), config.Respondd.Interfaces, config.Respondd.Port)
collector.Start(config.Respondd.CollectInterval.Duration)
defer collector.Close()
}
Expand Down
9 changes: 7 additions & 2 deletions config_example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ synchronize = "1m"
collect_interval = "1m"
# interface that has an IP in your mesh network
interfaces = ["br-ffhb"]
# list of sites to save stats for (empty for global only)
sites = []
# define a port to listen
# if not set or set to 0 the kernel will use a random free port at its own
#port = 10001

# table of a site to save stats for (not exists for global only)
#[respondd.sites.example]
## list of domains on this site to save stats for (empty for global only)
#domains = []
## example
#[respondd.sites.ffhb]
#domains = ["city"]

# A little build-in webserver, which statically serves a directory.
# This is useful for testing purposes or for a little standalone installation.
Expand Down
3 changes: 2 additions & 1 deletion data/nodeinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ type Owner struct {

// System struct
type System struct {
SiteCode string `json:"site_code,omitempty"`
SiteCode string `json:"site_code,omitempty"`
DomainCode string `json:"domain_code,omitempty"`
}

// Location struct
Expand Down
4 changes: 2 additions & 2 deletions database/all/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ func (conn *Connection) InsertLink(link *runtime.Link, time time.Time) {
}
}

func (conn *Connection) InsertGlobals(stats *runtime.GlobalStats, time time.Time, site string) {
func (conn *Connection) InsertGlobals(stats *runtime.GlobalStats, time time.Time, site string, domain string) {
for _, item := range conn.list {
item.InsertGlobals(stats, time, site)
item.InsertGlobals(stats, time, site, domain)
}
}

Expand Down
2 changes: 1 addition & 1 deletion database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Connection interface {
InsertLink(*runtime.Link, time.Time)

// InsertGlobals stores global statistics
InsertGlobals(*runtime.GlobalStats, time.Time, string)
InsertGlobals(*runtime.GlobalStats, time.Time, string, string)

// PruneNodes prunes historical per-node data
PruneNodes(deleteAfter time.Duration)
Expand Down
9 changes: 8 additions & 1 deletion database/graphite/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/fgrosse/graphigo"
)

func (c *Connection) InsertGlobals(stats *runtime.GlobalStats, time time.Time, site string) {
func (c *Connection) InsertGlobals(stats *runtime.GlobalStats, time time.Time, site string, domain string) {
measurementGlobal := MeasurementGlobal
counterMeasurementModel := CounterMeasurementModel
counterMeasurementFirmware := CounterMeasurementFirmware
Expand All @@ -20,6 +20,13 @@ func (c *Connection) InsertGlobals(stats *runtime.GlobalStats, time time.Time, s
counterMeasurementAutoupdater += "_" + site
}

if domain != runtime.GLOBAL_DOMAIN {
measurementGlobal += "_" + domain
counterMeasurementModel += "_" + domain
counterMeasurementFirmware += "_" + domain
counterMeasurementAutoupdater += "_" + domain
}

c.addPoint(GlobalStatsFields(measurementGlobal, stats))
c.addCounterMap(counterMeasurementModel, stats.Models, time)
c.addCounterMap(counterMeasurementFirmware, stats.Firmwares, time)
Expand Down
25 changes: 16 additions & 9 deletions database/influxdb/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,35 @@ import (
)

// InsertGlobals implementation of database
func (conn *Connection) InsertGlobals(stats *runtime.GlobalStats, time time.Time, site string) {
var tags models.Tags
func (conn *Connection) InsertGlobals(stats *runtime.GlobalStats, time time.Time, site string, domain string) {
tags := models.Tags{}

measurementGlobal := MeasurementGlobal
counterMeasurementModel := CounterMeasurementModel
counterMeasurementFirmware := CounterMeasurementFirmware
counterMeasurementAutoupdater := CounterMeasurementAutoupdater

if site != runtime.GLOBAL_SITE {
tags = models.Tags{
models.Tag{Key: []byte("site"), Value: []byte(site)},
}
tags.Set([]byte("site"), []byte(site))

measurementGlobal += "_site"
counterMeasurementModel += "_site"
counterMeasurementFirmware += "_site"
counterMeasurementAutoupdater += "_site"
}
if domain != runtime.GLOBAL_DOMAIN {
tags.Set([]byte("domain"), []byte(domain))

measurementGlobal += "_domain"
counterMeasurementModel += "_domain"
counterMeasurementFirmware += "_domain"
counterMeasurementAutoupdater += "_domain"
}

conn.addPoint(measurementGlobal, tags, GlobalStatsFields(stats), time)
conn.addCounterMap(counterMeasurementModel, stats.Models, time, site)
conn.addCounterMap(counterMeasurementFirmware, stats.Firmwares, time, site)
conn.addCounterMap(counterMeasurementAutoupdater, stats.Autoupdater, time, site)
conn.addCounterMap(counterMeasurementModel, stats.Models, time, site, domain)
conn.addCounterMap(counterMeasurementFirmware, stats.Firmwares, time, site, domain)
conn.addCounterMap(counterMeasurementAutoupdater, stats.Autoupdater, time, site, domain)
}

// GlobalStatsFields returns fields for InfluxDB
Expand All @@ -48,13 +54,14 @@ func GlobalStatsFields(stats *runtime.GlobalStats) map[string]interface{} {
// Saves the values of a CounterMap in the database.
// The key are used as 'value' tag.
// The value is used as 'counter' field.
func (conn *Connection) addCounterMap(name string, m runtime.CounterMap, t time.Time, site string) {
func (conn *Connection) addCounterMap(name string, m runtime.CounterMap, t time.Time, site string, domain string) {
for key, count := range m {
conn.addPoint(
name,
models.Tags{
models.Tag{Key: []byte("value"), Value: []byte(key)},
models.Tag{Key: []byte("site"), Value: []byte(site)},
models.Tag{Key: []byte("domain"), Value: []byte(domain)},
},
models.Fields{"count": count},
t,
Expand Down
70 changes: 50 additions & 20 deletions database/influxdb/global_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,24 @@ import (
"github.com/FreifunkBremen/yanic/runtime"
)

const TEST_SITE = "ffxx"
const (
TEST_SITE = "ffhb"
TEST_DOMAIN = "city"
)

func TestGlobalStats(t *testing.T) {
stats := runtime.NewGlobalStats(createTestNodes(), []string{TEST_SITE})
stats := runtime.NewGlobalStats(createTestNodes(), map[string][]string{TEST_SITE: {TEST_DOMAIN}})

assert := assert.New(t)

// check SITE_GLOBAL fields
fields := GlobalStatsFields(stats[runtime.GLOBAL_SITE])
fields := GlobalStatsFields(stats[runtime.GLOBAL_SITE][runtime.GLOBAL_DOMAIN])
assert.EqualValues(3, fields["nodes"])

fields = GlobalStatsFields(stats[TEST_SITE])
fields = GlobalStatsFields(stats[TEST_SITE][runtime.GLOBAL_DOMAIN])
assert.EqualValues(2, fields["nodes"])
fields = GlobalStatsFields(stats[TEST_SITE][TEST_DOMAIN])

assert.EqualValues(1, fields["nodes"])

conn := &Connection{
Expand All @@ -32,59 +38,80 @@ func TestGlobalStats(t *testing.T) {

global := 0
globalSite := 0
globalDomain := 0

model := 0
modelSite := 0
modelDomain := 0

firmware := 0
firmwareSite := 0
firmwareDomain := 0

autoupdater := 0
autoupdaterSite := 0
autoupdaterDomain := 0

wg := sync.WaitGroup{}
wg.Add(9)
wg.Add(15)
go func() {
for p := range conn.points {
switch p.Name() {
case MeasurementGlobal:
global++
break
case "global_site":
globalSite++
break
case "global_site_domain":
globalDomain++

case CounterMeasurementModel:
model++
break
case "model_site":
modelSite++
break
case "model_site_domain":
modelDomain++

case CounterMeasurementFirmware:
firmware++
break
case "firmware_site":
firmwareSite++
break
case "firmware_site_domain":
firmwareDomain++

case CounterMeasurementAutoupdater:
autoupdater++
break
case "autoupdater_site":
autoupdaterSite++
break
case "autoupdater_site_domain":
autoupdaterDomain++

default:
assert.Equal("invalid p.Name found", p.Name())
}
wg.Done()
}
}()
for site, stat := range stats {
conn.InsertGlobals(stat, time.Now(), site)
for site, domains := range stats {
for domain, stat := range domains {
conn.InsertGlobals(stat, time.Now(), site, domain)
}
}
wg.Wait()
assert.Equal(1, global)
assert.Equal(1, globalSite)
assert.Equal(1, globalDomain)

assert.Equal(2, model)
assert.Equal(1, modelSite)
assert.Equal(2, modelSite)
assert.Equal(1, modelDomain)

assert.Equal(1, firmware)
assert.Equal(0, firmwareSite)
assert.Equal(1, firmwareSite)
assert.Equal(0, firmwareDomain)

assert.Equal(2, autoupdater)
assert.Equal(1, autoupdaterSite)
assert.Equal(2, autoupdaterSite)
assert.Equal(1, autoupdaterDomain)
}

func createTestNodes() *runtime.Nodes {
Expand All @@ -102,7 +129,9 @@ func createTestNodes() *runtime.Nodes {
Hardware: data.Hardware{
Model: "TP-Link 841",
},
System: data.System{},
System: data.System{
SiteCode: TEST_SITE,
},
},
}
nodeData.Nodeinfo.Software.Firmware.Release = "2016.1.6+entenhausen1"
Expand Down Expand Up @@ -134,7 +163,8 @@ func createTestNodes() *runtime.Nodes {
Model: "Xeon Multi-Core",
},
System: data.System{
SiteCode: TEST_SITE,
SiteCode: TEST_SITE,
DomainCode: TEST_DOMAIN,
},
},
})
Expand Down
3 changes: 3 additions & 0 deletions database/influxdb/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ func (conn *Connection) InsertNode(node *runtime.Node) {
if nodeinfo.System.SiteCode != "" {
tags.SetString("site", nodeinfo.System.SiteCode)
}
if nodeinfo.System.DomainCode != "" {
tags.SetString("domain", nodeinfo.System.DomainCode)
}
if owner := nodeinfo.Owner; owner != nil {
tags.SetString("owner", owner.Contact)
}
Expand Down
4 changes: 2 additions & 2 deletions database/logging/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ func (conn *Connection) InsertLink(link *runtime.Link, time time.Time) {
conn.log("InsertLink: ", link)
}

func (conn *Connection) InsertGlobals(stats *runtime.GlobalStats, time time.Time, site string) {
conn.log("InsertGlobals: [", time.String(), "] site: ", site, ", nodes: ", stats.Nodes, ", clients: ", stats.Clients, " models: ", len(stats.Models))
func (conn *Connection) InsertGlobals(stats *runtime.GlobalStats, time time.Time, site string, domain string) {
conn.log("InsertGlobals: [", time.String(), "] site: ", site, " domain: ", domain, ", nodes: ", stats.Nodes, ", clients: ", stats.Clients, " models: ", len(stats.Models))
}

func (conn *Connection) PruneNodes(deleteAfter time.Duration) {
Expand Down
2 changes: 1 addition & 1 deletion database/logging/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestStart(t *testing.T) {
assert.Contains(string(dat), "InsertLink")

assert.NotContains(string(dat), "InsertGlobals")
conn.InsertGlobals(&runtime.GlobalStats{}, time.Now(), runtime.GLOBAL_SITE)
conn.InsertGlobals(&runtime.GlobalStats{}, time.Now(), runtime.GLOBAL_SITE, runtime.GLOBAL_DOMAIN)
dat, _ = ioutil.ReadFile(path)
assert.Contains(string(dat), "InsertGlobals")

Expand Down
Loading

0 comments on commit 44ecb0e

Please sign in to comment.