Skip to content

Commit 91628de

Browse files
committed
Add node_filesystem_mount_info metric
Fixes: prometheus#1384 Signed-off-by: Miguel Oliveira <miguel.oliveira4224@gmail.com>
1 parent b3bbd1f commit 91628de

File tree

9 files changed

+87
-81
lines changed

9 files changed

+87
-81
lines changed

collector/filesystem_common.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,12 @@ type filesystemCollector struct {
6969
sizeDesc, freeDesc, availDesc *prometheus.Desc
7070
filesDesc, filesFreeDesc *prometheus.Desc
7171
roDesc, deviceErrorDesc *prometheus.Desc
72+
mountInfoDesc *prometheus.Desc
7273
logger log.Logger
7374
}
7475

7576
type filesystemLabels struct {
76-
device, mountPoint, fsType, options, deviceError string
77+
device, mountPoint, fsType, options, deviceError, major, minor string
7778
}
7879

7980
type filesystemStats struct {
@@ -155,6 +156,13 @@ func NewFilesystemCollector(logger log.Logger) (Collector, error) {
155156
filesystemLabelNames, nil,
156157
)
157158

159+
mountInfoDesc := prometheus.NewDesc(
160+
prometheus.BuildFQName(namespace, subsystem, "mount_info"),
161+
"Filesystem mount information.",
162+
[]string{"device", "major", "minor", "mountpoint"},
163+
nil,
164+
)
165+
158166
return &filesystemCollector{
159167
excludedMountPointsPattern: mountPointPattern,
160168
excludedFSTypesPattern: filesystemsTypesPattern,
@@ -165,6 +173,7 @@ func NewFilesystemCollector(logger log.Logger) (Collector, error) {
165173
filesFreeDesc: filesFreeDesc,
166174
roDesc: roDesc,
167175
deviceErrorDesc: deviceErrorDesc,
176+
mountInfoDesc: mountInfoDesc,
168177
logger: logger,
169178
}, nil
170179
}
@@ -215,6 +224,10 @@ func (c *filesystemCollector) Update(ch chan<- prometheus.Metric) error {
215224
c.filesFreeDesc, prometheus.GaugeValue,
216225
s.filesFree, s.labels.device, s.labels.mountPoint, s.labels.fsType, s.labels.deviceError,
217226
)
227+
ch <- prometheus.MustNewConstMetric(
228+
c.mountInfoDesc, prometheus.GaugeValue,
229+
1.0, s.labels.device, s.labels.major, s.labels.minor, s.labels.mountPoint,
230+
)
218231
}
219232
return nil
220233
}

collector/filesystem_linux.go

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,11 @@ func stuckMountWatcher(mountPoint string, success chan struct{}, logger log.Logg
178178
}
179179

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

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

206+
major, minor := 0, 0
207+
_, err := fmt.Sscanf(parts[2], "%d:%d", &major, &minor)
208+
if err != nil {
209+
return nil, fmt.Errorf("malformed mount point information: %q", scanner.Text())
210+
}
211+
212+
m := 5
213+
for parts[m+1] != "-" {
214+
m++
215+
}
216+
206217
// Ensure we handle the translation of \040 and \011
207218
// as per fstab(5).
208-
parts[1] = strings.Replace(parts[1], "\\040", " ", -1)
209-
parts[1] = strings.Replace(parts[1], "\\011", "\t", -1)
219+
parts[4] = strings.Replace(parts[4], "\\040", " ", -1)
220+
parts[4] = strings.Replace(parts[4], "\\011", "\t", -1)
210221

211222
filesystems = append(filesystems, filesystemLabels{
212-
device: parts[0],
213-
mountPoint: rootfsStripPrefix(parts[1]),
214-
fsType: parts[2],
215-
options: parts[3],
223+
device: parts[m+3],
224+
mountPoint: rootfsStripPrefix(parts[4]),
225+
fsType: parts[m+2],
226+
options: parts[5],
227+
major: fmt.Sprint(major),
228+
minor: fmt.Sprint(minor),
216229
deviceError: "",
217230
})
218231
}

collector/filesystem_linux_test.go

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -50,36 +50,25 @@ func TestMountPointDetails(t *testing.T) {
5050
}
5151

