From 465b819296da432d098c0e110d932776f05eafd3 Mon Sep 17 00:00:00 2001 From: Tim Ehlers Date: Tue, 16 Apr 2019 23:37:45 -0500 Subject: [PATCH] add flag to remove uptime and n_users --- plugins/inputs/system/README.md | 6 +++- plugins/inputs/system/system.go | 56 ++++++++++++++++++++------------- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/plugins/inputs/system/README.md b/plugins/inputs/system/README.md index efaa8a17fa5f6..c5fa52d79b4b3 100644 --- a/plugins/inputs/system/README.md +++ b/plugins/inputs/system/README.md @@ -8,8 +8,12 @@ and number of users logged in. It is similar to the unix `uptime` command. ```toml # Read metrics about system load & uptime [[inputs.system]] - # no configuration + ## Uncomment the following line to disable uptime collection + # skip_uptime = true + ## Uncomment the following line to disable uptime collection + # skip_users = true ``` + #### Permissions: The `n_users` field requires read access to `/var/run/utmp`, and may require diff --git a/plugins/inputs/system/system.go b/plugins/inputs/system/system.go index 5c68870bbc1a2..63e2380373c39 100644 --- a/plugins/inputs/system/system.go +++ b/plugins/inputs/system/system.go @@ -15,20 +15,30 @@ import ( "github.com/shirou/gopsutil/load" ) -type SystemStats struct{} +type SystemStats struct { + SkipUsers bool + SkipUptime bool +} + +var SystemsampleConfig = ` + +## Uncomment to remove deprecated metrics. +# fielddrop = ["uptime_format"] +## Uncomment the following line to disable uptime collection +# skip_uptime = false +## Uncomment the following line to disable uptime collection +# skip_users = false +` func (_ *SystemStats) Description() string { return "Read metrics about system load & uptime" } func (_ *SystemStats) SampleConfig() string { - return ` - ## Uncomment to remove deprecated metrics. - # fielddrop = ["uptime_format"] -` + return SystemsampleConfig } -func (_ *SystemStats) Gather(acc telegraf.Accumulator) error { +func (s *SystemStats) Gather(acc telegraf.Accumulator) error { loadavg, err := load.Avg() if err != nil && !strings.Contains(err.Error(), "not implemented") { return err @@ -41,27 +51,31 @@ func (_ *SystemStats) Gather(acc telegraf.Accumulator) error { "n_cpus": runtime.NumCPU(), } - users, err := host.Users() - if err == nil { - fields["n_users"] = len(users) - } else if !os.IsPermission(err) { - return err + if !s.SkipUsers { + users, err := host.Users() + if err == nil { + fields["n_users"] = len(users) + } else if !os.IsPermission(err) { + return err + } } now := time.Now() acc.AddGauge("system", fields, nil, now) - uptime, err := host.Uptime() - if err != nil { - return err - } + if !s.SkipUptime { + uptime, err := host.Uptime() + if err != nil { + return err + } - acc.AddCounter("system", map[string]interface{}{ - "uptime": uptime, - }, nil, now) - acc.AddFields("system", map[string]interface{}{ - "uptime_format": formatUptime(uptime), - }, nil, now) + acc.AddCounter("system", map[string]interface{}{ + "uptime": uptime, + }, nil, now) + acc.AddFields("system", map[string]interface{}{ + "uptime_format": formatUptime(uptime), + }, nil, now) + } return nil }