Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add flag to remove uptime and n_users #5734

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion plugins/inputs/system/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make this a filter. I think just an exclude filter is sufficient. Allowed values would be uptime and users.

filter.NewIncludeExcludeFilter([]string{}, SomethingExclude)

The docker input plugin uses filters and may be a good reference.

## 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
Expand Down
56 changes: 35 additions & 21 deletions plugins/inputs/system/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,30 @@ import (
"github.com/shirou/gopsutil/load"
)

type SystemStats struct{}
type SystemStats struct {
SkipUsers bool
SkipUptime bool
}

var SystemsampleConfig = `

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this blank line.

## 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
Expand All @@ -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
}
Expand Down