-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add processes status stats in system input plugin
- Loading branch information
1 parent
bd3d0c3
commit dd52b4b
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package system | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/influxdata/telegraf" | ||
"github.com/influxdata/telegraf/plugins/inputs" | ||
"github.com/shirou/gopsutil/process" | ||
) | ||
|
||
type Processes struct { | ||
} | ||
|
||
func (_ *Processes) Description() string { | ||
return "Get the number of processes and group them by status (Linux only)" | ||
} | ||
|
||
func (_ *Processes) SampleConfig() string { return "" } | ||
|
||
func (s *Processes) Gather(acc telegraf.Accumulator) error { | ||
pids, err := process.Pids() | ||
if err != nil { | ||
return fmt.Errorf("error getting pids list: %s", err) | ||
} | ||
// TODO handle other OS (Windows/BSD/Solaris/OSX) | ||
fields := map[string]interface{}{ | ||
"paging": uint64(0), | ||
"blocked": uint64(0), | ||
"zombie": uint64(0), | ||
"stopped": uint64(0), | ||
"running": uint64(0), | ||
"sleeping": uint64(0), | ||
} | ||
for _, pid := range pids { | ||
process, err := process.NewProcess(pid) | ||
if err != nil { | ||
log.Printf("Can not get process %d status: %s", pid, err) | ||
continue | ||
} | ||
status, err := process.Status() | ||
if err != nil { | ||
log.Printf("Can not get process %d status: %s\n", pid, err) | ||
continue | ||
} | ||
_, exists := fields[status] | ||
if !exists { | ||
log.Printf("Status '%s' for process with pid: %d\n", status, pid) | ||
continue | ||
} | ||
fields[status] = fields[status].(uint64) + uint64(1) | ||
} | ||
|
||
acc.AddFields("processes", fields, nil) | ||
return nil | ||
} | ||
func init() { | ||
inputs.Add("processes", func() telegraf.Input { | ||
return &Processes{} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package system | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/influxdata/telegraf/testutil" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestProcesses(t *testing.T) { | ||
processes := &Processes{} | ||
var acc testutil.Accumulator | ||
|
||
err := processes.Gather(&acc) | ||
require.NoError(t, err) | ||
|
||
assert.True(t, acc.HasUIntField("processes", "running")) | ||
assert.True(t, acc.HasUIntField("processes", "sleeping")) | ||
assert.True(t, acc.HasUIntField("processes", "stopped")) | ||
} |