mirror of
https://github.com/weaveworks/scope.git
synced 2026-05-05 16:59:36 +00:00
Another implicit invariant in the data model is that edges are always of the form (local -> remote). That is, the source of an edge must always be a node that originates from within Scope's domain of visibility. This was evident by the presence of ingress and egress fields in edge/aggregate metadata. When building the sniffer, I accidentally and incorrectly violated this invariant, by constructing distinct edges for (local -> remote) and (remote -> local), and collapsing ingress and egress byte counts to a single scalar. I experienced a variety of subtle undefined behavior as a result. See #339. This change reverts to the old, correct methodology. Consequently the sniffer needs to be able to find out which side of the sniffed packet is local v. remote, and to do that it needs access to local networks. I moved the discovery from the probe/host package into probe/main.go. As part of that work I discovered that package report also maintains its own, independent "cache" of local networks. Except it contains only the (optional) Docker bridge network, if it's been populated by the probe, and it's only used by the report.Make{Endpoint,Address}NodeID constructors to scope local addresses. Normally, scoping happens during rendering, and only for pseudo nodes -- see current LeafMap Render localNetworks. This is pretty convoluted and should be either be made consistent or heavily commented.
83 lines
1.7 KiB
Go
83 lines
1.7 KiB
Go
package host
|
|
|
|
import (
|
|
"runtime"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/weaveworks/scope/report"
|
|
)
|
|
|
|
// Keys for use in NodeMetadata.
|
|
const (
|
|
Timestamp = "ts"
|
|
HostName = "host_name"
|
|
LocalNetworks = "local_networks"
|
|
OS = "os"
|
|
Load = "load"
|
|
KernelVersion = "kernel_version"
|
|
Uptime = "uptime"
|
|
)
|
|
|
|
// Exposed for testing.
|
|
const (
|
|
ProcUptime = "/proc/uptime"
|
|
ProcLoad = "/proc/loadavg"
|
|
)
|
|
|
|
// Exposed for testing.
|
|
var (
|
|
Now = func() string { return time.Now().UTC().Format(time.RFC3339Nano) }
|
|
)
|
|
|
|
// Reporter generates Reports containing the host topology.
|
|
type Reporter struct {
|
|
hostID string
|
|
hostName string
|
|
localNets report.Networks
|
|
}
|
|
|
|
// NewReporter returns a Reporter which produces a report containing host
|
|
// topology for this host.
|
|
func NewReporter(hostID, hostName string, localNets report.Networks) *Reporter {
|
|
return &Reporter{
|
|
hostID: hostID,
|
|
hostName: hostName,
|
|
localNets: localNets,
|
|
}
|
|
}
|
|
|
|
// Report implements Reporter.
|
|
func (r *Reporter) Report() (report.Report, error) {
|
|
var (
|
|
rep = report.MakeReport()
|
|
localCIDRs []string
|
|
)
|
|
|
|
for _, localNet := range r.localNets {
|
|
localCIDRs = append(localCIDRs, localNet.String())
|
|
}
|
|
|
|
uptime, err := GetUptime()
|
|
if err != nil {
|
|
return rep, err
|
|
}
|
|
|
|
kernel, err := GetKernelVersion()
|
|
if err != nil {
|
|
return rep, err
|
|
}
|
|
|
|
rep.Host.NodeMetadatas[report.MakeHostNodeID(r.hostID)] = report.MakeNodeMetadataWith(map[string]string{
|
|
Timestamp: Now(),
|
|
HostName: r.hostName,
|
|
LocalNetworks: strings.Join(localCIDRs, " "),
|
|
OS: runtime.GOOS,
|
|
Load: GetLoad(),
|
|
KernelVersion: kernel,
|
|
Uptime: uptime.String(),
|
|
})
|
|
|
|
return rep, nil
|
|
}
|