Migrated everything to new client_golang.

pull/16/head
Bjoern Rabenstein 2014-06-26 19:20:36 +02:00
parent e8ebcede5b
commit 0563ecd29d
11 changed files with 388 additions and 246 deletions

View File

@ -8,12 +8,11 @@ import (
) )
var ( var (
attributes = prometheus.NewGauge() attributes *prometheus.GaugeVec
) )
type attributesCollector struct { type attributesCollector struct {
registry prometheus.Registry config Config
config Config
} }
func init() { func init() {
@ -22,22 +21,33 @@ func init() {
// Takes a config struct and prometheus registry and returns a new Collector exposing // Takes a config struct and prometheus registry and returns a new Collector exposing
// labels from the config. // labels from the config.
func NewAttributesCollector(config Config, registry prometheus.Registry) (Collector, error) { func NewAttributesCollector(config Config) (Collector, error) {
c := attributesCollector{ c := attributesCollector{
config: config, config: config,
registry: registry,
} }
registry.Register( labelNames := []string{}
"node_attributes", for l := range c.config.Attributes {
"node_exporter attributes", labelNames = append(labelNames, l)
prometheus.NilLabels, }
attributes, gv := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: Namespace,
Name: "attributes",
Help: "The node_exporter attributes.",
},
labelNames,
) )
collector, err := prometheus.RegisterOrGet(gv)
if err != nil {
return nil, err
}
attributes = collector.(*prometheus.GaugeVec)
return &c, nil return &c, nil
} }
func (c *attributesCollector) Update() (updates int, err error) { func (c *attributesCollector) Update() (updates int, err error) {
glog.V(1).Info("Set node_attributes{%v}: 1", c.config.Attributes) glog.V(1).Info("Set node_attributes{%v}: 1", c.config.Attributes)
attributes.Set(c.config.Attributes, 1) attributes.Reset()
attributes.With(c.config.Attributes).Set(1)
return updates, err return updates, err
} }

View File

@ -1,11 +1,9 @@
// Exporter is a prometheus exporter using multiple Factories to collect and export system metrics. // Exporter is a prometheus exporter using multiple Factories to collect and export system metrics.
package collector package collector
import ( const Namespace = "node"
"github.com/prometheus/client_golang/prometheus"
)
var Factories = make(map[string]func(Config, prometheus.Registry) (Collector, error)) var Factories = make(map[string]func(Config) (Collector, error))
// Interface a collector has to implement. // Interface a collector has to implement.
type Collector interface { type Collector interface {

View File

@ -18,35 +18,119 @@ import (
const ( const (
procDiskStats = "/proc/diskstats" procDiskStats = "/proc/diskstats"
diskSubsystem = "disk"
) )
type diskStat struct {
name string
metric prometheus.Metric
documentation string
}
var ( var (
ignoredDevices = flag.String("diskstatsIgnoredDevices", "^(ram|loop|(h|s|xv)d[a-z])\\d+$", "Regexp of devices to ignore for diskstats.") ignoredDevices = flag.String("diskstatsIgnoredDevices", "^(ram|loop|(h|s|xv)d[a-z])\\d+$", "Regexp of devices to ignore for diskstats.")
diskLabelNames = []string{"device"}
// Docs from https://www.kernel.org/doc/Documentation/iostats.txt // Docs from https://www.kernel.org/doc/Documentation/iostats.txt
diskStatsMetrics = []diskStat{ diskStatsMetrics = []prometheus.Collector{
{"reads_completed", prometheus.NewCounter(), "The total number of reads completed successfully."}, prometheus.NewCounterVec(
{"reads_merged", prometheus.NewCounter(), "The number of reads merged. See https://www.kernel.org/doc/Documentation/iostats.txt"}, prometheus.CounterOpts{
{"sectors_read", prometheus.NewCounter(), "The total number of sectors read successfully."}, Namespace: Namespace,
{"read_time_ms", prometheus.NewCounter(), "the total number of milliseconds spent by all reads."}, Subsystem: diskSubsystem,
{"writes_completed", prometheus.NewCounter(), "The total number of writes completed successfully."}, Name: "reads_completed",
{"writes_merged", prometheus.NewCounter(), "The number of writes merged. See https://www.kernel.org/doc/Documentation/iostats.txt"}, Help: "The total number of reads completed successfully.",
{"sectors_written", prometheus.NewCounter(), "The total number of sectors written successfully."}, },
{"write_time_ms", prometheus.NewCounter(), "This is the total number of milliseconds spent by all writes."}, diskLabelNames,
{"io_now", prometheus.NewGauge(), "The number of I/Os currently in progress."}, ),
{"io_time_ms", prometheus.NewCounter(), "Milliseconds spent doing I/Os."}, prometheus.NewCounterVec(
{"io_time_weighted", prometheus.NewCounter(), "The weighted # of milliseconds spent doing I/Os. See https://www.kernel.org/doc/Documentation/iostats.txt"}, prometheus.CounterOpts{
Namespace: Namespace,
Subsystem: diskSubsystem,
Name: "reads_merged",
Help: "The number of reads merged. See https://www.kernel.org/doc/Documentation/iostats.txt.",
},
diskLabelNames,
),
prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: Namespace,
Subsystem: diskSubsystem,
Name: "sectors_read",
Help: "The total number of sectors read successfully.",
},
diskLabelNames,
),
prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: Namespace,
Subsystem: diskSubsystem,
Name: "read_time_ms",
Help: "The total number of milliseconds spent by all reads.",
},
diskLabelNames,
),
prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: Namespace,
Subsystem: diskSubsystem,
Name: "writes_completed",
Help: "The total number of writes completed successfully.",
},
diskLabelNames,
),
prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: Namespace,
Subsystem: diskSubsystem,
Name: "writes_merged",
Help: "The number of writes merged. See https://www.kernel.org/doc/Documentation/iostats.txt.",
},
diskLabelNames,
),
prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: Namespace,
Subsystem: diskSubsystem,
Name: "sectors_written",
Help: "The total number of sectors written successfully.",
},
diskLabelNames,
),
prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: Namespace,
Subsystem: diskSubsystem,
Name: "write_time_ms",
Help: "This is the total number of milliseconds spent by all writes.",
},
diskLabelNames,
),
prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: diskSubsystem,
Name: "io_now",
Help: "The number of I/Os currently in progress.",
},
diskLabelNames,
),
prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: Namespace,
Subsystem: diskSubsystem,
Name: "io_time_ms",
Help: "Milliseconds spent doing I/Os.",
},
diskLabelNames,
),
prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: Namespace,
Subsystem: diskSubsystem,
Name: "io_time_weighted",
Help: "The weighted # of milliseconds spent doing I/Os. See https://www.kernel.org/doc/Documentation/iostats.txt.",
},
diskLabelNames,
),
} }
) )
type diskstatsCollector struct { type diskstatsCollector struct {
registry prometheus.Registry
config Config config Config
ignoredDevicesPattern *regexp.Regexp ignoredDevicesPattern *regexp.Regexp
} }
@ -57,20 +141,16 @@ func init() {
// Takes a config struct and prometheus registry and returns a new Collector exposing // Takes a config struct and prometheus registry and returns a new Collector exposing
// disk device stats. // disk device stats.
func NewDiskstatsCollector(config Config, registry prometheus.Registry) (Collector, error) { func NewDiskstatsCollector(config Config) (Collector, error) {
c := diskstatsCollector{ c := diskstatsCollector{
config: config, config: config,
registry: registry,
ignoredDevicesPattern: regexp.MustCompile(*ignoredDevices), ignoredDevicesPattern: regexp.MustCompile(*ignoredDevices),
} }
for _, v := range diskStatsMetrics { for _, c := range diskStatsMetrics {
registry.Register( if _, err := prometheus.RegisterOrGet(c); err != nil {
"node_disk_"+v.name, return nil, err
v.documentation, }
prometheus.NilLabels,
v.metric,
)
} }
return &c, nil return &c, nil
} }
@ -91,13 +171,12 @@ func (c *diskstatsCollector) Update() (updates int, err error) {
if err != nil { if err != nil {
return updates, fmt.Errorf("Invalid value %s in diskstats: %s", value, err) return updates, fmt.Errorf("Invalid value %s in diskstats: %s", value, err)
} }
labels := map[string]string{"device": dev} counter, ok := diskStatsMetrics[k].(*prometheus.CounterVec)
counter, ok := diskStatsMetrics[k].metric.(prometheus.Counter)
if ok { if ok {
counter.Set(labels, v) counter.WithLabelValues(dev).Set(v)
} else { } else {
var gauge = diskStatsMetrics[k].metric.(prometheus.Gauge) var gauge = diskStatsMetrics[k].(*prometheus.GaugeVec)
gauge.Set(labels, v) gauge.WithLabelValues(dev).Set(v)
} }
} }
} }

