Get the graph list and register metrics on demand

Instead of registering them when getting the graph list.
pull/2/merge
Johannes 'fish' Ziemke 12 years ago
parent 1dbe3de133
commit d38d0a7730

@ -155,38 +155,36 @@ func (c *muninCollector) muninConfig(name string) (config map[string]map[string]
return config, graphConfig, err return config, graphConfig, err
} }
func (c *muninCollector) registerMetrics() (err error) {
items, err := c.muninList() func (c *muninCollector) registerMetric(name string) (err error) {
configs, graphConfig, err := c.muninConfig(name)
if err != nil { if err != nil {
return fmt.Errorf("Couldn't get graph list: %s", err) return fmt.Errorf("Couldn't get config for graph %s: %s", name, err)
} }
for _, name := range items { for metric, config := range configs {
c.graphs = append(c.graphs, name) metricName := strings.Replace(name+"-"+metric, ".", "_", -1)
configs, graphConfig, err := c.muninConfig(name) desc := graphConfig["graph_title"] + ": " + config["label"]
if err != nil { if config["info"] != "" {
return fmt.Errorf("Couldn't get config for graph %s: %s", name, err) desc = desc + ", " + config["info"]
}
for metric, config := range configs {
metricName := strings.Replace(name+"-"+metric, ".", "_", -1)
desc := graphConfig["graph_title"] + ": " + config["label"]
if config["info"] != "" {
desc = desc + ", " + config["info"]
}
gauge := prometheus.NewGauge()
debug(c.Name(), "Register %s: %s", metricName, desc)
c.gaugePerMetric[metricName] = gauge
c.registry.Register(metricName, desc, prometheus.NilLabels, gauge)
} }
gauge := prometheus.NewGauge()
debug(c.Name(), "Register %s: %s", metricName, desc)
c.gaugePerMetric[metricName] = gauge
c.registry.Register(metricName, desc, prometheus.NilLabels, gauge)
} }
return err return nil
} }
func (c *muninCollector) Update() (updates int, err error) { func (c *muninCollector) Update() (updates int, err error) {
err = c.registerMetrics() items, err := c.muninList()
if err != nil { if err != nil {
return updates, fmt.Errorf("Couldn't register metrics: %s", err) return updates, fmt.Errorf("Couldn't get graph list: %s", err)
}
for _, name := range items {
c.graphs = append(c.graphs, name)
} }
for _, graph := range c.graphs { for _, graph := range c.graphs {
@ -215,6 +213,12 @@ func (c *muninCollector) Update() (updates int, err error) {
continue continue
} }
key, value_s := strings.Split(parts[0], ".")[0], parts[1] key, value_s := strings.Split(parts[0], ".")[0], parts[1]
name := graph + "-" + key
if _, ok := c.gaugePerMetric[name]; !ok {
if err := c.registerMetric(name); err != nil {
return updates, err
}
}
value, err := strconv.ParseFloat(value_s, 64) value, err := strconv.ParseFloat(value_s, 64)
if err != nil { if err != nil {
debug(c.Name(), "Couldn't parse value in line %s, malformed?", line) debug(c.Name(), "Couldn't parse value in line %s, malformed?", line)
@ -223,7 +227,6 @@ func (c *muninCollector) Update() (updates int, err error) {
labels := map[string]string{ labels := map[string]string{
"hostname": c.hostname, "hostname": c.hostname,
} }
name := graph + "-" + key
debug(c.Name(), "Set %s{%s}: %f\n", name, labels, value) debug(c.Name(), "Set %s{%s}: %f\n", name, labels, value)
c.gaugePerMetric[name].Set(labels, value) c.gaugePerMetric[name].Set(labels, value)
updates++ updates++

Loading…
Cancel
Save