Documentation ¶
Index ¶
Constants ¶
const ( Timestamp = "ts" HostName = "host_name" LocalNetworks = "local_networks" OS = "os" KernelVersion = "kernel_version" Uptime = "uptime" Load1 = "load1" CPUUsage = "host_cpu_usage_percent" MemoryUsage = "host_mem_usage_bytes" ScopeVersion = "host_scope_version" )
Keys for use in Node.Latest.
const ( ProcUptime = "/proc/uptime" ProcLoad = "/proc/loadavg" ProcStat = "/proc/stat" ProcMemInfo = "/proc/meminfo" )
Exposed for testing.
const (
ExecHost = "host_exec"
)
Control IDs used by the host integration.
Variables ¶
var ( MetadataTemplates = report.MetadataTemplates{ KernelVersion: {ID: KernelVersion, Label: "Kernel Version", From: report.FromLatest, Priority: 1}, Uptime: {ID: Uptime, Label: "Uptime", From: report.FromLatest, Priority: 2}, HostName: {ID: HostName, Label: "Hostname", From: report.FromLatest, Priority: 11}, OS: {ID: OS, Label: "OS", From: report.FromLatest, Priority: 12}, LocalNetworks: {ID: LocalNetworks, Label: "Local Networks", From: report.FromSets, Priority: 13}, ScopeVersion: {ID: ScopeVersion, Label: "Scope Version", From: report.FromLatest, Priority: 14}, } MetricTemplates = report.MetricTemplates{ CPUUsage: {ID: CPUUsage, Label: "CPU", Format: report.PercentFormat, Priority: 1}, MemoryUsage: {ID: MemoryUsage, Label: "Memory", Format: report.FilesizeFormat, Priority: 2}, Load1: {ID: Load1, Label: "Load (1m)", Format: report.DefaultFormat, Group: "load", Priority: 11}, } )
Exposed for testing.
var GetCPUUsagePercent = func() (float64, float64) { stat, err := linuxproc.ReadStat(ProcStat) if err != nil { return 0.0, 0.0 } // From http://stackoverflow.com/questions/23367857/accurate-calculation-of-cpu-usage-given-in-percentage-in-linux var ( currentStat = stat.CPUStatAll prevIdle = previousStat.Idle + previousStat.IOWait idle = currentStat.Idle + currentStat.IOWait prevNonIdle = (previousStat.User + previousStat.Nice + previousStat.System + previousStat.IRQ + previousStat.SoftIRQ + previousStat.Steal) nonIdle = (currentStat.User + currentStat.Nice + currentStat.System + currentStat.IRQ + currentStat.SoftIRQ + currentStat.Steal) prevTotal = prevIdle + prevNonIdle total = idle + nonIdle // differentiate: actual value minus the previous one totald = total - prevTotal idled = idle - prevIdle ) previousStat = currentStat return float64(totald-idled) * 100. / float64(totald), float64(len(stat.CPUStats)) * 100. }
GetCPUUsagePercent returns the percent cpu usage and max (ie #cpus * 100)
var GetKernelVersion = func() (string, error) { var utsname syscall.Utsname if err := Uname(&utsname); err != nil { return "unknown", err } release := marshal.FromUtsname(utsname.Release) version := marshal.FromUtsname(utsname.Version) return fmt.Sprintf("%s %s", release, version), nil }
GetKernelVersion returns the kernel version as reported by uname.
var GetLoad = func(now time.Time) report.Metrics { buf, err := ioutil.ReadFile("/proc/loadavg") if err != nil { return nil } toks := strings.Fields(string(buf)) if len(toks) < 3 { return nil } one, err := strconv.ParseFloat(toks[0], 64) if err != nil { return nil } return report.Metrics{ Load1: report.MakeSingletonMetric(now, one), } }
GetLoad returns the current load averages as metrics.
var GetLocalNetworks = func() ([]*net.IPNet, error) { addrs, err := net.InterfaceAddrs() if err != nil { return nil, err } localNets := report.Networks{} for _, addr := range addrs { if ipNet, ok := addr.(*net.IPNet); ok { localNets = append(localNets, ipNet) } } return localNets, nil }
GetLocalNetworks is exported for mocking
var GetMemoryUsageBytes = func() (float64, float64) { meminfo, err := linuxproc.ReadMemInfo(ProcMemInfo) if err != nil { return 0.0, 0.0 } used := meminfo.MemTotal - meminfo.MemFree - meminfo.Buffers - meminfo.Cached return float64(used * kb), float64(meminfo.MemTotal * kb) }
GetMemoryUsageBytes returns the bytes memory usage and max
var GetUptime = func() (time.Duration, error) { buf, err := ioutil.ReadFile("/proc/uptime") if err != nil { return 0, err } fields := strings.Fields(string(buf)) if len(fields) != 2 { return 0, fmt.Errorf("invalid format: %s", string(buf)) } uptime, err := strconv.ParseFloat(fields[0], 64) if err != nil { return 0, err } return time.Duration(uptime) * time.Second, nil }
GetUptime returns the uptime of the host.
var Uname = syscall.Uname
Uname is swappable for mocking in tests.
Functions ¶
This section is empty.
Types ¶
type Reporter ¶
type Reporter struct {
// contains filtered or unexported fields
}
Reporter generates Reports containing the host topology.
func NewReporter ¶
func NewReporter(hostID, hostName, probeID, version string, pipes controls.PipeClient) *Reporter
NewReporter returns a Reporter which produces a report containing host topology for this host.
type Tagger ¶
type Tagger struct {
// contains filtered or unexported fields
}
Tagger tags each node in each topology of a report with the origin host node ID of this (probe) host. Effectively, a foreign key linking every node in every topology to an origin host node in the host topology.
func NewTagger ¶
NewTagger tags each node with a foreign key linking it to its origin host in the host topology.