Skip to content

Commit

Permalink
added up metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
czerwonk committed Jun 26, 2017
1 parent 096e924 commit b3b5157
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 30 deletions.
1 change: 1 addition & 0 deletions host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ type Host struct {
Cluster struct {
Id string `xml:"id,attr"`
} `xml:"cluster"`
Status string `xml:"status"`
}
35 changes: 29 additions & 6 deletions host/host_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ import (
"github.com/prometheus/common/log"
)

const prefix = "ovirt_host_"

var (
upDesc *prometheus.Desc
labelNames []string
)

func init() {
labelNames = []string{"name", "cluster"}
upDesc = prometheus.NewDesc(prefix+"up", "Host is running (1) or not (0)", labelNames, nil)
}

// HostCollector collects host statistics from oVirt
type HostCollector struct {
api *api.ApiClient
Expand All @@ -21,8 +33,7 @@ type HostCollector struct {

// NewCollector creates a new collector
func NewCollector(api *api.ApiClient) prometheus.Collector {
l := []string{"cluster"}
r := statistic.NewStatisticMetricRetriever("host", api, l)
r := statistic.NewStatisticMetricRetriever("host", api, labelNames)
c := cluster.NewRetriever(api)
return &HostCollector{api: api, retriever: r, clusterRetriever: c}
}
Expand Down Expand Up @@ -54,20 +65,32 @@ func (c *HostCollector) getMetrics() []prometheus.Metric {
}

func (c *HostCollector) retrieveMetrics() {
ressources := make(map[string]string)
ids := make([]string, 0)
labelValues := make(map[string][]string)

c.metrics = make([]prometheus.Metric, 0)
for _, h := range c.getHosts() {
ids = append(ids, h.Id)
cluster, err := c.clusterRetriever.Get(h.Cluster.Id)
if err != nil {
log.Error(err)
}

ressources[h.Id] = h.Name
labelValues[h.Id] = []string{cluster.Name}
labelValues[h.Id] = []string{h.Name, cluster.Name}

c.metrics = append(c.metrics, c.upMetric(&h, labelValues[h.Id]))
}

c.metrics = append(c.metrics, c.retriever.RetrieveMetrics(ids, labelValues)...)
}

func (c *HostCollector) upMetric(h *Host, labelValues []string) prometheus.Metric {
var up float64
if h.Status == "up" {
up = 1
}

c.metrics = c.retriever.RetrieveMetrics(ressources, labelValues)
return prometheus.MustNewConstMetric(upDesc, prometheus.GaugeValue, up, labelValues...)
}

func (c *HostCollector) getHosts() []Host {
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/prometheus/common/log"
)

const version string = "0.2.0"
const version string = "0.3.0"

var (
showVersion = flag.Bool("version", false, "Print version information.")
Expand Down
27 changes: 9 additions & 18 deletions statistic/mertic_retriever.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ func NewStatisticMetricRetriever(ressource string, api *api.ApiClient, labelName
return &StatisticMetricRetriever{ressource: ressource, api: api, labelNames: labelNames}
}

func (m *StatisticMetricRetriever) RetrieveMetrics(ressources map[string]string, labelValues map[string][]string) []prometheus.Metric {
func (m *StatisticMetricRetriever) RetrieveMetrics(ids []string, labelValues map[string][]string) []prometheus.Metric {
wg := &sync.WaitGroup{}
wg.Add(len(ressources))
wg.Add(len(ids))

ch := make(chan prometheus.Metric)
done := make(chan bool)

for id, name := range ressources {
go m.retrieveMetricsForId(id, name, ch, wg, labelValues)
for _, id := range ids {
go m.retrieveMetricsForId(id, ch, wg, labelValues)
}

go func() {
Expand All @@ -65,7 +65,7 @@ func (m *StatisticMetricRetriever) RetrieveMetrics(ressources map[string]string,
}
}

func (m *StatisticMetricRetriever) retrieveMetricsForId(id string, name string, ch chan<- prometheus.Metric,
func (m *StatisticMetricRetriever) retrieveMetricsForId(id string, ch chan<- prometheus.Metric,
wg *sync.WaitGroup, labelValues map[string][]string) {
defer wg.Done()

Expand All @@ -80,12 +80,12 @@ func (m *StatisticMetricRetriever) retrieveMetricsForId(id string, name string,

for _, s := range stats.Statistic {
if s.Kind == "gauge" {
ch <- m.convertToMetric(id, name, &s, labelValues)
ch <- m.convertToMetric(id, &s, labelValues)
}
}
}

func (m *StatisticMetricRetriever) convertToMetric(id string, name string, s *Statistic,
func (m *StatisticMetricRetriever) convertToMetric(id string, s *Statistic,
labelValues map[string][]string) prometheus.Metric {
metricName := strings.Replace(s.Name, ".", "_", -1)

Expand All @@ -94,16 +94,7 @@ func (m *StatisticMetricRetriever) convertToMetric(id string, name string, s *St
}

n := prometheus.BuildFQName("ovirt", m.ressource, metricName)
d := prometheus.NewDesc(n, s.Description, m.labelNames, nil)

labelNames := append([]string{"name"}, m.labelNames...)
d := prometheus.NewDesc(n, s.Description, labelNames, nil)

lv := append([]string{name}, labelValues[id]...)
r, err := prometheus.NewConstMetric(d, prometheus.GaugeValue, s.Values.Value.Datum, lv...)

if err != nil {
log.Errorln(err)
}

return r
return prometheus.MustNewConstMetric(d, prometheus.GaugeValue, s.Values.Value.Datum, labelValues[id]...)
}
1 change: 1 addition & 0 deletions vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ type Vm struct {
Cluster struct {
Id string `xml:"id,attr"`
} `xml:"cluster,omitempty"`
Status string `xml:"status"`
}
34 changes: 29 additions & 5 deletions vm/vm_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ import (
"github.com/prometheus/common/log"
)

const prefix = "ovirt_vm_"

var (
upDesc *prometheus.Desc
labelNames []string
)

func init() {
labelNames = []string{"name", "host", "cluster"}
upDesc = prometheus.NewDesc(prefix+"up", "VM is running (1) or not (0)", labelNames, nil)
}

// VmCollector collects virtual machine statistics from oVirt
type VmCollector struct {
api *api.ApiClient
Expand All @@ -23,7 +35,7 @@ type VmCollector struct {

// NewCollector creates a new collector
func NewCollector(api *api.ApiClient) prometheus.Collector {
r := statistic.NewStatisticMetricRetriever("vm", api, []string{"host", "cluster"})
r := statistic.NewStatisticMetricRetriever("vm", api, labelNames)
h := host.NewRetriever(api)
c := cluster.NewRetriever(api)
return &VmCollector{api: api, retriever: r, hostRetriever: h, clusterRetriever: c}
Expand Down Expand Up @@ -56,15 +68,27 @@ func (c *VmCollector) getMetrics() []prometheus.Metric {
}

func (c *VmCollector) retrieveMetrics() {
ressources := make(map[string]string)
ids := make([]string, 0)
labelValues := make(map[string][]string)

c.metrics = make([]prometheus.Metric, 0)
for _, vm := range c.getVms() {
ressources[vm.Id] = vm.Name
ids = append(ids, vm.Id)
labelValues[vm.Id] = c.getLabelValues(&vm)

c.metrics = append(c.metrics, c.upMetric(&vm, labelValues[vm.Id]))
}

c.metrics = append(c.metrics, c.retriever.RetrieveMetrics(ids, labelValues)...)
}

func (c *VmCollector) upMetric(vm *Vm, labelValues []string) prometheus.Metric {
var up float64
if vm.Status == "up" {
up = 1
}

c.metrics = c.retriever.RetrieveMetrics(ressources, labelValues)
return prometheus.MustNewConstMetric(upDesc, prometheus.GaugeValue, up, labelValues...)
}

func (c *VmCollector) getLabelValues(vm *Vm) []string {
Expand All @@ -83,7 +107,7 @@ func (c *VmCollector) getLabelValues(vm *Vm) []string {
log.Error(err)
}

return []string{h.Name, cl.Name}
return []string{vm.Name, h.Name, cl.Name}
}

func (c *VmCollector) getVms() []Vm {
Expand Down

0 comments on commit b3b5157

Please sign in to comment.