Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UUID and Instance UUID in VM labels #338

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.7-alpine
FROM python:3.12-alpine

LABEL MAINTAINER="Daniel Pryor <[email protected]>"
LABEL NAME=vmware_exporter
Expand Down
18 changes: 8 additions & 10 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ version: '2'
services:
vmware_exporter:
# Using the latest tag, but you can use vers(v0.9.5 for example
image: pryorda/vmware_exporter:latest
image: localhost/pryorda/vmware_exporter:dev
build:
context: .
dockerfile: Dockerfile
ports:
- "9275:9272"
environment:
VSPHERE_HOST: "vcenter-host"
VSPHERE_USER: "username"
VSPHERE_PASSWORD: "P@ssw0rd"
VSPHERE_IGNORE_SSL: "True"
VSPHERE_COLLECT_VMS: "False"
VSPHERE_COLLECT_VMGUESTS: "False"
- "9272:9272"
volumes:
- ./config.yml:/config.yml:ro
restart: always
#FOR DEBUG UNCOMMENT NEXT LINE
#command: ["-l","DEBUG"]
command: ["-l","DEBUG", "-c" , "/config.yml"]
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
prometheus-client==0.0.19
prometheus-client==0.21.0
pytz
pyvmomi>=6.5
twisted>=14.0.2
pyyaml>=5.1
service-identity
requests
26 changes: 20 additions & 6 deletions vmware_exporter/vmware_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ def __init__(

# label names and ammount will be needed later to insert labels from custom attributes
self._labelNames = {
'vms': ['vm_name', 'ds_name', 'host_name', 'dc_name', 'cluster_name'],
'vm_perf': ['vm_name', 'ds_name', 'host_name', 'dc_name', 'cluster_name'],
'vmguests': ['vm_name', 'ds_name', 'host_name', 'dc_name', 'cluster_name'],
'snapshots': ['vm_name', 'ds_name', 'host_name', 'dc_name', 'cluster_name'],
'vms': ['vm_name', 'ds_name', 'host_name', 'dc_name', 'cluster_name', 'uuid', 'instance_uuid', 'moid'],
'vm_perf': ['vm_name', 'ds_name', 'host_name', 'dc_name', 'cluster_name', 'uuid', 'instance_uuid', 'moid'],
'vmguests': ['vm_name', 'ds_name', 'host_name', 'dc_name', 'cluster_name', 'uuid', 'instance_uuid','moid'],
'snapshots': ['vm_name', 'ds_name', 'host_name', 'dc_name', 'cluster_name', 'uuid', 'instance_uuid','moid'],
'datastores': ['ds_name', 'dc_name', 'ds_cluster'],
'hosts': ['host_name', 'dc_name', 'cluster_name'],
'host_perf': ['host_name', 'dc_name', 'cluster_name'],
Expand Down Expand Up @@ -738,6 +738,8 @@ def vm_inventory(self):
'runtime.host',
'parent',
'summary.config.vmPathName',
'summary.config.uuid',
'summary.config.instanceUuid',
]

if self.collect_only['vms'] is True:
Expand Down Expand Up @@ -1104,6 +1106,15 @@ def vm_labels(self):
if host_moid in host_labels:
labels[moid] = labels[moid] + host_labels[host_moid]

if 'summary.config.uuid' in row:
labels[moid] += [row['summary.config.uuid']]
else:
labels[moid] += ["no_uuid"]
if 'summary.config.instanceUuid' in row:
labels[moid] += [row['summary.config.instanceUuid']]
else:
labels[moid] += ["no_instanceUuid"]
labels[moid] += [moid]
"""
this code was in vm_inventory before
but I have the feeling it is best placed here where
Expand Down Expand Up @@ -1188,7 +1199,7 @@ def updateMetricsLabelNames(self, metrics, metric_types):
for metric_name in self._metricNames.get(metric_type, []):
metric = metrics.get(metric_name)
labelnames = metric._labelnames
metric._labelnames = labelnames[0:len(self._labelNames[metric_type])]
metric._labelnames = list(labelnames[0:len(self._labelNames[metric_type])])
metric._labelnames += customAttributesLabelNames
metric._labelnames += labelnames[len(self._labelNames[metric_type]):]
metric._labelnames = list(map(lambda x: re.sub('[^a-zA-Z0-9_]', '_', x), metric._labelnames))
Expand Down Expand Up @@ -1939,6 +1950,8 @@ def _async_render_GET(self, request):
logging.error(traceback.format_exc())
request.setResponseCode(500)
request.write(b'# Collection failed')
if request._disconnected:
return
request.finish()

# We used to call request.processingFailed to send a traceback to browser
Expand Down Expand Up @@ -1982,10 +1995,11 @@ def generate_latest_metrics(self, request):
registry = CollectorRegistry()
registry.register(ListCollector(metrics))
output = generate_latest(registry)

request.setHeader("Content-Type", "text/plain; charset=UTF-8")
request.setResponseCode(200)
request.write(output)
if request._disconnected:
return
request.finish()


Expand Down