View File

@ -11,26 +11,68 @@ import (
"strings" "strings"
"syscall" "syscall"
"github.com/golang/glog" "github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
) )
const ( const (
procMounts = "/proc/mounts" procMounts = "/proc/mounts"
filesystemSubsystem = "filesystem"
) )
var ( var (
fsSizeMetric = prometheus.NewGauge() filesystemLabelNames = []string{"filesystem"}
fsFreeMetric = prometheus.NewGauge()
fsAvailMetric = prometheus.NewGauge() fsSizeMetric = prometheus.NewGaugeVec(
fsFilesMetric = prometheus.NewGauge() prometheus.GaugeOpts{
fsFilesFreeMetric = prometheus.NewGauge() 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,
)
ignoredMountPoints = flag.String("filesystemIgnoredMountPoints", "^/(sys|proc|dev)($|/)", "Regexp of mount points to ignore for filesystem collector.") ignoredMountPoints = flag.String("filesystemIgnoredMountPoints", "^/(sys|proc|dev)($|/)", "Regexp of mount points to ignore for filesystem collector.")
) )
type filesystemCollector struct { type filesystemCollector struct {
registry prometheus.Registry
config Config config Config
ignoredMountPointsPattern *regexp.Regexp ignoredMountPointsPattern *regexp.Regexp
} }
@ -41,42 +83,26 @@ func init() {
// Takes a config struct and prometheus registry and returns a new Collector exposing // Takes a config struct and prometheus registry and returns a new Collector exposing
// network device filesystems. // network device filesystems.
func NewFilesystemCollector(config Config, registry prometheus.Registry) (Collector, error) { func NewFilesystemCollector(config Config) (Collector, error) {
c := filesystemCollector{ c := filesystemCollector{
config: config, config: config,
registry: registry,
ignoredMountPointsPattern: regexp.MustCompile(*ignoredMountPoints), ignoredMountPointsPattern: regexp.MustCompile(*ignoredMountPoints),
} }
registry.Register( if _, err := prometheus.RegisterOrGet(fsSizeMetric); err != nil {
"node_filesystem_size", return nil, err
"Filesystem size in bytes.", }
prometheus.NilLabels, if _, err := prometheus.RegisterOrGet(fsFreeMetric); err != nil {
fsSizeMetric, return nil, err
) }
registry.Register( if _, err := prometheus.RegisterOrGet(fsAvailMetric); err != nil {
"node_filesystem_free", return nil, err
"Filesystem free space in bytes.", }
prometheus.NilLabels, if _, err := prometheus.RegisterOrGet(fsFilesMetric); err != nil {
fsFreeMetric, return nil, err
) }
registry.Register( if _, err := prometheus.RegisterOrGet(fsFilesFreeMetric); err != nil {
"node_filesystem_avail", return nil, err
"Filesystem space available to non-root users in bytes.", }
prometheus.NilLabels,
fsAvailMetric,
)
registry.Register(
"node_filesystem_files",
"Filesystem total file nodes.",
prometheus.NilLabels,
fsFilesMetric,
)
registry.Register(
"node_filesystem_files_free",
"Filesystem total free file nodes.",
prometheus.NilLabels,
fsFilesFreeMetric,
)
return &c, nil return &c, nil
} }
@ -96,11 +122,11 @@ func (c *filesystemCollector) Update() (updates int, err error) {
if err != nil { if err != nil {
return updates, fmt.Errorf("Statfs on %s returned %s", mp, err) return updates, fmt.Errorf("Statfs on %s returned %s", mp, err)
} }
fsSizeMetric.Set(map[string]string{"filesystem": mp}, float64(buf.Blocks)*float64(buf.Bsize)) fsSizeMetric.WithLabelValues(mp).Set(float64(buf.Blocks) * float64(buf.Bsize))
fsFreeMetric.Set(map[string]string{"filesystem": mp}, float64(buf.Bfree)*float64(buf.Bsize)) fsFreeMetric.WithLabelValues(mp).Set(float64(buf.Bfree) * float64(buf.Bsize))
fsAvailMetric.Set(map[string]string{"filesystem": mp}, float64(buf.Bavail)*float64(buf.Bsize)) fsAvailMetric.WithLabelValues(mp).Set(float64(buf.Bavail) * float64(buf.Bsize))
fsFilesMetric.Set(map[string]string{"filesystem": mp}, float64(buf.Files)) fsFilesMetric.WithLabelValues(mp).Set(float64(buf.Files))
fsFilesFreeMetric.Set(map[string]string{"filesystem": mp}, float64(buf.Ffree)) fsFilesFreeMetric.WithLabelValues(mp).Set(float64(buf.Ffree))
updates++ updates++
} }
return updates, err return updates, err

View File

@ -18,12 +18,18 @@ const (
) )
var ( var (
interruptsMetric = prometheus.NewCounter() interruptsMetric = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: Namespace,
Name: "interrupts",
Help: "Interrupt details from /proc/interrupts.",
},
[]string{"CPU", "type", "info", "devices"},
)
) )
type interruptsCollector struct { type interruptsCollector struct {
registry prometheus.Registry config Config
config Config
} }
func init() { func init() {
@ -32,17 +38,13 @@ func init() {
// Takes a config struct and prometheus registry and returns a new Collector exposing // Takes a config struct and prometheus registry and returns a new Collector exposing
// interrupts stats // interrupts stats
func NewInterruptsCollector(config Config, registry prometheus.Registry) (Collector, error) { func NewInterruptsCollector(config Config) (Collector, error) {
c := interruptsCollector{ c := interruptsCollector{
config: config, config: config,
registry: registry, }
if _, err := prometheus.RegisterOrGet(interruptsMetric); err != nil {
return nil, err
} }
registry.Register(
"node_interrupts",
"Interrupt details from /proc/interrupts",
prometheus.NilLabels,
interruptsMetric,
)
return &c, nil return &c, nil
} }
@ -58,13 +60,13 @@ func (c *interruptsCollector) Update() (updates int, err error) {
if err != nil { if err != nil {
return updates, fmt.Errorf("Invalid value %s in interrupts: %s", value, err) return updates, fmt.Errorf("Invalid value %s in interrupts: %s", value, err)
} }
labels := map[string]string{ labels := prometheus.Labels{
"CPU": strconv.Itoa(cpuNo), "CPU": strconv.Itoa(cpuNo),
"type": name, "type": name,
"info": interrupt.info, "info": interrupt.info,
"devices": interrupt.devices, "devices": interrupt.devices,
} }
interruptsMetric.Set(labels, fv) interruptsMetric.With(labels).Set(fv)
} }
} }
return updates, err return updates, err

