mirror of
https://github.com/weaveworks/scope.git
synced 2026-08-18 03:46:45 +00:00
NewNodeMetadata -> MakeNodeMetadata. It doesn't return a pointer, so Make is more idiomatic. Invoke MakeNodeMetadata when necessary. The zero value for a NodeMetadata is no longer valid. Split MakeNodeMetadata to two constructors. MakeNodeMetadata when you don't have anything to prepopulate; MakeNodeMetadataWith when you do. Also, a fix to the tests in app. We unmarshal a RenderableNode struct, which has a JSON-ignored NodeMetadata field. The zero value is invalid, so we need to fix that before performing comparisons.
90 lines
1.8 KiB
Go
90 lines
1.8 KiB
Go
package host
|
|
|
|
import (
|
|
"net"
|
|
"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 (
|
|
InterfaceAddrs = net.InterfaceAddrs
|
|
Now = func() string { return time.Now().UTC().Format(time.RFC3339Nano) }
|
|
)
|
|
|
|
// Reporter generates Reports containing the host topology.
|
|
type Reporter struct {
|
|
hostID string
|
|
hostName string
|
|
}
|
|
|
|
// NewReporter returns a Reporter which produces a report containing host
|
|
// topology for this host.
|
|
func NewReporter(hostID, hostName string) *Reporter {
|
|
return &Reporter{
|
|
hostID: hostID,
|
|
hostName: hostName,
|
|
}
|
|
}
|
|
|
|
// Report implements Reporter.
|
|
func (r *Reporter) Report() (report.Report, error) {
|
|
var (
|
|
rep = report.MakeReport()
|
|
localCIDRs []string
|
|
)
|
|
|
|
localNets, err := InterfaceAddrs()
|
|
if err != nil {
|
|
return rep, err
|
|
}
|
|
for _, localNet := range localNets {
|
|
// Not all networks are IP networks.
|
|
if ipNet, ok := localNet.(*net.IPNet); ok {
|
|
localCIDRs = append(localCIDRs, ipNet.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
|
|
}
|