-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
procstats patterns without pgrep #3559
Merged
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
9d4c282
remove pgrep for pattern matches, use go-ps instead
a54fd8d
add dep
c47eaef
remove debug
a2f429b
working implementation using the new gopsutil
vrecan 4b26b52
windows files and build changes for reg pgrep
vrecan 8b66be9
update the make file to test the new windows tests
vrecan 54ffbd2
update gopsutil with new wmi queries
vrecan 62285d9
Merge branch 'patterns_no_pgrep' of github.com:vrecan/telegraf into p…
0722c4a
add docs
aff075b
doc cleanup
e402310
move things around in the doc
d85d89b
more cleanup on the docs
56a0296
make doc a little more clear
452f496
revert godeps to old version of gopsutil and implement it directly
vrecan e1e32ef
Merge branch 'patterns_no_pgrep' of https://github.com/vrecan/telegra…
vrecan 93fc937
remove extra dep
vrecan e2f374c
revert change to pgrep
vrecan 37c6416
windows version of nativeFinder is working
vrecan e97d339
begin unix native finder
vrecan ed23fe3
update comments
vrecan 8f008d3
fully working end to end
vrecan aab50e7
use native Name function instead, much better performance and less
vrecan 78d4544
address comments
vrecan 50fd95a
add continue
vrecan 690ef25
change cgroup test to skip on windows
vrecan 0ecb670
move to single quotes
vrecan f115e18
add missing file
vrecan 797ec89
Merge branch 'master' of https://github.com/influxdata/telegraf into …
vrecan fc0f21c
Merge branch 'master' of https://github.com/influxdata/telegraf into …
vrecan 5a4c56e
address comments, rename files and general cleanup
vrecan 80c07ae
update name for not_windows
vrecan 20470f8
add missing file
vrecan d75e3db
rename again to see if that fixes the test
vrecan 529fd53
Merge branch 'master' of https://github.com/influxdata/telegraf into …
vrecan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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,57 @@ | ||
package procstat | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/shirou/gopsutil/process" | ||
) | ||
|
||
//NativeFinder uses gopsutil to find processes | ||
type NativeFinder struct { | ||
} | ||
|
||
//NewNativeFinder ... | ||
func NewNativeFinder() (PIDFinder, error) { | ||
return &NativeFinder{}, nil | ||
} | ||
|
||
//Uid will return all pids for the given user | ||
func (pg *NativeFinder) Uid(user string) ([]PID, error) { | ||
var dst []PID | ||
procs, err := process.Processes() | ||
if err != nil { | ||
return dst, err | ||
} | ||
for _, p := range procs { | ||
username, err := p.Username() | ||
if err != nil { | ||
//skip, this can happen if we don't have permissions or | ||
//the pid no longer exists | ||
continue | ||
} | ||
if username == user { | ||
dst = append(dst, PID(p.Pid)) | ||
} | ||
} | ||
return dst, nil | ||
} | ||
|
||
//PidFile returns the pid from the pid file given. | ||
func (pg *NativeFinder) PidFile(path string) ([]PID, error) { | ||
var pids []PID | ||
pidString, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return pids, fmt.Errorf("Failed to read pidfile '%s'. Error: '%s'", | ||
path, err) | ||
} | ||
pid, err := strconv.Atoi(strings.TrimSpace(string(pidString))) | ||
if err != nil { | ||
return pids, err | ||
} | ||
pids = append(pids, PID(pid)) | ||
return pids, nil | ||
|
||
} |
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,59 @@ | ||
// +build !windows | ||
|
||
package procstat | ||
|
||
import ( | ||
"regexp" | ||
|
||
"github.com/shirou/gopsutil/process" | ||
) | ||
|
||
//Pattern matches on the process name | ||
func (pg *NativeFinder) Pattern(pattern string) ([]PID, error) { | ||
var pids []PID | ||
regxPattern, err := regexp.Compile(pattern) | ||
if err != nil { | ||
return pids, err | ||
} | ||
procs, err := process.Processes() | ||
if err != nil { | ||
return pids, err | ||
} | ||
for _, p := range procs { | ||
name, err := p.Exe() | ||
if err != nil { | ||
//skip, this can be caused by the pid no longer existing | ||
//or you having no permissions to access it | ||
continue | ||
} | ||
if regxPattern.MatchString(name) { | ||
pids = append(pids, PID(p.Pid)) | ||
} | ||
} | ||
return pids, err | ||
} | ||
|
||
//FullPattern matches on the command line when the proccess was executed | ||
func (pg *NativeFinder) FullPattern(pattern string) ([]PID, error) { | ||
var pids []PID | ||
regxPattern, err := regexp.Compile(pattern) | ||
if err != nil { | ||
return pids, err | ||
} | ||
procs, err := process.Processes() | ||
if err != nil { | ||
return pids, err | ||
} | ||
for _, p := range procs { | ||
cmd, err := p.Cmdline() | ||
if err != nil { | ||
//skip, this can be caused by the pid no longer existing | ||
//or you having no permissions to access it | ||
continue | ||
} | ||
if regxPattern.MatchString(cmd) { | ||
pids = append(pids, PID(p.Pid)) | ||
} | ||
} | ||
return pids, err | ||
} |
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,91 @@ | ||
package procstat | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"regexp" | ||
"time" | ||
|
||
"github.com/StackExchange/wmi" | ||
"github.com/shirou/gopsutil/process" | ||
) | ||
|
||
//Timeout is the timeout used when making wmi calls | ||
var Timeout = 5 * time.Second | ||
|
||
type queryType string | ||
|
||
const ( | ||
like = queryType("LIKE") | ||
equals = queryType("=") | ||
notEqual = queryType("!=") | ||
) | ||
|
||
//Pattern matches on the process name | ||
func (pg *NativeFinder) Pattern(pattern string) ([]PID, error) { | ||
var pids []PID | ||
regxPattern, err := regexp.Compile(pattern) | ||
if err != nil { | ||
return pids, err | ||
} | ||
procs, err := process.Processes() | ||
if err != nil { | ||
return pids, err | ||
} | ||
for _, p := range procs { | ||
name, err := p.Name() | ||
if err != nil { | ||
//skip, this can be caused by the pid no longer existing | ||
//or you having no permissions to access it | ||
continue | ||
} | ||
if regxPattern.MatchString(name) { | ||
pids = append(pids, PID(p.Pid)) | ||
} | ||
} | ||
return pids, err | ||
} | ||
|
||
//FullPattern matches the cmdLine on windows and will find a pattern using a WMI like query | ||
func (pg *NativeFinder) FullPattern(pattern string) ([]PID, error) { | ||
var pids []PID | ||
procs, err := getWin32ProcsByVariable("CommandLine", like, pattern, Timeout) | ||
if err != nil { | ||
return pids, err | ||
} | ||
for _, p := range procs { | ||
pids = append(pids, PID(p.ProcessID)) | ||
} | ||
return pids, nil | ||
} | ||
|
||
//GetWin32ProcsByVariable allows you to query any variable with a like query | ||
func getWin32ProcsByVariable(variable string, qType queryType, value string, timeout time.Duration) ([]process.Win32_Process, error) { | ||
var dst []process.Win32_Process | ||
var query string | ||
// should look like "WHERE CommandLine LIKE "procstat" | ||
query = fmt.Sprintf("WHERE %s %s %q", variable, qType, value) | ||
q := wmi.CreateQuery(&dst, query) | ||
ctx, cancel := context.WithTimeout(context.Background(), timeout) | ||
defer cancel() | ||
err := WMIQueryWithContext(ctx, q, &dst) | ||
if err != nil { | ||
return []process.Win32_Process{}, fmt.Errorf("could not get win32Proc: %s", err) | ||
} | ||
return dst, nil | ||
} | ||
|
||
// WMIQueryWithContext - wraps wmi.Query with a timed-out context to avoid hanging | ||
func WMIQueryWithContext(ctx context.Context, query string, dst interface{}, connectServerArgs ...interface{}) error { | ||
errChan := make(chan error, 1) | ||
go func() { | ||
errChan <- wmi.Query(query, dst, connectServerArgs...) | ||
}() | ||
|
||
select { | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
case err := <-errChan: | ||
return err | ||
} | ||
} |
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,40 @@ | ||
package procstat | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"os/user" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGather_RealPattern(t *testing.T) { | ||
pg, err := NewNativeFinder() | ||
require.NoError(t, err) | ||
pids, err := pg.Pattern(`procstat`) | ||
require.NoError(t, err) | ||
fmt.Println(pids) | ||
assert.Equal(t, len(pids) > 0, true) | ||
} | ||
|
||
func TestGather_RealFullPattern(t *testing.T) { | ||
pg, err := NewNativeFinder() | ||
require.NoError(t, err) | ||
pids, err := pg.FullPattern(`%procstat%`) | ||
require.NoError(t, err) | ||
fmt.Println(pids) | ||
assert.Equal(t, len(pids) > 0, true) | ||
} | ||
|
||
func TestGather_RealUser(t *testing.T) { | ||
user, err := user.Current() | ||
require.NoError(t, err) | ||
pg, err := NewNativeFinder() | ||
require.NoError(t, err) | ||
pids, err := pg.Uid(user.Username) | ||
require.NoError(t, err) | ||
fmt.Println(pids) | ||
assert.Equal(t, len(pids) > 0, true) | ||
} |
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
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will have to remember to take a second pass at this documentation, so that it matches the current design