1
0
Fork 0

Adding upstream version 1.34.4.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-05-24 07:26:29 +02:00
parent e393c3af3f
commit 4978089aab
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
4963 changed files with 677545 additions and 0 deletions

View file

@ -0,0 +1,86 @@
# CPU Input Plugin
This plugin gather metrics on the system's CPUs.
⭐ Telegraf v0.1.5
🏷️ system
💻 all
## Global configuration options <!-- @/docs/includes/plugin_config.md -->
In addition to the plugin-specific configuration settings, plugins support
additional global and plugin configuration settings. These settings are used to
modify metrics, tags, and field or create aliases and configure ordering, etc.
See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
[CONFIGURATION.md]: ../../../docs/CONFIGURATION.md#plugins
## Configuration
```toml @sample.conf
# Read metrics about cpu usage
[[inputs.cpu]]
## Whether to report per-cpu stats or not
percpu = true
## Whether to report total system cpu stats or not
totalcpu = true
## If true, collect raw CPU time metrics
collect_cpu_time = false
## If true, compute and report the sum of all non-idle CPU states
## NOTE: The resulting 'time_active' field INCLUDES 'iowait'!
report_active = false
## If true and the info is available then add core_id and physical_id tags
core_tags = false
```
## Metrics
On Linux, consult `man proc` for details on the meanings of these values.
- cpu
- tags:
- cpu (CPU ID or `cpu-total`)
- fields:
- time_user (float)
- time_system (float)
- time_idle (float)
- time_active (float)
- time_nice (float)
- time_iowait (float)
- time_irq (float)
- time_softirq (float)
- time_steal (float)
- time_guest (float)
- time_guest_nice (float)
- usage_user (float, percent)
- usage_system (float, percent)
- usage_idle (float, percent)
- usage_active (float)
- usage_nice (float, percent)
- usage_iowait (float, percent)
- usage_irq (float, percent)
- usage_softirq (float, percent)
- usage_steal (float, percent)
- usage_guest (float, percent)
- usage_guest_nice (float, percent)
## Troubleshooting
On Linux systems the `/proc/stat` file is used to gather CPU times.
Percentages are based on the last 2 samples.
Tags core_id and physical_id are read from `/proc/cpuinfo` on Linux systems
## Example Output
```text
cpu,cpu=cpu0,host=loaner time_active=202224.15999999992,time_guest=30250.35,time_guest_nice=0,time_idle=1527035.04,time_iowait=1352,time_irq=0,time_nice=169.28,time_softirq=6281.4,time_steal=0,time_system=40097.14,time_user=154324.34 1568760922000000000
cpu,cpu=cpu0,host=loaner usage_active=31.249999981810106,usage_guest=2.083333333080696,usage_guest_nice=0,usage_idle=68.7500000181899,usage_iowait=0,usage_irq=0,usage_nice=0,usage_softirq=0,usage_steal=0,usage_system=4.166666666161392,usage_user=25.000000002273737 1568760922000000000
cpu,cpu=cpu1,host=loaner time_active=201890.02000000002,time_guest=30508.41,time_guest_nice=0,time_idle=264641.18,time_iowait=210.44,time_irq=0,time_nice=181.75,time_softirq=4537.88,time_steal=0,time_system=39480.7,time_user=157479.25 1568760922000000000
cpu,cpu=cpu1,host=loaner usage_active=12.500000010610771,usage_guest=2.0833333328280585,usage_guest_nice=0,usage_idle=87.49999998938922,usage_iowait=0,usage_irq=0,usage_nice=0,usage_softirq=2.0833333332070145,usage_steal=0,usage_system=4.166666665656117,usage_user=4.166666666414029 1568760922000000000
cpu,cpu=cpu2,host=loaner time_active=201382.78999999998,time_guest=30325.8,time_guest_nice=0,time_idle=264686.63,time_iowait=202.77,time_irq=0,time_nice=162.81,time_softirq=3378.34,time_steal=0,time_system=39270.59,time_user=158368.28 1568760922000000000
cpu,cpu=cpu2,host=loaner usage_active=15.999999993480742,usage_guest=1.9999999999126885,usage_guest_nice=0,usage_idle=84.00000000651926,usage_iowait=0,usage_irq=0,usage_nice=0,usage_softirq=2.0000000002764864,usage_steal=0,usage_system=3.999999999825377,usage_user=7.999999998923158 1568760922000000000
cpu,cpu=cpu3,host=loaner time_active=198953.51000000007,time_guest=30344.43,time_guest_nice=0,time_idle=265504.09,time_iowait=187.64,time_irq=0,time_nice=197.47,time_softirq=2301.47,time_steal=0,time_system=39313.73,time_user=156953.2 1568760922000000000
cpu,cpu=cpu3,host=loaner usage_active=10.41666667424579,usage_guest=0,usage_guest_nice=0,usage_idle=89.58333332575421,usage_iowait=0,usage_irq=0,usage_nice=0,usage_softirq=0,usage_steal=0,usage_system=4.166666666666667,usage_user=6.249999998484175 1568760922000000000
cpu,cpu=cpu-total,host=loaner time_active=804450.5299999998,time_guest=121429,time_guest_nice=0,time_idle=2321866.96,time_iowait=1952.86,time_irq=0,time_nice=711.32,time_softirq=16499.1,time_steal=0,time_system=158162.17,time_user=627125.08 1568760922000000000
cpu,cpu=cpu-total,host=loaner usage_active=17.616580305880305,usage_guest=1.036269430422946,usage_guest_nice=0,usage_idle=82.3834196941197,usage_iowait=0,usage_irq=0,usage_nice=0,usage_softirq=1.0362694300459534,usage_steal=0,usage_system=4.145077721691784,usage_user=11.398963731636465 1568760922000000000
```

