add docker
This commit is contained in:
118
pkg/sysstats/sysstats.go
Normal file
118
pkg/sysstats/sysstats.go
Normal file
@@ -0,0 +1,118 @@
|
||||
// Package sysstats reports host CPU, memory and disk usage plus the Go
|
||||
// process's own footprint. It reads /proc and statfs directly so the binary
|
||||
// keeps working in the scratch image without extra dependencies. Fields the
|
||||
// current OS cannot provide are left nil.
|
||||
package sysstats
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"runtime"
|
||||
"time"
|
||||
)
|
||||
|
||||
// cpuSampleWindow is how long CPU counters are sampled to compute usage.
|
||||
const cpuSampleWindow = 250 * time.Millisecond
|
||||
|
||||
type CPU struct {
|
||||
Cores int `json:"cores"`
|
||||
UsagePercent *float64 `json:"usagePercent"`
|
||||
LoadAvg *[3]float64 `json:"loadAvg"`
|
||||
}
|
||||
|
||||
type Memory struct {
|
||||
TotalBytes uint64 `json:"totalBytes"`
|
||||
UsedBytes uint64 `json:"usedBytes"`
|
||||
AvailableBytes uint64 `json:"availableBytes"`
|
||||
UsagePercent float64 `json:"usagePercent"`
|
||||
SwapTotalBytes uint64 `json:"swapTotalBytes"`
|
||||
SwapUsedBytes uint64 `json:"swapUsedBytes"`
|
||||
// LimitBytes is the container (cgroup) memory limit, nil when unlimited.
|
||||
LimitBytes *uint64 `json:"limitBytes"`
|
||||
}
|
||||
|
||||
type Disk struct {
|
||||
Path string `json:"path"`
|
||||
TotalBytes uint64 `json:"totalBytes"`
|
||||
UsedBytes uint64 `json:"usedBytes"`
|
||||
FreeBytes uint64 `json:"freeBytes"`
|
||||
UsagePercent float64 `json:"usagePercent"`
|
||||
}
|
||||
|
||||
type Process struct {
|
||||
Goroutines int `json:"goroutines"`
|
||||
HeapAllocBytes uint64 `json:"heapAllocBytes"`
|
||||
SysBytes uint64 `json:"sysBytes"`
|
||||
NumGC uint32 `json:"numGc"`
|
||||
UptimeSeconds int64 `json:"uptimeSeconds"`
|
||||
}
|
||||
|
||||
type Stats struct {
|
||||
Hostname string `json:"hostname"`
|
||||
OS string `json:"os"`
|
||||
Arch string `json:"arch"`
|
||||
GoVersion string `json:"goVersion"`
|
||||
HostUptimeSeconds *int64 `json:"hostUptimeSeconds"`
|
||||
CPU CPU `json:"cpu"`
|
||||
Memory *Memory `json:"memory"`
|
||||
Disks []Disk `json:"disks"`
|
||||
Process Process `json:"process"`
|
||||
CollectedAt time.Time `json:"collectedAt"`
|
||||
}
|
||||
|
||||
var processStart = time.Now()
|
||||
|
||||
// Collect gathers a snapshot. It blocks for about cpuSampleWindow while CPU
|
||||
// usage is sampled. Disk paths that cannot be read are skipped.
|
||||
func Collect(ctx context.Context, diskPaths []string) (Stats, error) {
|
||||
hostname, _ := os.Hostname()
|
||||
|
||||
usage, err := cpuUsage(ctx, cpuSampleWindow)
|
||||
if err != nil {
|
||||
return Stats{}, err
|
||||
}
|
||||
|
||||
disks := make([]Disk, 0, len(diskPaths))
|
||||
for _, p := range diskPaths {
|
||||
if d, err := diskUsage(p); err == nil {
|
||||
disks = append(disks, d)
|
||||
}
|
||||
}
|
||||
|
||||
var ms runtime.MemStats
|
||||
runtime.ReadMemStats(&ms)
|
||||
|
||||
return Stats{
|
||||
Hostname: hostname,
|
||||
OS: runtime.GOOS,
|
||||
Arch: runtime.GOARCH,
|
||||
GoVersion: runtime.Version(),
|
||||
HostUptimeSeconds: hostUptime(),
|
||||
CPU: CPU{
|
||||
Cores: runtime.NumCPU(),
|
||||
UsagePercent: usage,
|
||||
LoadAvg: loadAvg(),
|
||||
},
|
||||
Memory: memory(),
|
||||
Disks: disks,
|
||||
Process: Process{
|
||||
Goroutines: runtime.NumGoroutine(),
|
||||
HeapAllocBytes: ms.HeapAlloc,
|
||||
SysBytes: ms.Sys,
|
||||
NumGC: ms.NumGC,
|
||||
UptimeSeconds: int64(time.Since(processStart).Seconds()),
|
||||
},
|
||||
CollectedAt: time.Now(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func percent(part, total uint64) float64 {
|
||||
if total == 0 {
|
||||
return 0
|
||||
}
|
||||
return round2(float64(part) / float64(total) * 100)
|
||||
}
|
||||
|
||||
func round2(v float64) float64 {
|
||||
return float64(int64(v*100+0.5)) / 100
|
||||
}
|
||||
Reference in New Issue
Block a user