5252
expected := map[string]string{
53-
"/": "",
54-
"/sys": "",
55-
"/proc": "",
56-
"/dev": "",
57-
"/dev/pts": "",
58-
"/run": "",
59-
"/sys/kernel/security": "",
60-
"/dev/shm": "",
61-
"/run/lock": "",
62-
"/sys/fs/cgroup": "",
63-
"/sys/fs/cgroup/systemd": "",
64-
"/sys/fs/pstore": "",
65-
"/sys/fs/cgroup/cpuset": "",
66-
"/sys/fs/cgroup/cpu,cpuacct": "",
67-
"/sys/fs/cgroup/devices": "",
68-
"/sys/fs/cgroup/freezer": "",
69-
"/sys/fs/cgroup/net_cls,net_prio": "",
70-
"/sys/fs/cgroup/blkio": "",
71-
"/sys/fs/cgroup/perf_event": "",
72-
"/proc/sys/fs/binfmt_misc": "",
73-
"/dev/mqueue": "",
74-
"/sys/kernel/debug": "",
75-
"/dev/hugepages": "",
76-
"/sys/fs/fuse/connections": "",
77-
"/boot": "",
78-
"/run/rpc_pipefs": "",
79-
"/run/user/1000": "",
80-
"/run/user/1000/gvfs": "",
81-
"/var/lib/kubelet/plugins/kubernetes.io/vsphere-volume/mounts/[vsanDatastore] bafb9e5a-8856-7e6c-699c-801844e77a4a/kubernetes-dynamic-pvc-3eba5bba-48a3-11e8-89ab-005056b92113.vmdk": "",
82-
"/var/lib/kubelet/plugins/kubernetes.io/vsphere-volume/mounts/[vsanDatastore] bafb9e5a-8856-7e6c-699c-801844e77a4a/kubernetes-dynamic-pvc-3eba5bba-48a3-11e8-89ab-005056b92113.vmdk": "",
53+
"/": "",
54+
"/sys": "",
55+
"/proc": "",
56+
"/dev": "",
57+
"/dev/pts": "",
58+
"/run": "",
59+
"/sys/kernel/security": "",
60+
"/dev/shm": "",
61+
"/run/lock": "",
62+
"/sys/fs/cgroup": "",
63+
"/sys/fs/pstore": "",
64+
"/proc/sys/fs/binfmt_misc": "",
65+
"/dev/mqueue": "",
66+
"/sys/kernel/debug": "",
67+
"/dev/hugepages": "",
68+
"/sys/fs/fuse/connections": "",
69+
"/boot": "",
70+
"/run/user/1000": "",
71+
"/run/user/1000/gvfs": "",
8372
}
8473

8574
filesystems, err := mountPointDetails(log.NewNopLogger())
@@ -92,6 +81,10 @@ func TestMountPointDetails(t *testing.T) {
9281
t.Errorf("Got unexpected %s", fs.mountPoint)
9382
}
9483
}
84+
85+
if len(filesystems) != len(expected) {
86+
t.Errorf("Too few returned filesystems")
87+
}
9588
}
9689

9790
func TestMountsFallback(t *testing.T) {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
24 29 0:22 / /sys rw,nosuid,nodev,noexec,relatime shared:7 - sysfs sysfs rw
2+
25 29 0:23 / /proc rw,nosuid,nodev,noexec,relatime shared:13 - proc proc rw
3+
26 29 0:5 / /dev rw,nosuid,relatime shared:2 - devtmpfs udev rw,size=7978892k,nr_inodes=1994723,mode=755,inode64
4+
27 26 0:24 / /dev/pts rw,nosuid,noexec,relatime shared:3 - devpts devpts rw,gid=5,mode=620,ptmxmode=000
5+
28 29 0:25 / /run rw,nosuid,nodev,noexec,relatime shared:5 - tmpfs tmpfs rw,size=1603440k,mode=755,inode64
6+
29 1 259:2 / / rw,relatime shared:1 - ext4 /dev/nvme0n1p2 rw,errors=remount-ro
7+
30 24 0:6 / /sys/kernel/security rw,nosuid,nodev,noexec,relatime shared:8 - securityfs securityfs rw
8+
31 26 0:26 / /dev/shm rw,nosuid,nodev shared:4 - tmpfs tmpfs rw,inode64
9+
32 28 0:27 / /run/lock rw,nosuid,nodev,noexec,relatime shared:6 - tmpfs tmpfs rw,size=5120k,inode64
10+
33 24 0:28 / /sys/fs/cgroup rw,nosuid,nodev,noexec,relatime shared:9 - cgroup2 cgroup2 rw
11+
34 24 0:29 / /sys/fs/pstore rw,nosuid,nodev,noexec,relatime shared:10 - pstore pstore rw
12+
38 24 0:7 / /sys/kernel/debug rw,nosuid,nodev,noexec,relatime shared:15 - debugfs debugfs rw
13+
39 26 0:20 / /dev/mqueue rw,nosuid,nodev,noexec,relatime shared:16 - mqueue mqueue rw
14+
40 26 0:33 / /dev/hugepages rw,relatime shared:17 - hugetlbfs hugetlbfs rw,pagesize=2M
15+
42 24 0:34 / /sys/fs/fuse/connections rw,nosuid,nodev,noexec,relatime shared:19 - fusectl fusectl rw
16+
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
17+
265 37 0:41 / /proc/sys/fs/binfmt_misc rw,nosuid,nodev,noexec,relatime shared:94 - binfmt_misc binfmt_misc rw
18+
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
19+
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

collector/fixtures/proc/1/mounts

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
29 1 259:0 / /host rw,seclabel,relatime,data=ordered shared:1 - ext4 /dev/nvme1n0 rw
2+
30 1 260:0 / /host/media/volume1 rw,seclabel,relatime,data=ordered shared:1 - ext4 /dev/nvme1n1 rw
3+
31 1 261:0 / /host/media/volume2 rw,seclabel,relatime,data=ordered shared:1 - ext4 /dev/nvme1n2 rw
4+
31 26 0:26 / /dev/shm rw,nosuid,nodev shared:4 - tmpfs tmpfs rw,inode64
5+
32 28 0:27 / /run/lock rw,nosuid,nodev,noexec,relatime shared:6 - tmpfs tmpfs rw,size=5120k,inode64
6+
33 24 0:28 / /sys/fs/cgroup rw,nosuid,nodev,noexec,relatime shared:9 - cgroup2 cgroup2 rw

collector/fixtures_bindmount/proc/mounts

Lines changed: 0 additions & 6 deletions
This file was deleted.

collector/fixtures_hidepid/proc/mounts

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
29 1 259:2 / / rw,relatime shared:1 - ext4 /dev/nvme0n1p2 rw,errors=remount-ro

0 commit comments

Comments
 (0)