167
plugins/inputs/cpu/cpu.go Normal file
View file

@ -0,0 +1,167 @@
//go:generate ../../../tools/readme_config_includer/generator
package cpu
import (
_ "embed"
"errors"
"fmt"
"time"
"github.com/shirou/gopsutil/v4/cpu"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/common/psutil"
"github.com/influxdata/telegraf/plugins/inputs"
)
//go:embed sample.conf
var sampleConfig string
type CPU struct {
ps psutil.PS
lastStats map[string]cpu.TimesStat
cpuInfo map[string]cpu.InfoStat
coreID bool
physicalID bool
PerCPU bool `toml:"percpu"`
TotalCPU bool `toml:"totalcpu"`
CollectCPUTime bool `toml:"collect_cpu_time"`
ReportActive bool `toml:"report_active"`
CoreTags bool `toml:"core_tags"`
Log telegraf.Logger `toml:"-"`
}
func (*CPU) SampleConfig() string {
return sampleConfig
}
func (c *CPU) Init() error {
if c.CoreTags {
cpuInfo, err := cpu.Info()
if err == nil {
c.coreID = cpuInfo[0].CoreID != ""
c.physicalID = cpuInfo[0].PhysicalID != ""
c.cpuInfo = make(map[string]cpu.InfoStat)
for _, ci := range cpuInfo {
c.cpuInfo[fmt.Sprintf("cpu%d", ci.CPU)] = ci
}
} else {
c.Log.Warnf("Failed to gather info about CPUs: %s", err)
}
}
return nil
}
func (c *CPU) Gather(acc telegraf.Accumulator) error {
times, err := c.ps.CPUTimes(c.PerCPU, c.TotalCPU)
if err != nil {
return fmt.Errorf("error getting CPU info: %w", err)
}
now := time.Now()
for _, cts := range times {
tags := map[string]string{
"cpu": cts.CPU,
}
if c.coreID {
tags["core_id"] = c.cpuInfo[cts.CPU].CoreID
}
if c.physicalID {
tags["physical_id"] = c.cpuInfo[cts.CPU].PhysicalID
}
total := totalCPUTime(cts)
active := activeCPUTime(cts)
if c.CollectCPUTime {
// Add cpu time metrics
fieldsC := map[string]interface{}{
"time_user": cts.User,
"time_system": cts.System,
"time_idle": cts.Idle,
"time_nice": cts.Nice,
"time_iowait": cts.Iowait,
"time_irq": cts.Irq,
"time_softirq": cts.Softirq,
"time_steal": cts.Steal,
"time_guest": cts.Guest,
"time_guest_nice": cts.GuestNice,
}
if c.ReportActive {
fieldsC["time_active"] = activeCPUTime(cts)
}
acc.AddCounter("cpu", fieldsC, tags, now)
}
// 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 {
err = errors.New("current total CPU time is less than previous total CPU time")
break
}
if totalDelta == 0 {
continue
}
fieldsG := 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,
}
if c.ReportActive {
fieldsG["usage_active"] = 100 * (active - lastActive) / totalDelta
}
acc.AddGauge("cpu", fieldsG, tags, now)
}
c.lastStats = make(map[string]cpu.TimesStat)
for _, cts := range times {
c.lastStats[cts.CPU] = cts
}
return err
}
func totalCPUTime(t cpu.TimesStat) float64 {
total := t.User + t.System + t.Nice + t.Iowait + t.Irq + t.Softirq + t.Steal + t.Idle
return total
}
func activeCPUTime(t cpu.TimesStat) float64 {
active := totalCPUTime(t) - t.Idle
return active
}
func init() {
inputs.Add("cpu", func() telegraf.Input {
return &CPU{
PerCPU: true,
TotalCPU: true,
ps: psutil.NewSystemPS(),
}
})
}

