2014-06-05 19:44:44 +00:00
|
|
|
// +build !nofilesystem
|
|
|
|
|
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
|
2014-06-26 17:20:36 +00:00
|
|
|
"github.com/golang/glog"
|
2014-06-05 19:44:44 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2014-06-26 17:20:36 +00:00
|
|
|
procMounts = "/proc/mounts"
|
|
|
|
filesystemSubsystem = "filesystem"
|
2014-06-05 19:44:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2014-06-26 17:20:36 +00:00
|
|
|
filesystemLabelNames = []string{"filesystem"}
|
|
|
|
|
|
|
|
fsSizeMetric = prometheus.NewGaugeVec(
|
|
|
|
prometheus.GaugeOpts{
|
|
|
|
Namespace: Namespace,
|
|
|
|
Subsystem: filesystemSubsystem,
|
|
|
|
Name: "size",
|
|
|
|
Help: "Filesystem size in bytes.",
|
|
|
|
},
|
|
|
|
filesystemLabelNames,
|
|
|
|
)
|
|
|
|
fsFreeMetric = prometheus.NewGaugeVec(
|
|
|
|
prometheus.GaugeOpts{
|
|
|
|
Namespace: Namespace,
|
|
|
|
Subsystem: filesystemSubsystem,
|
|
|
|
Name: "free",
|
|
|
|
Help: "Filesystem free space in bytes.",
|
|
|
|
},
|
|
|
|
filesystemLabelNames,
|
|
|
|
)
|
|
|
|
fsAvailMetric = prometheus.NewGaugeVec(
|
|
|
|
prometheus.GaugeOpts{
|
|
|
|
Namespace: Namespace,
|
|
|
|
Subsystem: filesystemSubsystem,
|
|
|
|
Name: "avail",
|
|
|
|
Help: "Filesystem space available to non-root users in bytes.",
|
|
|
|
},
|
|
|
|
filesystemLabelNames,
|
|
|
|
)
|
|
|
|
fsFilesMetric = prometheus.NewGaugeVec(
|
|
|
|
prometheus.GaugeOpts{
|
|
|
|
Namespace: Namespace,
|
|
|
|
Subsystem: filesystemSubsystem,
|
|
|
|
Name: "files",
|
|
|
|
Help: "Filesystem total file nodes.",
|
|
|
|
},
|
|
|
|
filesystemLabelNames,
|
|
|
|
)
|
|
|
|
fsFilesFreeMetric = prometheus.NewGaugeVec(
|
|
|
|
prometheus.GaugeOpts{
|
|
|
|
Namespace: Namespace,
|
|
|
|
Subsystem: filesystemSubsystem,
|
|
|
|
Name: "files_free",
|
|
|
|
Help: "Filesystem total free file nodes.",
|
|
|
|
},
|
|
|
|
filesystemLabelNames,
|
|
|
|
)
|
2014-06-05 19:44:44 +00:00
|
|
|
|
|
|
|
ignoredMountPoints = flag.String("filesystemIgnoredMountPoints", "^/(sys|proc|dev)($|/)", "Regexp of mount points to ignore for filesystem collector.")
|
|
|
|
)
|
|
|
|
|
|
|
|
type filesystemCollector struct {
|
|
|
|
config Config
|
|
|
|
ignoredMountPointsPattern *regexp.Regexp
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Factories["filesystem"] = NewFilesystemCollector
|
|
|
|
}
|
|
|
|
|
|
|
|
// Takes a config struct and prometheus registry and returns a new Collector exposing
|
|
|
|
// network device filesystems.
|
2014-06-26 17:20:36 +00:00
|
|
|
func NewFilesystemCollector(config Config) (Collector, error) {
|
2014-06-05 19:44:44 +00:00
|
|
|
c := filesystemCollector{
|
2014-06-26 17:20:36 +00:00
|
|
|
config: config,
|
2014-06-05 19:44:44 +00:00
|
|
|
ignoredMountPointsPattern: regexp.MustCompile(*ignoredMountPoints),
|
|
|
|
}
|
|
|
|
return &c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expose filesystem fullness.
|
2014-10-29 14:16:43 +00:00
|
|
|
func (c *filesystemCollector) Update(ch chan<- prometheus.Metric) (err error) {
|
2014-06-05 19:44:44 +00:00
|
|
|
mps, err := mountPoints()
|
|
|
|
if err != nil {
|
2014-10-29 14:16:43 +00:00
|
|
|
return err
|
2014-06-05 19:44:44 +00:00
|
|
|
}
|
|
|
|
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 {
|
2014-10-29 14:16:43 +00:00
|
|
|
return fmt.Errorf("Statfs on %s returned %s", mp, err)
|
2014-06-05 19:44:44 +00:00
|
|
|
}
|
2014-06-26 17:20:36 +00:00
|
|
|
fsSizeMetric.WithLabelValues(mp).Set(float64(buf.Blocks) * float64(buf.Bsize))
|
|
|
|
fsFreeMetric.WithLabelValues(mp).Set(float64(buf.Bfree) * float64(buf.Bsize))
|
|
|
|
fsAvailMetric.WithLabelValues(mp).Set(float64(buf.Bavail) * float64(buf.Bsize))
|
|
|
|
fsFilesMetric.WithLabelValues(mp).Set(float64(buf.Files))
|
|
|
|
fsFilesFreeMetric.WithLabelValues(mp).Set(float64(buf.Ffree))
|
2014-06-05 19:44:44 +00:00
|
|
|
}
|
2014-10-29 14:16:43 +00:00
|
|
|
fsSizeMetric.Collect(ch)
|
|
|
|
fsFreeMetric.Collect(ch)
|
|
|
|
fsAvailMetric.Collect(ch)
|
|
|
|
fsFilesMetric.Collect(ch)
|
|
|
|
fsFilesFreeMetric.Collect(ch)
|
|
|
|
return err
|
2014-06-05 19:44:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|