Skip to content

Commit

Permalink
Add node_filesystem_mount_info metric
Browse files Browse the repository at this point in the history
Fixes: prometheus#1384

Signed-off-by: Miguel Oliveira <[email protected]>
  • Loading branch information
migeyel committed Mar 21, 2024
1 parent b3bbd1f commit 91628de
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 81 deletions.
15 changes: 14 additions & 1 deletion collector/filesystem_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ type filesystemCollector struct {
sizeDesc, freeDesc, availDesc *prometheus.Desc
filesDesc, filesFreeDesc *prometheus.Desc
roDesc, deviceErrorDesc *prometheus.Desc
mountInfoDesc *prometheus.Desc
logger log.Logger
}

type filesystemLabels struct {
device, mountPoint, fsType, options, deviceError string
device, mountPoint, fsType, options, deviceError, major, minor string
}

type filesystemStats struct {
Expand Down Expand Up @@ -155,6 +156,13 @@ func NewFilesystemCollector(logger log.Logger) (Collector, error) {
filesystemLabelNames, nil,
)

mountInfoDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "mount_info"),
"Filesystem mount information.",
[]string{"device", "major", "minor", "mountpoint"},
nil,
)

return &filesystemCollector{
excludedMountPointsPattern: mountPointPattern,
excludedFSTypesPattern: filesystemsTypesPattern,
Expand All @@ -165,6 +173,7 @@ func NewFilesystemCollector(logger log.Logger) (Collector, error) {
filesFreeDesc: filesFreeDesc,
roDesc: roDesc,
deviceErrorDesc: deviceErrorDesc,
mountInfoDesc: mountInfoDesc,
logger: logger,
}, nil
}
Expand Down Expand Up @@ -215,6 +224,10 @@ func (c *filesystemCollector) Update(ch chan<- prometheus.Metric) error {
c.filesFreeDesc, prometheus.GaugeValue,
s.filesFree, s.labels.device, s.labels.mountPoint, s.labels.fsType, s.labels.deviceError,
)
ch <- prometheus.MustNewConstMetric(
c.mountInfoDesc, prometheus.GaugeValue,
1.0, s.labels.device, s.labels.major, s.labels.minor, s.labels.mountPoint,
)
}
return nil
}
35 changes: 24 additions & 11 deletions collector/filesystem_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,11 @@ func stuckMountWatcher(mountPoint string, success chan struct{}, logger log.Logg
}