View File

@ -14,13 +14,19 @@ import (
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
) )
const lastLoginSubsystem = "last_login"
var ( var (
lastSeen = prometheus.NewGauge() lastSeen = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: lastLoginSubsystem,
Name: "time",
Help: "The time of the last login.",
})
) )
type lastLoginCollector struct { type lastLoginCollector struct {
registry prometheus.Registry config Config
config Config
} }
func init() { func init() {
@ -29,17 +35,13 @@ func init() {
// Takes a config struct and prometheus registry and returns a new Collector exposing // Takes a config struct and prometheus registry and returns a new Collector exposing
// load, seconds since last login and a list of tags as specified by config. // load, seconds since last login and a list of tags as specified by config.
func NewLastLoginCollector(config Config, registry prometheus.Registry) (Collector, error) { func NewLastLoginCollector(config Config) (Collector, error) {
c := lastLoginCollector{ c := lastLoginCollector{
config: config, config: config,
registry: registry, }
if _, err := prometheus.RegisterOrGet(lastSeen); err != nil {
return nil, err
} }
registry.Register(
"node_last_login_time",
"The time of the last login.",
prometheus.NilLabels,
lastSeen,
)
return &c, nil return &c, nil
} }
@ -50,7 +52,7 @@ func (c *lastLoginCollector) Update() (updates int, err error) {
} }
updates++ updates++
glog.V(1).Infof("Set node_last_login_time: %f", last) glog.V(1).Infof("Set node_last_login_time: %f", last)
lastSeen.Set(nil, last) lastSeen.Set(last)
return updates, err return updates, err
} }

