2015-04-01 16:34:32 +00:00
|
|
|
package system
|
|
|
|
|
2015-08-31 20:03:38 +00:00
|
|
|
import (
|
2015-11-15 20:46:01 +00:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2018-04-18 23:55:18 +00:00
|
|
|
"os"
|
2016-04-21 00:22:04 +00:00
|
|
|
"runtime"
|
2017-08-16 19:05:46 +00:00
|
|
|
"strings"
|
2018-04-27 21:55:10 +00:00
|
|
|
"time"
|
2015-11-15 20:46:01 +00:00
|
|
|
|
|
|
|
"github.com/shirou/gopsutil/host"
|
|
|
|
"github.com/shirou/gopsutil/load"
|
2015-05-18 19:15:15 +00:00
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2015-08-31 20:03:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type SystemStats struct{}
|
2015-05-18 19:15:15 +00:00
|
|
|
|
2015-05-18 22:10:11 +00:00
|
|
|
func (_ *SystemStats) Description() string {
|
2015-08-31 20:03:38 +00:00
|
|
|
return "Read metrics about system load & uptime"
|
2015-05-18 22:10:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (_ *SystemStats) SampleConfig() string { return "" }
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
func (_ *SystemStats) Gather(acc telegraf.Accumulator) error {
|
2016-05-19 14:05:08 +00:00
|
|
|
loadavg, err := load.Avg()
|
2017-08-16 19:05:46 +00:00
|
|
|
if err != nil && !strings.Contains(err.Error(), "not implemented") {
|
2015-08-31 20:03:38 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-04-18 23:55:18 +00:00
|
|
|
fields := map[string]interface{}{
|
|
|
|
"load1": loadavg.Load1,
|
|
|
|
"load5": loadavg.Load5,
|
|
|
|
"load15": loadavg.Load15,
|
|
|
|
"n_cpus": runtime.NumCPU(),
|
2015-04-06 23:03:09 +00:00
|
|
|
}
|
|
|
|
|
2016-03-09 16:22:34 +00:00
|
|
|
users, err := host.Users()
|
2018-04-18 23:55:18 +00:00
|
|
|
if err == nil {
|
|
|
|
fields["n_users"] = len(users)
|
|
|
|
} else if !os.IsPermission(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-04-27 21:55:10 +00:00
|
|
|
now := time.Now()
|
|
|
|
acc.AddGauge("system", fields, nil, now)
|
2018-04-18 23:55:18 +00:00
|
|
|
|
|
|
|
hostinfo, err := host.Info()
|
2016-03-09 16:22:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-08-31 16:27:37 +00:00
|
|
|
acc.AddCounter("system", map[string]interface{}{
|
2017-12-13 19:13:56 +00:00
|
|
|
"uptime": hostinfo.Uptime,
|
2018-04-27 21:55:10 +00:00
|
|
|
}, nil, now)
|
2017-12-13 19:13:56 +00:00
|
|
|
acc.AddFields("system", map[string]interface{}{
|
2015-12-11 20:07:32 +00:00
|
|
|
"uptime_format": format_uptime(hostinfo.Uptime),
|
2018-04-27 21:55:10 +00:00
|
|
|
}, nil, now)
|
2015-05-18 19:15:15 +00:00
|
|
|
|
2015-04-06 16:32:10 +00:00
|
|
|
return nil
|
2015-04-01 16:34:32 +00:00
|
|
|
}
|
|
|
|
|
2015-11-15 20:46:01 +00:00
|
|
|
func format_uptime(uptime uint64) string {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
w := bufio.NewWriter(buf)
|
|
|
|
|
|
|
|
days := uptime / (60 * 60 * 24)
|
|
|
|
|
|
|
|
if days != 0 {
|
|
|
|
s := ""
|
|
|
|
if days > 1 {
|
|
|
|
s = "s"
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "%d day%s, ", days, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
minutes := uptime / 60
|
|
|
|
hours := minutes / 60
|
|
|
|
hours %= 24
|
|
|
|
minutes %= 60
|
|
|
|
|
|
|
|
fmt.Fprintf(w, "%2d:%02d", hours, minutes)
|
|
|
|
|
|
|
|
w.Flush()
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2015-04-01 16:34:32 +00:00
|
|
|
func init() {
|
2016-01-27 21:21:36 +00:00
|
|
|
inputs.Add("system", func() telegraf.Input {
|
2015-08-31 20:03:38 +00:00
|
|
|
return &SystemStats{}
|
2015-04-01 16:34:32 +00:00
|
|
|
})
|
|
|
|
}
|