Ignore error when utmp is missing (#5742)

This commit is contained in:
Tim Ehlers 2019-06-24 20:48:07 -05:00 committed by Daniel Nelson
parent bd9ddd8cb1
commit a5c94db625
2 changed files with 6 additions and 3 deletions

View File

@ -13,7 +13,7 @@ and number of users logged in. It is similar to the unix `uptime` command.
#### Permissions: #### Permissions:
The `n_users` field requires read access to `/var/run/utmp`, and may require 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: ### Metrics:

View File

@ -4,6 +4,7 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"fmt" "fmt"
"log"
"os" "os"
"runtime" "runtime"
"strings" "strings"
@ -44,8 +45,10 @@ func (_ *SystemStats) Gather(acc telegraf.Accumulator) error {
users, err := host.Users() users, err := host.Users()
if err == nil { if err == nil {
fields["n_users"] = len(users) fields["n_users"] = len(users)
} else if !os.IsPermission(err) { } else if os.IsNotExist(err) {
return err log.Printf("D! [inputs.system] Error reading users: %v", err)
} else if os.IsPermission(err) {
log.Printf("D! [inputs.system] %v", err)
} }
now := time.Now() now := time.Now()