func mountPointDetails(logger log.Logger) ([]filesystemLabels, error) {
file, err := os.Open(procFilePath("1/mounts"))
file, err := os.Open(procFilePath("1/mountinfo"))
if errors.Is(err, os.ErrNotExist) {
// Fallback to `/proc/mounts` if `/proc/1/mounts` is missing due hidepid.
level.Debug(logger).Log("msg", "Reading root mounts failed, falling back to system mounts", "err", err)
file, err = os.Open(procFilePath("mounts"))
// Fallback to `/proc/self/mountinfo` if `/proc/1/mountinfo` is missing due hidepid.
level.Debug(logger).Log("msg", "Reading root mounts failed, falling back to self mounts", "err", err)
file, err = os.Open(procFilePath("self/mountinfo"))
}
if err != nil {
return nil, err
Expand All @@ -199,20 +199,33 @@ func parseFilesystemLabels(r io.Reader) ([]filesystemLabels, error) {
for scanner.Scan() {
parts := strings.Fields(scanner.Text())

if len(parts) < 4 {
if len(parts) < 10 {
return nil, fmt.Errorf("malformed mount point information: %q", scanner.Text())
}

major, minor := 0, 0
_, err := fmt.Sscanf(parts[2], "%d:%d", &major, &minor)
if err != nil {
return nil, fmt.Errorf("malformed mount point information: %q", scanner.Text())
}

m := 5
for parts[m+1] != "-" {
m++
}

// Ensure we handle the translation of \040 and \011
// as per fstab(5).
parts[1] = strings.Replace(parts[1], "\\040", " ", -1)
parts[1] = strings.Replace(parts[1], "\\011", "\t", -1)
parts[4] = strings.Replace(parts[4], "\\040", " ", -1)
parts[4] = strings.Replace(parts[4], "\\011", "\t", -1)

filesystems = append(filesystems, filesystemLabels{
device: parts[0],
mountPoint: rootfsStripPrefix(parts[1]),
fsType: parts[2],
options: parts[3],
device: parts[m+3],
mountPoint: rootfsStripPrefix(parts[4]),
fsType: parts[m+2],
options: parts[5],
major: fmt.Sprint(major),
minor: fmt.Sprint(minor),
deviceError: "",
})
}
Expand Down
53 changes: 23 additions & 30 deletions collector/filesystem_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,36 +50,25 @@ func TestMountPointDetails(t *testing.T) {
}

expected := map[string]string{
"/": "",
"/sys": "",
"/proc": "",
"/dev": "",
"/dev/pts": "",
"/run": "",
"/sys/kernel/security": "",
"/dev/shm": "",
"/run/lock": "",
"/sys/fs/cgroup": "",
"/sys/fs/cgroup/systemd": "",
"/sys/fs/pstore": "",
"/sys/fs/cgroup/cpuset": "",
"/sys/fs/cgroup/cpu,cpuacct": "",
"/sys/fs/cgroup/devices": "",
"/sys/fs/cgroup/freezer": "",
"/sys/fs/cgroup/net_cls,net_prio": "",
"/sys/fs/cgroup/blkio": "",
"/sys/fs/cgroup/perf_event": "",
"/proc/sys/fs/binfmt_misc": "",
"/dev/mqueue": "",
"/sys/kernel/debug": "",
"/dev/hugepages": "",
"/sys/fs/fuse/connections": "",
"/boot": "",
"/run/rpc_pipefs": "",
"/run/user/1000": "",
"/run/user/1000/gvfs": "",
"/var/lib/kubelet/plugins/kubernetes.io/vsphere-volume/mounts/[vsanDatastore] bafb9e5a-8856-7e6c-699c-801844e77a4a/kubernetes-dynamic-pvc-3eba5bba-48a3-11e8-89ab-005056b92113.vmdk": "",
"/var/lib/kubelet/plugins/kubernetes.io/vsphere-volume/mounts/[vsanDatastore] bafb9e5a-8856-7e6c-699c-801844e77a4a/kubernetes-dynamic-pvc-3eba5bba-48a3-11e8-89ab-005056b92113.vmdk": "",
"/": "",
"/sys": "",
"/proc": "",
"/dev": "",
"/dev/pts": "",
"/run": "",
"/sys/kernel/security": "",
"/dev/shm": "",
"/run/lock": "",
"/sys/fs/cgroup": "",
"/sys/fs/pstore": "",
"/proc/sys/fs/binfmt_misc": "",
"/dev/mqueue": "",
"/sys/kernel/debug": "",
"/dev/hugepages": "",
"/sys/fs/fuse/connections": "",
"/boot": "",
"/run/user/1000": "",
"/run/user/1000/gvfs": "",
}

filesystems, err := mountPointDetails(log.NewNopLogger())
Expand All @@ -92,6 +81,10 @@ func TestMountPointDetails(t *testing.T) {
t.Errorf("Got unexpected %s", fs.mountPoint)
}
}

if len(filesystems) != len(expected) {
t.Errorf("Too few returned filesystems")
}
}

func TestMountsFallback(t *testing.T) {
Expand Down
19 changes: 19 additions & 0 deletions collector/fixtures/proc/1/mountinfo
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
24 29 0:22 / /sys rw,nosuid,nodev,noexec,relatime shared:7 - sysfs sysfs rw
25 29 0:23 / /proc rw,nosuid,nodev,noexec,relatime shared:13 - proc proc rw
26 29 0:5 / /dev rw,nosuid,relatime shared:2 - devtmpfs udev rw,size=7978892k,nr_inodes=1994723,mode=755,inode64
27 26 0:24 / /dev/pts rw,nosuid,noexec,relatime shared:3 - devpts devpts rw,gid=5,mode=620,ptmxmode=000
28 29 0:25 / /run rw,nosuid,nodev,noexec,relatime shared:5 - tmpfs tmpfs rw,size=1603440k,mode=755,inode64
29 1 259:2 / / rw,relatime shared:1 - ext4 /dev/nvme0n1p2 rw,errors=remount-ro
30 24 0:6 / /sys/kernel/security rw,nosuid,nodev,noexec,relatime shared:8 - securityfs securityfs rw
31 26 0:26 / /dev/shm rw,nosuid,nodev shared:4 - tmpfs tmpfs rw,inode64
32 28 0:27 / /run/lock rw,nosuid,nodev,noexec,relatime shared:6 - tmpfs tmpfs rw,size=5120k,inode64
33 24 0:28 / /sys/fs/cgroup rw,nosuid,nodev,noexec,relatime shared:9 - cgroup2 cgroup2 rw
34 24 0:29 / /sys/fs/pstore rw,nosuid,nodev,noexec,relatime shared:10 - pstore pstore rw
38 24 0:7 / /sys/kernel/debug rw,nosuid,nodev,noexec,relatime shared:15 - debugfs debugfs rw
39 26 0:20 / /dev/mqueue rw,nosuid,nodev,noexec,relatime shared:16 - mqueue mqueue rw
40 26 0:33 / /dev/hugepages rw,relatime shared:17 - hugetlbfs hugetlbfs rw,pagesize=2M
42 24 0:34 / /sys/fs/fuse/connections rw,nosuid,nodev,noexec,relatime shared:19 - fusectl fusectl rw
163 29 259:1 / /boot rw,relatime shared:92 - vfat /dev/nvme0n1p1 rw,fmask=0077,dmask=0077,codepage=437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro
265 37 0:41 / /proc/sys/fs/binfmt_misc rw,nosuid,nodev,noexec,relatime shared:94 - binfmt_misc binfmt_misc rw
3002 28 0:79 / /run/user/1000 rw,nosuid,nodev,relatime shared:1225 - tmpfs tmpfs rw,size=1603436k,nr_inodes=400859,mode=700,uid=1000,gid=1000,inode64
3147 3002 0:81 / /run/user/1000/gvfs rw,nosuid,nodev,relatime shared:1290 - fuse.gvfsd-fuse gvfsd-fuse rw,user_id=1000,group_id=1000
32 changes: 0 additions & 32 deletions collector/fixtures/proc/1/mounts

This file was deleted.

6 changes: 6 additions & 0 deletions collector/fixtures_bindmount/proc/1/mountinfo
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
29 1 259:0 / /host rw,seclabel,relatime,data=ordered shared:1 - ext4 /dev/nvme1n0 rw
30 1 260:0 / /host/media/volume1 rw,seclabel,relatime,data=ordered shared:1 - ext4 /dev/nvme1n1 rw
31 1 261:0 / /host/media/volume2 rw,seclabel,relatime,data=ordered shared:1 - ext4 /dev/nvme1n2 rw
31 26 0:26 / /dev/shm rw,nosuid,nodev shared:4 - tmpfs tmpfs rw,inode64
32 28 0:27 / /run/lock rw,nosuid,nodev,noexec,relatime shared:6 - tmpfs tmpfs rw,size=5120k,inode64
33 24 0:28 / /sys/fs/cgroup rw,nosuid,nodev,noexec,relatime shared:9 - cgroup2 cgroup2 rw
6 changes: 0 additions & 6 deletions collector/fixtures_bindmount/proc/mounts

This file was deleted.

1 change: 0 additions & 1 deletion collector/fixtures_hidepid/proc/mounts

This file was deleted.

1 change: 1 addition & 0 deletions collector/fixtures_hidepid/proc/self/mountinfo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
29 1 259:2 / / rw,relatime shared:1 - ext4 /dev/nvme0n1p2 rw,errors=remount-ro

0 comments on commit 91628de

Please sign in to comment.