View File

@ -17,12 +17,15 @@ const (
) )
var ( var (
load1 = prometheus.NewGauge() load1 = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: Namespace,
Name: "load1",
Help: "1m load average.",
})
) )
type loadavgCollector struct { type loadavgCollector struct {
registry prometheus.Registry config Config
config Config
} }
func init() { func init() {
@ -31,18 +34,14 @@ func init() {
// Takes a config struct and prometheus registry and returns a new Collector exposing // Takes a config struct and prometheus registry and returns a new Collector exposing
// load, seconds since last login and a list of tags as specified by config. // load, seconds since last login and a list of tags as specified by config.
func NewLoadavgCollector(config Config, registry prometheus.Registry) (Collector, error) { func NewLoadavgCollector(config Config) (Collector, error) {
c := loadavgCollector{ c := loadavgCollector{
config: config, config: config,
registry: registry,
} }
registry.Register( if _, err := prometheus.RegisterOrGet(load1); err != nil {
"node_load1", return nil, err
"1m load average", }
prometheus.NilLabels,
load1,
)
return &c, nil return &c, nil
} }
@ -53,7 +52,7 @@ func (c *loadavgCollector) Update() (updates int, err error) {
} }
updates++ updates++
glog.V(1).Infof("Set node_load: %f", load) glog.V(1).Infof("Set node_load: %f", load)
load1.Set(nil, load) load1.Set(load)
return updates, err return updates, err
} }

