diff --git a/probe/host/reporter.go b/probe/host/reporter.go index 9baf72cf91..4b23b0483c 100644 --- a/probe/host/reporter.go +++ b/probe/host/reporter.go @@ -1,6 +1,7 @@ package host import ( + "net" "runtime" "time" @@ -33,24 +34,38 @@ const ( // Reporter generates Reports containing the host topology. type Reporter struct { - hostID string - hostName string - localNets report.Networks + hostID string + hostName string } // NewReporter returns a Reporter which produces a report containing host // topology for this host. -func NewReporter(hostID, hostName string, localNets report.Networks) *Reporter { +func NewReporter(hostID, hostName string) *Reporter { return &Reporter{ - hostID: hostID, - hostName: hostName, - localNets: localNets, + hostID: hostID, + hostName: hostName, } } // Name of this reporter, for metrics gathering func (Reporter) Name() string { return "Host" } +// LocalNetworks is exported for mocking +var GetLocalNetworks = func() ([]*net.IPNet, error) { + addrs, err := net.InterfaceAddrs() + if err != nil { + return nil, err + } + localNets := report.Networks{} + for _, addr := range addrs { + // Not all addrs are IPNets. + if ipNet, ok := addr.(*net.IPNet); ok { + localNets = append(localNets, ipNet) + } + } + return localNets, nil +} + // Report implements Reporter. func (r *Reporter) Report() (report.Report, error) { var ( @@ -58,7 +73,11 @@ func (r *Reporter) Report() (report.Report, error) { localCIDRs []string ) - for _, localNet := range r.localNets { + localNets, err := GetLocalNetworks() + if err != nil { + return rep, nil + } + for _, localNet := range localNets { localCIDRs = append(localCIDRs, localNet.String()) } diff --git a/probe/host/reporter_test.go b/probe/host/reporter_test.go index 1d15e5434b..4bf7ba2e1f 100644 --- a/probe/host/reporter_test.go +++ b/probe/host/reporter_test.go @@ -29,7 +29,6 @@ func TestReporter(t *testing.T) { uptime = "278h55m43s" kernel = "release version" _, ipnet, _ = net.ParseCIDR(network) - localNets = report.Networks([]*net.IPNet{ipnet}) ) mtime.NowForce(timestamp) @@ -41,6 +40,7 @@ func TestReporter(t *testing.T) { oldGetUptime = host.GetUptime oldGetCPUUsagePercent = host.GetCPUUsagePercent oldGetMemoryUsageBytes = host.GetMemoryUsageBytes + oldGetLocalNetworks = host.GetLocalNetworks ) defer func() { host.GetKernelVersion = oldGetKernelVersion @@ -48,14 +48,16 @@ func TestReporter(t *testing.T) { host.GetUptime = oldGetUptime host.GetCPUUsagePercent = oldGetCPUUsagePercent host.GetMemoryUsageBytes = oldGetMemoryUsageBytes + host.GetLocalNetworks = oldGetLocalNetworks }() host.GetKernelVersion = func() (string, error) { return release + " " + version, nil } host.GetLoad = func(time.Time) report.Metrics { return metrics } host.GetUptime = func() (time.Duration, error) { return time.ParseDuration(uptime) } host.GetCPUUsagePercent = func() (float64, float64) { return 30.0, 100.0 } host.GetMemoryUsageBytes = func() (float64, float64) { return 60.0, 100.0 } + host.GetLocalNetworks = func() ([]*net.IPNet, error) { return []*net.IPNet{ipnet}, nil } - rpt, err := host.NewReporter(hostID, hostname, localNets).Report() + rpt, err := host.NewReporter(hostID, hostname).Report() if err != nil { t.Fatal(err) } diff --git a/prog/probe.go b/prog/probe.go index 02e56355a3..bb6533ae8c 100644 --- a/prog/probe.go +++ b/prog/probe.go @@ -112,18 +112,6 @@ func probeMain() { log.Printf("probe starting, version %s, ID %s", version, probeID) go check() - addrs, err := net.InterfaceAddrs() - if err != nil { - log.Fatal(err) - } - localNets := report.Networks{} - for _, addr := range addrs { - // Not all addrs are IPNets. - if ipNet, ok := addr.(*net.IPNet); ok { - localNets = append(localNets, ipNet) - } - } - if len(flag.Args()) > 0 { targets = flag.Args() } @@ -154,7 +142,7 @@ func probeMain() { p.AddTicker(processCache) p.AddReporter( endpointReporter, - host.NewReporter(hostID, hostName, localNets), + host.NewReporter(hostID, hostName), process.NewReporter(processCache, hostID, process.GetDeltaTotalJiffies), ) p.AddTagger(probe.NewTopologyTagger(), host.NewTagger(hostID, probeID))