Skip to content

Commit

Permalink
added metrics for host/vm cpu/memory
Browse files Browse the repository at this point in the history
  • Loading branch information
czerwonk committed Jul 25, 2017
1 parent 22c8785 commit 2867b71
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 8 deletions.
9 changes: 9 additions & 0 deletions host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,13 @@ type Host struct {
Id string `xml:"id,attr"`
} `xml:"cluster"`
Status string `xml:"status"`
Cpu struct {
Speed int `xml:"speed"`
Topology struct {
Cores int `xml:"cores"`
Sockets int `xml:"sockets"`
Threads int `xml:"threads"`
} `xml:"topology"`
} `xml:"cpu"`
Memory int64 `xml:"memory"`
}
29 changes: 26 additions & 3 deletions host/host_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,23 @@ import (
const prefix = "ovirt_host_"

var (
upDesc *prometheus.Desc
labelNames []string
upDesc *prometheus.Desc
cpuCoresDesc *prometheus.Desc
cpuSocketsDesc *prometheus.Desc
cpuThreadsDesc *prometheus.Desc
cpuSpeedDesc *prometheus.Desc
memoryDesc *prometheus.Desc
labelNames []string
)

func init() {
labelNames = []string{"name", "cluster"}
upDesc = prometheus.NewDesc(prefix+"up", "Host is running (1) or not (0)", labelNames, nil)
cpuCoresDesc = prometheus.NewDesc(prefix+"cpu_cores", "Number of CPU cores assigned", labelNames, nil)
cpuSocketsDesc = prometheus.NewDesc(prefix+"cpu_sockets", "Number of sockets", labelNames, nil)
cpuThreadsDesc = prometheus.NewDesc(prefix+"cpu_threads", "Number of threads", labelNames, nil)
cpuSpeedDesc = prometheus.NewDesc(prefix+"cpu_speed", "CPU speed in MHz", labelNames, nil)
memoryDesc = prometheus.NewDesc(prefix+"memory_installed_bytes", "CPU speed in MHz", labelNames, nil)
}

// HostCollector collects host statistics from oVirt
Expand Down Expand Up @@ -78,12 +88,25 @@ func (c *HostCollector) retrieveMetrics() {

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

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

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

func (c *HostCollector) addMetricsForHost(host *Host, labelValues []string) {
c.metrics = append(c.metrics, c.upMetric(host, labelValues))
c.addMetric(cpuCoresDesc, float64(host.Cpu.Topology.Cores), labelValues)
c.addMetric(cpuThreadsDesc, float64(host.Cpu.Topology.Threads), labelValues)
c.addMetric(cpuSocketsDesc, float64(host.Cpu.Topology.Sockets), labelValues)
c.addMetric(cpuSpeedDesc, float64(host.Cpu.Speed), labelValues)
c.addMetric(memoryDesc, float64(host.Memory), labelValues)
}

func (c *HostCollector) addMetric(desc *prometheus.Desc, v float64, labelValues []string) {
c.metrics = append(c.metrics, prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, v, labelValues...))
}

func (c *HostCollector) upMetric(h *Host, labelValues []string) prometheus.Metric {
var up float64
if h.Status == "up" {
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.3.0"
const version string = "0.4.0"

var (
showVersion = flag.Bool("version", false, "Print version information.")
Expand Down
2 changes: 1 addition & 1 deletion vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"revisionTime": "2017-06-08T22:14:41Z"
},
{
"checksumSHA1": "rIYurTMbnyUj/eMHsaxTBnTvSnY=",
"checksumSHA1": "Tkb1hBdBWeO7SGjixS2Hm48F6+s=",
"path": "golang.org/x/sys/unix",
"revision": "fb4cac33e3196ff7f507ab9b2d2a44b0142f5b5a",
"revisionTime": "2017-06-14T06:48:48Z"
Expand Down
7 changes: 7 additions & 0 deletions vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,11 @@ type Vm struct {
Id string `xml:"id,attr"`
} `xml:"cluster,omitempty"`
Status string `xml:"status"`
Cpu struct {
Topology struct {
Cores int `xml:"cores"`
Sockets int `xml:"sockets"`
Threads int `xml:"threads"`
} `xml:"topology"`
} `xml:"cpu"`
}
23 changes: 20 additions & 3 deletions vm/vm_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ import (
const prefix = "ovirt_vm_"

var (
upDesc *prometheus.Desc
labelNames []string
upDesc *prometheus.Desc
cpuCoresDesc *prometheus.Desc
cpuSocketsDesc *prometheus.Desc
cpuThreadsDesc *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)
cpuCoresDesc = prometheus.NewDesc(prefix+"cpu_cores", "Number of CPU cores assigned", labelNames, nil)
cpuSocketsDesc = prometheus.NewDesc(prefix+"cpu_sockets", "Number of sockets", labelNames, nil)
cpuThreadsDesc = prometheus.NewDesc(prefix+"cpu_threads", "Number of threads", labelNames, nil)
}

// VmCollector collects virtual machine statistics from oVirt
Expand Down Expand Up @@ -76,12 +82,23 @@ func (c *VmCollector) retrieveMetrics() {
ids = append(ids, vm.Id)
labelValues[vm.Id] = c.getLabelValues(&vm)

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

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

func (c *VmCollector) addMetricsForVm(vm *Vm, labelValues []string) {
c.metrics = append(c.metrics, c.upMetric(vm, labelValues))
c.addMetric(cpuCoresDesc, float64(vm.Cpu.Topology.Cores), labelValues)
c.addMetric(cpuThreadsDesc, float64(vm.Cpu.Topology.Threads), labelValues)
c.addMetric(cpuSocketsDesc, float64(vm.Cpu.Topology.Sockets), labelValues)
}

func (c *VmCollector) addMetric(desc *prometheus.Desc, v float64, labelValues []string) {
c.metrics = append(c.metrics, prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, v, labelValues...))
}

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

0 comments on commit 2867b71

Please sign in to comment.