View file

@ -0,0 +1,249 @@
package cpu
import (
"testing"
"github.com/shirou/gopsutil/v4/cpu"
"github.com/stretchr/testify/require"
"github.com/influxdata/telegraf/plugins/common/psutil"
"github.com/influxdata/telegraf/testutil"
)
func newCPUStats(ps psutil.PS) *CPU {
return &CPU{
ps: ps,
CollectCPUTime: true,
ReportActive: true,
}
}
func TestCPUStats(t *testing.T) {
var mps psutil.MockPS
defer mps.AssertExpectations(t)
var acc testutil.Accumulator
cts := cpu.TimesStat{
CPU: "cpu0",
User: 8.8,
System: 8.2,
Idle: 80.1,
Nice: 1.3,
Iowait: 0.8389,
Irq: 0.6,
Softirq: 0.11,
Steal: 0.0511,
Guest: 3.1,
GuestNice: 0.324,
}
cts2 := cpu.TimesStat{
CPU: "cpu0",
User: 24.9, // increased by 16.1
System: 10.9, // increased by 2.7
Idle: 157.9798, // increased by 77.8798 (for total increase of 100)
Nice: 3.5, // increased by 2.2
Iowait: 0.929, // increased by 0.0901
Irq: 1.2, // increased by 0.6
Softirq: 0.31, // increased by 0.2
Steal: 0.2812, // increased by 0.2301
Guest: 11.4, // increased by 8.3
GuestNice: 2.524, // increased by 2.2
}
mps.On("CPUTimes").Return([]cpu.TimesStat{cts}, nil)
cs := newCPUStats(&mps)
err := cs.Gather(&acc)
require.NoError(t, err)
// Computed values are checked with delta > 0 because of floating point arithmetic
// imprecision
assertContainsTaggedFloat(t, &acc, "time_user", 8.8, 0)
assertContainsTaggedFloat(t, &acc, "time_system", 8.2, 0)
assertContainsTaggedFloat(t, &acc, "time_idle", 80.1, 0)
assertContainsTaggedFloat(t, &acc, "time_active", 19.9, 0.0005)
assertContainsTaggedFloat(t, &acc, "time_nice", 1.3, 0)
assertContainsTaggedFloat(t, &acc, "time_iowait", 0.8389, 0)
assertContainsTaggedFloat(t, &acc, "time_irq", 0.6, 0)
assertContainsTaggedFloat(t, &acc, "time_softirq", 0.11, 0)
assertContainsTaggedFloat(t, &acc, "time_steal", 0.0511, 0)
assertContainsTaggedFloat(t, &acc, "time_guest", 3.1, 0)
assertContainsTaggedFloat(t, &acc, "time_guest_nice", 0.324, 0)
mps2 := psutil.MockPS{}
mps2.On("CPUTimes").Return([]cpu.TimesStat{cts2}, nil)
cs.ps = &mps2
// Should have added cpu percentages too
err = cs.Gather(&acc)
require.NoError(t, err)
assertContainsTaggedFloat(t, &acc, "time_user", 24.9, 0)
assertContainsTaggedFloat(t, &acc, "time_system", 10.9, 0)
assertContainsTaggedFloat(t, &acc, "time_idle", 157.9798, 0)
assertContainsTaggedFloat(t, &acc, "time_active", 42.0202, 0.0005)
assertContainsTaggedFloat(t, &acc, "time_nice", 3.5, 0)
assertContainsTaggedFloat(t, &acc, "time_iowait", 0.929, 0)
assertContainsTaggedFloat(t, &acc, "time_irq", 1.2, 0)
assertContainsTaggedFloat(t, &acc, "time_softirq", 0.31, 0)
assertContainsTaggedFloat(t, &acc, "time_steal", 0.2812, 0)
assertContainsTaggedFloat(t, &acc, "time_guest", 11.4, 0)
assertContainsTaggedFloat(t, &acc, "time_guest_nice", 2.524, 0)
assertContainsTaggedFloat(t, &acc, "usage_user", 7.8, 0.0005)
assertContainsTaggedFloat(t, &acc, "usage_system", 2.7, 0.0005)
assertContainsTaggedFloat(t, &acc, "usage_idle", 77.8798, 0.0005)
assertContainsTaggedFloat(t, &acc, "usage_active", 22.1202, 0.0005)
assertContainsTaggedFloat(t, &acc, "usage_nice", 0, 0.0005)
assertContainsTaggedFloat(t, &acc, "usage_iowait", 0.0901, 0.0005)
assertContainsTaggedFloat(t, &acc, "usage_irq", 0.6, 0.0005)
assertContainsTaggedFloat(t, &acc, "usage_softirq", 0.2, 0.0005)
assertContainsTaggedFloat(t, &acc, "usage_steal", 0.2301, 0.0005)
assertContainsTaggedFloat(t, &acc, "usage_guest", 8.3, 0.0005)
assertContainsTaggedFloat(t, &acc, "usage_guest_nice", 2.2, 0.0005)
}
// Asserts that a given accumulator contains a measurement of type float64 with
// specific tags within a certain distance of a given expected value. Asserts a failure
// if the measurement is of the wrong type, or if no matching measurements are found
//
// Parameters:
//
// t *testing.T : Testing object to use
// acc testutil.Accumulator: Accumulator to examine
// field string : Name of field to examine
// expectedValue float64 : Value to search for within the measurement
// delta float64 : Maximum acceptable distance of an accumulated value
// from the expectedValue parameter. Useful when
// floating-point arithmetic imprecision makes looking
// for an exact match impractical
func assertContainsTaggedFloat(
t *testing.T,
acc *testutil.Accumulator,
field string,
expectedValue, delta float64,
) {
var actualValue float64
measurement := "cpu" // always cpu
for _, pt := range acc.Metrics {
if pt.Measurement == measurement {
for fieldname, value := range pt.Fields {
if fieldname == field {
if value, ok := value.(float64); ok {
actualValue = value
if (value >= expectedValue-delta) && (value <= expectedValue+delta) {
// Found the point, return without failing
return
}
} else {
require.Failf(t, "Wrong type", "Measurement %q does not have type float64", measurement)
}
}
}
}
}
require.Failf(t, "Measurement not found",
"Could not find measurement %q with requested tags within %f of %f, Actual: %f", measurement, delta, expectedValue, actualValue)
}
// TestCPUCountChange tests that no errors are encountered if the number of
// CPUs increases as reported with LXC.
func TestCPUCountIncrease(t *testing.T) {
var mps psutil.MockPS
var mps2 psutil.MockPS
var acc testutil.Accumulator
var err error
cs := newCPUStats(&mps)
mps.On("CPUTimes").Return(
[]cpu.TimesStat{
{
CPU: "cpu0",
},
}, nil)
err = cs.Gather(&acc)
require.NoError(t, err)
mps2.On("CPUTimes").Return(
[]cpu.TimesStat{
{
CPU: "cpu0",
},
{
CPU: "cpu1",
},
}, nil)
cs.ps = &mps2
err = cs.Gather(&acc)
require.NoError(t, err)
}
// TestCPUTimesDecrease tests that telegraf continue to works after
// CPU times decrease, which seems to occur when Linux system is suspended.
func TestCPUTimesDecrease(t *testing.T) {
var mps psutil.MockPS
defer mps.AssertExpectations(t)
var acc testutil.Accumulator
cts := cpu.TimesStat{
CPU: "cpu0",
User: 18,
Idle: 80,
Iowait: 2,
}
cts2 := cpu.TimesStat{
CPU: "cpu0",
User: 38, // increased by 20
Idle: 40, // decreased by 40
Iowait: 1, // decreased by 1
}
cts3 := cpu.TimesStat{
CPU: "cpu0",
User: 56, // increased by 18
Idle: 120, // increased by 80
Iowait: 3, // increased by 2
}
mps.On("CPUTimes").Return([]cpu.TimesStat{cts}, nil)
cs := newCPUStats(&mps)
err := cs.Gather(&acc)
require.NoError(t, err)
// Computed values are checked with delta > 0 because of floating point arithmetic
// imprecision
assertContainsTaggedFloat(t, &acc, "time_user", 18, 0)
assertContainsTaggedFloat(t, &acc, "time_idle", 80, 0)
assertContainsTaggedFloat(t, &acc, "time_iowait", 2, 0)
mps2 := psutil.MockPS{}
mps2.On("CPUTimes").Return([]cpu.TimesStat{cts2}, nil)
cs.ps = &mps2
// CPU times decreased. An error should be raised
err = cs.Gather(&acc)
require.Error(t, err)
mps3 := psutil.MockPS{}
mps3.On("CPUTimes").Return([]cpu.TimesStat{cts3}, nil)
cs.ps = &mps3
err = cs.Gather(&acc)
require.NoError(t, err)
assertContainsTaggedFloat(t, &acc, "time_user", 56, 0)
assertContainsTaggedFloat(t, &acc, "time_idle", 120, 0)
assertContainsTaggedFloat(t, &acc, "time_iowait", 3, 0)
assertContainsTaggedFloat(t, &acc, "usage_user", 18, 0.0005)
assertContainsTaggedFloat(t, &acc, "usage_idle", 80, 0.0005)
assertContainsTaggedFloat(t, &acc, "usage_iowait", 2, 0.0005)
}

View file

@ -0,0 +1,13 @@
# Read metrics about cpu usage
[[inputs.cpu]]
## Whether to report per-cpu stats or not
percpu = true
## Whether to report total system cpu stats or not
totalcpu = true
## If true, collect raw CPU time metrics
collect_cpu_time = false
## If true, compute and report the sum of all non-idle CPU states
## NOTE: The resulting 'time_active' field INCLUDES 'iowait'!
report_active = false
## If true and the info is available then add core_id and physical_id tags
core_tags = false