node_exporter/collector/runit_collector.go

84 lines
1.8 KiB
Go
Raw Normal View History

// +build runit
package collector
2013-07-25 13:30:35 +00:00
import (
"github.com/golang/glog"
2013-07-25 13:30:35 +00:00
"github.com/prometheus/client_golang/prometheus"
"github.com/soundcloud/go-runit/runit"
)
type runitCollector struct {
config Config
2013-07-25 13:55:07 +00:00
state prometheus.Gauge
stateDesired prometheus.Gauge
stateNormal prometheus.Gauge
2013-07-25 13:30:35 +00:00
}
func init() {
Factories["runit"] = NewRunitCollector
}
func NewRunitCollector(config Config, registry prometheus.Registry) (Collector, error) {
2013-07-25 13:30:35 +00:00
c := runitCollector{
2013-07-25 13:55:07 +00:00
config: config,
state: prometheus.NewGauge(),
stateDesired: prometheus.NewGauge(),
stateNormal: prometheus.NewGauge(),
2013-07-25 13:30:35 +00:00
}
registry.Register(
2013-07-25 13:55:07 +00:00
"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.",
2013-07-25 13:55:07 +00:00
prometheus.NilLabels,
c.stateDesired,
)
registry.Register(
"node_service_normal_state",
"node_exporter: normal state of runit service.",
2013-07-25 13:30:35 +00:00
prometheus.NilLabels,
2013-07-25 13:55:07 +00:00
c.stateNormal,
2013-07-25 13:30:35 +00:00
)
return &c, nil
2013-07-25 13:30:35 +00:00
}
func (c *runitCollector) Update() (updates int, err error) {
services, err := runit.GetServices("/etc/service")
2013-07-25 13:30:35 +00:00
if err != nil {
return 0, err
}
for _, service := range services {
status, err := service.Status()
if err != nil {
glog.V(1).Infof("Couldn't get status for %s: %s, skipping...", service.Name, err)
continue
2013-07-25 13:30:35 +00:00
}
2013-07-25 13:55:07 +00:00
glog.V(1).Infof("%s is %d on pid %d for %d seconds", service.Name, status.State, status.Pid, status.Duration)
2013-07-25 13:30:35 +00:00
labels := map[string]string{
2013-07-25 13:55:07 +00:00
"service": service.Name,
2013-07-25 13:30:35 +00:00
}
2013-07-25 13:55:07 +00:00
c.state.Set(labels, float64(status.State))
c.stateDesired.Set(labels, float64(status.Want))
2013-07-25 13:30:35 +00:00
if status.NormallyUp {
2013-07-25 13:55:07 +00:00
c.stateNormal.Set(labels, 1)
2013-07-25 13:30:35 +00:00
} else {
2013-07-25 13:55:07 +00:00
c.stateNormal.Set(labels, 1)
2013-07-25 13:30:35 +00:00
}
2013-07-25 13:55:07 +00:00
updates += 3
2013-07-25 13:30:35 +00:00
}
2013-07-25 13:55:07 +00:00
2013-07-25 13:30:35 +00:00
return updates, err
}