node_exporter/collector/filesystem_freebsd.go

135 lines
3.0 KiB
Go
Raw Normal View History

2015-05-13 10:27:59 +00:00
// +build !nofilesystem
package collector
import (
"errors"
"flag"
"regexp"
"unsafe"
"github.com/prometheus/client_golang/prometheus"
2015-06-24 06:05:09 +00:00
"github.com/prometheus/log"
2015-05-13 10:27:59 +00:00
)
/*
#include <sys/param.h>
#include <sys/ucred.h>
#include <sys/mount.h>
#include <stdio.h>
*/
import "C"
const (
subsystem = "filesystem"
2015-05-13 10:27:59 +00:00
)
var (
ignoredMountPoints = flag.String(
"collector.filesystem.ignored-mount-points",
"^/(dev)($|/)",
"Regexp of mount points to ignore for filesystem collector.")
2015-05-13 10:27:59 +00:00
)
type filesystemCollector struct {
ignoredMountPointsPattern *regexp.Regexp
}
func init() {
Factories["filesystem"] = NewFilesystemCollector
}
2015-06-23 13:49:18 +00:00
func NewFilesystemCollector() (Collector, error) {
pattern := regexp.MustCompile(*ignoredMountPoints)
2015-05-13 10:27:59 +00:00
return &filesystemCollector{
ignoredMountPointsPattern: pattern,
2015-05-13 10:27:59 +00:00
}, 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,
)
)
2015-05-13 10:27:59 +00:00
// Expose filesystem fullness.
func (c *filesystemCollector) Update(ch chan<- prometheus.Metric) (err error) {
var mntbuf *C.struct_statfs
count := C.getmntinfo(&mntbuf, C.MNT_NOWAIT)
if count == 0 {
2015-07-13 21:29:55 +00:00
return errors.New("getmntinfo() failed")
2015-05-13 10:27:59 +00:00
}
mnt := (*[1 << 30]C.struct_statfs)(unsafe.Pointer(mntbuf))
for i := 0; i < int(count); i++ {
name := C.GoString(&mnt[i].f_mntonname[0])
if c.ignoredMountPointsPattern.MatchString(name) {
2015-06-24 06:05:09 +00:00
log.Debugf("Ignoring mount point: %s", name)
2015-05-13 10:27:59 +00:00
continue
}
ch <- prometheus.MustNewConstMetric(
sizeDesc,
prometheus.GaugeValue,
float64(mnt[i].f_blocks) * float64(mnt[i].f_bsize),
mpd.device, mpd.mountPoint, mpd.fsType,
)
ch <- prometheus.MustNewConstMetric(
freeDesc,
prometheus.GaugeValue,
float64(mnt[i].f_bfree) * float64(mnt[i].f_bsize),
mpd.device, mpd.mountPoint, mpd.fsType,
)
2015-05-13 10:27:59 +00:00
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
2015-05-13 10:27:59 +00:00
}