View File

@ -16,7 +16,8 @@ import (
) )
const ( const (
procMemInfo = "/proc/meminfo" procMemInfo = "/proc/meminfo"
memInfoSubsystem = "memory"
) )
var ( var (
@ -24,8 +25,7 @@ var (
) )
type meminfoCollector struct { type meminfoCollector struct {
registry prometheus.Registry config Config
config Config
} }
func init() { func init() {
@ -34,10 +34,9 @@ func init() {
// Takes a config struct and prometheus registry and returns a new Collector exposing // Takes a config struct and prometheus registry and returns a new Collector exposing
// memory stats. // memory stats.
func NewMeminfoCollector(config Config, registry prometheus.Registry) (Collector, error) { func NewMeminfoCollector(config Config) (Collector, error) {
c := meminfoCollector{ c := meminfoCollector{
config: config, config: config,
registry: registry,
} }
return &c, nil return &c, nil
} }
@ -50,16 +49,16 @@ func (c *meminfoCollector) Update() (updates int, err error) {
glog.V(1).Infof("Set node_mem: %#v", memInfo) glog.V(1).Infof("Set node_mem: %#v", memInfo)
for k, v := range memInfo { for k, v := range memInfo {
if _, ok := memInfoMetrics[k]; !ok { if _, ok := memInfoMetrics[k]; !ok {
memInfoMetrics[k] = prometheus.NewGauge() gauge := prometheus.NewGauge(prometheus.GaugeOpts{
c.registry.Register( Namespace: Namespace,
"node_memory_"+k, Subsystem: memInfoSubsystem,
k+" from /proc/meminfo", Name: k,
prometheus.NilLabels, Help: k + " from /proc/meminfo.",
memInfoMetrics[k], })
) memInfoMetrics[k] = prometheus.MustRegisterOrGet(gauge).(prometheus.Gauge)
} }
updates++ updates++
memInfoMetrics[k].Set(nil, v) memInfoMetrics[k].Set(v)
} }
return updates, err return updates, err
} }

View File

@ -14,16 +14,16 @@ import (
) )
const ( const (
procNetDev = "/proc/net/dev" procNetDev = "/proc/net/dev"
netStatsSubsystem = "network"
) )
var ( var (
netStatsMetrics = map[string]prometheus.Gauge{} netStatsMetrics = map[string]*prometheus.GaugeVec{}
) )
type netDevCollector struct { type netDevCollector struct {
registry prometheus.Registry config Config
config Config
} }
func init() { func init() {
@ -32,10 +32,9 @@ func init() {
// Takes a config struct and prometheus registry and returns a new Collector exposing // Takes a config struct and prometheus registry and returns a new Collector exposing
// network device stats. // network device stats.
func NewNetDevCollector(config Config, registry prometheus.Registry) (Collector, error) { func NewNetDevCollector(config Config) (Collector, error) {
c := netDevCollector{ c := netDevCollector{
config: config, config: config,
registry: registry,
} }
return &c, nil return &c, nil
} }
@ -50,20 +49,23 @@ func (c *netDevCollector) Update() (updates int, err error) {
for t, value := range stats { for t, value := range stats {
key := direction + "_" + t key := direction + "_" + t
if _, ok := netStatsMetrics[key]; !ok { if _, ok := netStatsMetrics[key]; !ok {
netStatsMetrics[key] = prometheus.NewGauge() gv := prometheus.NewGaugeVec(
c.registry.Register( prometheus.GaugeOpts{
"node_network_"+key, Namespace: Namespace,
t+" "+direction+" from /proc/net/dev", Subsystem: netStatsSubsystem,
prometheus.NilLabels, Name: key,
netStatsMetrics[key], Help: fmt.Sprintf("%s %s from /proc/net/dev.", t, direction),
},
[]string{"device"},
) )
netStatsMetrics[key] = prometheus.MustRegisterOrGet(gv).(*prometheus.GaugeVec)
} }
updates++ updates++
v, err := strconv.ParseFloat(value, 64) v, err := strconv.ParseFloat(value, 64)
if err != nil { if err != nil {
return updates, fmt.Errorf("Invalid value %s in netstats: %s", value, err) return updates, fmt.Errorf("Invalid value %s in netstats: %s", value, err)
} }
netStatsMetrics[key].Set(map[string]string{"device": dev}, v) netStatsMetrics[key].WithLabelValues(dev).Set(v)
} }
} }
} }

View File

@ -19,18 +19,48 @@ const (
) )
var ( var (
cpuMetrics = prometheus.NewCounter() cpuMetrics = prometheus.NewCounterVec(
intrMetric = prometheus.NewCounter() prometheus.CounterOpts{
ctxtMetric = prometheus.NewCounter() Namespace: Namespace,
btimeMetric = prometheus.NewGauge() Name: "cpu",
forksMetric = prometheus.NewCounter() Help: "Seconds the cpus spent in each mode.",
procsRunningMetric = prometheus.NewGauge() },
procsBlockedMetric = prometheus.NewGauge() []string{"cpu", "mode"},
)
intrMetric = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: Namespace,
Name: "intr",
Help: "Total number of interrupts serviced.",
})
ctxtMetric = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: Namespace,
Name: "context_switches",
Help: "Total number of context switches.",
})
forksMetric = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: Namespace,
Name: "forks",
Help: "Total number of forks.",
})
btimeMetric = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: Namespace,
Name: "boot_time",
Help: "Node boot time, in unixtime.",
})
procsRunningMetric = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: Namespace,
Name: "procs_running",
Help: "Number of processes in runnable state.",
})
procsBlockedMetric = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: Namespace,
Name: "procs_blocked",
Help: "Number of processes blocked waiting for I/O to complete.",
})
) )
type statCollector struct { type statCollector struct {
registry prometheus.Registry config Config
config Config
} }
func init() { func init() {
@ -39,53 +69,31 @@ func init() {
// Takes a config struct and prometheus registry and returns a new Collector exposing // Takes a config struct and prometheus registry and returns a new Collector exposing
// network device stats. // network device stats.
func NewStatCollector(config Config, registry prometheus.Registry) (Collector, error) { func NewStatCollector(config Config) (Collector, error) {
c := statCollector{ c := statCollector{
config: config, config: config,
registry: registry, }
if _, err := prometheus.RegisterOrGet(cpuMetrics); err != nil {
return nil, err
}
if _, err := prometheus.RegisterOrGet(intrMetric); err != nil {
return nil, err
}
if _, err := prometheus.RegisterOrGet(ctxtMetric); err != nil {
return nil, err
}
if _, err := prometheus.RegisterOrGet(forksMetric); err != nil {
return nil, err
}
if _, err := prometheus.RegisterOrGet(btimeMetric); err != nil {
return nil, err
}
if _, err := prometheus.RegisterOrGet(procsRunningMetric); err != nil {
return nil, err
}
if _, err := prometheus.RegisterOrGet(procsBlockedMetric); err != nil {
return nil, err
} }
registry.Register(
"node_cpu",
"Seconds the cpus spent in each mode.",
prometheus.NilLabels,
cpuMetrics,
)
registry.Register(
"node_intr",
"Total number of interrupts serviced",
prometheus.NilLabels,
intrMetric,
)
registry.Register(
"node_context_switches",
"Total number of context switches.",
prometheus.NilLabels,
ctxtMetric,
)
registry.Register(
"node_forks",
"Total number of forks.",
prometheus.NilLabels,
forksMetric,
)
registry.Register(
"node_boot_time",
"Node boot time, in unixtime.",
prometheus.NilLabels,
btimeMetric,
)
registry.Register(
"node_procs_running",
"Number of processes in runnable state.",
prometheus.NilLabels,
procsRunningMetric,
)
registry.Register(
"node_procs_blocked",
"Number of processes blocked waiting for I/O to complete.",
prometheus.NilLabels,
procsBlockedMetric,
)
return &c, nil return &c, nil
} }
@ -113,9 +121,9 @@ func (c *statCollector) Update() (updates int, err error) {
if err != nil { if err != nil {
return updates, err return updates, err
} }
// Convert from ticks to seconds // Convert from ticks to seconds
value /= float64(C.sysconf(C._SC_CLK_TCK)) value /= float64(C.sysconf(C._SC_CLK_TCK))
cpuMetrics.Set(map[string]string{"cpu": parts[0], "mode": cpuFields[i]}, value) cpuMetrics.With(prometheus.Labels{"cpu": parts[0], "mode": cpuFields[i]}).Set(value)
} }
case parts[0] == "intr": case parts[0] == "intr":
// Only expose the overall number, use the 'interrupts' collector for more detail. // Only expose the overall number, use the 'interrupts' collector for more detail.
@ -123,37 +131,37 @@ func (c *statCollector) Update() (updates int, err error) {
if err != nil { if err != nil {
return updates, err return updates, err
} }
intrMetric.Set(prometheus.NilLabels, value) intrMetric.Set(value)
case parts[0] == "ctxt": case parts[0] == "ctxt":
value, err := strconv.ParseFloat(parts[1], 64) value, err := strconv.ParseFloat(parts[1], 64)
if err != nil { if err != nil {
return updates, err return updates, err
} }
ctxtMetric.Set(prometheus.NilLabels, value) ctxtMetric.Set(value)
case parts[0] == "processes": case parts[0] == "processes":
value, err := strconv.ParseFloat(parts[1], 64) value, err := strconv.ParseFloat(parts[1], 64)
if err != nil { if err != nil {
return updates, err return updates, err
} }
forksMetric.Set(prometheus.NilLabels, value) forksMetric.Set(value)
case parts[0] == "btime": case parts[0] == "btime":
value, err := strconv.ParseFloat(parts[1], 64) value, err := strconv.ParseFloat(parts[1], 64)
if err != nil { if err != nil {
return updates, err return updates, err
} }
btimeMetric.Set(prometheus.NilLabels, value) btimeMetric.Set(value)
case parts[0] == "procs_running": case parts[0] == "procs_running":
value, err := strconv.ParseFloat(parts[1], 64) value, err := strconv.ParseFloat(parts[1], 64)
if err != nil { if err != nil {
return updates, err return updates, err
} }
procsRunningMetric.Set(prometheus.NilLabels, value) procsRunningMetric.Set(value)
case parts[0] == "procs_blocked": case parts[0] == "procs_blocked":
value, err := strconv.ParseFloat(parts[1], 64) value, err := strconv.ParseFloat(parts[1], 64)
if err != nil { if err != nil {
return updates, err return updates, err
} }
procsBlockedMetric.Set(prometheus.NilLabels, value) procsBlockedMetric.Set(value)
} }
} }
return updates, err return updates, err

