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

Closed
wants to merge 4 commits into from
Closed
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
8 changes: 8 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
config.yml
alerts
build
dashboards
dist
kubernetes
openshift
systemd
tests
11 changes: 7 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
FROM python:3.7-alpine
FROM python:3.12.3-alpine

LABEL MAINTAINER="Daniel Pryor <[email protected]>"
LABEL NAME=vmware_exporter

WORKDIR /opt/vmware_exporter/
COPY . /opt/vmware_exporter/

COPY requirements.txt setup.py README.md MANIFEST.in /opt/vmware_exporter/
RUN set -x; buildDeps="gcc python3-dev musl-dev libffi-dev openssl openssl-dev rust cargo" \
&& apk add --no-cache --update $buildDeps \
&& pip install -r requirements.txt . \
&& pip install -r requirements.txt \
&& apk del $buildDeps
COPY vmware_exporter/ /opt/vmware_exporter/vmware_exporter
RUN pip install .



EXPOSE 9272

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: docker.io/anthony84657/vmware_exporter:beta
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" ]
32 changes: 26 additions & 6 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
prometheus-client==0.0.19
pytz
pyvmomi>=6.5
twisted>=14.0.2
pyyaml>=5.1
service-identity
attrs==24.2.0
Automat==24.8.1
certifi==2024.8.30
cffi==1.17.1
charset-normalizer==3.3.2
constantly==23.10.4
cryptography==43.0.1
hyperlink==21.0.0
idna==3.10
incremental==24.7.2
prometheus_client==0.0.19
pyasn1==0.6.1
pyasn1_modules==0.4.1
pycparser==2.22
pytz==2024.2
pyvmomi==8.0.3.0.1
PyYAML==6.0.2
requests==2.32.3
service-identity==24.1.0
setuptools==72.1.0
six==1.16.0
Twisted==24.7.0
typing_extensions==4.12.2
urllib3==2.2.3
wheel==0.44.0
zope.interface==7.0.3
37 changes: 31 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 @@ -155,6 +155,10 @@ def _create_metric_containers(self):
'vmware_vm_guest_disk_capacity',
'Disk capacity metric per partition',
labels=self._labelNames['vmguests'] + ['partition', ]),
'vmware_vm_guest_disk_used_percent': GaugeMetricFamily(
'vmware_vm_guest_disk_used_percent',
'Disk capacity used per partition in percent',
labels=self._labelNames['vmguests'] + ['partition', ]),
'vmware_vm_guest_tools_running_status': GaugeMetricFamily(
'vmware_vm_guest_tools_running_status',
'VM tools running status',
Expand Down Expand Up @@ -738,6 +742,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 +1110,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 +1203,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 @@ -1592,6 +1607,13 @@ def _vmware_get_vms(self, metrics):
metrics['vmware_vm_guest_disk_capacity'].add_metric(
labels + [disk.diskPath], disk.capacity
)
try:
percent_used = ((disk.capacity - disk.freeSpace) / disk.capacity) * 100
metrics["vmware_vm_guest_disk_used_percent"].add_metric(
labels + [disk.diskPath], percent_used
)
except ZeroDivisionError:
pass

if 'guest.toolsStatus' in row:
metrics['vmware_vm_guest_tools_running_status'].add_metric(
Expand Down Expand Up @@ -1939,6 +1961,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 +2006,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