From ada754e7f654ea4342dc96924a32591c5e4bbc49 Mon Sep 17 00:00:00 2001 From: Johannes 'fish' Ziemke Date: Thu, 25 Jul 2013 15:30:35 +0200 Subject: [PATCH 1/2] Add runit exporter --- exporter/exporter.go | 7 ++++- exporter/runit_collector.go | 63 +++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 exporter/runit_collector.go diff --git a/exporter/exporter.go b/exporter/exporter.go index 6c3f42928d..7c34746139 100644 --- a/exporter/exporter.go +++ b/exporter/exporter.go @@ -80,12 +80,17 @@ func New(configFile string) (e exporter, err error) { log.Fatalf("Couldn't attach collector: %s", err) } + cr, err := NewRunitCollector(e.config, e.registry) + if err != nil { + log.Fatalf("Couldn't attach collector: %s", err) + } + cg, err := NewGmondCollector(e.config, e.registry) if err != nil { log.Fatalf("Couldn't attach collector: %s", err) } - e.collectors = []Collector{&cn, &cg} + e.collectors = []Collector{&cn, &cr, &cg} if e.config.ListeningAddress != "" { e.listeningAddress = e.config.ListeningAddress diff --git a/exporter/runit_collector.go b/exporter/runit_collector.go new file mode 100644 index 0000000000..bea9dcd561 --- /dev/null +++ b/exporter/runit_collector.go @@ -0,0 +1,63 @@ +package exporter + +import ( + "github.com/prometheus/client_golang/prometheus" + "github.com/soundcloud/go-runit/runit" +) + +var () + +type runitCollector struct { + name string + config config + serviceStatus prometheus.Gauge +} + +func NewRunitCollector(config config, registry prometheus.Registry) (runitCollector, error) { + c := runitCollector{ + name: "runit_collector", + config: config, + serviceStatus: prometheus.NewGauge(), + } + + registry.Register( + "node_service_status", + "node_exporter: status of runit service.", + prometheus.NilLabels, + c.serviceStatus, + ) + + return c, nil +} + +func (c *runitCollector) Name() string { return c.name } + +func (c *runitCollector) Update() (updates int, err error) { + services, err := runit.GetServices() + if err != nil { + return 0, err + } + + for _, service := range services { + status, err := service.Status() + if err != nil { + return 0, err + } + debug(c.Name(), "%s is %d on pid %d for %d seconds", service.Name, status.State, status.Pid, status.Duration) + labels := map[string]string{ + "name": service.Name, + "state": runit.StateToString[status.State], + "want": runit.StateToString[status.Want], + } + + if status.NormallyUp { + labels["normally_up"] = "yes" + } else { + labels["normally_up"] = "no" + } + + c.serviceStatus.Set(labels, float64(status.Duration)) + updates++ + } + return updates, err +} From 3a58f3b22f59e04a7e51dc650ab0721d0a79e4c0 Mon Sep 17 00:00:00 2001 From: Johannes 'fish' Ziemke Date: Thu, 25 Jul 2013 15:55:07 +0200 Subject: [PATCH 2/2] Rename metric and removed cruft --- exporter/runit_collector.go | 52 ++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/exporter/runit_collector.go b/exporter/runit_collector.go index bea9dcd561..8d85d604ae 100644 --- a/exporter/runit_collector.go +++ b/exporter/runit_collector.go @@ -5,26 +5,42 @@ import ( "github.com/soundcloud/go-runit/runit" ) -var () - type runitCollector struct { - name string - config config - serviceStatus prometheus.Gauge + name string + config config + state prometheus.Gauge + stateDesired prometheus.Gauge + stateNormal prometheus.Gauge } func NewRunitCollector(config config, registry prometheus.Registry) (runitCollector, error) { c := runitCollector{ - name: "runit_collector", - config: config, - serviceStatus: prometheus.NewGauge(), + name: "runit_collector", + config: config, + state: prometheus.NewGauge(), + stateDesired: prometheus.NewGauge(), + stateNormal: prometheus.NewGauge(), } registry.Register( - "node_service_status", + "node_service_state", + "node_exporter: state of runit service.", + prometheus.NilLabels, + c.state, + ) + + registry.Register( + "node_service_desired_state", + "node_exporter: desired status of runit service.", + prometheus.NilLabels, + c.stateDesired, + ) + + registry.Register( + "node_service_normal_state", "node_exporter: status of runit service.", prometheus.NilLabels, - c.serviceStatus, + c.stateNormal, ) return c, nil @@ -43,21 +59,21 @@ func (c *runitCollector) Update() (updates int, err error) { if err != nil { return 0, err } + debug(c.Name(), "%s is %d on pid %d for %d seconds", service.Name, status.State, status.Pid, status.Duration) labels := map[string]string{ - "name": service.Name, - "state": runit.StateToString[status.State], - "want": runit.StateToString[status.Want], + "service": service.Name, } + c.state.Set(labels, float64(status.State)) + c.stateDesired.Set(labels, float64(status.Want)) if status.NormallyUp { - labels["normally_up"] = "yes" + c.stateNormal.Set(labels, 1) } else { - labels["normally_up"] = "no" + c.stateNormal.Set(labels, 1) } - - c.serviceStatus.Set(labels, float64(status.Duration)) - updates++ + updates += 3 } + return updates, err }