/
dom_collector.go
120 lines (112 loc) · 3.01 KB
/
dom_collector.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"github.com/prometheus/client_golang/prometheus"
"xenbits.xen.org/git-http/xen.git/tools/golang/xenlight"
"gopkg.in/alecthomas/kingpin.v2"
"strconv"
)
var (
domCpuTimeDesc = prometheus.NewDesc(
"xen_domain_cpu_time_total",
"CPU time used by the domain",
[]string{"domain_name"}, nil,
)
domVcpuTimeDesc = prometheus.NewDesc(
"xen_domain_vcpu_time_total",
"CPU time per vCPU for the domain",
[]string{"domain_name", "cpu_id"}, nil,
)
domCpuCountDesc = prometheus.NewDesc(
"xen_domain_cpu_count",
"Number of available CPU for domain",
[]string{"domain_name"}, nil,
)
domCpuOnlineDesc = prometheus.NewDesc(
"xen_domain_cpu_online_count",
"Number of online CPU for domain",
[]string{"domain_name"}, nil,
)
domMemoryMaxDesc = prometheus.NewDesc(
"xen_domain_memory_max_bytes",
"Total ammount of RAM on the domain",
[]string{"domain_name"}, nil,
)
domMemoryCurrentDesc = prometheus.NewDesc(
"xen_domain_memory_current_bytes",
"Current ammount of RAM used by the domain",
[]string{"domain_name"}, nil,
)
domMemoryOutstandingDesc = prometheus.NewDesc(
"xen_domain_memory_outstanding_bytes",
"Total ammount of outstanding RAM for the domain",
[]string{"domain_name"}, nil,
)
domVcpuShowDetails = kingpin.Flag(
"collector.domain.show-vcpus-details",
"Enable the collection of per-vcpu time",
).Default("false").Bool()
)
type DomainCollector struct{}
func init() {
registerCollector("domain", defaultEnabled, NewDomainCollector)
}
func NewDomainCollector() prometheus.Collector {
return &DomainCollector{}
}
func (collector DomainCollector) Describe(ch chan<- *prometheus.Desc) {
prometheus.DescribeByCollect(collector, ch)
}
func (collector DomainCollector) Collect(ch chan<- prometheus.Metric) {
xenlight.Ctx.Open()
dominfos := xenlight.Ctx.ListDomain()
for _, dominfo := range dominfos {
domName := xenlight.Ctx.DomidToName(dominfo.Domid)
ch <- prometheus.MustNewConstMetric(
domCpuCountDesc,
prometheus.GaugeValue,
float64(dominfo.VcpuMaxId+1),
domName,
)
ch <- prometheus.MustNewConstMetric(
domCpuOnlineDesc,
prometheus.GaugeValue,
float64(dominfo.VcpuOnline),
domName,
)
ch <- prometheus.MustNewConstMetric(
domCpuTimeDesc,
prometheus.CounterValue,
dominfo.CpuTime.Seconds(),
domName,
)
if *domVcpuShowDetails {
vcpus := xenlight.Ctx.ListVcpu(dominfo.Domid)
for _, vcpu := range vcpus {
ch <- prometheus.MustNewConstMetric(
domVcpuTimeDesc,
prometheus.CounterValue,
vcpu.VCpuTime.Seconds(),
domName, strconv.FormatUint(uint64(vcpu.Vcpuid), 10),
)
}
}
ch <- prometheus.MustNewConstMetric(
domMemoryMaxDesc,
prometheus.GaugeValue,
float64(dominfo.MaxMemkb)*1024,
domName,
)
ch <- prometheus.MustNewConstMetric(
domMemoryCurrentDesc,
prometheus.GaugeValue,
float64(dominfo.CurrentMemkb)*1024,
domName,
)
ch <- prometheus.MustNewConstMetric(
domMemoryOutstandingDesc,
prometheus.GaugeValue,
float64(dominfo.OutstandingMemkb)*1024,
domName,
)
}
}