Move exporter to main and listen/interval to flags
This commit is contained in:
parent
9f0dcc1d91
commit
3ac5222f8b
9 changed files with 164 additions and 193 deletions
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"`
|
||||
}
|
||||
61
collector/ganglia/format.go
Normal file
61
collector/ganglia/format.go
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
// Types for unmarshalling gmond's XML output.
|
||||
//
|
||||
// Not used elements in gmond's XML output are commented.
|
||||
// In case you want to use them, please change the names so that one
|
||||
// can understand without needing to know what the acronym stands for.
|
||||
package ganglia
|
||||
|
||||
import "encoding/xml"
|
||||
|
||||
type ExtraElement struct {
|
||||
Name string `xml:"NAME,attr"`
|
||||
Val string `xml:"VAL,attr"`
|
||||
}
|
||||
|
||||
type ExtraData struct {
|
||||
ExtraElements []ExtraElement `xml:"EXTRA_ELEMENT"`
|
||||
}
|
||||
|
||||
type Metric struct {
|
||||
Name string `xml:"NAME,attr"`
|
||||
Value float64 `xml:"VAL,attr"`
|
||||
/*
|
||||
Unit string `xml:"UNITS,attr"`
|
||||
Slope string `xml:"SLOPE,attr"`
|
||||
Tn int `xml:"TN,attr"`
|
||||
Tmax int `xml:"TMAX,attr"`
|
||||
Dmax int `xml:"DMAX,attr"`
|
||||
*/
|
||||
ExtraData ExtraData `xml:"EXTRA_DATA"`
|
||||
}
|
||||
|
||||
type Host struct {
|
||||
Name string `xml:"NAME,attr"`
|
||||
/*
|
||||
Ip string `xml:"IP,attr"`
|
||||
Tags string `xml:"TAGS,attr"`
|
||||
Reported int `xml:"REPORTED,attr"`
|
||||
Tn int `xml:"TN,attr"`
|
||||
Tmax int `xml:"TMAX,attr"`
|
||||
Dmax int `xml:"DMAX,attr"`
|
||||
Location string `xml:"LOCATION,attr"`
|
||||
GmondStarted int `xml:"GMOND_STARTED",attr"`
|
||||
*/
|
||||
Metrics []Metric `xml:"METRIC"`
|
||||
}
|
||||
|
||||
type Cluster struct {
|
||||
Name string `xml:"NAME,attr"`
|
||||
/*
|
||||
Owner string `xml:"OWNER,attr"`
|
||||
LatLong string `xml:"LATLONG,attr"`
|
||||
Url string `xml:"URL,attr"`
|
||||
Localtime int `xml:"LOCALTIME,attr"`
|
||||
*/
|
||||
Hosts []Host `xml:"HOST"`
|
||||
}
|
||||
|
||||
type Ganglia struct {
|
||||
XMLNAME xml.Name `xml:"GANGLIA_XML"`
|
||||
Clusters []Cluster `xml:"CLUSTER"`
|
||||
}
|
||||
111
collector/gmond_collector.go
Normal file
111
collector/gmond_collector.go
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
// +build ganglia
|
||||
|
||||
package collector
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"regexp"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/node_exporter/collector/ganglia"
|
||||
)
|
||||
|
||||
const (
|
||||
gangliaAddress = "127.0.0.1:8649"
|
||||
gangliaProto = "tcp"
|
||||
gangliaTimeout = 30 * time.Second
|
||||
)
|
||||
|
||||
type gmondCollector struct {
|
||||
name string
|
||||
Metrics map[string]prometheus.Gauge
|
||||
config Config
|
||||
registry prometheus.Registry
|
||||
}
|
||||
|
||||
func init() {
|
||||
Factories = append(Factories, NewGmondCollector)
|
||||
}
|
||||
|
||||
var illegalCharsRE = regexp.MustCompile(`[^a-zA-Z0-9_]`)
|
||||
|
||||
// Takes a config struct and prometheus registry and returns a new Collector scraping ganglia.
|
||||
func NewGmondCollector(config Config, registry prometheus.Registry) (Collector, error) {
|
||||
c := gmondCollector{
|
||||
name: "gmond_collector",
|
||||
config: config,
|
||||
Metrics: make(map[string]prometheus.Gauge),
|
||||
registry: registry,
|
||||
}
|
||||
|
||||
return &c, nil
|
||||
}
|
||||
|
||||
func (c *gmondCollector) Name() string { return c.name }
|
||||
|
||||
func (c *gmondCollector) setMetric(name string, labels map[string]string, metric ganglia.Metric) {
|
||||
if _, ok := c.Metrics[name]; !ok {
|
||||
var desc string
|
||||
var title string
|
||||
for _, element := range metric.ExtraData.ExtraElements {
|
||||
switch element.Name {
|
||||
case "DESC":
|
||||
desc = element.Val
|
||||
case "TITLE":
|
||||
title = element.Val
|
||||
}
|
||||
if title != "" && desc != "" {
|
||||
break
|
||||
}
|
||||
}
|
||||
debug(c.Name(), "Register %s: %s", name, desc)
|
||||
gauge := prometheus.NewGauge()
|
||||
c.Metrics[name] = gauge
|
||||
c.registry.Register(name, desc, prometheus.NilLabels, gauge) // one gauge per metric!
|
||||
}
|
||||
debug(c.Name(), "Set %s{%s}: %f", name, labels, metric.Value)
|
||||
c.Metrics[name].Set(labels, metric.Value)
|
||||
}
|
||||
|
||||
func (c *gmondCollector) Update() (updates int, err error) {
|
||||
conn, err := net.Dial(gangliaProto, gangliaAddress)
|
||||
debug(c.Name(), "gmondCollector Update")
|
||||
if err != nil {
|
||||
return updates, fmt.Errorf("Can't connect to gmond: %s", err)
|
||||
}
|
||||
conn.SetDeadline(time.Now().Add(gangliaTimeout))
|
||||
|
||||
ganglia := ganglia.Ganglia{}
|
||||
decoder := xml.NewDecoder(bufio.NewReader(conn))
|
||||
decoder.CharsetReader = toUtf8
|
||||
|
||||
err = decoder.Decode(&ganglia)
|
||||
if err != nil {
|
||||
return updates, fmt.Errorf("Couldn't parse xml: %s", err)
|
||||
}
|
||||
|
||||
for _, cluster := range ganglia.Clusters {
|
||||
for _, host := range cluster.Hosts {
|
||||
|
||||
for _, metric := range host.Metrics {
|
||||
name := illegalCharsRE.ReplaceAllString(metric.Name, "_")
|
||||
|
||||
var labels = map[string]string{
|
||||
"cluster": cluster.Name,
|
||||
}
|
||||
c.setMetric(name, labels, metric)
|
||||
updates++
|
||||
}
|
||||
}
|
||||
}
|
||||
return updates, err
|
||||
}
|
||||
|
||||
func toUtf8(charset string, input io.Reader) (io.Reader, error) {
|
||||
return input, nil //FIXME
|
||||
}
|
||||
29
collector/helper.go
Normal file
29
collector/helper.go
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
package collector
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var verbose = flag.Bool("verbose", false, "Verbose output.")
|
||||
|
||||
func debug(name string, format string, a ...interface{}) {
|
||||
if *verbose {
|
||||
f := fmt.Sprintf("%s: %s", name, format)
|
||||
log.Printf(f, a...)
|
||||
}
|
||||
}
|
||||
|
||||
func splitToInts(str string, sep string) (ints []int, err error) {
|
||||
for _, part := range strings.Split(str, sep) {
|
||||
i, err := strconv.Atoi(part)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Could not split '%s' because %s is no int: %s", str, part, err)
|
||||
}
|
||||
ints = append(ints, i)
|
||||
}
|
||||
return ints, nil
|
||||
}
|
||||
415
collector/native_collector.go
Normal file
415
collector/native_collector.go
Normal file
|
|
@ -0,0 +1,415 @@
|
|||
// +build !nonative
|
||||
|
||||
package collector
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
const (
|
||||
procLoad = "/proc/loadavg"
|
||||
procMemInfo = "/proc/meminfo"
|
||||
procInterrupts = "/proc/interrupts"
|
||||
procNetDev = "/proc/net/dev"
|
||||
procDiskStats = "/proc/diskstats"
|
||||
)
|
||||
|
||||
var (
|
||||
diskStatsHeader = []string{
|
||||
"reads_completed", "reads_merged",
|
||||
"sectors_read", "read_time_ms",
|
||||
"writes_completed", "writes_merged",
|
||||
"sectors_written", "write_time_ms",
|
||||
"io_now", "io_time_ms", "io_time_weighted",
|
||||
}
|
||||
)
|
||||
|
||||
type nativeCollector struct {
|
||||
loadAvg prometheus.Gauge
|
||||
attributes prometheus.Gauge
|
||||
lastSeen prometheus.Gauge
|
||||
memInfo prometheus.Gauge
|
||||
interrupts prometheus.Counter
|
||||
netStats prometheus.Counter
|
||||
diskStats prometheus.Counter
|
||||
name string
|
||||
config Config
|
||||
}
|
||||
|
||||
func init() {
|
||||
Factories = append(Factories, NewNativeCollector)
|
||||
}
|
||||
|
||||
// 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.
|
||||
func NewNativeCollector(config Config, registry prometheus.Registry) (Collector, error) {
|
||||
c := nativeCollector{
|
||||
name: "native_collector",
|
||||
config: config,
|
||||
loadAvg: prometheus.NewGauge(),
|
||||
attributes: prometheus.NewGauge(),
|
||||
lastSeen: prometheus.NewGauge(),
|
||||
memInfo: prometheus.NewGauge(),
|
||||
interrupts: prometheus.NewCounter(),
|
||||
netStats: prometheus.NewCounter(),
|
||||
diskStats: prometheus.NewCounter(),
|
||||
}
|
||||
|
||||
registry.Register(
|
||||
"node_load",
|
||||
"node_exporter: system load.",
|
||||
prometheus.NilLabels,
|
||||
c.loadAvg,
|
||||
)
|
||||
|
||||
registry.Register(
|
||||
"node_last_login_seconds",
|
||||
"node_exporter: seconds since last login.",
|
||||
prometheus.NilLabels,
|
||||
c.lastSeen,
|
||||
)
|
||||
|
||||
registry.Register(
|
||||
"node_attributes",
|
||||
"node_exporter: system attributes.",
|
||||
prometheus.NilLabels,
|
||||
c.attributes,
|
||||
)
|
||||
|
||||
registry.Register(
|
||||
"node_mem",
|
||||
"node_exporter: memory details.",
|
||||
prometheus.NilLabels,
|
||||
c.memInfo,
|
||||
)
|
||||
|
||||
registry.Register(
|
||||
"node_interrupts",
|
||||
"node_exporter: interrupt details.",
|
||||
prometheus.NilLabels,
|
||||
c.interrupts,
|
||||
)
|
||||
|
||||
registry.Register(
|
||||
"node_net",
|
||||
"node_exporter: network stats.",
|
||||
prometheus.NilLabels,
|
||||
c.netStats,
|
||||
)
|
||||
|
||||
registry.Register(
|
||||
"node_disk",
|
||||
"node_exporter: disk stats.",
|
||||
prometheus.NilLabels,
|
||||
c.diskStats,
|
||||
)
|
||||
return &c, nil
|
||||
}
|
||||
|
||||
func (c *nativeCollector) Name() string { return c.name }
|
||||
|
||||
func (c *nativeCollector) Update() (updates int, err error) {
|
||||
last, err := getSecondsSinceLastLogin()
|
||||
if err != nil {
|
||||
return updates, fmt.Errorf("Couldn't get last seen: %s", err)
|
||||
}
|
||||
updates++
|
||||
debug(c.Name(), "Set node_last_login_seconds: %f", last)
|
||||
c.lastSeen.Set(nil, last)
|
||||
|
||||
load, err := getLoad()
|
||||
if err != nil {
|
||||
return updates, fmt.Errorf("Couldn't get load: %s", err)
|
||||
}
|
||||
updates++
|
||||
debug(c.Name(), "Set node_load: %f", load)
|
||||
c.loadAvg.Set(nil, load)
|
||||
|
||||
debug(c.Name(), "Set node_attributes{%v}: 1", c.config.Attributes)
|
||||
c.attributes.Set(c.config.Attributes, 1)
|
||||
|
||||
memInfo, err := getMemInfo()
|
||||
if err != nil {
|
||||
return updates, fmt.Errorf("Couldn't get meminfo: %s", err)
|
||||
}
|
||||
debug(c.Name(), "Set node_mem: %#v", memInfo)
|
||||
for k, v := range memInfo {
|
||||
updates++
|
||||
fv, err := strconv.ParseFloat(v, 64)
|
||||
if err != nil {
|
||||
return updates, fmt.Errorf("Invalid value in meminfo: %s", err)
|
||||
}
|
||||
c.memInfo.Set(map[string]string{"type": k}, fv)
|
||||
}
|
||||
|
||||
interrupts, err := getInterrupts()
|
||||
if err != nil {
|
||||
return updates, fmt.Errorf("Couldn't get interrupts: %s", err)
|
||||
}
|
||||
for name, interrupt := range interrupts {
|
||||
for cpuNo, value := range interrupt.values {
|
||||
updates++
|
||||
fv, err := strconv.ParseFloat(value, 64)
|
||||
if err != nil {
|
||||
return updates, fmt.Errorf("Invalid value %s in interrupts: %s", value, err)
|
||||
}
|
||||
labels := map[string]string{
|
||||
"CPU": strconv.Itoa(cpuNo),
|
||||
"type": name,
|
||||
"info": interrupt.info,
|
||||
"devices": interrupt.devices,
|
||||
}
|
||||
c.interrupts.Set(labels, fv)
|
||||
}
|
||||
}
|
||||
|
||||
netStats, err := getNetStats()
|
||||
if err != nil {
|
||||
return updates, fmt.Errorf("Couldn't get netstats: %s", err)
|
||||
}
|
||||
for direction, devStats := range netStats {
|
||||
for dev, stats := range devStats {
|
||||
for t, value := range stats {
|
||||
updates++
|
||||
v, err := strconv.ParseFloat(value, 64)
|
||||
if err != nil {
|
||||
return updates, fmt.Errorf("Invalid value %s in interrupts: %s", value, err)
|
||||
}
|
||||
labels := map[string]string{
|
||||
"device": dev,
|
||||
"direction": direction,
|
||||
"type": t,
|
||||
}
|
||||
c.netStats.Set(labels, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
diskStats, err := getDiskStats()
|
||||
if err != nil {
|
||||
return updates, fmt.Errorf("Couldn't get diskstats: %s", err)
|
||||
}
|
||||
for dev, stats := range diskStats {
|
||||
for k, value := range stats {
|
||||
updates++
|
||||
v, err := strconv.ParseFloat(value, 64)
|
||||
if err != nil {
|
||||
return updates, fmt.Errorf("Invalid value %s in diskstats: %s", value, err)
|
||||
}
|
||||
labels := map[string]string{"device": dev, "type": k}
|
||||
c.diskStats.Set(labels, v)
|
||||
}
|
||||
}
|
||||
return updates, err
|
||||
}
|
||||
|
||||
func getLoad() (float64, error) {
|
||||
data, err := ioutil.ReadFile(procLoad)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
parts := strings.Fields(string(data))
|
||||
load, err := strconv.ParseFloat(parts[0], 64)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("Could not parse load '%s': %s", parts[0], err)
|
||||
}
|
||||
return load, nil
|
||||
}
|
||||
|
||||
func getSecondsSinceLastLogin() (float64, error) {
|
||||
who := exec.Command("who", "/var/log/wtmp", "-l", "-u", "-s")
|
||||
|
||||
output, err := who.StdoutPipe()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
err = who.Start()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
reader := bufio.NewReader(output)
|
||||
|
||||
var last time.Time
|
||||
for {
|
||||
line, isPrefix, err := reader.ReadLine()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if isPrefix {
|
||||
return 0, fmt.Errorf("line to long: %s(...)", line)
|
||||
}
|
||||
|
||||
fields := strings.Fields(string(line))
|
||||
lastDate := fields[2]
|
||||
lastTime := fields[3]
|
||||
|
||||
dateParts, err := splitToInts(lastDate, "-") // 2013-04-16
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("Couldn't parse date in line '%s': %s", fields, err)
|
||||
}
|
||||
|
||||
timeParts, err := splitToInts(lastTime, ":") // 11:33
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("Couldn't parse time in line '%s': %s", fields, err)
|
||||
}
|
||||
|
||||
last_t := time.Date(dateParts[0], time.Month(dateParts[1]), dateParts[2], timeParts[0], timeParts[1], 0, 0, time.UTC)
|
||||
last = last_t
|
||||
}
|
||||
err = who.Wait()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return float64(time.Now().Sub(last).Seconds()), nil
|
||||
}
|
||||
|
||||
func getMemInfo() (map[string]string, error) {
|
||||
memInfo := map[string]string{}
|
||||
fh, err := os.Open(procMemInfo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer fh.Close()
|
||||
scanner := bufio.NewScanner(fh)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
parts := strings.Fields(string(line))
|
||||
key := ""
|
||||
switch len(parts) {
|
||||
case 2: // no unit
|
||||
key = parts[0][:len(parts[0])-1] // remove trailing : from key
|
||||
case 3: // has unit
|
||||
key = fmt.Sprintf("%s_%s", parts[0][:len(parts[0])-1], parts[2])
|
||||
default:
|
||||
return nil, fmt.Errorf("Invalid line in %s: %s", procMemInfo, line)
|
||||
}
|
||||
memInfo[key] = parts[1]
|
||||
}
|
||||
return memInfo, nil
|
||||
|
||||
}
|
||||
|
||||
type interrupt struct {
|
||||
info string
|
||||
devices string
|
||||
values []string
|
||||
}
|
||||
|
||||
func getInterrupts() (map[string]interrupt, error) {
|
||||
interrupts := map[string]interrupt{}
|
||||
fh, err := os.Open(procInterrupts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer fh.Close()
|
||||
scanner := bufio.NewScanner(fh)
|
||||
if !scanner.Scan() {
|
||||
return nil, fmt.Errorf("%s empty", procInterrupts)
|
||||
}
|
||||
cpuNum := len(strings.Fields(string(scanner.Text()))) // one header per cpu
|
||||
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
parts := strings.Fields(string(line))
|
||||
if len(parts) < cpuNum+2 { // irq + one column per cpu + details,
|
||||
continue // we ignore ERR and MIS for now
|
||||
}
|
||||
intName := parts[0][:len(parts[0])-1] // remove trailing :
|
||||
intr := interrupt{
|
||||
values: parts[1:cpuNum],
|
||||
}
|
||||
|
||||
if _, err := strconv.Atoi(intName); err == nil { // numeral interrupt
|
||||
intr.info = parts[cpuNum+1]
|
||||
intr.devices = strings.Join(parts[cpuNum+2:], " ")
|
||||
} else {
|
||||
intr.info = strings.Join(parts[cpuNum+1:], " ")
|
||||
}
|
||||
interrupts[intName] = intr
|
||||
}
|
||||
return interrupts, nil
|
||||
}
|
||||
|
||||
func getNetStats() (map[string]map[string]map[string]string, error) {
|
||||
netStats := map[string]map[string]map[string]string{}
|
||||
netStats["transmit"] = map[string]map[string]string{}
|
||||
netStats["receive"] = map[string]map[string]string{}
|
||||
fh, err := os.Open(procNetDev)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer fh.Close()
|
||||
scanner := bufio.NewScanner(fh)
|
||||
scanner.Scan() // skip first header
|
||||
scanner.Scan()
|
||||
parts := strings.Split(string(scanner.Text()), "|")
|
||||
if len(parts) != 3 { // interface + receive + transmit
|
||||
return nil, fmt.Errorf("Invalid header line in %s: %s",
|
||||
procNetDev, scanner.Text())
|
||||
}
|
||||
header := strings.Fields(parts[1])
|
||||
for scanner.Scan() {
|
||||
parts := strings.Fields(string(scanner.Text()))
|
||||
if len(parts) != 2*len(header)+1 {
|
||||
return nil, fmt.Errorf("Invalid line in %s: %s",
|
||||
procNetDev, scanner.Text())
|
||||
}
|
||||
|
||||
dev := parts[0][:len(parts[0])-1]
|
||||
receive, err := parseNetDevLine(parts[1:len(header)+1], header)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
transmit, err := parseNetDevLine(parts[len(header)+1:], header)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
netStats["transmit"][dev] = transmit
|
||||
netStats["receive"][dev] = receive
|
||||
}
|
||||
return netStats, nil
|
||||
}
|
||||
|
||||
func parseNetDevLine(parts []string, header []string) (map[string]string, error) {
|
||||
devStats := map[string]string{}
|
||||
for i, v := range parts {
|
||||
devStats[header[i]] = v
|
||||
}
|
||||
return devStats, nil
|
||||
}
|
||||
|
||||
func getDiskStats() (map[string]map[string]string, error) {
|
||||
diskStats := map[string]map[string]string{}
|
||||
fh, err := os.Open(procDiskStats)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer fh.Close()
|
||||
scanner := bufio.NewScanner(fh)
|
||||
for scanner.Scan() {
|
||||
parts := strings.Fields(string(scanner.Text()))
|
||||
if len(parts) != len(diskStatsHeader)+3 { // we strip major, minor and dev
|
||||
return nil, fmt.Errorf("Invalid line in %s: %s", procDiskStats, scanner.Text())
|
||||
}
|
||||
dev := parts[2]
|
||||
diskStats[dev] = map[string]string{}
|
||||
for i, v := range parts[3:] {
|
||||
diskStats[dev][diskStatsHeader[i]] = v
|
||||
}
|
||||
}
|
||||
return diskStats, nil
|
||||
}
|
||||
86
collector/runit_collector.go
Normal file
86
collector/runit_collector.go
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
// +build runit
|
||||
|
||||
package collector
|
||||
|
||||
import (
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/soundcloud/go-runit/runit"
|
||||
)
|
||||
|
||||
type runitCollector struct {
|
||||
name string
|
||||
config Config
|
||||
state prometheus.Gauge
|
||||
stateDesired prometheus.Gauge
|
||||
stateNormal prometheus.Gauge
|
||||
}
|
||||
|
||||
func init() {
|
||||
Factories = append(Factories, NewRunitCollector)
|
||||
}
|
||||
|
||||
func NewRunitCollector(config Config, registry prometheus.Registry) (Collector, error) {
|
||||
c := runitCollector{
|
||||
name: "runit_collector",
|
||||
config: config,
|
||||
state: prometheus.NewGauge(),
|
||||
stateDesired: prometheus.NewGauge(),
|
||||
stateNormal: prometheus.NewGauge(),
|
||||
}
|
||||
|
||||
registry.Register(
|
||||
"node_service_state",
|
||||
"node_exporter: state of runit service.",
|
||||
prometheus.NilLabels,
|
||||
c.state,
|
||||
)
|
||||
|
||||
registry.Register(
|
||||
"node_service_desired_state",
|
||||
"node_exporter: desired state of runit service.",
|
||||
prometheus.NilLabels,
|
||||
c.stateDesired,
|
||||
)
|
||||
|
||||
registry.Register(
|
||||
"node_service_normal_state",
|
||||
"node_exporter: normal state of runit service.",
|
||||
prometheus.NilLabels,
|
||||
c.stateNormal,
|
||||
)
|
||||
|
||||
return &c, nil
|
||||
}
|
||||
|
||||
func (c *runitCollector) Name() string { return c.name }
|
||||
|
||||
func (c *runitCollector) Update() (updates int, err error) {
|
||||
services, err := runit.GetServices("/etc/service")
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
for _, service := range services {
|
||||
status, err := service.Status()
|
||||
if err != nil {
|
||||
debug(c.Name(), "Couldn't get status for %s: %s, skipping...", service.Name, err)
|
||||
continue
|
||||
}
|
||||
|
||||
debug(c.Name(), "%s is %d on pid %d for %d seconds", service.Name, status.State, status.Pid, status.Duration)
|
||||
labels := map[string]string{
|
||||
"service": service.Name,
|
||||
}
|
||||
|
||||
c.state.Set(labels, float64(status.State))
|
||||
c.stateDesired.Set(labels, float64(status.Want))
|
||||
if status.NormallyUp {
|
||||
c.stateNormal.Set(labels, 1)
|
||||
} else {
|
||||
c.stateNormal.Set(labels, 1)
|
||||
}
|
||||
updates += 3
|
||||
}
|
||||
|
||||
return updates, err
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue