2016-03-08 10:42:31 +00:00
|
|
|
// +build !windows
|
|
|
|
|
2016-03-06 06:04:54 +00:00
|
|
|
package system
|
|
|
|
|
|
|
|
import (
|
2016-03-08 10:42:31 +00:00
|
|
|
"bytes"
|
2016-03-06 06:04:54 +00:00
|
|
|
"fmt"
|
2016-03-08 10:42:31 +00:00
|
|
|
"io/ioutil"
|
2016-03-06 06:04:54 +00:00
|
|
|
"log"
|
2016-03-08 10:42:31 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2016-06-03 11:22:08 +00:00
|
|
|
"path/filepath"
|
2016-03-08 10:42:31 +00:00
|
|
|
"runtime"
|
|
|
|
"strconv"
|
2017-05-17 00:33:35 +00:00
|
|
|
"syscall"
|
2016-03-06 06:04:54 +00:00
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Processes struct {
|
2016-03-08 10:42:31 +00:00
|
|
|
execPS func() ([]byte, error)
|
2017-06-23 18:11:33 +00:00
|
|
|
getHostProc func() string
|
2016-06-03 11:22:08 +00:00
|
|
|
readProcFile func(filename string) ([]byte, error)
|
2016-03-08 10:42:31 +00:00
|
|
|
|
|
|
|
forcePS bool
|
|
|
|
forceProc bool
|
2016-03-06 06:04:54 +00:00
|
|
|
}
|
|
|
|
|
2016-03-08 10:42:31 +00:00
|
|
|
func (p *Processes) Description() string {
|
|
|
|
return "Get the number of processes and group them by status"
|
2016-03-06 06:04:54 +00:00
|
|
|
}
|
|
|
|
|
2016-03-08 10:42:31 +00:00
|
|
|
func (p *Processes) SampleConfig() string { return "" }
|
2016-03-06 06:04:54 +00:00
|
|
|
|
2016-03-08 10:42:31 +00:00
|
|
|
func (p *Processes) Gather(acc telegraf.Accumulator) error {
|
|
|
|
// Get an empty map of metric fields
|
|
|
|
fields := getEmptyFields()
|
|
|
|
|
|
|
|
// Decide if we will use 'ps' to get stats (use procfs otherwise)
|
|
|
|
usePS := true
|
|
|
|
if runtime.GOOS == "linux" {
|
|
|
|
usePS = false
|
2016-03-06 06:04:54 +00:00
|
|
|
}
|
2016-03-08 10:42:31 +00:00
|
|
|
if p.forcePS {
|
|
|
|
usePS = true
|
|
|
|
} else if p.forceProc {
|
|
|
|
usePS = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gather stats from 'ps' or procfs
|
|
|
|
if usePS {
|
|
|
|
if err := p.gatherFromPS(fields); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := p.gatherFromProc(fields); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-31 16:27:37 +00:00
|
|
|
acc.AddGauge("processes", fields, nil)
|
2016-03-08 10:42:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-23 18:11:33 +00:00
|
|
|
func GetHostProc() string {
|
|
|
|
procPath := "/proc"
|
|
|
|
if os.Getenv("HOST_PROC") != "" {
|
|
|
|
procPath = os.Getenv("HOST_PROC")
|
|
|
|
}
|
|
|
|
return procPath
|
|
|
|
}
|
|
|
|
|
2016-03-08 10:42:31 +00:00
|
|
|
// Gets empty fields of metrics based on the OS
|
|
|
|
func getEmptyFields() map[string]interface{} {
|
2016-03-06 06:04:54 +00:00
|
|
|
fields := map[string]interface{}{
|
2016-03-08 10:42:31 +00:00
|
|
|
"blocked": int64(0),
|
2016-03-09 21:55:26 +00:00
|
|
|
"zombies": int64(0),
|
2016-03-08 10:42:31 +00:00
|
|
|
"stopped": int64(0),
|
|
|
|
"running": int64(0),
|
|
|
|
"sleeping": int64(0),
|
|
|
|
"total": int64(0),
|
2016-05-04 23:52:47 +00:00
|
|
|
"unknown": int64(0),
|
2016-03-08 10:42:31 +00:00
|
|
|
}
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "freebsd":
|
|
|
|
fields["idle"] = int64(0)
|
|
|
|
fields["wait"] = int64(0)
|
|
|
|
case "darwin":
|
|
|
|
fields["idle"] = int64(0)
|
|
|
|
case "openbsd":
|
|
|
|
fields["idle"] = int64(0)
|
|
|
|
case "linux":
|
2017-03-09 19:28:54 +00:00
|
|
|
fields["dead"] = int64(0)
|
2016-03-08 10:42:31 +00:00
|
|
|
fields["paging"] = int64(0)
|
|
|
|
fields["total_threads"] = int64(0)
|
|
|
|
}
|
|
|
|
return fields
|
|
|
|
}
|
|
|
|
|
|
|
|
// exec `ps` to get all process states
|
|
|
|
func (p *Processes) gatherFromPS(fields map[string]interface{}) error {
|
|
|
|
out, err := p.execPS()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, status := range bytes.Fields(out) {
|
|
|
|
if i == 0 && string(status) == "STAT" {
|
|
|
|
// This is a header, skip it
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch status[0] {
|
|
|
|
case 'W':
|
|
|
|
fields["wait"] = fields["wait"].(int64) + int64(1)
|
|
|
|
case 'U', 'D', 'L':
|
|
|
|
// Also known as uninterruptible sleep or disk sleep
|
|
|
|
fields["blocked"] = fields["blocked"].(int64) + int64(1)
|
|
|
|
case 'Z':
|
2016-03-09 21:55:26 +00:00
|
|
|
fields["zombies"] = fields["zombies"].(int64) + int64(1)
|
2017-03-09 19:28:54 +00:00
|
|
|
case 'X':
|
|
|
|
fields["dead"] = fields["dead"].(int64) + int64(1)
|
2016-03-08 10:42:31 +00:00
|
|
|
case 'T':
|
|
|
|
fields["stopped"] = fields["stopped"].(int64) + int64(1)
|
|
|
|
case 'R':
|
|
|
|
fields["running"] = fields["running"].(int64) + int64(1)
|
|
|
|
case 'S':
|
|
|
|
fields["sleeping"] = fields["sleeping"].(int64) + int64(1)
|
|
|
|
case 'I':
|
|
|
|
fields["idle"] = fields["idle"].(int64) + int64(1)
|
2016-05-04 23:52:47 +00:00
|
|
|
case '?':
|
|
|
|
fields["unknown"] = fields["unknown"].(int64) + int64(1)
|
2016-03-08 10:42:31 +00:00
|
|
|
default:
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Printf("I! processes: Unknown state [ %s ] from ps",
|
2016-03-08 10:42:31 +00:00
|
|
|
string(status[0]))
|
|
|
|
}
|
|
|
|
fields["total"] = fields["total"].(int64) + int64(1)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// get process states from /proc/(pid)/stat files
|
|
|
|
func (p *Processes) gatherFromProc(fields map[string]interface{}) error {
|
2017-06-23 18:11:33 +00:00
|
|
|
filenames, err := filepath.Glob(GetHostProc() + "/[0-9]*/stat")
|
|
|
|
|
2016-03-08 10:42:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-06-03 11:22:08 +00:00
|
|
|
for _, filename := range filenames {
|
|
|
|
_, err := os.Stat(filename)
|
|
|
|
data, err := p.readProcFile(filename)
|
2016-03-06 06:04:54 +00:00
|
|
|
if err != nil {
|
2016-03-08 10:42:31 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if data == nil {
|
2016-03-06 06:04:54 +00:00
|
|
|
continue
|
|
|
|
}
|
2016-03-08 10:42:31 +00:00
|
|
|
|
2016-04-05 16:21:57 +00:00
|
|
|
// Parse out data after (<cmd name>)
|
|
|
|
i := bytes.LastIndex(data, []byte(")"))
|
|
|
|
if i == -1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
data = data[i+2:]
|
|
|
|
|
2016-03-08 10:42:31 +00:00
|
|
|
stats := bytes.Fields(data)
|
|
|
|
if len(stats) < 3 {
|
2016-06-03 11:22:08 +00:00
|
|
|
return fmt.Errorf("Something is terribly wrong with %s", filename)
|
2016-03-08 10:42:31 +00:00
|
|
|
}
|
2016-04-05 16:21:57 +00:00
|
|
|
switch stats[0][0] {
|
2016-03-08 10:42:31 +00:00
|
|
|
case 'R':
|
|
|
|
fields["running"] = fields["running"].(int64) + int64(1)
|
|
|
|
case 'S':
|
|
|
|
fields["sleeping"] = fields["sleeping"].(int64) + int64(1)
|
|
|
|
case 'D':
|
|
|
|
fields["blocked"] = fields["blocked"].(int64) + int64(1)
|
|
|
|
case 'Z':
|
|
|
|
fields["zombies"] = fields["zombies"].(int64) + int64(1)
|
2017-03-09 19:28:54 +00:00
|
|
|
case 'X':
|
|
|
|
fields["dead"] = fields["dead"].(int64) + int64(1)
|
2016-03-08 10:42:31 +00:00
|
|
|
case 'T', 't':
|
|
|
|
fields["stopped"] = fields["stopped"].(int64) + int64(1)
|
|
|
|
case 'W':
|
|
|
|
fields["paging"] = fields["paging"].(int64) + int64(1)
|
|
|
|
default:
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Printf("I! processes: Unknown state [ %s ] in file %s",
|
2016-06-03 11:22:08 +00:00
|
|
|
string(stats[0][0]), filename)
|
2016-03-08 10:42:31 +00:00
|
|
|
}
|
|
|
|
fields["total"] = fields["total"].(int64) + int64(1)
|
|
|
|
|
2016-04-05 16:21:57 +00:00
|
|
|
threads, err := strconv.Atoi(string(stats[17]))
|
2016-03-08 10:42:31 +00:00
|
|
|
if err != nil {
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Printf("I! processes: Error parsing thread count: %s", err)
|
2016-03-06 06:04:54 +00:00
|
|
|
continue
|
|
|
|
}
|
2016-03-08 10:42:31 +00:00
|
|
|
fields["total_threads"] = fields["total_threads"].(int64) + int64(threads)
|
2016-03-06 06:04:54 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-03-08 10:42:31 +00:00
|
|
|
|
2016-06-03 11:22:08 +00:00
|
|
|
func readProcFile(filename string) ([]byte, error) {
|
|
|
|
data, err := ioutil.ReadFile(filename)
|
2016-03-08 10:42:31 +00:00
|
|
|
if err != nil {
|
2016-06-03 11:22:08 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2017-05-17 00:33:35 +00:00
|
|
|
|
|
|
|
// Reading from /proc/<PID> fails with ESRCH if the process has
|
|
|
|
// been terminated between open() and read().
|
|
|
|
if perr, ok := err.(*os.PathError); ok && perr.Err == syscall.ESRCH {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-03-08 10:42:31 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func execPS() ([]byte, error) {
|
|
|
|
bin, err := exec.LookPath("ps")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := exec.Command(bin, "axo", "state").Output()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, err
|
|
|
|
}
|
|
|
|
|
2016-03-06 06:04:54 +00:00
|
|
|
func init() {
|
|
|
|
inputs.Add("processes", func() telegraf.Input {
|
2016-03-08 10:42:31 +00:00
|
|
|
return &Processes{
|
|
|
|
execPS: execPS,
|
|
|
|
readProcFile: readProcFile,
|
|
|
|
}
|
2016-03-06 06:04:54 +00:00
|
|
|
})
|
|
|
|
}
|