aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYecheng Fu <cofyc.jackson@gmail.com>2018-10-04 20:11:21 +0800
committerBen Kochie <superq@gmail.com>2018-10-04 14:11:21 +0200
commit0f9842f20a036d2db8f29102e6bc0cd7b1672865 (patch)
treeb6b8451169448d437f82d13c7020c25ddd51edc5
parent2269df255c8627f819afd3d2d6bb99d809b2ac9c (diff)
downloadprometheus_node_collector-0f9842f20a036d2db8f29102e6bc0cd7b1672865.tar.bz2
prometheus_node_collector-0f9842f20a036d2db8f29102e6bc0cd7b1672865.tar.xz
prometheus_node_collector-0f9842f20a036d2db8f29102e6bc0cd7b1672865.zip
[continue 912] strip rootfs prefix for run in docker (#1058)
* strip rootfs prefix for run in docker * Use `/` as default value of path.rootfs, and parse mounts from `/proc/1/mounts`. * No need to mount `/proc` and `/sys` because we share host's PID namespace, which allows processes within the container to see all of the processes on the system. Closes: #66 Signed-off-by: Ivan Mikheykin <ivan.mikheykin@flant.com> Signed-off-by: Yecheng Fu <cofyc.jackson@gmail.com>
-rw-r--r--README.md7
-rw-r--r--collector/filesystem_linux.go6
-rw-r--r--collector/fixtures/proc/1/mounts32
-rw-r--r--collector/paths.go9
4 files changed, 48 insertions, 6 deletions
diff --git a/README.md b/README.md
index b3940c5..320b54f 100644
--- a/README.md
+++ b/README.md
@@ -144,12 +144,17 @@ The `node_exporter` is designed to monitor the host system. It's not recommended
144to deploy it as a Docker container because it requires access to the host system. 144to deploy it as a Docker container because it requires access to the host system.
145Be aware that any non-root mount points you want to monitor will need to be bind-mounted 145Be aware that any non-root mount points you want to monitor will need to be bind-mounted
146into the container. 146into the container.
147If you start container for host monitoring, specify `path.rootfs` argument.
148This argument must match path in bind-mount of host root. The node\_exporter will use
149`path.rootfs` as prefix to access host filesystem.
147 150
148```bash 151```bash
149docker run -d \ 152docker run -d \
150 --net="host" \ 153 --net="host" \
151 --pid="host" \ 154 --pid="host" \
152 quay.io/prometheus/node-exporter 155 -v "/:/host:ro,rslave" \
156 quay.io/prometheus/node-exporter \
157 --path.rootfs /host
153``` 158```
154 159
155On some systems, the `timex` collector requires an additional Docker flag, 160On some systems, the `timex` collector requires an additional Docker flag,
diff --git a/collector/filesystem_linux.go b/collector/filesystem_linux.go
index 5e2e37b..74701d4 100644
--- a/collector/filesystem_linux.go
+++ b/collector/filesystem_linux.go
@@ -70,7 +70,7 @@ func (c *filesystemCollector) GetStats() ([]filesystemStats, error) {
70 go stuckMountWatcher(labels.mountPoint, success) 70 go stuckMountWatcher(labels.mountPoint, success)
71 71
72 buf := new(syscall.Statfs_t) 72 buf := new(syscall.Statfs_t)
73 err = syscall.Statfs(labels.mountPoint, buf) 73 err = syscall.Statfs(rootfsFilePath(labels.mountPoint), buf)
74 74
75 stuckMountsMtx.Lock() 75 stuckMountsMtx.Lock()
76 close(success) 76 close(success)
@@ -86,7 +86,7 @@ func (c *filesystemCollector) GetStats() ([]filesystemStats, error) {
86 labels: labels, 86 labels: labels,
87 deviceError: 1, 87 deviceError: 1,
88 }) 88 })
89 log.Debugf("Error on statfs() system call for %q: %s", labels.mountPoint, err) 89 log.Debugf("Error on statfs() system call for %q: %s", rootfsFilePath(labels.mountPoint), err)
90 continue 90 continue
91 } 91 }
92 92
@@ -133,7 +133,7 @@ func stuckMountWatcher(mountPoint string, success chan struct{}) {
133} 133}
134 134
135func mountPointDetails() ([]filesystemLabels, error) { 135func mountPointDetails() ([]filesystemLabels, error) {
136 file, err := os.Open(procFilePath("mounts")) 136 file, err := os.Open(procFilePath("1/mounts"))
137 if err != nil { 137 if err != nil {
138 return nil, err 138 return nil, err
139 } 139 }
diff --git a/collector/fixtures/proc/1/mounts b/collector/fixtures/proc/1/mounts
new file mode 100644
index 0000000..7452d49
--- /dev/null
+++ b/collector/fixtures/proc/1/mounts
@@ -0,0 +1,32 @@
1rootfs / rootfs rw 0 0
2sysfs /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0
3proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0
4udev /dev devtmpfs rw,relatime,size=10240k,nr_inodes=1008585,mode=755 0 0
5devpts /dev/pts devpts rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000 0 0
6tmpfs /run tmpfs rw,nosuid,relatime,size=1617716k,mode=755 0 0
7/dev/dm-2 / ext4 rw,relatime,errors=remount-ro,data=ordered 0 0
8securityfs /sys/kernel/security securityfs rw,nosuid,nodev,noexec,relatime 0 0
9tmpfs /dev/shm tmpfs rw,nosuid,nodev 0 0
10tmpfs /run/lock tmpfs rw,nosuid,nodev,noexec,relatime,size=5120k 0 0
11tmpfs /sys/fs/cgroup tmpfs ro,nosuid,nodev,noexec,mode=755 0 0
12cgroup /sys/fs/cgroup/systemd cgroup rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/lib/systemd/systemd-cgroups-agent,name=systemd 0 0
13pstore /sys/fs/pstore pstore rw,nosuid,nodev,noexec,relatime 0 0
14cgroup /sys/fs/cgroup/cpuset cgroup rw,nosuid,nodev,noexec,relatime,cpuset 0 0
15cgroup /sys/fs/cgroup/cpu,cpuacct cgroup rw,nosuid,nodev,noexec,relatime,cpu,cpuacct 0 0
16cgroup /sys/fs/cgroup/devices cgroup rw,nosuid,nodev,noexec,relatime,devices 0 0
17cgroup /sys/fs/cgroup/freezer cgroup rw,nosuid,nodev,noexec,relatime,freezer 0 0
18cgroup /sys/fs/cgroup/net_cls,net_prio cgroup rw,nosuid,nodev,noexec,relatime,net_cls,net_prio 0 0
19cgroup /sys/fs/cgroup/blkio cgroup rw,nosuid,nodev,noexec,relatime,blkio 0 0
20cgroup /sys/fs/cgroup/perf_event cgroup rw,nosuid,nodev,noexec,relatime,perf_event 0 0
21systemd-1 /proc/sys/fs/binfmt_misc autofs rw,relatime,fd=22,pgrp=1,timeout=300,minproto=5,maxproto=5,direct 0 0
22mqueue /dev/mqueue mqueue rw,relatime 0 0
23debugfs /sys/kernel/debug debugfs rw,relatime 0 0
24hugetlbfs /dev/hugepages hugetlbfs rw,relatime 0 0
25fusectl /sys/fs/fuse/connections fusectl rw,relatime 0 0
26/dev/sda3 /boot ext2 rw,relatime 0 0
27rpc_pipefs /run/rpc_pipefs rpc_pipefs rw,relatime 0 0
28binfmt_misc /proc/sys/fs/binfmt_misc binfmt_misc rw,relatime 0 0
29tmpfs /run/user/1000 tmpfs rw,nosuid,nodev,relatime,size=808860k,mode=700,uid=1000,gid=1000 0 0
30gvfsd-fuse /run/user/1000/gvfs fuse.gvfsd-fuse rw,nosuid,nodev,relatime,user_id=1000,group_id=1000 0 0
31/dev/sda /var/lib/kubelet/plugins/kubernetes.io/vsphere-volume/mounts/[vsanDatastore]\040bafb9e5a-8856-7e6c-699c-801844e77a4a/kubernetes-dynamic-pvc-3eba5bba-48a3-11e8-89ab-005056b92113.vmdk ext4 rw,relatime,data=ordered 0 0
32/dev/sda /var/lib/kubelet/plugins/kubernetes.io/vsphere-volume/mounts/[vsanDatastore]\011bafb9e5a-8856-7e6c-699c-801844e77a4a/kubernetes-dynamic-pvc-3eba5bba-48a3-11e8-89ab-005056b92113.vmdk ext4 rw,relatime,data=ordered 0 0
diff --git a/collector/paths.go b/collector/paths.go
index f8e6e6e..a3a61ad 100644
--- a/collector/paths.go
+++ b/collector/paths.go
@@ -22,8 +22,9 @@ import (
22 22
23var ( 23var (
24 // The path of the proc filesystem. 24 // The path of the proc filesystem.
25 procPath = kingpin.Flag("path.procfs", "procfs mountpoint.").Default(procfs.DefaultMountPoint).String() 25 procPath = kingpin.Flag("path.procfs", "procfs mountpoint.").Default(procfs.DefaultMountPoint).String()
26 sysPath = kingpin.Flag("path.sysfs", "sysfs mountpoint.").Default("/sys").String() 26 sysPath = kingpin.Flag("path.sysfs", "sysfs mountpoint.").Default("/sys").String()
27 rootfsPath = kingpin.Flag("path.rootfs", "rootfs mountpoint.").Default("/").String()
27) 28)
28 29
29func procFilePath(name string) string { 30func procFilePath(name string) string {
@@ -33,3 +34,7 @@ func procFilePath(name string) string {
33func sysFilePath(name string) string { 34func sysFilePath(name string) string {
34 return path.Join(*sysPath, name) 35 return path.Join(*sysPath, name)
35} 36}
37
38func rootfsFilePath(name string) string {
39 return path.Join(*rootfsPath, name)
40}