Merge pull request #533 from prometheus/grobie/fix-filesystem-error-metric

Fix export of stale device error metrics for unmounted filesystems
This commit is contained in:
Johannes 'fish' Ziemke 2017-03-23 11:39:44 +01:00 committed by GitHub
commit 4f59422d68
3 changed files with 36 additions and 21 deletions

View File

@ -34,7 +34,7 @@ import "C"
const (
defIgnoredMountPoints = "^/(dev)($|/)"
defIgnoredFSTypes = "^devfs$"
MNT_RDONLY = 0x1
readOnly = 0x1 // MNT_RDONLY
)
// Expose filesystem fullness.
@ -62,7 +62,7 @@ func (c *filesystemCollector) GetStats() (stats []filesystemStats, err error) {
}
var ro float64
if (mnt[i].f_flags & MNT_RDONLY) != 0 {
if (mnt[i].f_flags & readOnly) != 0 {
ro = 1
}

View File

@ -44,11 +44,11 @@ var (
)
type filesystemCollector struct {
ignoredMountPointsPattern *regexp.Regexp
ignoredFSTypesPattern *regexp.Regexp
sizeDesc, freeDesc, availDesc,
filesDesc, filesFreeDesc, roDesc *prometheus.Desc
devErrors *prometheus.CounterVec
ignoredMountPointsPattern *regexp.Regexp
ignoredFSTypesPattern *regexp.Regexp
sizeDesc, freeDesc, availDesc *prometheus.Desc
filesDesc, filesFreeDesc *prometheus.Desc
roDesc, deviceErrorDesc *prometheus.Desc
}
type filesystemLabels struct {
@ -56,8 +56,10 @@ type filesystemLabels struct {
}
type filesystemStats struct {
labels filesystemLabels
size, free, avail, files, filesFree, ro float64
labels filesystemLabels
size, free, avail float64
files, filesFree float64
ro, deviceError float64
}
func init() {
@ -106,10 +108,11 @@ func NewFilesystemCollector() (Collector, error) {
filesystemLabelNames, nil,
)
devErrors := prometheus.NewCounterVec(prometheus.CounterOpts{
Name: prometheus.BuildFQName(Namespace, subsystem, "device_errors_total"),
Help: "Total number of errors occurred when getting stats for device",
}, filesystemLabelNames)
deviceErrorDesc := prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "device_error"),
"Whether an error occured while getting statistics for the given device.",
filesystemLabelNames, nil,
)
return &filesystemCollector{
ignoredMountPointsPattern: mountPointPattern,
@ -120,7 +123,7 @@ func NewFilesystemCollector() (Collector, error) {
filesDesc: filesDesc,
filesFreeDesc: filesFreeDesc,
roDesc: roDesc,
devErrors: devErrors,
deviceErrorDesc: deviceErrorDesc,
}, nil
}
@ -137,6 +140,14 @@ func (c *filesystemCollector) Update(ch chan<- prometheus.Metric) error {
}
seen[s.labels] = true
ch <- prometheus.MustNewConstMetric(
c.deviceErrorDesc, prometheus.GaugeValue,
s.deviceError, s.labels.device, s.labels.mountPoint, s.labels.fsType,
)
if s.deviceError > 0 {
continue
}
ch <- prometheus.MustNewConstMetric(
c.sizeDesc, prometheus.GaugeValue,
s.size, s.labels.device, s.labels.mountPoint, s.labels.fsType,
@ -162,6 +173,5 @@ func (c *filesystemCollector) Update(ch chan<- prometheus.Metric) error {
s.ro, s.labels.device, s.labels.mountPoint, s.labels.fsType,
)
}
c.devErrors.Collect(ch)
return nil
}

View File

@ -46,13 +46,15 @@ func (c *filesystemCollector) GetStats() ([]filesystemStats, error) {
log.Debugf("Ignoring fs type: %s", labels.fsType)
continue
}
labelValues := []string{labels.device, labels.mountPoint, labels.fsType}
buf := new(syscall.Statfs_t)
err := syscall.Statfs(labels.mountPoint, buf)
if err != nil {
c.devErrors.WithLabelValues(labelValues...).Inc()
log.Debugf("Statfs on %s returned %s",
labels.mountPoint, err)
stats = append(stats, filesystemStats{
labels: labels,
deviceError: 1,
})
log.Errorf("Error on statfs() system call for %q: %s", labels.mountPoint, err)
continue
}
@ -82,11 +84,14 @@ func mountPointDetails() ([]filesystemLabels, error) {
defer file.Close()
filesystems := []filesystemLabels{}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
parts := strings.Fields(scanner.Text())
filesystems = append(filesystems, filesystemLabels{parts[0], parts[1], parts[2]})
filesystems = append(filesystems, filesystemLabels{
device: parts[0],
mountPoint: parts[1],
fsType: parts[2],
})
}
return filesystems, scanner.Err()
}