diff --git a/common.go b/common.go index c8502f1..e818cd1 100644 --- a/common.go +++ b/common.go @@ -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 { diff --git a/config/config.go b/config/config.go index cc49977..7ee3cbd 100644 --- a/config/config.go +++ b/config/config.go @@ -9,6 +9,7 @@ import ( "regexp" "strings" "text/template" + "time" common "github.com/ncabatoff/process-exporter" "gopkg.in/yaml.v2" @@ -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 } ) @@ -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() } diff --git a/config/config_test.go b/config/config_test.go index ef207bc..138538c 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -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) { @@ -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()) } diff --git a/proc/tracker.go b/proc/tracker.go index da8b7d5..bfd89c6 100644 --- a/proc/tracker.go +++ b/proc/tracker.go @@ -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 {