2014-02-07 16:09:39 +00:00
|
|
|
// +build ganglia
|
|
|
|
|
2014-02-18 11:35:11 +00:00
|
|
|
package collector
|
2013-05-07 14:40:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
2013-08-15 11:20:06 +00:00
|
|
|
"regexp"
|
2013-05-14 13:45:38 +00:00
|
|
|
"time"
|
2014-02-18 11:35:11 +00:00
|
|
|
|
2014-06-04 11:12:34 +00:00
|
|
|
"github.com/golang/glog"
|
2014-02-18 11:35:11 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/node_exporter/collector/ganglia"
|
2013-05-07 14:40:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2014-04-10 10:32:38 +00:00
|
|
|
gangliaAddress = "127.0.0.1:8649"
|
|
|
|
gangliaProto = "tcp"
|
|
|
|
gangliaTimeout = 30 * time.Second
|
|
|
|
gangliaMetricsPrefix = "ganglia_"
|
2013-05-07 14:40:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type gmondCollector struct {
|
2014-06-26 19:16:21 +00:00
|
|
|
Metrics map[string]*prometheus.GaugeVec
|
|
|
|
config Config
|
2013-05-07 14:40:10 +00:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:09:39 +00:00
|
|
|
func init() {
|
2014-06-04 11:12:34 +00:00
|
|
|
Factories["gmond"] = NewGmondCollector
|
2014-02-07 16:09:39 +00:00
|
|
|
}
|
|
|
|
|
2013-08-15 11:20:06 +00:00
|
|
|
var illegalCharsRE = regexp.MustCompile(`[^a-zA-Z0-9_]`)
|
|
|
|
|
2013-05-07 14:40:10 +00:00
|
|
|
// Takes a config struct and prometheus registry and returns a new Collector scraping ganglia.
|
2014-06-26 19:16:21 +00:00
|
|
|
func NewGmondCollector(config Config) (Collector, error) {
|
2014-02-07 16:09:39 +00:00
|
|
|
c := gmondCollector{
|
2014-06-26 19:16:21 +00:00
|
|
|
config: config,
|
|
|
|
Metrics: map[string]*prometheus.GaugeVec{},
|
2013-05-07 14:40:10 +00:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:09:39 +00:00
|
|
|
return &c, nil
|
2013-05-07 14:40:10 +00:00
|
|
|
}
|
|
|
|
|
2014-06-26 19:16:21 +00:00
|
|
|
func (c *gmondCollector) setMetric(name, cluster string, metric ganglia.Metric) {
|
2013-05-07 14:40:10 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2014-06-04 11:12:34 +00:00
|
|
|
glog.V(1).Infof("Register %s: %s", name, desc)
|
2014-06-26 19:16:21 +00:00
|
|
|
gv := prometheus.NewGaugeVec(
|
|
|
|
prometheus.GaugeOpts{
|
|
|
|
Namespace: gangliaMetricsPrefix,
|
|
|
|
Name: name,
|
|
|
|
Help: desc,
|
|
|
|
},
|
|
|
|
[]string{"cluster"},
|
|
|
|
)
|
2013-05-07 14:40:10 +00:00
|
|
|
}
|
2014-06-26 19:16:21 +00:00
|
|
|
glog.V(1).Infof("Set %s{cluster=%q}: %f", name, cluster, metric.Value)
|
|
|
|
c.Metrics[name].WithLabelValues(cluster).Set(metric.Value)
|
2013-05-07 14:40:10 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 14:16:43 +00:00
|
|
|
func (c *gmondCollector) Update(ch chan<- prometheus.Metric) (err error) {
|
2013-05-07 14:40:10 +00:00
|
|
|
conn, err := net.Dial(gangliaProto, gangliaAddress)
|
2014-06-04 11:12:34 +00:00
|
|
|
glog.V(1).Infof("gmondCollector Update")
|
2013-05-07 14:40:10 +00:00
|
|
|
if err != nil {
|
2014-10-29 14:16:43 +00:00
|
|
|
return fmt.Errorf("Can't connect to gmond: %s", err)
|
2013-05-07 14:40:10 +00:00
|
|
|
}
|
|
|
|
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 {
|
2014-10-29 14:16:43 +00:00
|
|
|
return fmt.Errorf("Couldn't parse xml: %s", err)
|
2013-05-07 14:40:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, cluster := range ganglia.Clusters {
|
|
|
|
for _, host := range cluster.Hosts {
|
|
|
|
|
|
|
|
for _, metric := range host.Metrics {
|
2014-06-26 19:16:21 +00:00
|
|
|
name := illegalCharsRE.ReplaceAllString(metric.Name, "_")
|
2013-05-07 14:40:10 +00:00
|
|
|
|
2014-06-26 19:16:21 +00:00
|
|
|
c.setMetric(name, cluster.Name, metric)
|
2013-05-07 14:40:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-10-29 14:16:43 +00:00
|
|
|
for _, m := range c.Metrics {
|
|
|
|
m.Collect(ch)
|
|
|
|
}
|
|
|
|
return err
|
2013-05-07 14:40:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func toUtf8(charset string, input io.Reader) (io.Reader, error) {
|
|
|
|
return input, nil //FIXME
|
|
|
|
}
|