-
Notifications
You must be signed in to change notification settings - Fork 1
/
lxdcollect.go
151 lines (138 loc) · 5.68 KB
/
lxdcollect.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
lxd "github.com/lxc/lxd/client"
"github.com/prometheus/client_golang/prometheus"
)
//Define a struct for you collector that contains pointers
//to prometheus descriptors for each metric you wish to expose.
//Note you can also include fields of other types if they provide utility
//but we just won't be exposing them as metrics.
type lxdcollector struct {
cpuUsage *prometheus.Desc
memUsage *prometheus.Desc
memUsagePeak *prometheus.Desc
swapUsage *prometheus.Desc
swapUsagePeak *prometheus.Desc
processCount *prometheus.Desc
diskUsage *prometheus.Desc
containerPid *prometheus.Desc
networkUsage *prometheus.Desc
containerRunningStatus *prometheus.Desc
}
//You must create a constructor for you collector that
//initializes every descriptor and returns a pointer to the collector
func newLxdCollector() *lxdcollector {
return &lxdcollector{
cpuUsage: prometheus.NewDesc("lxd_container_cpu_usage",
"Container Cpu Usage in Seconds",
[]string{"container_name"}, nil,
),
memUsage: prometheus.NewDesc("lxd_container_mem_usage",
"Container Memory Usage",
[]string{"container_name"}, nil,
),
memUsagePeak: prometheus.NewDesc("lxd_container_mem_usage_peak",
"Container Memory Usage Peak",
[]string{"container_name"}, nil,
),
swapUsage: prometheus.NewDesc("lxd_container_swap_usage",
"Container Swap Usage",
[]string{"container_name"}, nil,
),
swapUsagePeak: prometheus.NewDesc("lxd_container_swap_usage_peak",
"Container Swap Usage Peak",
[]string{"container_name"}, nil,
),
processCount: prometheus.NewDesc("lxd_container_process_count",
"Container number of process Running",
[]string{"container_name"}, nil,
),
diskUsage: prometheus.NewDesc("lxd_container_disk_usage",
"Container Disk Usage",
[]string{"container_name", "disk_device"}, nil,
),
containerPid: prometheus.NewDesc("lxd_container_pid",
"Container PID",
[]string{"container_name"}, nil,
),
networkUsage: prometheus.NewDesc("lxd_container_network_usage",
"Container Network Usage",
[]string{"container_name", "interface", "operation"}, nil,
),
containerRunningStatus: prometheus.NewDesc("lxd_container_running_status",
"Container Running Status",
[]string{"container_name"}, nil,
),
}
}
//Each and every collector must implement the Describe function.
//It essentially writes all descriptors to the prometheus desc channel.
func (collector *lxdcollector) Describe(ch chan<- *prometheus.Desc) {
//Update this section with the each metric you create for a given collector
ch <- collector.cpuUsage
ch <- collector.memUsage
ch <- collector.memUsagePeak
ch <- collector.swapUsage
ch <- collector.swapUsagePeak
ch <- collector.processCount
ch <- collector.diskUsage
ch <- collector.containerPid
ch <- collector.networkUsage
ch <- collector.containerRunningStatus
}
//Collect implements required collect function for all promehteus collectors
func (collector *lxdcollector) Collect(ch chan<- prometheus.Metric) {
//Implement logic here to determine proper metric value to return to prometheus
//for each descriptor or call other functions that do so.
connection, _ := lxd.ConnectLXDUnix("", nil)
names, _ := connection.GetContainerNames()
var (
cpuUsageValue float64
memUsageValue float64
memUsagePeakValue float64
swapUsageValue float64
swapUsagePeakValue float64
processCountValue float64
diskUsageValue float64
containerPidValue float64
)
for _, name := range names {
state, _, _ := connection.GetContainerState(name)
// fmt.Println(state)
cpuUsageValue = float64(state.CPU.Usage)
memUsageValue = float64(state.Memory.Usage)
memUsagePeakValue = float64(state.Memory.UsagePeak)
swapUsageValue = float64(state.Memory.SwapUsage)
swapUsagePeakValue = float64(state.Memory.SwapUsagePeak)
processCountValue = float64(state.Processes)
containerPidValue = float64(state.Pid)
// containerRunningStatusValue = float64(state.Status)
//Write latest value for each metric in the prometheus metric channel.
//Note that you can pass CounterValue, GaugeValue, or UntypedValue types here.
ch <- prometheus.MustNewConstMetric(collector.cpuUsage, prometheus.GaugeValue, cpuUsageValue, name)
ch <- prometheus.MustNewConstMetric(collector.memUsage, prometheus.GaugeValue, memUsageValue, name)
ch <- prometheus.MustNewConstMetric(collector.memUsagePeak, prometheus.GaugeValue, memUsagePeakValue, name)
ch <- prometheus.MustNewConstMetric(collector.swapUsage, prometheus.GaugeValue, swapUsageValue, name)
ch <- prometheus.MustNewConstMetric(collector.swapUsagePeak, prometheus.GaugeValue, swapUsagePeakValue, name)
ch <- prometheus.MustNewConstMetric(collector.processCount, prometheus.GaugeValue, processCountValue, name)
ch <- prometheus.MustNewConstMetric(collector.containerPid, prometheus.GaugeValue, containerPidValue, name)
for key, value := range state.Disk {
diskUsageValue = float64(value.Usage)
ch <- prometheus.MustNewConstMetric(collector.diskUsage, prometheus.GaugeValue, diskUsageValue, name, key)
}
for key, value := range state.Network {
operations := map[string]float64{
"BytesReceived": float64(value.Counters.BytesReceived),
"BytesSent": float64(value.Counters.BytesSent),
"PacketsReceived": float64(value.Counters.PacketsReceived),
"PacketsSent": float64(value.Counters.PacketsSent),
}
for i, j := range operations {
ch <- prometheus.MustNewConstMetric(collector.networkUsage, prometheus.GaugeValue, j, name, key, i)
}
}
if state.Status == "Running" {
ch <- prometheus.MustNewConstMetric(collector.containerRunningStatus, prometheus.GaugeValue, 1, name)
}
}
}