2015-09-26 15:36:40 +00:00
|
|
|
// Copyright 2015 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-02-28 16:44:53 +00:00
|
|
|
// Package collector includes all individual collectors to gather and export system metrics.
|
2014-02-18 11:35:11 +00:00
|
|
|
package collector
|
|
|
|
|
2014-10-29 14:16:43 +00:00
|
|
|
import (
|
2017-09-28 13:06:26 +00:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2014-10-29 14:16:43 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2016-12-22 16:38:25 +00:00
|
|
|
"github.com/prometheus/common/log"
|
2017-09-28 13:06:26 +00:00
|
|
|
"gopkg.in/alecthomas/kingpin.v2"
|
2014-10-29 14:16:43 +00:00
|
|
|
)
|
|
|
|
|
2017-02-28 16:44:53 +00:00
|
|
|
// Namespace defines the common namespace to be used by all metrics.
|
2017-09-28 13:06:26 +00:00
|
|
|
const namespace = "node"
|
2014-02-18 11:35:11 +00:00
|
|
|
|
2017-09-28 13:06:26 +00:00
|
|
|
var (
|
|
|
|
scrapeDurationDesc = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, "scrape", "collector_duration_seconds"),
|
|
|
|
"node_exporter: Duration of a collector scrape.",
|
|
|
|
[]string{"collector"},
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
scrapeSuccessDesc = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, "scrape", "collector_success"),
|
|
|
|
"node_exporter: Whether a collector succeeded.",
|
|
|
|
[]string{"collector"},
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
)
|
2014-02-18 11:35:11 +00:00
|
|
|
|
2016-12-22 16:38:25 +00:00
|
|
|
func warnDeprecated(collector string) {
|
|
|
|
log.Warnf("The %s collector is deprecated and will be removed in the future!", collector)
|
|
|
|
}
|
|
|
|
|
2017-09-28 13:06:26 +00:00
|
|
|
const (
|
|
|
|
defaultEnabled = true
|
|
|
|
defaultDisabled = false
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
factories = make(map[string]func() (Collector, error))
|
|
|
|
collectorState = make(map[string]*bool)
|
|
|
|
)
|
|
|
|
|
|
|
|
func registerCollector(collector string, isDefaultEnabled bool, factory func() (Collector, error)) {
|
|
|
|
var helpDefaultState string
|
|
|
|
if isDefaultEnabled {
|
|
|
|
helpDefaultState = "enabled"
|
|
|
|
} else {
|
|
|
|
helpDefaultState = "disabled"
|
|
|
|
}
|
|
|
|
|
|
|
|
flagName := fmt.Sprintf("collector.%s", collector)
|
|
|
|
flagHelp := fmt.Sprintf("Enable the %s collector (default: %s).", collector, helpDefaultState)
|
|
|
|
defaultValue := fmt.Sprintf("%v", isDefaultEnabled)
|
|
|
|
|
|
|
|
flag := kingpin.Flag(flagName, flagHelp).Default(defaultValue).Bool()
|
|
|
|
collectorState[collector] = flag
|
|
|
|
|
|
|
|
factories[collector] = factory
|
|
|
|
}
|
|
|
|
|
|
|
|
// NodeCollector implements the prometheus.Collector interface.
|
|
|
|
type nodeCollector struct {
|
|
|
|
Collectors map[string]Collector
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewNodeCollector creates a new NodeCollector
|
2017-10-14 12:23:42 +00:00
|
|
|
func NewNodeCollector(filters ...string) (*nodeCollector, error) {
|
|
|
|
f := make(map[string]bool)
|
|
|
|
for _, filter := range filters {
|
|
|
|
enabled, exist := collectorState[filter]
|
|
|
|
if !exist {
|
|
|
|
return nil, fmt.Errorf("missing collector: %s", filter)
|
|
|
|
}
|
|
|
|
if !*enabled {
|
|
|
|
return nil, fmt.Errorf("disabled collector: %s", filter)
|
|
|
|
}
|
|
|
|
f[filter] = true
|
|
|
|
}
|
2017-09-28 13:06:26 +00:00
|
|
|
collectors := make(map[string]Collector)
|
|
|
|
for key, enabled := range collectorState {
|
|
|
|
if *enabled {
|
|
|
|
collector, err := factories[key]()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-10-14 12:23:42 +00:00
|
|
|
if len(f) == 0 || f[key] {
|
|
|
|
collectors[key] = collector
|
|
|
|
}
|
2017-09-28 13:06:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return &nodeCollector{Collectors: collectors}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Describe implements the prometheus.Collector interface.
|
|
|
|
func (n nodeCollector) Describe(ch chan<- *prometheus.Desc) {
|
|
|
|
ch <- scrapeDurationDesc
|
|
|
|
ch <- scrapeSuccessDesc
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect implements the prometheus.Collector interface.
|
|
|
|
func (n nodeCollector) Collect(ch chan<- prometheus.Metric) {
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(len(n.Collectors))
|
|
|
|
for name, c := range n.Collectors {
|
|
|
|
go func(name string, c Collector) {
|
|
|
|
execute(name, c, ch)
|
|
|
|
wg.Done()
|
|
|
|
}(name, c)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func execute(name string, c Collector, ch chan<- prometheus.Metric) {
|
|
|
|
begin := time.Now()
|
|
|
|
err := c.Update(ch)
|
|
|
|
duration := time.Since(begin)
|
|
|
|
var success float64
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("ERROR: %s collector failed after %fs: %s", name, duration.Seconds(), err)
|
|
|
|
success = 0
|
|
|
|
} else {
|
|
|
|
log.Debugf("OK: %s collector succeeded after %fs.", name, duration.Seconds())
|
|
|
|
success = 1
|
|
|
|
}
|
|
|
|
ch <- prometheus.MustNewConstMetric(scrapeDurationDesc, prometheus.GaugeValue, duration.Seconds(), name)
|
|
|
|
ch <- prometheus.MustNewConstMetric(scrapeSuccessDesc, prometheus.GaugeValue, success, name)
|
|
|
|
}
|
|
|
|
|
2017-02-28 16:44:53 +00:00
|
|
|
// Collector is the interface a collector has to implement.
|
2014-02-18 11:35:11 +00:00
|
|
|
type Collector interface {
|
|
|
|
// Get new metrics and expose them via prometheus registry.
|
2017-02-28 18:47:20 +00:00
|
|
|
Update(ch chan<- prometheus.Metric) error
|
2014-02-18 11:35:11 +00:00
|
|
|
}
|
2016-12-28 14:21:31 +00:00
|
|
|
|
|
|
|
type typedDesc struct {
|
|
|
|
desc *prometheus.Desc
|
|
|
|
valueType prometheus.ValueType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *typedDesc) mustNewConstMetric(value float64, labels ...string) prometheus.Metric {
|
|
|
|
return prometheus.MustNewConstMetric(d.desc, d.valueType, value, labels...)
|
|
|
|
}
|