Emit parent PID, commandline regexes and parent commandline regexes as tags in procstat
This commit is contained in:
parent
f62c493c77
commit
7659b08a40
|
@ -15,12 +15,14 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Procstat struct {
|
type Procstat struct {
|
||||||
PidFile string `toml:"pid_file"`
|
PidFile string `toml:"pid_file"`
|
||||||
Exe string
|
Exe string
|
||||||
Pattern string
|
Pattern string
|
||||||
Prefix string
|
Prefix string
|
||||||
ProcessName string
|
ProcessName string
|
||||||
User string
|
CmdlineRegex string
|
||||||
|
ParentCmdlineRegex string
|
||||||
|
User string
|
||||||
|
|
||||||
// pidmap maps a pid to a process object, so we don't recreate every gather
|
// pidmap maps a pid to a process object, so we don't recreate every gather
|
||||||
pidmap map[int32]*process.Process
|
pidmap map[int32]*process.Process
|
||||||
|
@ -70,7 +72,7 @@ func (p *Procstat) Gather(acc telegraf.Accumulator) error {
|
||||||
p.Exe, p.PidFile, p.Pattern, p.User, err.Error())
|
p.Exe, p.PidFile, p.Pattern, p.User, err.Error())
|
||||||
} else {
|
} else {
|
||||||
for pid, proc := range p.pidmap {
|
for pid, proc := range p.pidmap {
|
||||||
p := NewSpecProcessor(p.ProcessName, p.Prefix, acc, proc, p.tagmap[pid])
|
p := NewSpecProcessor(p.ProcessName, p.Prefix, p.CmdlineRegex, p.ParentCmdlineRegex, acc, proc, p.tagmap[pid])
|
||||||
p.pushMetrics()
|
p.pushMetrics()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,8 @@ package procstat
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/shirou/gopsutil/process"
|
"github.com/shirou/gopsutil/process"
|
||||||
|
|
||||||
|
@ -19,6 +21,8 @@ type SpecProcessor struct {
|
||||||
func NewSpecProcessor(
|
func NewSpecProcessor(
|
||||||
processName string,
|
processName string,
|
||||||
prefix string,
|
prefix string,
|
||||||
|
cmdlineRegex string,
|
||||||
|
parentCmdlineRegex string,
|
||||||
acc telegraf.Accumulator,
|
acc telegraf.Accumulator,
|
||||||
p *process.Process,
|
p *process.Process,
|
||||||
tags map[string]string,
|
tags map[string]string,
|
||||||
|
@ -31,6 +35,33 @@ func NewSpecProcessor(
|
||||||
tags["process_name"] = name
|
tags["process_name"] = name
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if cmdlineRegex != "" {
|
||||||
|
val := CmdlineSubstrings(p, &cmdlineRegex)
|
||||||
|
for i, value := range val {
|
||||||
|
tags["cmdline." + strconv.Itoa(i)] = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ppid, err := p.Ppid()
|
||||||
|
if err == nil {
|
||||||
|
tags["ppid"] = strconv.Itoa(int(ppid))
|
||||||
|
|
||||||
|
pp, err := process.NewProcess(ppid)
|
||||||
|
if err == nil {
|
||||||
|
parentName, err := pp.Name()
|
||||||
|
if err == nil {
|
||||||
|
tags["parent_name"] = parentName
|
||||||
|
}
|
||||||
|
if parentCmdlineRegex != "" {
|
||||||
|
val := CmdlineSubstrings(pp, &parentCmdlineRegex)
|
||||||
|
for i, value := range val {
|
||||||
|
tags["pcmdline." + strconv.Itoa(i)] = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return &SpecProcessor{
|
return &SpecProcessor{
|
||||||
Prefix: prefix,
|
Prefix: prefix,
|
||||||
tags: tags,
|
tags: tags,
|
||||||
|
@ -40,6 +71,20 @@ func NewSpecProcessor(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CmdlineSubstrings(p *process.Process, regex *string) []string {
|
||||||
|
cmdline, err := p.Cmdline()
|
||||||
|
if err == nil {
|
||||||
|
re, err := regexp.Compile(*regex)
|
||||||
|
if err == nil {
|
||||||
|
cmdlineSubmatch := re.FindStringSubmatch(cmdline)
|
||||||
|
if cmdlineSubmatch != nil {
|
||||||
|
return cmdlineSubmatch[1:]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (p *SpecProcessor) pushMetrics() {
|
func (p *SpecProcessor) pushMetrics() {
|
||||||
var prefix string
|
var prefix string
|
||||||
if p.Prefix != "" {
|
if p.Prefix != "" {
|
||||||
|
|
Loading…
Reference in New Issue