-
Notifications
You must be signed in to change notification settings - Fork 5
/
vcenter.py
executable file
·450 lines (387 loc) · 23.5 KB
/
vcenter.py
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# collectd-vcenter - vcenter.py
#
# Author : Loic Lambiel @ exoscale
# Description : This is a collectd python module to gather stats from Vmware vcenter
import collectd
from pysphere import VIServer
RUN = 0
METRIC_TYPES = {
'datastorecapacity': ('z_dscapacity', 'current'),
'datastorefreespace': ('z_dsfreespace', 'current'),
'datastoreusagepercent': ('z_dsusage_pct', 'current'),
'zonedatacenterscount': ('z_datacenters_count', 'current'),
'zoneclusterscount': ('z_clusters_count', 'current'),
'zonehostscount': ('z_hosts_count', 'current'),
'zonememoryusage': ('z_memory_usage', 'current'),
'zonecpuusage': ('z_cpu_usage', 'current'),
'zonememoryusagepercent': ('z_memory_usage_pct', 'current'),
'zonecpuusagepercent': ('z_cpu_usage_pct', 'current'),
'zonetotalmemory': ('z_total_memory', 'current'),
'zonecputotal': ('z_cpu_total', 'current'),
'hoststatus': ('h_status', 'current'),
'hostmemoryusage': ('h_memory_usage', 'current'),
'hostcpuusage': ('h_cpu_usage', 'current'),
'hostmemoryusagepercent': ('h_memory_usage_pct', 'current'),
'hostcpuusagepercent': ('h_cpu_usage_pct', 'current'),
'hosttotalmemory': ('h_total_memory', 'current'),
'hostcputotal': ('h_cpu_total', 'current'),
'hostrunningvms': ('h_vm_running_count', 'current'),
'hoststoppedvms': ('h_vm_stopped_count', 'current'),
'hosttotalvms': ('h_vm_total_count', 'current'),
'zonerunningvms': ('z_vm_running_count', 'current'),
'zonestoppedvms': ('z_vm_stopped_count', 'current'),
'zonetotalvms': ('z_vm_total_count', 'current'),
'datacenterclusterscount': ('d_clusters_count', 'current'),
'datacenterhostscount': ('d_hosts_count', 'current'),
'clussterhostscount': ('c_hosts_count', 'current'),
'datacenterrunningvms': ('d_vm_running_count', 'current'),
'datacenterstoppedvms': ('d_vm_stopped_count', 'current'),
'datacentertotalvms': ('d_vm_total_count', 'current'),
'datacentermemoryusage': ('d_memory_usage', 'current'),
'datacentercpuusage': ('d_cpu_usage', 'current'),
'datacentermemoryusagepercent': ('d_memory_usage_pct', 'current'),
'datacentercpuusagepercent': ('d_cpu_usage_pct', 'current'),
'datacentertotalmemory': ('d_total_memory', 'current'),
'datacentercputotal': ('d_cpu_total_count', 'current'),
'clusterrunningvms': ('c_vm_running_count', 'current'),
'clusterstoppedvms': ('c_vm_stopped_count', 'current'),
'clustertotalvms': ('c_vm_total_count', 'current'),
'clustermemoryusage': ('c_memory_usage', 'current'),
'clustercpuusage': ('c_cpu_usage', 'current'),
'clustermemoryusagepercent': ('c_memory_usage_percent', 'current'),
'clustercpuusagepercent': ('c_cpu_usage_percent', 'current'),
'clustertotalmemory': ('c_total_memory', 'current'),
'clustercputotal': ('c_cpu_total', 'current'),
}
METRIC_DELIM = '.'
def get_stats():
stats = dict()
v = VCENTERLIST.split()
for vcenter in v:
logger('verb', "get_stats calls vcenter %s user %s" % (vcenter, USERNAME))
server = VIServer()
try:
server.connect(vcenter, USERNAME, PASSWORD)
except Exception:
logger('warn', "failed to connect to %s" % (vcenter))
continue
# get datastores
for ds, dsname in server.get_datastores().items():
DatastoreCapacity = 0
DatastoreFreespace = 0
DatastoreUsagePercent = 0
try:
logger('verb', "get_stats calls Datastore metrics query on vcenter: %s for datastore: %s" % (vcenter, dsname))
props = server._retrieve_properties_traversal(property_names=['name', 'summary.capacity', 'summary.freeSpace'], from_node=ds, obj_type="Datastore")
for prop_set in props:
# mor = prop_set.Obj #in case you need it
for prop in prop_set.PropSet:
if prop.Name == "summary.capacity":
DatastoreCapacity = (prop.Val / 1048576)
elif prop.Name == "summary.freeSpace":
DatastoreFreespace = (prop.Val / 1048576)
except Exception:
logger('warn', "failed to get Datastore metrics value on vcenter: %s for datastore: %s" % (vcenter, dsname))
DatastoreUsagePercent = (((DatastoreCapacity - DatastoreFreespace) * 100) / DatastoreCapacity)
metricnameZoneDatastoreCapacity = METRIC_DELIM.join([vcenter.lower(), "datastores", dsname.lower(), 'datastorecapacity'])
metricnameZoneDatastoreFreespace = METRIC_DELIM.join([vcenter.lower(), "datastores", dsname.lower(), 'datastorefreespace'])
metricnameZoneDatastoreUsagePercent = METRIC_DELIM.join([vcenter.lower(), "datastores", dsname.lower(), 'datastoreusagepercent'])
try:
stats[metricnameZoneDatastoreCapacity] = DatastoreCapacity
stats[metricnameZoneDatastoreFreespace] = DatastoreFreespace
stats[metricnameZoneDatastoreUsagePercent] = DatastoreUsagePercent
except (TypeError, ValueError):
pass
ZoneDatacentersCount = 0
ZoneClustersCount = 0
ZoneHostsCount = 0
ZoneRunningVMS = 0
ZoneStoppedVMS = 0
ZoneTotalVMS = 0
ZoneMemoryUsage = 0
ZoneCpuUsage = 0
ZoneTotalMemory = 0
ZoneCpuTotal = 0
logger('verb', "get_stats calls get_datacenters query on vcenter: %s" % (vcenter))
datacenters = server.get_datacenters()
logger('verb', "get_stats completed get_datacenters query on vcenter: %s" % (vcenter))
ZoneDatacentersCount = len(datacenters)
for d, dname in server.get_datacenters().items():
if "." in dname:
dname = dname.split(".")[0]
DatacenterRunningVMS = 0
DatacenterStoppedVMS = 0
DatacenterTotalVMS = 0
DatacenterClustersCount = 0
DatacenterHostsCount = 0
DatacenterMemoryUsage = 0
DatacenterCpuUsage = 0
DatacenterTotalMemory = 0
DatacenterCpuTotal = 0
logger('verb', "get_stats calls get_clusters query on vcenter: %s for datacenter: %s" % (vcenter, dname))
clusters = server.get_clusters(d)
logger('verb', "get_stats completed get_clusters query on vcenter: %s for datacenter: %s" % (vcenter, dname))
DatacenterClustersCount = len(clusters)
ZoneClustersCount = ZoneClustersCount + DatacenterClustersCount
for c, cname in server.get_clusters(d).items():
if "." in cname:
cname = cname.split(".")[0]
ClusterMemoryUsage = 0
ClusterCpuUsage = 0
ClusterTotalMemory = 0
ClusterCpuTotal = 0
ClusterRunningVMS = 0
ClusterStoppedVMS = 0
ClusterTotalVMS = 0
logger('verb', "get_stats calls get_hosts query on vcenter: %s for cluster: %s" % (vcenter, cname))
hosts = server.get_hosts(c)
logger('verb', "get_stats completed get_hosts query on vcenter: %s for cluster: %s" % (vcenter, cname))
ClusterHostsCount = len(hosts)
DatacenterHostsCount = DatacenterHostsCount + ClusterHostsCount
ZoneHostsCount = ZoneHostsCount + DatacenterHostsCount
for h, hname in server.get_hosts(c).items():
HostMemoryUsage = 0
HostCpuUsage = 0
HostTotalMemory = 0
HostNumCpuCores = 0
HostMhzPerCore = 0
HostStatus = ''
if "." in hname:
hname = hname.split(".")[0]
try:
logger('verb', "get_stats calls Host CPU and Memory metrics query on vcenter: %s for host: %s" % (vcenter, hname))
props = server._retrieve_properties_traversal(property_names=['name', 'summary.overallStatus', 'summary.quickStats.overallMemoryUsage', 'summary.quickStats.overallCpuUsage', 'summary.hardware.memorySize', 'summary.hardware.numCpuCores', 'summary.hardware.cpuMhz'], from_node=h, obj_type="HostSystem")
for prop_set in props:
# mor = prop_set.Obj #in case you need it
for prop in prop_set.PropSet:
if prop.Name == "summary.quickStats.overallMemoryUsage":
HostMemoryUsage = prop.Val
elif prop.Name == "summary.quickStats.overallCpuUsage":
HostCpuUsage = prop.Val
elif prop.Name == "summary.hardware.memorySize":
HostTotalMemory = (prop.Val / 1048576)
elif prop.Name == "summary.hardware.numCpuCores":
HostNumCpuCores = prop.Val
elif prop.Name == "summary.hardware.cpuMhz":
HostMhzPerCore = prop.Val
elif prop.Name == "summary.overallStatus":
HostStatus = prop.Val
if HostStatus == "green":
HostStatus = 0
elif HostStatus == "gray":
HostStatus = 1
elif HostStatus == "yellow":
HostStatus = 2
elif HostStatus == "red":
HostStatus = 3
except Exception:
logger('warn', "failed to get Host CPU and Memory metrics value on vcenter: %s for host: %s" % (vcenter, hname))
try:
logger('verb', "get_stats calls HostRunningVMS query on vcenter: %s for host: %s" % (vcenter, hname))
HostRunningVMS = len(server.get_registered_vms(h, status='poweredOn'))
except Exception:
logger('warn', "failed to get nb of running VMS value on %s" % (hname))
try:
logger('verb', "get_stats calls HostStoppedVMS query on vcenter: %s for host: %s" % (vcenter, hname))
HostStoppedVMS = len(server.get_registered_vms(h, status='poweredOff'))
except Exception:
logger('warn', "failed to get nb of stopped VMS value on %s" % (hname))
try:
logger('verb', "get_stats calls HostTotalVMS query on vcenter: %s for host: %s" % (vcenter, hname))
HostTotalVMS = len(server.get_registered_vms(h))
except Exception:
logger('warn', "failed to get all VMS count on %s" % (hname))
HostCpuTotal = (HostNumCpuCores * HostMhzPerCore)
HostMemoryUsagePercent = ((HostMemoryUsage * 100) / HostTotalMemory)
HostCpuUsagePercent = ((HostCpuUsage * 100) / HostCpuTotal)
metricnameHostStatus = METRIC_DELIM.join([vcenter.lower(), dname.lower(), cname.lower(), hname.lower(), 'hoststatus'])
metricnameHostMemoryUsagePercent = METRIC_DELIM.join([vcenter.lower(), dname.lower(), cname.lower(), hname.lower(), 'hostmemoryusagepercent'])
metricnameHostCpuUsagePercent = METRIC_DELIM.join([vcenter.lower(), dname.lower(), cname.lower(), hname.lower(), 'hostcpuusagepercent'])
metricnameHostMemoryUsage = METRIC_DELIM.join([vcenter.lower(), dname.lower(), cname.lower(), hname.lower(), 'hostmemoryusage'])
metricnameHostCpuUsage = METRIC_DELIM.join([vcenter.lower(), dname.lower(), cname.lower(), hname.lower(), 'hostcpuusage'])
metricnameHostTotalMemory = METRIC_DELIM.join([vcenter.lower(), dname.lower(), cname.lower(), hname.lower(), 'hosttotalmemory'])
metricnameHostCpuTotal = METRIC_DELIM.join([vcenter.lower(), dname.lower(), cname.lower(), hname.lower(), 'hostcputotal'])
metricnameHostRunningVMS = METRIC_DELIM.join([vcenter.lower(), dname.lower(), cname.lower(), hname.lower(), 'hostrunningvms'])
metricnameHostStoppedVMS = METRIC_DELIM.join([vcenter.lower(), dname.lower(), cname.lower(), hname.lower(), 'hoststoppedvms'])
metricnameHostTotalVMS = METRIC_DELIM.join([vcenter.lower(), dname.lower(), cname.lower(), hname.lower(), 'hosttotalvms'])
ClusterMemoryUsage = ClusterMemoryUsage + HostMemoryUsage
ClusterCpuUsage = ClusterCpuUsage + HostCpuUsage
ClusterTotalMemory = ClusterTotalMemory + HostTotalMemory
ClusterCpuTotal = ClusterCpuTotal + HostCpuTotal
ClusterRunningVMS = ClusterRunningVMS + HostRunningVMS
ClusterStoppedVMS = ClusterStoppedVMS + HostStoppedVMS
ClusterTotalVMS = ClusterTotalVMS + HostTotalVMS
ClusterMemoryUsagePercent = ((ClusterMemoryUsage * 100) / ClusterTotalMemory)
ClusterCpuUsagePercent = ((ClusterCpuUsage * 100) / ClusterCpuTotal)
try:
stats[metricnameHostStatus] = HostStatus
stats[metricnameHostMemoryUsage] = HostMemoryUsage
stats[metricnameHostCpuUsage] = HostCpuUsage
stats[metricnameHostTotalMemory] = HostTotalMemory
stats[metricnameHostCpuUsagePercent] = HostCpuUsagePercent
stats[metricnameHostMemoryUsagePercent] = HostMemoryUsagePercent
stats[metricnameHostCpuTotal] = HostCpuTotal
stats[metricnameHostRunningVMS] = HostRunningVMS
stats[metricnameHostStoppedVMS] = HostStoppedVMS
stats[metricnameHostTotalVMS] = HostTotalVMS
except (TypeError, ValueError):
pass
DatacenterRunningVMS = DatacenterRunningVMS + ClusterRunningVMS
DatacenterStoppedVMS = DatacenterStoppedVMS + ClusterStoppedVMS
DatacenterTotalVMS = DatacenterTotalVMS + ClusterTotalVMS
DatacenterMemoryUsage = DatacenterMemoryUsage + ClusterMemoryUsage
DatacenterCpuUsage = DatacenterCpuUsage + ClusterCpuUsage
DatacenterTotalMemory = DatacenterTotalMemory + ClusterTotalMemory
DatacenterCpuTotal = DatacenterCpuTotal + ClusterCpuTotal
DatacenterMemoryUsagePercent = ((DatacenterMemoryUsage * 100) / DatacenterTotalMemory)
DatacenterCpuUsagePercent = ((DatacenterCpuUsage * 100) / DatacenterCpuTotal)
metricnameClusterRunningVMS = METRIC_DELIM.join([vcenter.lower(), dname.lower(), cname.lower(), 'clusterrunningvms'])
metricnameClusterStoppedVMS = METRIC_DELIM.join([vcenter.lower(), dname.lower(), cname.lower(), 'clusterstoppedvms'])
metricnameClusterTotalVMS = METRIC_DELIM.join([vcenter.lower(), dname.lower(), cname.lower(), 'clustertotalvms'])
metricnameClusterMemoryUsage = METRIC_DELIM.join([vcenter.lower(), dname.lower(), cname.lower(), 'clustermemoryusage'])
metricnameClusterCpuUsage = METRIC_DELIM.join([vcenter.lower(), dname.lower(), cname.lower(), 'clustercpuusage'])
metricnameClusterTotalMemory = METRIC_DELIM.join([vcenter.lower(), dname.lower(), cname.lower(), 'clustertotalmemory'])
metricnameClusterCpuTotal = METRIC_DELIM.join([vcenter.lower(), dname.lower(), cname.lower(), 'clustercputotal'])
metricnameClusterMemoryUsagePercent = METRIC_DELIM.join([vcenter.lower(), dname.lower(), cname.lower(), 'clustermemoryusagepercent'])
metricnameClusterCpuUsagePercent = METRIC_DELIM.join([vcenter.lower(), dname.lower(), cname.lower(), 'clustercpuusagepercent'])
try:
stats[metricnameClusterRunningVMS] = ClusterRunningVMS
stats[metricnameClusterStoppedVMS] = ClusterStoppedVMS
stats[metricnameClusterTotalVMS] = ClusterTotalVMS
stats[metricnameClusterMemoryUsage] = ClusterMemoryUsage
stats[metricnameClusterCpuUsage] = ClusterCpuUsage
stats[metricnameClusterMemoryUsagePercent] = ClusterMemoryUsagePercent
stats[metricnameClusterCpuUsagePercent] = ClusterCpuUsagePercent
stats[metricnameClusterTotalMemory] = ClusterTotalMemory
stats[metricnameClusterCpuTotal] = ClusterCpuTotal
except (TypeError, ValueError):
pass
# post datacenter metrics count here
ZoneRunningVMS = ZoneRunningVMS + DatacenterRunningVMS
ZoneStoppedVMS = ZoneStoppedVMS + DatacenterStoppedVMS
ZoneTotalVMS = ZoneTotalVMS + DatacenterTotalVMS
ZoneMemoryUsage = ZoneMemoryUsage + DatacenterMemoryUsage
ZoneCpuUsage = ZoneCpuUsage + DatacenterCpuUsage
ZoneTotalMemory = ZoneTotalMemory + DatacenterTotalMemory
ZoneCpuTotal = ZoneCpuTotal + DatacenterCpuTotal
ZoneMemoryUsagePercent = ((ZoneMemoryUsage * 100) / ZoneTotalMemory)
ZoneCpuUsagePercent = ((ZoneCpuUsage * 100) / ZoneCpuTotal)
metricnameDatacenterRunningVMS = METRIC_DELIM.join([vcenter.lower(), dname.lower(), 'datacenterrunningvms'])
metricnameDatacenterStoppedVMS = METRIC_DELIM.join([vcenter.lower(), dname.lower(), 'datacenterstoppedvms'])
metricnameDatacenterTotalVMS = METRIC_DELIM.join([vcenter.lower(), dname.lower(), 'datacentertotalvms'])
metricnameDatacenterMemoryUsage = METRIC_DELIM.join([vcenter.lower(), dname.lower(), 'datacentermemoryusage'])
metricnameDatacenterCpuUsage = METRIC_DELIM.join([vcenter.lower(), dname.lower(), 'datacentercpuusage'])
metricnameDatacenterMemoryUsagePercent = METRIC_DELIM.join([vcenter.lower(), dname.lower(), 'datacentermemoryusagepercent'])
metricnameDatacenterCpuUsagePercent = METRIC_DELIM.join([vcenter.lower(), dname.lower(), 'datacentercpuusagepercent'])
metricnameDatacenterTotalMemory = METRIC_DELIM.join([vcenter.lower(), dname.lower(), 'datacentertotalmemory'])
metricnameDatacenterCpuTotal = METRIC_DELIM.join([vcenter.lower(), dname.lower(), 'datacentercputotal'])
try:
stats[metricnameDatacenterRunningVMS] = DatacenterRunningVMS
stats[metricnameDatacenterStoppedVMS] = DatacenterStoppedVMS
stats[metricnameDatacenterTotalVMS] = DatacenterTotalVMS
stats[metricnameDatacenterMemoryUsage] = DatacenterMemoryUsage
stats[metricnameDatacenterCpuUsage] = DatacenterCpuUsage
stats[metricnameDatacenterMemoryUsagePercent] = DatacenterMemoryUsagePercent
stats[metricnameDatacenterCpuUsagePercent] = DatacenterCpuUsagePercent
stats[metricnameDatacenterTotalMemory] = DatacenterTotalMemory
stats[metricnameDatacenterCpuTotal] = DatacenterCpuTotal
except (TypeError, ValueError):
pass
# post zone metrics count here
metricnameZoneRunningVMS = METRIC_DELIM.join([vcenter.lower(), 'zonerunningvms'])
metricnameZoneStoppedVMS = METRIC_DELIM.join([vcenter.lower(), 'zonestoppedvms'])
metricnameZoneTotalVMS = METRIC_DELIM.join([vcenter.lower(), 'zonetotalvms'])
metricnameZoneMemoryUsage = METRIC_DELIM.join([vcenter.lower(), 'zonememoryusage'])
metricnameZoneCpuUsage = METRIC_DELIM.join([vcenter.lower(), 'zonecpuusage'])
metricnameZoneMemoryUsagePercent = METRIC_DELIM.join([vcenter.lower(), 'zonememoryusagepercent'])
metricnameZoneCpuUsagePercent = METRIC_DELIM.join([vcenter.lower(), 'zonecpuusagepercent'])
metricnameZoneTotalMemory = METRIC_DELIM.join([vcenter.lower(), 'zonetotalmemory'])
metricnameZoneCpuTotal = METRIC_DELIM.join([vcenter.lower(), 'zonecputotal'])
try:
stats[metricnameZoneRunningVMS] = ZoneRunningVMS
stats[metricnameZoneStoppedVMS] = ZoneStoppedVMS
stats[metricnameZoneTotalVMS] = ZoneTotalVMS
stats[metricnameZoneMemoryUsage] = ZoneMemoryUsage
stats[metricnameZoneCpuUsage] = ZoneCpuUsage
stats[metricnameZoneMemoryUsagePercent] = ZoneMemoryUsagePercent
stats[metricnameZoneCpuUsagePercent] = ZoneCpuUsagePercent
stats[metricnameZoneTotalMemory] = ZoneTotalMemory
stats[metricnameZoneCpuTotal] = ZoneCpuTotal
except (TypeError, ValueError):
pass
metricnameZoneDatacentersCount = METRIC_DELIM.join([vcenter.lower(), 'zonedatacenterscount'])
metricnameZoneClustersCount = METRIC_DELIM.join([vcenter.lower(), 'zoneclusterscount'])
metricnameZoneHostsCount = METRIC_DELIM.join([vcenter.lower(), 'zonehostscount'])
try:
stats[metricnameZoneDatacentersCount] = ZoneDatacentersCount
stats[metricnameZoneClustersCount] = ZoneClustersCount
stats[metricnameZoneHostsCount] = ZoneHostsCount
except (TypeError, ValueError):
pass
server.disconnect()
return stats
# callback configuration for module
def configure_callback(conf):
global NAME, VCENTERLIST, USERNAME, PASSWORD, VERBOSE_LOGGING, SKIP
NAME = 'Vcenter'
VCENTERLIST = ''
USERNAME = ''
PASSWORD = ''
VERBOSE_LOGGING = False
SKIP = 10
for node in conf.children:
if node.key == "Vcenter":
VCENTERLIST = node.values[0]
elif node.key == "Username":
USERNAME = node.values[0]
elif node.key == "Password":
PASSWORD = node.values[0]
elif node.key == "Verbose":
VERBOSE_LOGGING = bool(node.values[0])
elif node.key == "Skip":
SKIP = int(node.values[0])
else:
logger('warn', 'Unknown config key: %s' % node.key)
def read_callback():
global RUN, SKIP
RUN += 1
if RUN % SKIP != 1:
return
logger('verb', "beginning read_callback")
info = get_stats()
if not info:
logger('warn', "%s: No data received" % NAME)
return
for key, value in info.items():
key_prefix = ''
key_root = key
logger('verb', "read_callback key %s" % (key))
logger('verb', "read_callback value %s" % (value))
if value not in METRIC_TYPES:
try:
key_prefix, key_root = key.rsplit(METRIC_DELIM, 1)
except ValueError:
pass
if key_root not in METRIC_TYPES:
continue
key_root, val_type = METRIC_TYPES[key_root]
key_name = METRIC_DELIM.join([key_prefix, key_root])
logger('verb', "key_name %s" % (key_name))
val = collectd.Values(plugin=NAME, type=val_type)
val.type_instance = key_name
val.values = [value]
val.dispatch()
# logging function
def logger(t, msg):
if t == 'err':
collectd.error('%s: %s' % (NAME, msg))
elif t == 'warn':
collectd.warning('%s: %s' % (NAME, msg))
elif t == 'verb':
if VERBOSE_LOGGING:
collectd.info('%s: %s' % (NAME, msg))
else:
collectd.notice('%s: %s' % (NAME, msg))
# main
collectd.register_config(configure_callback)
collectd.register_read(read_callback)