Skip to content

Commit

Permalink
Add cwd template variable
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandreh2ag committed May 6, 2024
1 parent eaf09b7 commit 3dc5828
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ Template variables available:
- `{{.Cgroups}}` contains (if supported) the cgroups of the process
(`/proc/self/cgroup`). This is particularly useful for identifying to which container
a process belongs.
- `{{.Cwd}}` contains the current working directory of the process (`/proc/self/cwd`).

Using `PID` or `StartTime` is discouraged: this is almost never what you want,
and is likely to result in high cardinality metrics which Prometheus will have
Expand Down
1 change: 1 addition & 0 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type (
Cmdline []string
Cgroups []string
Username string
Cwd string
PID int
StartTime time.Time
}
Expand Down
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type (
ExeBase string
ExeFull string
Username string
Cwd string
PID int
StartTime time.Time
Matches map[string]string
Expand Down Expand Up @@ -124,6 +125,7 @@ func (m *matchNamer) MatchAndName(nacl common.ProcAttributes) (bool, string) {
Username: nacl.Username,
PID: nacl.PID,
StartTime: nacl.StartTime,
Cwd: nacl.Cwd,
})
return true, buf.String()
}
Expand Down
14 changes: 13 additions & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,13 @@ process_names:
- comm:
- cat
name: "{{.StartTime}}"
- comm:
- echo
name: "{{.Cwd}}"
`
cfg, err := GetConfig(yml, false)
c.Assert(err, IsNil)
c.Check(cfg.MatchNamers.matchers, HasLen, 3)
c.Check(cfg.MatchNamers.matchers, HasLen, 4)

postgres := common.ProcAttributes{Name: "postmaster", Cmdline: []string{"/usr/bin/postmaster", "-D", "/data/pg"}}
found, name := cfg.MatchNamers.matchers[0].MatchAndName(postgres)
Expand All @@ -92,4 +95,13 @@ process_names:
found, name = cfg.MatchNamers.matchers[2].MatchAndName(cat)
c.Check(found, Equals, true)
c.Check(name, Equals, now.String())

echo := common.ProcAttributes{
Name: "echo",
Cmdline: []string{"/bin/echo"},
Cwd: "/",
}
found, name = cfg.MatchNamers.matchers[3].MatchAndName(echo)
c.Check(found, Equals, true)
c.Check(name, Equals, "/")
}
2 changes: 1 addition & 1 deletion proc/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (n namer) MatchAndName(nacl common.ProcAttributes) (bool, string) {

func newProcIDStatic(pid, ppid int, startTime uint64, name string, cmdline []string) (ID, Static) {
return ID{pid, startTime},
Static{name, cmdline, []string{}, ppid, time.Unix(int64(startTime), 0).UTC(), 1000}
Static{name, cmdline, []string{}, "/", ppid, time.Unix(int64(startTime), 0).UTC(), 1000}
}

func newProc(pid int, name string, m Metrics) IDInfo {
Expand Down
25 changes: 25 additions & 0 deletions proc/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/prometheus/procfs"
Expand All @@ -31,6 +32,7 @@ type (
Name string
Cmdline []string
Cgroups []string
Cwd string
ParentPid int
StartTime time.Time
EffectiveUID int
Expand Down Expand Up @@ -137,6 +139,7 @@ type (
status *procfs.ProcStatus
cmdline []string
cgroups []procfs.Cgroup
cwd string
io *procfs.ProcIO
fs *FS
wchan *string
Expand Down Expand Up @@ -313,6 +316,21 @@ func (p *proccache) getCgroups() ([]procfs.Cgroup, error) {
return p.cgroups, nil
}

func (p *proccache) getCwd() (string, error) {
if p.cwd == "" {
cwd, err := p.Cwd()

if err != nil && strings.Contains(err.Error(), "permission denied") {
cwd = "-"
} else if err != nil {
return "", err
}
p.cwd = cwd
}

return p.cwd, nil
}

// GetProcID implements Proc.
func (p *proccache) GetProcID() (ID, error) {
if p.procid == nil {
Expand Down Expand Up @@ -394,10 +412,17 @@ func (p *proccache) GetStatic() (Static, error) {
}
}

// /proc/<pid>/cwd is normally world-readable.
cwd, err := p.getCwd()
if err != nil {
return Static{}, err
}

return Static{
Name: stat.Comm,
Cmdline: cmdline,
Cgroups: cgroupsStr,
Cwd: cwd,
ParentPid: stat.PPID,
StartTime: startTime,
EffectiveUID: int(status.UIDs[1]),
Expand Down
1 change: 1 addition & 0 deletions proc/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func TestReadFixture(t *testing.T) {
Name: "process-exporte",
Cmdline: []string{"./process-exporter", "-procnames", "bash"},
Cgroups: []string{"/system.slice/docker-8dde0b0d6e919baef8d635cd9399b22639ed1e400eaec1b1cb94ff3b216cf3c3.scope"},
Cwd: "/tmp",
ParentPid: 10884,
StartTime: stime,
EffectiveUID: 1000,
Expand Down
1 change: 1 addition & 0 deletions proc/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ func (t *Tracker) Update(iter Iter) (CollectErrors, []Update, error) {
Cmdline: idinfo.Cmdline,
Cgroups: idinfo.Cgroups,
Username: t.lookupUid(idinfo.EffectiveUID),
Cwd: idinfo.Cwd,
PID: idinfo.Pid,
StartTime: idinfo.StartTime,
}
Expand Down

0 comments on commit 3dc5828

Please sign in to comment.