Skip to content

Commit

Permalink
Decide what the local networks are at runtime, not once at startup.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwilkie committed Feb 4, 2016
1 parent 7fcb4c3 commit cda5835
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 23 deletions.
35 changes: 27 additions & 8 deletions probe/host/reporter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package host

import (
"net"
"runtime"
"time"

Expand Down Expand Up @@ -33,32 +34,50 @@ 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 (
rep = report.MakeReport()
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())
}

Expand Down
6 changes: 4 additions & 2 deletions probe/host/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -41,21 +40,24 @@ func TestReporter(t *testing.T) {
oldGetUptime = host.GetUptime
oldGetCPUUsagePercent = host.GetCPUUsagePercent
oldGetMemoryUsageBytes = host.GetMemoryUsageBytes
oldGetLocalNetworks = host.GetLocalNetworks
)
defer func() {
host.GetKernelVersion = oldGetKernelVersion
host.GetLoad = oldGetLoad
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)
}
Expand Down
14 changes: 1 addition & 13 deletions prog/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit cda5835

Please sign in to comment.