Revamp the filesystem collector to use throw-away ConstMetrics.

This commit is contained in:
Martín Ferrari 2015-09-15 12:17:15 +00:00
parent e180b882d6
commit 8c2316e8a9
2 changed files with 155 additions and 133 deletions

View File

@ -21,78 +21,65 @@ import (
import "C" import "C"
const ( const (
filesystemSubsystem = "filesystem" subsystem = "filesystem"
) )
var ( var (
ignoredMountPoints = flag.String("collector.filesystem.ignored-mount-points", "^/(dev)($|/)", "Regexp of mount points to ignore for filesystem collector.") ignoredMountPoints = flag.String(
"collector.filesystem.ignored-mount-points",
"^/(dev)($|/)",
"Regexp of mount points to ignore for filesystem collector.")
) )
type filesystemCollector struct { type filesystemCollector struct {
ignoredMountPointsPattern *regexp.Regexp ignoredMountPointsPattern *regexp.Regexp
size, free, avail, files, filesFree *prometheus.GaugeVec
} }
func init() { func init() {
Factories["filesystem"] = NewFilesystemCollector Factories["filesystem"] = NewFilesystemCollector
} }
// Takes a prometheus registry and returns a new Collector exposing
// Filesystems stats.
func NewFilesystemCollector() (Collector, error) { func NewFilesystemCollector() (Collector, error) {
var filesystemLabelNames = []string{"filesystem"} pattern := regexp.MustCompile(*ignoredMountPoints)
return &filesystemCollector{ return &filesystemCollector{
ignoredMountPointsPattern: regexp.MustCompile(*ignoredMountPoints), ignoredMountPointsPattern: pattern,
size: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: filesystemSubsystem,
Name: "size_bytes",
Help: "Filesystem size in bytes.",
},
filesystemLabelNames,
),
free: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: filesystemSubsystem,
Name: "free_bytes",
Help: "Filesystem free space in bytes.",
},
filesystemLabelNames,
),
avail: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: filesystemSubsystem,
Name: "avail_bytes",
Help: "Filesystem space available to non-root users in bytes.",
},
filesystemLabelNames,
),
files: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: filesystemSubsystem,
Name: "file_nodes",
Help: "Filesystem total file nodes.",
},
filesystemLabelNames,
),
filesFree: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: filesystemSubsystem,
Name: "file_free_nodes",
Help: "Filesystem total free file nodes.",
},
filesystemLabelNames,
),
}, nil }, nil
} }
var (
filesystemLabelNames = []string{"device", "mountpoint", "fstype"}
sizeDesc = prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "size"),
"Filesystem size in bytes.",
filesystemLabelNames, nil,
)
freeDesc = prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "free"),
"Filesystem free space in bytes.",
filesystemLabelNames, nil,
)
availDesc = prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "avail"),
"Filesystem space available to non-root users in bytes.",
filesystemLabelNames, nil,
)
filesDesc = prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "files"),
"Filesystem total file nodes.",
filesystemLabelNames, nil,
)
filesFreeDesc = prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "files_free"),
"Filesystem total free file nodes.",
filesystemLabelNames, nil,
)
)
// Expose filesystem fullness. // Expose filesystem fullness.
func (c *filesystemCollector) Update(ch chan<- prometheus.Metric) (err error) { func (c *filesystemCollector) Update(ch chan<- prometheus.Metric) (err error) {
var mntbuf *C.struct_statfs var mntbuf *C.struct_statfs
@ -108,17 +95,40 @@ func (c *filesystemCollector) Update(ch chan<- prometheus.Metric) (err error) {
log.Debugf("Ignoring mount point: %s", name) log.Debugf("Ignoring mount point: %s", name)
continue continue
} }
c.size.WithLabelValues(name).Set(float64(mnt[i].f_blocks) * float64(mnt[i].f_bsize)) ch <- prometheus.MustNewConstMetric(
c.free.WithLabelValues(name).Set(float64(mnt[i].f_bfree) * float64(mnt[i].f_bsize)) sizeDesc,
c.avail.WithLabelValues(name).Set(float64(mnt[i].f_bavail) * float64(mnt[i].f_bsize)) prometheus.GaugeValue,
c.files.WithLabelValues(name).Set(float64(mnt[i].f_files)) float64(mnt[i].f_blocks) * float64(mnt[i].f_bsize),
c.filesFree.WithLabelValues(name).Set(float64(mnt[i].f_ffree)) mpd.device, mpd.mountPoint, mpd.fsType,
} )
c.size.Collect(ch) ch <- prometheus.MustNewConstMetric(
c.free.Collect(ch) freeDesc,
c.avail.Collect(ch) prometheus.GaugeValue,
c.files.Collect(ch) float64(mnt[i].f_bfree) * float64(mnt[i].f_bsize),
c.filesFree.Collect(ch) mpd.device, mpd.mountPoint, mpd.fsType,
return err )
ch <- prometheus.MustNewConstMetric(
availDesc,
prometheus.GaugeValue,
float64(mnt[i].f_bavail) * float64(mnt[i].f_bsize),
mpd.device, mpd.mountPoint, mpd.fsType,
)
ch <- prometheus.MustNewConstMetric(
filesDesc,
prometheus.GaugeValue,
float64(mnt[i].f_files),
mpd.device, mpd.mountPoint, mpd.fsType,
)
ch <- prometheus.MustNewConstMetric(
filesFreeDesc,
prometheus.GaugeValue,
float64(mnt[i].f_ffree),
mpd.device, mpd.mountPoint, mpd.fsType,
)
}
return nil
} }

