categraf/inputs/cpu/cpu.go

122 lines
2.9 KiB
Go
Raw Normal View History

2022-04-15 12:35:45 +08:00
package cpu
import (
"log"
cpuUtil "github.com/shirou/gopsutil/v3/cpu"
2022-04-17 12:54:32 +08:00
"flashcat.cloud/categraf/config"
2022-04-15 12:35:45 +08:00
"flashcat.cloud/categraf/inputs"
"flashcat.cloud/categraf/inputs/system"
"flashcat.cloud/categraf/types"
)
2022-04-17 08:02:33 +08:00
const inputName = "cpu"
2022-04-15 12:35:45 +08:00
type CPUStats struct {
2022-04-15 13:58:46 +08:00
ps system.PS
lastStats map[string]cpuUtil.TimesStat
2022-04-17 12:54:32 +08:00
PrintConfigs bool `toml:"print_configs"`
Interval config.Duration `toml:"interval"`
CollectPerCPU bool `toml:"collect_per_cpu"`
2022-04-15 12:35:45 +08:00
}
func init() {
ps := system.NewSystemPS()
2022-04-17 08:02:33 +08:00
inputs.Add(inputName, func() inputs.Input {
2022-04-15 12:35:45 +08:00
return &CPUStats{
2022-04-16 16:52:23 +08:00
ps: ps,
2022-04-15 12:35:45 +08:00
}
})
}
2022-04-16 16:52:23 +08:00
func (s *CPUStats) GetInputName() string {
2022-04-17 08:02:33 +08:00
return inputName
2022-04-15 12:35:45 +08:00
}
2022-04-17 12:54:32 +08:00
func (s *CPUStats) GetInterval() config.Duration {
return s.Interval
2022-04-15 12:35:45 +08:00
}
// overwrite func
2022-04-16 16:52:23 +08:00
func (c *CPUStats) Init() error {
return nil
2022-04-15 18:19:15 +08:00
}
func (c *CPUStats) Gather() []*types.Sample {
var samples []*types.Sample
2022-04-15 12:35:45 +08:00
times, err := c.ps.CPUTimes(c.CollectPerCPU, true)
if err != nil {
log.Println("E! failed to get cpu metrics:", err)
2022-04-15 18:19:15 +08:00
return samples
2022-04-15 12:35:45 +08:00
}
for _, cts := range times {
tags := map[string]string{
"cpu": cts.CPU,
}
total := totalCPUTime(cts)
active := activeCPUTime(cts)
// Add in percentage
if len(c.lastStats) == 0 {
// If it's the 1st gather, can't get CPU Usage stats yet
continue
}
lastCts, ok := c.lastStats[cts.CPU]
if !ok {
continue
}
lastTotal := totalCPUTime(lastCts)
lastActive := activeCPUTime(lastCts)
totalDelta := total - lastTotal
if totalDelta < 0 {
log.Println("W! current total CPU time is less than previous total CPU time")
break
}
if totalDelta == 0 {
continue
}
fields := map[string]interface{}{
"usage_user": 100 * (cts.User - lastCts.User - (cts.Guest - lastCts.Guest)) / totalDelta,
"usage_system": 100 * (cts.System - lastCts.System) / totalDelta,
"usage_idle": 100 * (cts.Idle - lastCts.Idle) / totalDelta,
"usage_nice": 100 * (cts.Nice - lastCts.Nice - (cts.GuestNice - lastCts.GuestNice)) / totalDelta,
"usage_iowait": 100 * (cts.Iowait - lastCts.Iowait) / totalDelta,
"usage_irq": 100 * (cts.Irq - lastCts.Irq) / totalDelta,
"usage_softirq": 100 * (cts.Softirq - lastCts.Softirq) / totalDelta,
"usage_steal": 100 * (cts.Steal - lastCts.Steal) / totalDelta,
"usage_guest": 100 * (cts.Guest - lastCts.Guest) / totalDelta,
"usage_guest_nice": 100 * (cts.GuestNice - lastCts.GuestNice) / totalDelta,
"usage_active": 100 * (active - lastActive) / totalDelta,
}
samples = append(samples, inputs.NewSamples(fields, tags)...)
}
c.lastStats = make(map[string]cpuUtil.TimesStat)
for _, cts := range times {
c.lastStats[cts.CPU] = cts
}
2022-04-15 18:19:15 +08:00
return samples
2022-04-15 12:35:45 +08:00
}
func totalCPUTime(t cpuUtil.TimesStat) float64 {
total := t.User + t.System + t.Nice + t.Iowait + t.Irq + t.Softirq + t.Steal + t.Idle
return total
}
func activeCPUTime(t cpuUtil.TimesStat) float64 {
active := totalCPUTime(t) - t.Idle
return active
}