mirror of
https://github.com/prometheus/node_exporter.git
synced 2024-12-28 15:09:45 -08:00
Move exporter to main and listen/interval to flags
This commit is contained in:
parent
9f0dcc1d91
commit
3ac5222f8b
21
collector/collector.go
Normal file
21
collector/collector.go
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
// Exporter is a prometheus exporter using multiple Factories to collect and export system metrics.
|
||||||
|
package collector
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Factories []func(Config, prometheus.Registry) (Collector, error)
|
||||||
|
|
||||||
|
// Interface a collector has to implement.
|
||||||
|
type Collector interface {
|
||||||
|
// Get new metrics and expose them via prometheus registry.
|
||||||
|
Update() (n int, err error)
|
||||||
|
|
||||||
|
// Returns the name of the collector
|
||||||
|
Name() string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
Attributes map[string]string `json:"attributes"`
|
||||||
|
}
|
|
@ -1,17 +1,18 @@
|
||||||
// +build ganglia
|
// +build ganglia
|
||||||
|
|
||||||
package exporter
|
package collector
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
|
||||||
"github.com/prometheus/node_exporter/exporter/ganglia"
|
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"regexp"
|
"regexp"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
"github.com/prometheus/node_exporter/collector/ganglia"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -23,18 +24,18 @@ const (
|
||||||
type gmondCollector struct {
|
type gmondCollector struct {
|
||||||
name string
|
name string
|
||||||
Metrics map[string]prometheus.Gauge
|
Metrics map[string]prometheus.Gauge
|
||||||
config config
|
config Config
|
||||||
registry prometheus.Registry
|
registry prometheus.Registry
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
collectorFactories = append(collectorFactories, NewGmondCollector)
|
Factories = append(Factories, NewGmondCollector)
|
||||||
}
|
}
|
||||||
|
|
||||||
var illegalCharsRE = regexp.MustCompile(`[^a-zA-Z0-9_]`)
|
var illegalCharsRE = regexp.MustCompile(`[^a-zA-Z0-9_]`)
|
||||||
|
|
||||||
// Takes a config struct and prometheus registry and returns a new Collector scraping ganglia.
|
// Takes a config struct and prometheus registry and returns a new Collector scraping ganglia.
|
||||||
func NewGmondCollector(config config, registry prometheus.Registry) (Collector, error) {
|
func NewGmondCollector(config Config, registry prometheus.Registry) (Collector, error) {
|
||||||
c := gmondCollector{
|
c := gmondCollector{
|
||||||
name: "gmond_collector",
|
name: "gmond_collector",
|
||||||
config: config,
|
config: config,
|
|
@ -1,12 +1,15 @@
|
||||||
package exporter
|
package collector
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var verbose = flag.Bool("verbose", false, "Verbose output.")
|
||||||
|
|
||||||
func debug(name string, format string, a ...interface{}) {
|
func debug(name string, format string, a ...interface{}) {
|
||||||
if *verbose {
|
if *verbose {
|
||||||
f := fmt.Sprintf("%s: %s", name, format)
|
f := fmt.Sprintf("%s: %s", name, format)
|
|
@ -1,11 +1,10 @@
|
||||||
// +build !nonative
|
// +build !nonative
|
||||||
|
|
||||||
package exporter
|
package collector
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
@ -13,6 +12,8 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -42,16 +43,16 @@ type nativeCollector struct {
|
||||||
netStats prometheus.Counter
|
netStats prometheus.Counter
|
||||||
diskStats prometheus.Counter
|
diskStats prometheus.Counter
|
||||||
name string
|
name string
|
||||||
config config
|
config Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
collectorFactories = append(collectorFactories, NewNativeCollector)
|
Factories = append(Factories, NewNativeCollector)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 NewNativeCollector(config config, registry prometheus.Registry) (Collector, error) {
|
func NewNativeCollector(config Config, registry prometheus.Registry) (Collector, error) {
|
||||||
c := nativeCollector{
|
c := nativeCollector{
|
||||||
name: "native_collector",
|
name: "native_collector",
|
||||||
config: config,
|
config: config,
|
||||||
|
@ -160,7 +161,7 @@ func (c *nativeCollector) Update() (updates int, err error) {
|
||||||
updates++
|
updates++
|
||||||
fv, err := strconv.ParseFloat(value, 64)
|
fv, err := strconv.ParseFloat(value, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return updates, fmt.Errorf("Invalid value in interrupts: %s", fv, err)
|
return updates, fmt.Errorf("Invalid value %s in interrupts: %s", value, err)
|
||||||
}
|
}
|
||||||
labels := map[string]string{
|
labels := map[string]string{
|
||||||
"CPU": strconv.Itoa(cpuNo),
|
"CPU": strconv.Itoa(cpuNo),
|
|
@ -1,6 +1,6 @@
|
||||||
// +build runit
|
// +build runit
|
||||||
|
|
||||||
package exporter
|
package collector
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
@ -9,17 +9,17 @@ import (
|
||||||
|
|
||||||
type runitCollector struct {
|
type runitCollector struct {
|
||||||
name string
|
name string
|
||||||
config config
|
config Config
|
||||||
state prometheus.Gauge
|
state prometheus.Gauge
|
||||||
stateDesired prometheus.Gauge
|
stateDesired prometheus.Gauge
|
||||||
stateNormal prometheus.Gauge
|
stateNormal prometheus.Gauge
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
collectorFactories = append(collectorFactories, NewRunitCollector)
|
Factories = append(Factories, NewRunitCollector)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRunitCollector(config config, registry prometheus.Registry) (Collector, error) {
|
func NewRunitCollector(config Config, registry prometheus.Registry) (Collector, error) {
|
||||||
c := runitCollector{
|
c := runitCollector{
|
||||||
name: "runit_collector",
|
name: "runit_collector",
|
||||||
config: config,
|
config: config,
|
|
@ -1,167 +0,0 @@
|
||||||
// Exporter is a prometheus exporter using multiple collectorFactories to collect and export system metrics.
|
|
||||||
package exporter
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
|
||||||
"github.com/prometheus/client_golang/prometheus/exp"
|
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"os/signal"
|
|
||||||
"runtime/pprof"
|
|
||||||
"sync"
|
|
||||||
"syscall"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
var verbose = flag.Bool("verbose", false, "Verbose output.")
|
|
||||||
var collectorFactories []func(config, prometheus.Registry) (Collector, error)
|
|
||||||
|
|
||||||
// Interface a collector has to implement.
|
|
||||||
type Collector interface {
|
|
||||||
// Get new metrics and expose them via prometheus registry.
|
|
||||||
Update() (n int, err error)
|
|
||||||
|
|
||||||
// Returns the name of the collector
|
|
||||||
Name() string
|
|
||||||
}
|
|
||||||
|
|
||||||
type config struct {
|
|
||||||
Attributes map[string]string `json:"attributes"`
|
|
||||||
ListeningAddress string `json:"listeningAddress"`
|
|
||||||
ScrapeInterval int `json:"scrapeInterval"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *exporter) loadConfig() (err error) {
|
|
||||||
log.Printf("Reading config %s", e.configFile)
|
|
||||||
bytes, err := ioutil.ReadFile(e.configFile)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
return json.Unmarshal(bytes, &e.config) // Make sure this is safe
|
|
||||||
}
|
|
||||||
|
|
||||||
type exporter struct {
|
|
||||||
configFile string
|
|
||||||
listeningAddress string
|
|
||||||
scrapeInterval time.Duration
|
|
||||||
scrapeDurations prometheus.Histogram
|
|
||||||
metricsUpdated prometheus.Gauge
|
|
||||||
config config
|
|
||||||
registry prometheus.Registry
|
|
||||||
Collectors []Collector
|
|
||||||
MemProfile string
|
|
||||||
}
|
|
||||||
|
|
||||||
// New takes the path to a config file and returns an exporter instance
|
|
||||||
func New(configFile string) (e exporter, err error) {
|
|
||||||
registry := prometheus.NewRegistry()
|
|
||||||
e = exporter{
|
|
||||||
configFile: configFile,
|
|
||||||
scrapeDurations: prometheus.NewDefaultHistogram(),
|
|
||||||
metricsUpdated: prometheus.NewGauge(),
|
|
||||||
listeningAddress: ":8080",
|
|
||||||
scrapeInterval: 60 * time.Second,
|
|
||||||
registry: registry,
|
|
||||||
}
|
|
||||||
|
|
||||||
err = e.loadConfig()
|
|
||||||
if err != nil {
|
|
||||||
return e, fmt.Errorf("Couldn't read config: %s", err)
|
|
||||||
}
|
|
||||||
for _, fn := range collectorFactories {
|
|
||||||
c, err := fn(e.config, e.registry)
|
|
||||||
if err != nil {
|
|
||||||
return e, err
|
|
||||||
}
|
|
||||||
e.Collectors = append(e.Collectors, c)
|
|
||||||
}
|
|
||||||
|
|
||||||
if e.config.ListeningAddress != "" {
|
|
||||||
e.listeningAddress = e.config.ListeningAddress
|
|
||||||
}
|
|
||||||
if e.config.ScrapeInterval != 0 {
|
|
||||||
e.scrapeInterval = time.Duration(e.config.ScrapeInterval) * time.Second
|
|
||||||
}
|
|
||||||
|
|
||||||
registry.Register("node_exporter_scrape_duration_seconds", "node_exporter: Duration of a scrape job.", prometheus.NilLabels, e.scrapeDurations)
|
|
||||||
registry.Register("node_exporter_metrics_updated", "node_exporter: Number of metrics updated.", prometheus.NilLabels, e.metricsUpdated)
|
|
||||||
|
|
||||||
return e, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *exporter) serveStatus() {
|
|
||||||
exp.Handle(prometheus.ExpositionResource, e.registry.Handler())
|
|
||||||
http.ListenAndServe(e.listeningAddress, exp.DefaultCoarseMux)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *exporter) Execute(c Collector) {
|
|
||||||
begin := time.Now()
|
|
||||||
updates, err := c.Update()
|
|
||||||
duration := time.Since(begin)
|
|
||||||
|
|
||||||
label := map[string]string{
|
|
||||||
"collector": c.Name(),
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("ERROR: %s failed after %fs: %s", c.Name(), duration.Seconds(), err)
|
|
||||||
label["result"] = "error"
|
|
||||||
} else {
|
|
||||||
log.Printf("OK: %s success after %fs.", c.Name(), duration.Seconds())
|
|
||||||
label["result"] = "success"
|
|
||||||
}
|
|
||||||
e.scrapeDurations.Add(label, duration.Seconds())
|
|
||||||
e.metricsUpdated.Set(label, float64(updates))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *exporter) Loop() {
|
|
||||||
sigHup := make(chan os.Signal)
|
|
||||||
sigUsr1 := make(chan os.Signal)
|
|
||||||
signal.Notify(sigHup, syscall.SIGHUP)
|
|
||||||
signal.Notify(sigUsr1, syscall.SIGUSR1)
|
|
||||||
|
|
||||||
go e.serveStatus()
|
|
||||||
|
|
||||||
tick := time.Tick(e.scrapeInterval)
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-sigHup:
|
|
||||||
err := e.loadConfig()
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Couldn't reload config: %s", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
log.Printf("Got new config")
|
|
||||||
tick = time.Tick(e.scrapeInterval)
|
|
||||||
|
|
||||||
case <-tick:
|
|
||||||
log.Printf("Starting new scrape interval")
|
|
||||||
wg := sync.WaitGroup{}
|
|
||||||
wg.Add(len(e.Collectors))
|
|
||||||
for _, c := range e.Collectors {
|
|
||||||
go func(c Collector) {
|
|
||||||
e.Execute(c)
|
|
||||||
wg.Done()
|
|
||||||
}(c)
|
|
||||||
}
|
|
||||||
wg.Wait()
|
|
||||||
|
|
||||||
case <-sigUsr1:
|
|
||||||
log.Printf("got signal")
|
|
||||||
if e.MemProfile != "" {
|
|
||||||
log.Printf("Writing memory profile to %s", e.MemProfile)
|
|
||||||
f, err := os.Create(e.MemProfile)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
pprof.WriteHeapProfile(f)
|
|
||||||
f.Close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,5 +1,4 @@
|
||||||
{
|
{
|
||||||
"scrapeInterval": 10,
|
|
||||||
"attributes" : {
|
"attributes" : {
|
||||||
"web-server" : "1",
|
"web-server" : "1",
|
||||||
"zone" : "a",
|
"zone" : "a",
|
||||||
|
|
127
node_exporter.go
127
node_exporter.go
|
@ -1,27 +1,140 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"runtime/pprof"
|
||||||
|
"sync"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/prometheus/node_exporter/exporter"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
"github.com/prometheus/client_golang/prometheus/exp"
|
||||||
|
"github.com/prometheus/node_exporter/collector"
|
||||||
)
|
)
|
||||||
|
|
||||||
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")
|
||||||
|
listeningAddress = flag.String("listen", ":8080", "address to listen on")
|
||||||
|
interval = flag.Duration("interval", 60*time.Second, "refresh interval")
|
||||||
|
scrapeDurations = prometheus.NewDefaultHistogram()
|
||||||
|
metricsUpdated = prometheus.NewGauge()
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
registry := prometheus.NewRegistry()
|
||||||
exporter, err := exporter.New(*configFile)
|
collectors, err := loadCollectors(*configFile, registry)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Couldn't instantiate exporter: %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)
|
||||||
|
registry.Register("node_exporter_metrics_updated", "node_exporter: Number of metrics updated.", prometheus.NilLabels, metricsUpdated)
|
||||||
|
|
||||||
log.Printf("Registered collectors:")
|
log.Printf("Registered collectors:")
|
||||||
for _, c := range exporter.Collectors {
|
for _, c := range collectors {
|
||||||
log.Print(" - ", c.Name())
|
log.Print(" - ", c.Name())
|
||||||
}
|
}
|
||||||
exporter.Loop()
|
|
||||||
|
sigHup := make(chan os.Signal)
|
||||||
|
sigUsr1 := make(chan os.Signal)
|
||||||
|
signal.Notify(sigHup, syscall.SIGHUP)
|
||||||
|
signal.Notify(sigUsr1, syscall.SIGUSR1)
|
||||||
|
|
||||||
|
go serveStatus(registry)
|
||||||
|
|
||||||
|
tick := time.Tick(*interval)
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-sigHup:
|
||||||
|
collectors, err = loadCollectors(*configFile, registry)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Couldn't load config and collectors: %s", err)
|
||||||
|
}
|
||||||
|
log.Printf("Reload collectors and config")
|
||||||
|
tick = time.Tick(*interval)
|
||||||
|
|
||||||
|
case <-tick:
|
||||||
|
log.Printf("Starting new interval")
|
||||||
|
wg := sync.WaitGroup{}
|
||||||
|
wg.Add(len(collectors))
|
||||||
|
for _, c := range collectors {
|
||||||
|
go func(c collector.Collector) {
|
||||||
|
Execute(c)
|
||||||
|
wg.Done()
|
||||||
|
}(c)
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
case <-sigUsr1:
|
||||||
|
log.Printf("got signal")
|
||||||
|
if *memProfile != "" {
|
||||||
|
log.Printf("Writing memory profile to %s", *memProfile)
|
||||||
|
f, err := os.Create(*memProfile)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
pprof.WriteHeapProfile(f)
|
||||||
|
f.Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadCollectors(file string, registry prometheus.Registry) ([]collector.Collector, error) {
|
||||||
|
collectors := []collector.Collector{}
|
||||||
|
config, err := getConfig(file)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Couldn't read config %s: %s", file, err)
|
||||||
|
}
|
||||||
|
for _, fn := range collector.Factories {
|
||||||
|
c, err := fn(*config, registry)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
collectors = append(collectors, c)
|
||||||
|
}
|
||||||
|
return collectors, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getConfig(file string) (*collector.Config, error) {
|
||||||
|
config := &collector.Config{}
|
||||||
|
log.Printf("Reading config %s", *configFile)
|
||||||
|
bytes, err := ioutil.ReadFile(*configFile)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return config, json.Unmarshal(bytes, &config)
|
||||||
|
}
|
||||||
|
|
||||||
|
func serveStatus(registry prometheus.Registry) {
|
||||||
|
exp.Handle(prometheus.ExpositionResource, registry.Handler())
|
||||||
|
http.ListenAndServe(*listeningAddress, exp.DefaultCoarseMux)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Execute(c collector.Collector) {
|
||||||
|
begin := time.Now()
|
||||||
|
updates, err := c.Update()
|
||||||
|
duration := time.Since(begin)
|
||||||
|
|
||||||
|
label := map[string]string{
|
||||||
|
"collector": c.Name(),
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("ERROR: %s failed after %fs: %s", c.Name(), duration.Seconds(), err)
|
||||||
|
label["result"] = "error"
|
||||||
|
} else {
|
||||||
|
log.Printf("OK: %s success after %fs.", c.Name(), duration.Seconds())
|
||||||
|
label["result"] = "success"
|
||||||
|
}
|
||||||
|
scrapeDurations.Add(label, duration.Seconds())
|
||||||
|
metricsUpdated.Set(label, float64(updates))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue