Skip to content

Commit

Permalink
Add cmdline tag to procstat input (influxdata#5681)
Browse files Browse the repository at this point in the history
  • Loading branch information
scottprichard authored and Mathieu Lecarme committed Apr 17, 2020
1 parent e8275ac commit 6f1ab1e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions plugins/inputs/procstat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ Processes can be selected for monitoring using one of several methods:
## Field name prefix
# prefix = ""

## When true add the full cmdline as a tag.
# cmdline_tag = false

## Add PID as a tag instead of a field; useful to differentiate between
## processes whose tags are otherwise the same. Can create a large number
## of series, use judiciously.
Expand Down Expand Up @@ -72,6 +75,7 @@ implemented as a WMI query. The pattern allows fuzzy matching using only
- procstat
- tags:
- pid (when `pid_tag` is true)
- cmdline (when 'cmdline_tag' is true)
- process_name
- pidfile (when defined)
- exe (when defined)
Expand Down
1 change: 1 addition & 0 deletions plugins/inputs/procstat/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Process interface {
IOCounters() (*process.IOCountersStat, error)
MemoryInfo() (*process.MemoryInfoStat, error)
Name() (string, error)
Cmdline() (string, error)
NumCtxSwitches() (*process.NumCtxSwitchesStat, error)
NumFDs() (int32, error)
NumThreads() (int32, error)
Expand Down
14 changes: 14 additions & 0 deletions plugins/inputs/procstat/procstat.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Procstat struct {
Exe string
Pattern string
Prefix string
CmdLineTag bool `toml:"cmdline_tag"`
ProcessName string
User string
SystemdUnit string
Expand Down Expand Up @@ -65,6 +66,9 @@ var sampleConfig = `
## Field name prefix
# prefix = ""
## When true add the full cmdline as a tag.
# cmdline_tag = false
## Add PID as a tag instead of a field; useful to differentiate between
## processes whose tags are otherwise the same. Can create a large number
## of series, use judiciously.
Expand Down Expand Up @@ -170,6 +174,16 @@ func (p *Procstat) addMetric(proc Process, acc telegraf.Accumulator) {
fields["pid"] = int32(proc.PID())
}

//If cmd_line tag is true and it is not already set add cmdline as a tag
if p.CmdLineTag {
if _, ok := proc.Tags()["cmdline"]; !ok {
Cmdline, err := proc.Cmdline()
if err == nil {
proc.Tags()["cmdline"] = Cmdline
}
}
}

numThreads, err := proc.NumThreads()
if err == nil {
fields[prefix+"num_threads"] = numThreads
Expand Down
4 changes: 4 additions & 0 deletions plugins/inputs/procstat/procstat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ func (pg *testPgrep) PidFile(path string) ([]PID, error) {
return pg.pids, pg.err
}

func (p *testProc) Cmdline() (string, error) {
return "test_proc", nil
}

func (pg *testPgrep) Pattern(pattern string) ([]PID, error) {
return pg.pids, pg.err
}
Expand Down

0 comments on commit 6f1ab1e

Please sign in to comment.