View File

@ -17,10 +17,11 @@ import (
"github.com/golang/glog" "github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/exp"
"github.com/prometheus/node_exporter/collector" "github.com/prometheus/node_exporter/collector"
) )
const subsystem = "exporter"
var ( var (
configFile = flag.String("config", "node_exporter.conf", "config file.") configFile = flag.String("config", "node_exporter.conf", "config file.")
memProfile = flag.String("memprofile", "", "write memory profile to this file") memProfile = flag.String("memprofile", "", "write memory profile to this file")
@ -28,8 +29,27 @@ var (
enabledCollectors = flag.String("enabledCollectors", "attributes,diskstats,filesystem,loadavg,meminfo,stat,netdev", "comma-seperated list of collectors to use") enabledCollectors = flag.String("enabledCollectors", "attributes,diskstats,filesystem,loadavg,meminfo,stat,netdev", "comma-seperated list of collectors to use")
printCollectors = flag.Bool("printCollectors", false, "If true, print available collectors and exit") printCollectors = flag.Bool("printCollectors", false, "If true, print available collectors and exit")
interval = flag.Duration("interval", 60*time.Second, "refresh interval") interval = flag.Duration("interval", 60*time.Second, "refresh interval")
scrapeDurations = prometheus.NewDefaultHistogram()
metricsUpdated = prometheus.NewGauge() collectorLabelNames = []string{"collector", "result"}
scrapeDurations = prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Namespace: collector.Namespace,
Subsystem: subsystem,
Name: "scrape_duration_seconds",
Help: "node_exporter: Duration of a scrape job.",
},
collectorLabelNames,
)
metricsUpdated = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: collector.Namespace,
Subsystem: subsystem,
Name: "metrics_updated",
Help: "node_exporter: Number of metrics updated.",
},
collectorLabelNames,
)
) )
func main() { func main() {
@ -41,14 +61,13 @@ func main() {
} }
return return
} }
registry := prometheus.NewRegistry() collectors, err := loadCollectors(*configFile)
collectors, err := loadCollectors(*configFile, registry)
if err != nil { if err != nil {
log.Fatalf("Couldn't load config and collectors: %s", err) log.Fatalf("Couldn't load config and collectors: %s", err)
} }
registry.Register("node_exporter_scrape_duration_seconds", "node_exporter: Duration of a scrape job.", prometheus.NilLabels, scrapeDurations) prometheus.MustRegister(scrapeDurations)
registry.Register("node_exporter_metrics_updated", "node_exporter: Number of metrics updated.", prometheus.NilLabels, metricsUpdated) prometheus.MustRegister(metricsUpdated)
glog.Infof("Enabled collectors:") glog.Infof("Enabled collectors:")
for n, _ := range collectors { for n, _ := range collectors {
@ -60,7 +79,7 @@ func main() {
signal.Notify(sigHup, syscall.SIGHUP) signal.Notify(sigHup, syscall.SIGHUP)
signal.Notify(sigUsr1, syscall.SIGUSR1) signal.Notify(sigUsr1, syscall.SIGUSR1)
go serveStatus(registry) go serveStatus()
glog.Infof("Starting initial collection") glog.Infof("Starting initial collection")
collect(collectors) collect(collectors)
@ -69,7 +88,7 @@ func main() {
for { for {
select { select {
case <-sigHup: case <-sigHup:
collectors, err = loadCollectors(*configFile, registry) collectors, err = loadCollectors(*configFile)
if err != nil { if err != nil {
log.Fatalf("Couldn't load config and collectors: %s", err) log.Fatalf("Couldn't load config and collectors: %s", err)
} }
@ -96,7 +115,7 @@ func main() {
} }
func loadCollectors(file string, registry prometheus.Registry) (map[string]collector.Collector, error) { func loadCollectors(file string) (map[string]collector.Collector, error) {
collectors := map[string]collector.Collector{} collectors := map[string]collector.Collector{}
config, err := getConfig(file) config, err := getConfig(file)
if err != nil { if err != nil {
@ -107,7 +126,7 @@ func loadCollectors(file string, registry prometheus.Registry) (map[string]colle
if !ok { if !ok {
log.Fatalf("Collector '%s' not available", name) log.Fatalf("Collector '%s' not available", name)
} }
c, err := fn(*config, registry) c, err := fn(*config)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -126,9 +145,9 @@ func getConfig(file string) (*collector.Config, error) {
return config, json.Unmarshal(bytes, &config) return config, json.Unmarshal(bytes, &config)
} }
func serveStatus(registry prometheus.Registry) { func serveStatus() {
exp.Handle(prometheus.ExpositionResource, registry.Handler()) http.Handle("/metrics", prometheus.Handler())
http.ListenAndServe(*listeningAddress, exp.DefaultCoarseMux) http.ListenAndServe(*listeningAddress, nil)
} }
func collect(collectors map[string]collector.Collector) { func collect(collectors map[string]collector.Collector) {
@ -147,17 +166,15 @@ func Execute(name string, c collector.Collector) {
begin := time.Now() begin := time.Now()
updates, err := c.Update() updates, err := c.Update()
duration := time.Since(begin) duration := time.Since(begin)
var result string
label := map[string]string{
"collector": name,
}
if err != nil { if err != nil {
glog.Infof("ERROR: %s failed after %fs: %s", name, duration.Seconds(), err) glog.Infof("ERROR: %s failed after %fs: %s", name, duration.Seconds(), err)
label["result"] = "error" result = "error"
} else { } else {
glog.Infof("OK: %s success after %fs.", name, duration.Seconds()) glog.Infof("OK: %s success after %fs.", name, duration.Seconds())
label["result"] = "success" result = "success"
} }
scrapeDurations.Add(label, duration.Seconds()) scrapeDurations.WithLabelValues(name, result).Observe(duration.Seconds())
metricsUpdated.Set(label, float64(updates)) metricsUpdated.WithLabelValues(name, result).Set(float64(updates))
} }