Skip to content

Commit eea8b61

Browse files
migeyelv-zhuravlev
authored andcommitted
Add node_filesystem_mount_info metric (prometheus#2970)
* Add node_filesystem_mount_info metric Fixes: prometheus#1384 --------- Signed-off-by: Miguel Oliveira <miguel.oliveira4224@gmail.com> Signed-off-by: Vitaly Zhuravlev <v-zhuravlev@users.noreply.github.com>
1 parent 14b2603 commit eea8b61

File tree

9 files changed

+84
-51
lines changed

9 files changed

+84
-51
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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,18 @@ func TestMountPointDetails(t *testing.T) {
8787
t.Log(err)
8888
}
8989

90+
foundSet := map[string]bool{}
9091
for _, fs := range filesystems {
9192
if _, ok := expected[fs.mountPoint]; !ok {
9293
t.Errorf("Got unexpected %s", fs.mountPoint)
9394
}
95+
foundSet[fs.mountPoint] = true
96+
}
97+
98+
for mountPoint := range expected {
99+
if _, ok := foundSet[mountPoint]; !ok {
100+
t.Errorf("Expected %s, got nothing", mountPoint)
101+
}
94102
}
95103
}
96104

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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
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,relatime shared:5 - tmpfs tmpfs rw,size=1617716k,mode=755
6+
29 1 259:2 / / rw,relatime shared:1 - ext4 /dev/dm-2 errors=remount-ro,data=ordered
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
10+
33 24 0:28 / /sys/fs/cgroup ro,nosuid,nodev,noexec shared:9 - tmpfs tmpfs ro,mode=755
11+
34 31 0:24 / /sys/fs/cgroup/systemd rw,nosuid,nodev,noexec,relatime shared:10 - cgroup cgroup rw,xattr,release_agent=/lib/systemd/systemd-cgroups-agent,name=systemd
12+
35 32 0:25 / /sys/fs/pstore rw,nosuid,nodev,noexec,relatime shared:11 - pstore pstore rw
13+
36 33 0:26 / /sys/fs/cgroup/cpuset rw,nosuid,nodev,noexec,relatime shared:12 - cgroup cgroup rw,cpuset
14+
37 34 0:27 / /sys/fs/cgroup/cpu,cpuacct rw,nosuid,nodev,noexec,relatime shared:14 - cgroup cgroup rw,cpu,cpuacct
15+
38 35 0:28 / /sys/fs/cgroup/devices rw,nosuid,nodev,noexec,relatime shared:16 - cgroup cgroup rw,devices
16+
39 36 0:29 / /sys/fs/cgroup/freezer rw,nosuid,nodev,noexec,relatime shared:17 - cgroup cgroup rw,freezer
17+
40 37 0:30 / /sys/fs/cgroup/net_cls,net_prio rw,nosuid,nodev,noexec,relatime shared:18 - cgroup cgroup rw,net_cls,net_prio
18+
41 38 0:31 / /sys/fs/cgroup/blkio rw,nosuid,nodev,noexec,relatime shared:19 - cgroup cgroup rw,blkio
19+
42 39 0:32 / /sys/fs/cgroup/perf_event rw,nosuid,nodev,noexec,relatime shared:20 - cgroup cgroup rw,perf_event
20+
43 40 0:33 / /proc/sys/fs/binfmt_misc rw,relatime shared:21 - systemd-1 autofs rw,fd=22,pgrp=1,timeout=300,minproto=5,maxproto=5,direct
21+
44 41 0:34 / /dev/mqueue rw,relatime shared:22 - mqueue mqueue rw
22+
45 42 0:35 / /sys/kernel/debug rw,relatime shared:23 - debugfs debugfs rw
23+
46 43 0:36 / /dev/hugepages rw,relatime shared:24 - hugetlbfs hugetlbfs rw
24+
47 44 0:37 / /sys/fs/fuse/connections rw,relatime shared:25 - fusectl fusectl rw
25+
48 45 260:3 / /boot rw,relatime shared:92 - ext2 /dev/sda3 rw
26+
49 46 0:39 / /run/rpc_pipefs rw,relatime shared:27 - rpc_pipefs rpc_pipefs rw
27+
265 37 0:41 / /proc/sys/fs/binfmt_misc rw,nosuid,nodev,noexec,relatime shared:94 - binfmt_misc binfmt_misc rw
28+
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
29+
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
30+
3148 3003 260:0 / /var/lib/kubelet/plugins/kubernetes.io/vsphere-volume/mounts/[vsanDatastore]\040bafb9e5a-8856-7e6c-699c-801844e77a4a/kubernetes-dynamic-pvc-3eba5bba-48a3-11e8-89ab-005056b92113.vmdk rw,relatime shared:31 - ext4 /dev/sda rw,data=ordered
31+
3149 3004 260:0 / /var/lib/kubelet/plugins/kubernetes.io/vsphere-volume/mounts/[vsanDatastore]\011bafb9e5a-8856-7e6c-699c-801844e77a4a/kubernetes-dynamic-pvc-3eba5bba-48a3-11e8-89ab-005056b92113.vmdk rw,relatime shared:32 - ext4 /dev/sda rw,data=ordered

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)