-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
385 lines (329 loc) · 12.4 KB
/
main.go
File metadata and controls
385 lines (329 loc) · 12.4 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package main
// https://pve.proxmox.com/pve-docs/api-viewer/
// https://pbs.proxmox.com/docs/command-syntax.html#proxmox-backup-manager
import (
"encoding/json"
"fmt"
"log"
"os/exec"
"time"
"net/http"
"sort"
"strings"
"flag"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/client_golang/prometheus/collectors"
)
type VM struct {
Cpu float64 `json:"cpu"`
Disk int64 `json:"disk"`
DiskRead int64 `json:"diskread"`
DiskWrite int64 `json:"diskwrite"`
ID string `json:"id"`
MaxCpu int64 `json:"maxcpu"`
MaxDisk int64 `json:"maxdisk"`
MaxMem int64 `json:"maxmem"`
Mem int64 `json:"mem"`
Name string `json:"name"`
NetIn int64 `json:"netin"`
NetOut int64 `json:"netout"`
Node string `json:"node"`
Status string `json:"status"`
Template int64 `json:"template"`
Type string `json:"type"`
Uptime int64 `json:"uptime"`
VMID int64 `json:"vmid"`
}
type NODE struct {
CgroupMode int64 `json:"cgroup-mode"`
Cpu float64 `json:"cpu"`
Disk int64 `json:"disk"`
ID string `json:"id"`
Level string `json:"level"`
MaxCpu int64 `json:"maxcpu"`
MaxDisk int64 `json:"maxdisk"`
MaxMem int64 `json:"maxmem"`
Mem int64 `json:"mem"`
Node string `json:"node"`
Status string `json:"status"`
Type string `json:"type"`
Uptime int64 `json:"uptime"`
}
type STORAGE struct {
Content string `json:"content"`
Disk int64 `json:"disk"`
ID string `json:"id"`
MaxDisk int64 `json:"maxdisk"`
Node string `json:"node"`
PluginType string `json:"plugintype"`
Shared int64 `json:"shared"`
Status string `json:"status"`
Storage string `json:"storage"`
Type string `json:"type"`
}
var (
pveResourceCpu = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "pve_resource_cpu",
Help: "CPU utilization",
},
[]string{"id", "name", "node", "template", "type", "vmid"},
)
pveResourceDisk = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "pve_resource_disk",
Help: "Used disk space in bytes",
},
[]string{"id", "name", "node", "template", "type", "vmid"},
)
pveResourceMaxDisk = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "pve_resource_maxdisk",
Help: "Storage size in bytes",
},
[]string{"id", "name", "node", "template", "type", "vmid"},
)
pveResourceDiskRead = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "pve_resource_diskread",
Help: "The amount of bytes the guest read from its block devices since the guest was started",
},
[]string{"id", "name", "node", "template", "type", "vmid"},
)
pveResourceDiskWrite = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "pve_resource_diskwrite",
Help: "The amount of bytes the guest wrote to its block devices since the guest was started",
},
[]string{"id", "name", "node", "template", "type", "vmid"},
)
pveResourceMaxCpu = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "pve_resource_maxcpu",
Help: "Number of available CPUs",
},
[]string{"id", "name", "node", "template", "type", "vmid"},
)
pveResourceMaxMem = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "pve_resource_maxmem",
Help: "Number of available memory in bytes",
},
[]string{"id", "name", "node", "template", "type", "vmid"},
)
pveResourceMem = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "pve_resource_mem",
Help: "Used memory in bytes",
},
[]string{"id", "name", "node", "template", "type", "vmid"},
)
pveResourceNetIn = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "pve_resource_netin",
Help: "The amount of traffic in bytes that was sent to the guest over the network since it was started",
},
[]string{"id", "name", "node", "template", "type", "vmid"},
)
pveResourceNetOut = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "pve_resource_netout",
Help: "The amount of traffic in bytes that was sent from the guest over the network since it was started",
},
[]string{"id", "name", "node", "template", "type", "vmid"},
)
pveResourceUptime = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "pve_resource_uptime",
Help: "Uptime of node or virtual guest in seconds",
},
[]string{"id", "name", "node", "template", "type", "vmid"},
)
pveResourceStatus = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "pve_resource_status",
Help: "Resource type dependent status",
},
[]string{"id", "name", "node", "template", "type", "vmid"},
)
pveStorageDisk = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "pve_storage_disk",
Help: "Used disk space in bytes",
},
[]string{"id", "name", "node", "type", "plugintype", "shared", "content"},
)
pveStorageMaxDisk = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "pve_storage_maxdisk",
Help: "Storage size in bytes",
},
[]string{"id", "name", "node", "type", "plugintype", "shared", "content"},
)
pveStorageStatus = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "pve_storage_status",
Help: "Storage size in bytes",
},
[]string{"id", "name", "node", "type", "plugintype", "shared", "content"},
)
web_listen_address string
runtime_scrape_interval int
buildTime = "1970-01-01T00:00:00Z"
version = "0.0.0"
)
func init() {
// Unregister default collectors
prometheus.Unregister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}))
prometheus.Unregister(collectors.NewGoCollector())
// Register Prometheus metrics
prometheus.MustRegister(pveResourceCpu)
prometheus.MustRegister(pveResourceDisk)
prometheus.MustRegister(pveResourceDiskRead)
prometheus.MustRegister(pveResourceDiskWrite)
prometheus.MustRegister(pveResourceMaxCpu)
prometheus.MustRegister(pveResourceMaxDisk)
prometheus.MustRegister(pveResourceMaxMem)
prometheus.MustRegister(pveResourceMem)
prometheus.MustRegister(pveResourceNetIn)
prometheus.MustRegister(pveResourceNetOut)
prometheus.MustRegister(pveResourceUptime)
prometheus.MustRegister(pveResourceStatus)
prometheus.MustRegister(pveStorageDisk)
prometheus.MustRegister(pveStorageMaxDisk)
prometheus.MustRegister(pveStorageStatus)
}
func fetchAndUpdateVMpveResourceMetrics() error {
cmd := exec.Command("pvesh", "get", "/cluster/resources", "--type", "vm", "--output-format", "json")
output, err := cmd.Output()
if err != nil {
return fmt.Errorf("error executing pvesh command: %w", err)
}
var vms []VM
if err := json.Unmarshal(output, &vms); err != nil {
return fmt.Errorf("error parsing JSON: %w", err)
}
for _, vm := range vms {
labelID := fmt.Sprintf("node/%s/%s", vm.Node, vm.ID)
labelVMID := fmt.Sprintf("%d", vm.VMID)
labelNode := vm.Node
labelTemplate := fmt.Sprintf("%d", vm.Template)
labelType := vm.Type
statusValue := 0
if vm.Status == "running" {
statusValue = 1
}
// Update Prometheus metrics
pveResourceCpu.WithLabelValues(labelID, vm.Name, labelNode, labelTemplate, labelType, labelVMID).Set(vm.Cpu)
pveResourceDisk.WithLabelValues(labelID, vm.Name, labelNode, labelTemplate, labelType, labelVMID).Set(float64(vm.Disk))
pveResourceDiskRead.WithLabelValues(labelID, vm.Name, labelNode, labelTemplate, labelType, labelVMID).Set(float64(vm.DiskRead))
pveResourceDiskWrite.WithLabelValues(labelID, vm.Name, labelNode, labelTemplate, labelType, labelVMID).Set(float64(vm.DiskWrite))
pveResourceMaxCpu.WithLabelValues(labelID, vm.Name, labelNode, labelTemplate, labelType, labelVMID).Set(float64(vm.MaxCpu))
pveResourceMaxDisk.WithLabelValues(labelID, vm.Name, labelNode, labelTemplate, labelType, labelVMID).Set(float64(vm.MaxDisk))
pveResourceMaxMem.WithLabelValues(labelID, vm.Name, labelNode, labelTemplate, labelType, labelVMID).Set(float64(vm.MaxMem))
pveResourceMem.WithLabelValues(labelID, vm.Name, labelNode, labelTemplate, labelType, labelVMID).Set(float64(vm.Mem))
pveResourceNetIn.WithLabelValues(labelID, vm.Name, labelNode, labelTemplate, labelType, labelVMID).Set(float64(vm.NetIn))
pveResourceNetOut.WithLabelValues(labelID, vm.Name, labelNode, labelTemplate, labelType, labelVMID).Set(float64(vm.NetOut))
pveResourceUptime.WithLabelValues(labelID, vm.Name, labelNode, labelTemplate, labelType, labelVMID).Set(float64(vm.Uptime))
pveResourceStatus.WithLabelValues(labelID, vm.Name, labelNode, labelTemplate, labelType, labelVMID).Set(float64(statusValue))
}
return nil
}
func fetchAndUpdateNodepveResourceMetrics() error {
cmd := exec.Command("pvesh", "get", "/cluster/resources", "--type", "node", "--output-format", "json")
output, err := cmd.Output()
if err != nil {
return fmt.Errorf("error executing pvesh command: %w", err)
}
var nodes []NODE
if err := json.Unmarshal(output, &nodes); err != nil {
return fmt.Errorf("error parsing JSON: %w", err)
}
for _, node := range nodes {
labelID := node.ID
labelNode := node.Node
labelType := node.Type
labelName := node.Node
statusValue := 0
if node.Status == "online" {
statusValue = 1
}
// Update Prometheus metrics
pveResourceCpu.WithLabelValues(labelID, labelName, labelNode, "0", labelType, "0").Set(node.Cpu)
pveResourceDisk.WithLabelValues(labelID, labelName, labelNode, "0", labelType, "0").Set(float64(node.Disk))
pveResourceMaxCpu.WithLabelValues(labelID, labelName, labelNode, "0", labelType, "0").Set(float64(node.MaxCpu))
pveResourceMaxDisk.WithLabelValues(labelID, labelName, labelNode, "0", labelType, "0").Set(float64(node.MaxDisk))
pveResourceMaxMem.WithLabelValues(labelID, labelName, labelNode, "0", labelType, "0").Set(float64(node.MaxMem))
pveResourceMem.WithLabelValues(labelID, labelName, labelNode, "0", labelType, "0").Set(float64(node.Mem))
pveResourceUptime.WithLabelValues(labelID, labelName, labelNode, "0", labelType, "0").Set(float64(node.Uptime))
pveResourceStatus.WithLabelValues(labelID, labelName, labelNode, "0", labelType, "0").Set(float64(statusValue))
}
return nil
}
func fetchAndUpdateStoragepveResourceMetrics() error {
cmd := exec.Command("pvesh", "get", "/cluster/resources", "--type", "storage", "--output-format", "json")
output, err := cmd.Output()
if err != nil {
return fmt.Errorf("error executing pvesh command: %w", err)
}
var storages []STORAGE
if err := json.Unmarshal(output, &storages); err != nil {
return fmt.Errorf("error parsing JSON: %w", err)
}
for _, storage := range storages {
labelID := storage.ID
labelName := storage.Storage
labelNode := storage.Node
labelType := storage.Type
labelPluginType := storage.PluginType
labelShared := fmt.Sprintf("%d", storage.Shared)
// Sort and format the content list
contentList := strings.Split(storage.Content, ",")
sort.Strings(contentList)
labelContent := strings.Join(contentList, ",")
statusValue := 0
if storage.Status == "available" {
statusValue = 1
}
// Update Prometheus metrics
pveStorageDisk.WithLabelValues(labelID, labelName, labelNode, labelType, labelPluginType, labelShared, labelContent).Set(float64(storage.Disk))
pveStorageMaxDisk.WithLabelValues(labelID, labelName, labelNode, labelType, labelPluginType, labelShared, labelContent).Set(float64(storage.MaxDisk))
pveStorageStatus.WithLabelValues(labelID, labelName, labelNode, labelType, labelPluginType, labelShared, labelContent).Set(float64(statusValue))
}
return nil
}
func main() {
showVersion := flag.Bool("version", false, "Print version and build time")
flag.StringVar(&web_listen_address, "web.listen-address", "[::]:9221", "Address on which to expose metrics and web server")
flag.IntVar(&runtime_scrape_interval, "runtime.scrape_interval", 10, "Interval in seconds to scrape proxmox metrics")
flag.Parse()
if *showVersion {
fmt.Printf("Prometheus Proxmox Exporter Version: %s\n", version)
fmt.Printf("Build Time: %s\n", buildTime)
return
}
http.Handle("/metrics", promhttp.Handler())
go func() {
defer func() {
if r := recover(); r != nil {
log.Printf("Metrics fetcher crashed: %v", r)
}
}()
ticker := time.NewTicker(time.Duration(runtime_scrape_interval) * time.Second)
defer ticker.Stop()
for range ticker.C {
if err := fetchAndUpdateVMpveResourceMetrics(); err != nil {
log.Printf("Error fetching VM metrics: %v", err)
}
if err := fetchAndUpdateNodepveResourceMetrics(); err != nil {
log.Printf("Error fetching Node metrics: %v", err)
}
if err := fetchAndUpdateStoragepveResourceMetrics(); err != nil {
log.Printf("Error fetching Storage metrics: %v", err)
}
}
}()
log.Println("Prometheus proxmox exporter server started on", web_listen_address)
log.Fatal(http.ListenAndServe(web_listen_address, nil))
}