forked from prometheus/node_exporter
/
filesystem.go
138 lines (125 loc) · 3.39 KB
/
filesystem.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// +build !nofilesystem
package collector
import (
"bufio"
"flag"
"fmt"
"os"
"regexp"
"strings"
"syscall"
"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus"
)
const (
procMounts = "/proc/mounts"
filesystemSubsystem = "filesystem"
)
var (
ignoredMountPoints = flag.String("filesystemIgnoredMountPoints", "^/(sys|proc|dev)($|/)", "Regexp of mount points to ignore for filesystem collector.")
)
type filesystemCollector struct {
config Config
ignoredMountPointsPattern *regexp.Regexp
size, free, avail, files, filesFree *prometheus.GaugeVec
}
func init() {
Factories["filesystem"] = NewFilesystemCollector
}
// Takes a config struct and prometheus registry and returns a new Collector exposing
// network device filesystems.
func NewFilesystemCollector(config Config) (Collector, error) {
var filesystemLabelNames = []string{"filesystem"}
return &filesystemCollector{
config: config,
ignoredMountPointsPattern: regexp.MustCompile(*ignoredMountPoints),
size: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: filesystemSubsystem,
Name: "size",
Help: "Filesystem size in bytes.",
},
filesystemLabelNames,
),
free: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: filesystemSubsystem,
Name: "free",
Help: "Filesystem free space in bytes.",
},
filesystemLabelNames,
),
avail: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: filesystemSubsystem,
Name: "avail",
Help: "Filesystem space available to non-root users in bytes.",
},
filesystemLabelNames,
),
files: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: filesystemSubsystem,
Name: "files",
Help: "Filesystem total file nodes.",
},
filesystemLabelNames,
),
filesFree: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: filesystemSubsystem,
Name: "files_free",
Help: "Filesystem total free file nodes.",
},
filesystemLabelNames,
),
}, nil
}
// Expose filesystem fullness.
func (c *filesystemCollector) Update(ch chan<- prometheus.Metric) (err error) {
mps, err := mountPoints()
if err != nil {
return err
}
for _, mp := range mps {
if c.ignoredMountPointsPattern.MatchString(mp) {
glog.V(1).Infof("Ignoring mount point: %s", mp)
continue
}
buf := new(syscall.Statfs_t)
err := syscall.Statfs(mp, buf)
if err != nil {
return fmt.Errorf("Statfs on %s returned %s", mp, err)
}
c.size.WithLabelValues(mp).Set(float64(buf.Blocks) * float64(buf.Bsize))
c.free.WithLabelValues(mp).Set(float64(buf.Bfree) * float64(buf.Bsize))
c.avail.WithLabelValues(mp).Set(float64(buf.Bavail) * float64(buf.Bsize))
c.files.WithLabelValues(mp).Set(float64(buf.Files))
c.filesFree.WithLabelValues(mp).Set(float64(buf.Ffree))
}
c.size.Collect(ch)
c.free.Collect(ch)
c.avail.Collect(ch)
c.files.Collect(ch)
c.filesFree.Collect(ch)
return err
}
func mountPoints() ([]string, error) {
file, err := os.Open(procMounts)
if err != nil {
return nil, err
}
defer file.Close()
mountPoints := []string{}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
parts := strings.Fields(scanner.Text())
mountPoints = append(mountPoints, parts[1])
}
return mountPoints, nil
}