Skip to content

Commit

Permalink
skip when utmp is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
ehlerst committed Apr 17, 2019
1 parent 6a7d0c1 commit c2af719
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
5 changes: 3 additions & 2 deletions plugins/inputs/system/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ 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
## Print out when utmp is not available for n_users
# warn_on_missing = true
```
#### Permissions:

The `n_users` field requires read access to `/var/run/utmp`, and may require
the `telegraf` user to be added to the `utmp` group on some systems.
the `telegraf` user to be added to the `utmp` group on some systems. If this file does not exist `n_users` will be skipped.

### Metrics:

Expand Down
13 changes: 10 additions & 3 deletions plugins/inputs/system/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"fmt"
"log"
"os"
"runtime"
"strings"
Expand All @@ -15,7 +16,9 @@ import (
"github.com/shirou/gopsutil/load"
)

type SystemStats struct{}
type SystemStats struct{
WarnOnMissing bool
}

func (_ *SystemStats) Description() string {
return "Read metrics about system load & uptime"
Expand All @@ -25,10 +28,12 @@ func (_ *SystemStats) SampleConfig() string {
return `
## Uncomment to remove deprecated metrics.
# fielddrop = ["uptime_format"]
## Print out when utmp is not available for n_users
# warn_on_missing = true
`
}

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 @@ -44,7 +49,9 @@ func (_ *SystemStats) Gather(acc telegraf.Accumulator) error {
users, err := host.Users()
if err == nil {
fields["n_users"] = len(users)
} else if !os.IsPermission(err) {
} else if os.IsNotExist(err) && s.WarnOnMissing {
log.Printf("[inputs.system]: %s\n", err.Error())
} else if !os.IsPermission(err) && !os.IsNotExist(err) {
return err
}

Expand Down

0 comments on commit c2af719

Please sign in to comment.