/
nodes.go
executable file
·303 lines (259 loc) · 7.4 KB
/
nodes.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
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
package proxmox
import (
"fmt"
"github.com/patrickmn/go-cache"
proxmox "github.com/starttoaster/go-proxmox"
log "github.com/starttoaster/proxmox-exporter/internal/logger"
)
// GetNodes returns a proxmox NodeStatuses object or an error from the /nodes endpoint
func GetNodes() (*proxmox.GetNodesResponse, error) {
// Chech cache
var nodes *proxmox.GetNodesResponse
if x, found := cash.Get("GetNodes"); found {
var ok bool
nodes, ok = x.(*proxmox.GetNodesResponse)
if ok {
log.Logger.Debug("proxmox request was found in cache for GetNodes")
return nodes, nil
}
}
// Make request if not found in cache
var err error
for name, c := range clients {
// Check if client was banned, skip if is
if c.banned {
continue
}
nodes, _, err = c.client.Nodes.GetNodes()
if err == nil {
break
} else {
banClient(name, c)
}
}
if err != nil {
return nil, err
}
if nodes == nil {
return nil, fmt.Errorf("request to get node list was not successful. It's possible all clients are banned")
}
// Update cache
cash.Set("GetNodes", nodes, cache.DefaultExpiration)
return nodes, nil
}
// GetNodeStatus returns a proxmox Node object or an error from the /nodes/%s/status endpoint
func GetNodeStatus(name string) (*proxmox.GetNodeStatusResponse, error) {
// Chech cache
var node *proxmox.GetNodeStatusResponse
if x, found := cash.Get(fmt.Sprintf("GetNodeStatus_%s", name)); found {
var ok bool
node, ok = x.(*proxmox.GetNodeStatusResponse)
if ok {
log.Logger.Debug("proxmox request was found in cache for GetNodeStatus", "node", name)
return node, nil
}
}
// Make request if not found in cache
var err error
for clientName, c := range clients {
// Check if client was banned, skip if is
if c.banned {
continue
}
node, _, err = c.client.Nodes.GetNodeStatus(name)
if err == nil {
break
} else {
banClient(clientName, c)
}
}
if err != nil {
return nil, err
}
if node == nil {
return nil, fmt.Errorf("request to get node status was not successful. It's possible all clients are banned")
}
// Update cache
cash.Set(fmt.Sprintf("GetNodeStatus_%s", name), node, cache.DefaultExpiration)
return node, nil
}
// GetNodeQemu returns the virtual machines for a node
func GetNodeQemu(name string) (*proxmox.GetNodeQemuResponse, error) {
// Chech cache
var vms *proxmox.GetNodeQemuResponse
if x, found := cash.Get(fmt.Sprintf("GetNodeQemu_%s", name)); found {
var ok bool
vms, ok = x.(*proxmox.GetNodeQemuResponse)
if ok {
log.Logger.Debug("proxmox request was found in cache for GetNodeQemu", "node", name)
return vms, nil
}
}
// Make request if not found in cache
var err error
for clientName, c := range clients {
// Check if client was banned, skip if is
if c.banned {
continue
}
vms, _, err = c.client.Nodes.GetNodeQemu(name)
if err == nil {
break
} else {
banClient(clientName, c)
}
}
if err != nil {
return nil, err
}
if vms == nil {
return nil, fmt.Errorf("request to get node VMs was not successful. It's possible all clients are banned")
}
// Update per-node cache since we have it
cash.Set(fmt.Sprintf("GetNodeQemu_%s", name), vms, cache.DefaultExpiration)
return vms, nil
}
// GetNodeLxc returns the LXCs for a node
func GetNodeLxc(name string) (*proxmox.GetNodeLxcResponse, error) {
// Chech cache
var lxcs *proxmox.GetNodeLxcResponse
if x, found := cash.Get(fmt.Sprintf("GetNodeLxc_%s", name)); found {
var ok bool
lxcs, ok = x.(*proxmox.GetNodeLxcResponse)
if ok {
log.Logger.Debug("proxmox request was found in cache for GetNodeLxc", "node", name)
return lxcs, nil
}
}
// Make request if not found in cache
var err error
for clientName, c := range clients {
// Check if client was banned, skip if is
if c.banned {
continue
}
lxcs, _, err = c.client.Nodes.GetNodeLxc(name)
if err == nil {
break
} else {
banClient(clientName, c)
}
}
if err != nil {
return nil, err
}
if lxcs == nil {
return nil, fmt.Errorf("request to get node LXCs was not successful. It's possible all clients are banned")
}
// Update per-node cache since we have it
cash.Set(fmt.Sprintf("GetNodeLxc_%s", name), lxcs, cache.DefaultExpiration)
return lxcs, nil
}
// GetNodeDisksList returns the disks for a node
func GetNodeDisksList(name string) (*proxmox.GetNodeDisksListResponse, error) {
// Chech cache
var disks *proxmox.GetNodeDisksListResponse
if x, found := cash.Get(fmt.Sprintf("GetNodeDisksList_%s", name)); found {
var ok bool
disks, ok = x.(*proxmox.GetNodeDisksListResponse)
if ok {
log.Logger.Debug("proxmox request was found in cache for GetNodeDisksList", "node", name)
return disks, nil
}
}
// Make request if not found in cache
var err error
for clientName, c := range clients {
// Check if client was banned, skip if is
if c.banned {
continue
}
disks, _, err = c.client.Nodes.GetNodeDisksList(name)
if err == nil {
break
} else {
banClient(clientName, c)
}
}
if err != nil {
return nil, err
}
if disks == nil {
return nil, fmt.Errorf("request to get node disks was not successful. It's possible all clients are banned")
}
// Update per-node cache since we have it
cash.Set(fmt.Sprintf("GetNodeDisksList_%s", name), disks, cache.DefaultExpiration)
return disks, nil
}
// GetNodeCertificatesInfo returns the certificates for a node
func GetNodeCertificatesInfo(name string) (*proxmox.GetNodeCertificatesInfoResponse, error) {
// Chech cache
var certs *proxmox.GetNodeCertificatesInfoResponse
if x, found := cash.Get(fmt.Sprintf("GetNodeCertificatesInfo_%s", name)); found {
var ok bool
certs, ok = x.(*proxmox.GetNodeCertificatesInfoResponse)
if ok {
log.Logger.Debug("proxmox request was found in cache for GetNodeCertificatesInfo", "node", name)
return certs, nil
}
}
// Make request if not found in cache
var err error
for clientName, c := range clients {
// Check if client was banned, skip if is
if c.banned {
continue
}
certs, _, err = c.client.Nodes.GetNodeCertificatesInfo(name)
if err == nil {
break
} else {
banClient(clientName, c)
}
}
if err != nil {
return nil, err
}
if certs == nil {
return nil, fmt.Errorf("request to get node certificates was not successful. It's possible all clients are banned")
}
// Update per-node cache since we have it
cash.Set(fmt.Sprintf("GetNodeCertificatesInfo_%s", name), certs, cache.DefaultExpiration)
return certs, nil
}
// GetNodeStorage returns the storage for a node
func GetNodeStorage(name string) (*proxmox.GetNodeStorageResponse, error) {
// Chech cache
var store *proxmox.GetNodeStorageResponse
if x, found := cash.Get(fmt.Sprintf("GetNodeStorage_%s", name)); found {
var ok bool
store, ok = x.(*proxmox.GetNodeStorageResponse)
if ok {
log.Logger.Debug("proxmox request was found in cache for GetNodeStorage", "node", name)
return store, nil
}
}
// Make request if not found in cache
var err error
for clientName, c := range clients {
// Check if client was banned, skip if is
if c.banned {
continue
}
store, _, err = c.client.Nodes.GetNodeStorage(name)
if err == nil {
break
} else {
banClient(clientName, c)
}
}
if err != nil {
return nil, err
}
if store == nil {
return nil, fmt.Errorf("request to get node storage was not successful. It's possible all clients are banned")
}
// Update per-node cache since we have it
cash.Set(fmt.Sprintf("GetNodeStorage_%s", name), store, cache.DefaultExpiration)
return store, nil
}