refactor: make complete respondd config available during collecting

Right now, only the interface config and site domains are available when
collecting data. Make the complete respondd config available during collecting,
so the collect process can be configured more easily.

Motivation for this change is the support for custom fields.
This commit is contained in:
nrbffs 2019-11-17 10:33:20 +01:00 committed by genofire
parent edfb403884
commit ab798f0dd6
4 changed files with 26 additions and 18 deletions

View File

@ -44,11 +44,12 @@ var queryCmd = &cobra.Command{
}
ifacesConfigs = append(ifacesConfigs, ifaceConfig)
}
var config respond.Config
config.Interfaces = ifacesConfigs
nodes := runtime.NewNodes(&runtime.NodesConfig{})
sitesDomains := make(map[string][]string)
collector := respond.NewCollector(nil, nodes, sitesDomains, ifacesConfigs)
collector := respond.NewCollector(nil, nodes, &config)
defer collector.Close()
collector.SendPacket(dstAddress)

View File

@ -55,7 +55,7 @@ var serveCmd = &cobra.Command{
time.Sleep(delay)
}
collector = respond.NewCollector(allDatabase.Conn, nodes, config.Respondd.SitesDomains(), config.Respondd.Interfaces)
collector = respond.NewCollector(allDatabase.Conn, nodes, &config.Respondd)
collector.Start(config.Respondd.CollectInterval.Duration)
defer collector.Close()
}

View File

@ -20,12 +20,12 @@ import (
type Collector struct {
connections []multicastConn // UDP sockets
queue chan *Response // received responses
db database.Connection
nodes *runtime.Nodes
sitesDomains map[string][]string
interval time.Duration // Interval for multicast packets
stop chan interface{}
queue chan *Response // received responses
db database.Connection
nodes *runtime.Nodes
interval time.Duration // Interval for multicast packets
stop chan interface{}
config *Config
}
type multicastConn struct {
@ -35,17 +35,17 @@ type multicastConn struct {
}
// NewCollector creates a Collector struct
func NewCollector(db database.Connection, nodes *runtime.Nodes, sitesDomains map[string][]string, ifaces []InterfaceConfig) *Collector {
func NewCollector(db database.Connection, nodes *runtime.Nodes, config *Config) *Collector {
coll := &Collector{
db: db,
nodes: nodes,
sitesDomains: sitesDomains,
queue: make(chan *Response, 400),
stop: make(chan interface{}),
db: db,
nodes: nodes,
queue: make(chan *Response, 400),
stop: make(chan interface{}),
config: config,
}
for _, iface := range ifaces {
for _, iface := range config.Interfaces {
coll.listenUDP(iface)
}
@ -349,7 +349,7 @@ func (coll *Collector) globalStatsWorker() {
// saves global statistics
func (coll *Collector) saveGlobalStats() {
stats := runtime.NewGlobalStats(coll.nodes, coll.sitesDomains)
stats := runtime.NewGlobalStats(coll.nodes, coll.config.SitesDomains())
for site, domains := range stats {
for domain, stat := range domains {

View File

@ -16,8 +16,15 @@ const (
func TestCollector(t *testing.T) {
nodes := runtime.NewNodes(&runtime.NodesConfig{})
config := &Config{
Sites: map[string]SiteConfig{
SITE_TEST: {
Domains: []string{DOMAIN_TEST},
},
},
}
collector := NewCollector(nil, nodes, map[string][]string{SITE_TEST: {DOMAIN_TEST}}, []InterfaceConfig{})
collector := NewCollector(nil, nodes, config)
collector.Start(time.Millisecond)
time.Sleep(time.Millisecond * 10)
collector.Close()