Skip to content

Commit

Permalink
Merge pull request #694 from kongfei605/procstat_update
Browse files Browse the repository at this point in the history
[procstat] exec and cmdline match support regular expression
  • Loading branch information
kongfei605 committed Oct 23, 2023
2 parents cfe0dd6 + 8b3420e commit 3446c06
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions inputs/procstat/procstat.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"os/exec"
"regexp"
"runtime"
"strings"
"time"
Expand Down Expand Up @@ -32,6 +33,12 @@ type Instance struct {
GatherPerPid bool `toml:"gather_per_pid"`
GatherMoreMetrics []string `toml:"gather_more_metrics"`

SearchExecRegexp string `toml:"search_exec_regexp"`
searchExecRegexp *regexp.Regexp `toml:"-"`

SearchCmdLineRegexp string `toml:"search_cmdline_regexp"`
searchCmdLineRegexp *regexp.Regexp `toml:"-"`

searchString string
solarisMode bool
procs map[PID]Process
Expand All @@ -55,6 +62,14 @@ func (ins *Instance) Init() error {
} else if ins.SearchWinService != "" {
ins.searchString = ins.SearchWinService
log.Println("I! procstat: search_win_service:", ins.SearchWinService)
} else if ins.SearchExecRegexp != "" {
ins.searchExecRegexp = regexp.MustCompile(ins.SearchExecRegexp)
ins.searchString = ins.SearchExecRegexp
log.Println("I! procstat: search_exec_regexp:", ins.SearchExecRegexp)
} else if ins.SearchCmdLineRegexp != "" {
ins.searchCmdLineRegexp = regexp.MustCompile(ins.SearchCmdLineRegexp)
ins.searchString = ins.SearchCmdLineRegexp
log.Println("I! procstat: search_cmdline_regexp:", ins.SearchCmdLineRegexp)
} else {
return errors.New("the fields should not be all blank: search_exec_substring, search_cmdline_substring, search_win_service")
}
Expand Down Expand Up @@ -100,6 +115,24 @@ func UserFilter(username string) Filter {
}
}

func ExecFilter(reg *regexp.Regexp) Filter {
return func(p *process.Process) bool {
if e, _ := p.Exe(); reg.MatchString(e) {
return true
}
return false
}
}

func CmdLineFilter(reg *regexp.Regexp) Filter {
return func(p *process.Process) bool {
if c, _ := p.Cmdline(); reg.MatchString(c) {
return true
}
return false
}
}

func (ins *Instance) Gather(slist *types.SampleList) {
var (
pids []PID
Expand All @@ -111,11 +144,17 @@ func (ins *Instance) Gather(slist *types.SampleList) {
if ins.SearchUser != "" {
opts = append(opts, UserFilter(ins.SearchUser))
}
if ins.searchExecRegexp != nil {
opts = append(opts, ExecFilter(ins.searchExecRegexp))
}
if ins.searchCmdLineRegexp != nil {
opts = append(opts, CmdLineFilter(ins.searchCmdLineRegexp))
}

pg, _ := NewNativeFinder()
if ins.SearchExecSubstring != "" {
if ins.SearchExecSubstring != "" || ins.searchExecRegexp != nil {
pids, err = pg.Pattern(ins.SearchExecSubstring, opts...)
} else if ins.SearchCmdlineSubstring != "" {
} else if ins.SearchCmdlineSubstring != "" || ins.searchCmdLineRegexp != nil {
pids, err = pg.FullPattern(ins.SearchCmdlineSubstring, opts...)
} else if ins.SearchWinService != "" {
pids, err = ins.winServicePIDs()
Expand Down

0 comments on commit 3446c06

Please sign in to comment.