2022-04-15 12:35:45 +08:00
|
|
|
package cpu
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
|
|
|
cpuUtil "github.com/shirou/gopsutil/v3/cpu"
|
2022-04-25 15:34:15 +08:00
|
|
|
"github.com/toolkits/pkg/container/list"
|
2022-04-15 12:35:45 +08:00
|
|
|
|
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"
|
2022-07-03 22:01:41 +08:00
|
|
|
"flashcat.cloud/categraf/types"
|
2022-04-15 12:35:45 +08:00
|
|
|
)
|
|
|
|
|
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-29 00:02:20 +08:00
|
|
|
config.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-28 15:24:48 +08:00
|
|
|
func (s *CPUStats) Prefix() string {
|
2022-04-17 08:02:33 +08:00
|
|
|
return inputName
|
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
|
|
|
}
|
|
|
|
|
2022-04-18 17:35:16 +08:00
|
|
|
func (c *CPUStats) Drop() {
|
|
|
|
}
|
|
|
|
|
2022-04-25 15:34:15 +08:00
|
|
|
func (c *CPUStats) Gather(slist *list.SafeList) {
|
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-25 15:34:15 +08:00
|
|
|
return
|
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,
|
|
|
|
}
|
|
|
|
|
2022-07-03 22:01:41 +08:00
|
|
|
types.PushSamples(slist, fields, tags)
|
2022-04-15 12:35:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
c.lastStats = make(map[string]cpuUtil.TimesStat)
|
|
|
|
for _, cts := range times {
|
|
|
|
c.lastStats[cts.CPU] = cts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|