Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#138 Add cwd template variable #300

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, "/")
}
1 change: 1 addition & 0 deletions fixtures/14804/cwd
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
26 changes: 26 additions & 0 deletions proc/read.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package proc

import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -31,6 +33,7 @@ type (
Name string
Cmdline []string
Cgroups []string
Cwd string
ParentPid int
StartTime time.Time
EffectiveUID int
Expand Down Expand Up @@ -137,6 +140,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 +317,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 && errors.Is(err, fs.ErrPermission) {
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 +413,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