View File

@ -16,12 +16,15 @@ import (
) )
const ( const (
procMounts = "/proc/mounts" procMounts = "/proc/mounts"
filesystemSubsystem = "filesystem" subsystem = "filesystem"
) )
var ( var (
ignoredMountPoints = flag.String("collector.filesystem.ignored-mount-points", "^/(sys|proc|dev)($|/)", "Regexp of mount points to ignore for filesystem collector.") ignoredMountPoints = flag.String(
"collector.filesystem.ignored-mount-points",
"^/(sys|proc|dev)($|/)",
"Regexp of mount points to ignore for filesystem collector.")
) )
type filesystemDetails struct { type filesystemDetails struct {
@ -32,69 +35,53 @@ type filesystemDetails struct {
type filesystemCollector struct { type filesystemCollector struct {
ignoredMountPointsPattern *regexp.Regexp ignoredMountPointsPattern *regexp.Regexp
size, free, avail, files, filesFree *prometheus.GaugeVec
} }
func init() { func init() {
Factories["filesystem"] = NewFilesystemCollector Factories["filesystem"] = NewFilesystemCollector
} }
// Takes a prometheus registry and returns a new Collector exposing
// network device filesystems.
func NewFilesystemCollector() (Collector, error) { func NewFilesystemCollector() (Collector, error) {
var filesystemLabelNames = []string{"device", "mountpoint", "fstype"} pattern := regexp.MustCompile(*ignoredMountPoints)
return &filesystemCollector{ return &filesystemCollector{
ignoredMountPointsPattern: regexp.MustCompile(*ignoredMountPoints), ignoredMountPointsPattern: pattern,
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 }, nil
} }
var (
filesystemLabelNames = []string{"device", "mountpoint", "fstype"}
sizeDesc = prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "size"),
"Filesystem size in bytes.",
filesystemLabelNames, nil,
)
freeDesc = prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "free"),
"Filesystem free space in bytes.",
filesystemLabelNames, nil,
)
availDesc = prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "avail"),
"Filesystem space available to non-root users in bytes.",
filesystemLabelNames, nil,
)
filesDesc = prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "files"),
"Filesystem total file nodes.",
filesystemLabelNames, nil,
)
filesFreeDesc = prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "files_free"),
"Filesystem total free file nodes.",
filesystemLabelNames, nil,
)
)
// Expose filesystem fullness. // Expose filesystem fullness.
func (c *filesystemCollector) Update(ch chan<- prometheus.Metric) (err error) { func (c *filesystemCollector) Update(ch chan<- prometheus.Metric) (err error) {
mpds, err := mountPointDetails() mpds, err := mountPointDetails()
@ -109,21 +96,46 @@ func (c *filesystemCollector) Update(ch chan<- prometheus.Metric) (err error) {
buf := new(syscall.Statfs_t) buf := new(syscall.Statfs_t)
err := syscall.Statfs(mpd.mountPoint, buf) err := syscall.Statfs(mpd.mountPoint, buf)
if err != nil { if err != nil {
return fmt.Errorf("Statfs on %s returned %s", mpd.mountPoint, err) return fmt.Errorf("Statfs on %s returned %s",
mpd.mountPoint, err)
} }
c.size.WithLabelValues(mpd.device, mpd.mountPoint, mpd.fsType).Set(float64(buf.Blocks) * float64(buf.Bsize)) ch <- prometheus.MustNewConstMetric(
c.free.WithLabelValues(mpd.device, mpd.mountPoint, mpd.fsType).Set(float64(buf.Bfree) * float64(buf.Bsize)) sizeDesc,
c.avail.WithLabelValues(mpd.device, mpd.mountPoint, mpd.fsType).Set(float64(buf.Bavail) * float64(buf.Bsize)) prometheus.GaugeValue,
c.files.WithLabelValues(mpd.device, mpd.mountPoint, mpd.fsType).Set(float64(buf.Files)) float64(buf.Blocks) * float64(buf.Bsize),
c.filesFree.WithLabelValues(mpd.device, mpd.mountPoint, mpd.fsType).Set(float64(buf.Ffree)) mpd.device, mpd.mountPoint, mpd.fsType,
)
ch <- prometheus.MustNewConstMetric(
freeDesc,
prometheus.GaugeValue,
float64(buf.Bfree) * float64(buf.Bsize),
mpd.device, mpd.mountPoint, mpd.fsType,
)
ch <- prometheus.MustNewConstMetric(
availDesc,
prometheus.GaugeValue,
float64(buf.Bavail) * float64(buf.Bsize),
mpd.device, mpd.mountPoint, mpd.fsType,
)
ch <- prometheus.MustNewConstMetric(
filesDesc,
prometheus.GaugeValue,
float64(buf.Files),
mpd.device, mpd.mountPoint, mpd.fsType,
)
ch <- prometheus.MustNewConstMetric(
filesFreeDesc,
prometheus.GaugeValue,
float64(buf.Ffree),
mpd.device, mpd.mountPoint, mpd.fsType,
)
} }
c.size.Collect(ch) return nil
c.free.Collect(ch)
c.avail.Collect(ch)
c.files.Collect(ch)
c.filesFree.Collect(ch)
return err
} }
func mountPointDetails() ([]filesystemDetails, error) { func mountPointDetails() ([]filesystemDetails, error) {