Skip to content

Commit 3dc5828

Browse files
committed
Add cwd template variable
1 parent eaf09b7 commit 3dc5828

File tree

8 files changed

+45
-2
lines changed

8 files changed

+45
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ Template variables available:
111111
- `{{.Cgroups}}` contains (if supported) the cgroups of the process
112112
(`/proc/self/cgroup`). This is particularly useful for identifying to which container
113113
a process belongs.
114+
- `{{.Cwd}}` contains the current working directory of the process (`/proc/self/cwd`).
114115

115116
Using `PID` or `StartTime` is discouraged: this is almost never what you want,
116117
and is likely to result in high cardinality metrics which Prometheus will have

common.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type (
1111
Cmdline []string
1212
Cgroups []string
1313
Username string
14+
Cwd string
1415
PID int
1516
StartTime time.Time
1617
}

config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ type (
5555
ExeBase string
5656
ExeFull string
5757
Username string
58+
Cwd string
5859
PID int
5960
StartTime time.Time
6061
Matches map[string]string
@@ -124,6 +125,7 @@ func (m *matchNamer) MatchAndName(nacl common.ProcAttributes) (bool, string) {
124125
Username: nacl.Username,
125126
PID: nacl.PID,
126127
StartTime: nacl.StartTime,
128+
Cwd: nacl.Cwd,
127129
})
128130
return true, buf.String()
129131
}

config/config_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,13 @@ process_names:
6464
- comm:
6565
- cat
6666
name: "{{.StartTime}}"
67+
- comm:
68+
- echo
69+
name: "{{.Cwd}}"
6770
`
6871
cfg, err := GetConfig(yml, false)
6972
c.Assert(err, IsNil)
70-
c.Check(cfg.MatchNamers.matchers, HasLen, 3)
73+
c.Check(cfg.MatchNamers.matchers, HasLen, 4)
7174

7275
postgres := common.ProcAttributes{Name: "postmaster", Cmdline: []string{"/usr/bin/postmaster", "-D", "/data/pg"}}
7376
found, name := cfg.MatchNamers.matchers[0].MatchAndName(postgres)
@@ -92,4 +95,13 @@ process_names:
9295
found, name = cfg.MatchNamers.matchers[2].MatchAndName(cat)
9396
c.Check(found, Equals, true)
9497
c.Check(name, Equals, now.String())
98+
99+
echo := common.ProcAttributes{
100+
Name: "echo",
101+
Cmdline: []string{"/bin/echo"},
102+
Cwd: "/",
103+
}
104+
found, name = cfg.MatchNamers.matchers[3].MatchAndName(echo)
105+
c.Check(found, Equals, true)
106+
c.Check(name, Equals, "/")
95107
}

proc/base_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (n namer) MatchAndName(nacl common.ProcAttributes) (bool, string) {
6767

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

7373
func newProc(pid int, name string, m Metrics) IDInfo {

proc/read.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"path/filepath"
77
"strconv"
8+
"strings"
89
"time"
910

1011
"github.com/prometheus/procfs"
@@ -31,6 +32,7 @@ type (
3132
Name string
3233
Cmdline []string
3334
Cgroups []string
35+
Cwd string
3436
ParentPid int
3537
StartTime time.Time
3638
EffectiveUID int
@@ -137,6 +139,7 @@ type (
137139
status *procfs.ProcStatus
138140
cmdline []string
139141
cgroups []procfs.Cgroup
142+
cwd string
140143
io *procfs.ProcIO
141144
fs *FS
142145
wchan *string
@@ -313,6 +316,21 @@ func (p *proccache) getCgroups() ([]procfs.Cgroup, error) {
313316
return p.cgroups, nil
314317
}
315318

319+
func (p *proccache) getCwd() (string, error) {
320+
if p.cwd == "" {
321+
cwd, err := p.Cwd()
322+
323+
if err != nil && strings.Contains(err.Error(), "permission denied") {
324+
cwd = "-"
325+
} else if err != nil {
326+
return "", err
327+
}
328+
p.cwd = cwd
329+
}
330+
331+
return p.cwd, nil
332+
}
333+
316334
// GetProcID implements Proc.
317335
func (p *proccache) GetProcID() (ID, error) {
318336
if p.procid == nil {
@@ -394,10 +412,17 @@ func (p *proccache) GetStatic() (Static, error) {
394412
}
395413
}
396414

415+
// /proc/<pid>/cwd is normally world-readable.
416+
cwd, err := p.getCwd()
417+
if err != nil {
418+
return Static{}, err
419+
}
420+
397421
return Static{
398422
Name: stat.Comm,
399423
Cmdline: cmdline,
400424
Cgroups: cgroupsStr,
425+
Cwd: cwd,
401426
ParentPid: stat.PPID,
402427
StartTime: startTime,
403428
EffectiveUID: int(status.UIDs[1]),

proc/read_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ func TestReadFixture(t *testing.T) {
6464
Name: "process-exporte",
6565
Cmdline: []string{"./process-exporter", "-procnames", "bash"},
6666
Cgroups: []string{"/system.slice/docker-8dde0b0d6e919baef8d635cd9399b22639ed1e400eaec1b1cb94ff3b216cf3c3.scope"},
67+
Cwd: "/tmp",
6768
ParentPid: 10884,
6869
StartTime: stime,
6970
EffectiveUID: 1000,

proc/tracker.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ func (t *Tracker) Update(iter Iter) (CollectErrors, []Update, error) {
432432
Cmdline: idinfo.Cmdline,
433433
Cgroups: idinfo.Cgroups,
434434
Username: t.lookupUid(idinfo.EffectiveUID),
435+
Cwd: idinfo.Cwd,
435436
PID: idinfo.Pid,
436437
StartTime: idinfo.StartTime,
437438
}

0 commit comments

Comments
 (0)