Skip to content

Commit

Permalink
PID and StartTime are now available as templates to use in
Browse files Browse the repository at this point in the history
defining a groupname.
  • Loading branch information
ncabatoff committed Jan 1, 2020
1 parent 83caee9 commit f91ecbb
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 21 deletions.
13 changes: 9 additions & 4 deletions common.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package common

import "fmt"
import (
"fmt"
"time"
)

type (
ProcAttributes struct {
Name string
Cmdline []string
Username string
Name string
Cmdline []string
Username string
PID int
StartTime time.Time
}

MatchNamer interface {
Expand Down
25 changes: 15 additions & 10 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"regexp"
"strings"
"text/template"
"time"

common "github.com/ncabatoff/process-exporter"
"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -53,11 +54,13 @@ type (
}

templateParams struct {
Comm string
ExeBase string
ExeFull string
Username string
Matches map[string]string
Comm string
ExeBase string
ExeFull string
Username string
PID int
StartTime time.Time
Matches map[string]string
}
)

Expand Down Expand Up @@ -117,11 +120,13 @@ func (m *matchNamer) MatchAndName(nacl common.ProcAttributes) (bool, string) {

var buf bytes.Buffer
m.template.Execute(&buf, &templateParams{
Comm: nacl.Name,
ExeBase: exebase,
ExeFull: exefull,
Matches: matches,
Username: nacl.Username,
Comm: nacl.Name,
ExeBase: exebase,
ExeFull: exefull,
Matches: matches,
Username: nacl.Username,
PID: nacl.PID,
StartTime: nacl.StartTime,
})
return true, buf.String()
}
Expand Down
26 changes: 22 additions & 4 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
// "github.com/kylelemons/godebug/pretty"
common "github.com/ncabatoff/process-exporter"
. "gopkg.in/check.v1"
"time"
)

func (s MySuite) TestConfigBasic(c *C) {
Expand Down Expand Up @@ -59,19 +60,36 @@ process_names:
name: "{{.ExeBase}}:{{.Matches.Path}}"
- exe:
- prometheus
name: "{{.ExeFull}}"
name: "{{.ExeFull}}:{{.PID}}"
- comm:
- cat
name: "{{.StartTime}}"
`
cfg, err := GetConfig(yml, false)
c.Assert(err, IsNil)
c.Check(cfg.MatchNamers.matchers, HasLen, 2)
c.Check(cfg.MatchNamers.matchers, HasLen, 3)

postgres := common.ProcAttributes{Name: "postmaster", Cmdline: []string{"/usr/bin/postmaster", "-D", "/data/pg"}}
found, name := cfg.MatchNamers.matchers[0].MatchAndName(postgres)
c.Check(found, Equals, true)
c.Check(name, Equals, "postmaster:pg")

pm := common.ProcAttributes{Name: "prometheus", Cmdline: []string{"/usr/local/bin/prometheus"}}
pm := common.ProcAttributes{
Name: "prometheus",
Cmdline: []string{"/usr/local/bin/prometheus"},
PID: 23,
}
found, name = cfg.MatchNamers.matchers[1].MatchAndName(pm)
c.Check(found, Equals, true)
c.Check(name, Equals, "/usr/local/bin/prometheus")
c.Check(name, Equals, "/usr/local/bin/prometheus:23")

now := time.Now()
cat := common.ProcAttributes{
Name: "cat",
Cmdline: []string{"/bin/cat"},
StartTime: now,
}
found, name = cfg.MatchNamers.matchers[2].MatchAndName(cat)
c.Check(found, Equals, true)
c.Check(name, Equals, now.String())
}
8 changes: 5 additions & 3 deletions proc/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,11 @@ func (t *Tracker) Update(iter Iter) (CollectErrors, []Update, error) {
untracked := make(map[ID]IDInfo)
for _, idinfo := range newProcs {
nacl := common.ProcAttributes{
Name: idinfo.Name,
Cmdline: idinfo.Cmdline,
Username: t.lookupUid(idinfo.EffectiveUID),
Name: idinfo.Name,
Cmdline: idinfo.Cmdline,
Username: t.lookupUid(idinfo.EffectiveUID),
PID: idinfo.Pid,
StartTime: idinfo.StartTime,
}
wanted, gname := t.namer.MatchAndName(nacl)
if wanted {
Expand Down

0 comments on commit f91ecbb

Please # to comment.