Skip to content

Commit

Permalink
Custom static metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
crabhi committed Apr 9, 2024
1 parent 164b4cc commit 442a31a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
35 changes: 34 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ Proxmox VE Configuration
For security reasons it is essential to add a user with read-only access
(PVEAuditor role) for the purpose of metrics collection.

Refer to the `Proxmox Documentation`_ for the several ways of creating a user.
Refer to the `Proxmox Documentation`_ for the several ways of creating a user.
Once created, assign the user the `/` path permission.

Prometheus Configuration
Expand Down Expand Up @@ -296,3 +296,36 @@ Grafana Dashboards
.. _`supports Let's Encrypt`: https://pve.proxmox.com/pve-docs/pve-admin-guide.html#sysadmin_certificate_management
.. _`Proxmox Documentation`: https://pve.proxmox.com/pve-docs/pve-admin-guide.html#pveum_permission_management
.. _`Proxmox via Prometheus by Pietro Saccardi`: https://grafana.com/grafana/dashboards/10347-proxmox-via-prometheus/


Custom Metrics
--------------

You can add custom (static) metrics into the Notes section of Proxmox. These will be exported by this exporter.

Go to Proxmox - Datacenter - Notes and click the Edit button. Add a Markdown header that says exactly
``Prometheus metrics`` and a backtick-delimited (\`\`\`) block with the actual metrics.

For example:

.. code:: markdown
## Prometheus metrics
```
# These metrics are parsed by our prometheus-pve-exporter which
# expects the exact heading above
# HELP my_metric_bytes Whatever
# TYPE my_metric_bytes gauge
my_metric_bytes 350e9
```
converts into

.. code::
# HELP my_metric_bytes Whatever
# TYPE my_metric_bytes gauge
my_metric_bytes{__source="Datacenter > Notes"} 3.5e+011
in the exporter output.
33 changes: 33 additions & 0 deletions src/pve_exporter/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
import collections
import itertools
import logging
import re

from prometheus_client.samples import Sample
from prometheus_client.parser import text_string_to_metric_families
from prometheus_client.registry import Collector
from proxmoxer import ProxmoxAPI
from proxmoxer.core import ResourceException
Expand Down Expand Up @@ -359,12 +362,42 @@ def collect(self): # pylint: disable=missing-docstring
return [disk_size]


class ClusterCustomMetricsCollector(Collector):
"""
Collects custom labels defined in the Notes section
"""
def __init__(self, pve):
self._pve = pve

def collect(self):
metrics = []
notes = self._pve.cluster.options.get().get('description', '')
if m := re.match(r'#+\s*Prometheus metrics\s*```(.*?)```', notes, re.MULTILINE | re.DOTALL):
custom_metrics_text = m.group(1)

for metric in text_string_to_metric_families(custom_metrics_text):
metric.name = f'pve_{metric.name}'
new_samples = [
Sample(name=f'pve_{sample.name}', labels=sample.labels | {'__source': 'Datacenter > Notes'},
exemplar=sample.exemplar, value=sample.value)
for sample in metric.samples
]
metric.samples = new_samples

metrics.append(metric)

return metrics


def collect_pve(config, host, options: CollectorsOptions):
"""Scrape a host and return prometheus text format for it"""

pve = ProxmoxAPI(host, **config)

registry = CollectorRegistry()

registry.register(ClusterCustomMetricsCollector(pve))

if options.status:
registry.register(StatusCollector(pve))
if options.resources:
Expand Down

0 comments on commit 442a31a

Please sign in to comment.