-
Notifications
You must be signed in to change notification settings - Fork 70
/
regex_process_match_finder.go
82 lines (63 loc) · 2.17 KB
/
regex_process_match_finder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package recipes
import (
"context"
"regexp"
log "github.com/sirupsen/logrus"
"github.com/newrelic/newrelic-cli/internal/install/types"
)
type RegexProcessMatchFinder struct{}
func NewRegexProcessMatchFinder() *RegexProcessMatchFinder {
f := RegexProcessMatchFinder{}
return &f
}
func (f *RegexProcessMatchFinder) FindMatches(ctx context.Context, processes []types.GenericProcess, recipe types.OpenInstallationRecipe) []types.MatchedProcess {
matches := []types.MatchedProcess{}
for _, p := range processes {
m := f.findMatches(recipe, p)
matches = append(matches, m...)
}
if len(matches) > 0 {
log.Debugf("Finished matching recipe %s to running processes, found %d matches.", recipe.Name, len(matches))
}
return matches
}
func (f *RegexProcessMatchFinder) FindMatchesMultiple(ctx context.Context, processes []types.GenericProcess, recipes []types.OpenInstallationRecipe) []types.MatchedProcess {
matches := []types.MatchedProcess{}
log.Debugf("Filtering recipes with %d processes...", len(processes))
for _, r := range recipes {
m := f.FindMatches(ctx, processes, r)
matches = append(matches, m...)
}
if len(matches) > 0 {
log.Debugf("Filtering recipes with processes done, found %d matches.", len(matches))
}
return matches
}
func (f *RegexProcessMatchFinder) findMatches(r types.OpenInstallationRecipe, process types.GenericProcess) []types.MatchedProcess {
matches := []types.MatchedProcess{}
var newrelicInstallRegex = regexp.MustCompile(`(?i).newrelic(|\.exe['"]?) install.`)
for _, pattern := range r.ProcessMatch {
cmd, err := process.Cmd()
if err != nil {
// Process no longer exist, skip
continue
}
if len(newrelicInstallRegex.FindString(cmd)) > 0 {
continue
}
matched, err := regexp.Match(pattern, []byte(cmd))
if err != nil {
log.Tracef("could not execute pattern %s against process invocation %s, %s", pattern, cmd, err)
continue
}
if matched {
mp := &types.MatchedProcess{}
mp.GenericProcess = process
mp.MatchingPattern = pattern
mp.MatchingRecipe = r
log.Tracef("Process matching pattern %s with %s for recipe %s.", pattern, cmd, r.DisplayName)
matches = append(matches, *mp)
}
}
return matches
}