<commit_before>import itertools
from proxmoxer import ProxmoxAPI

from prometheus_client import CollectorRegistry, generate_latest
from prometheus_client.core import GaugeMetricFamily

class StatusCollector(object):
  """
  Collects Proxmox VE Node/VM/CT-Status

    # HELP pve_up Node/VM/CT-Status is online/running
    # TYPE pve_up gauge
    pve_up{id="node/proxmox-host"} 1.0
    pve_up{id="lxc/101"} 1.0
    pve_up{id="qemu/102"} 1.0
  """

  def __init__(self, pve):
    self._pve = pve

  def collect(self):
    status_metrics = GaugeMetricFamily(
      'pve_up',
      'Node/VM/CT-Status is online/running',
      labels=['id'])

    for node in self._pve.cluster.status.get():
      label_values = [node['id']]
      status_metrics.add_metric(label_values, node['online'])

    for resource in self._pve.cluster.resources.get(type='vm'):
      label_values = [resource['id']]
      status_metrics.add_metric(label_values, resource['status'] == 'running')

    yield status_metrics

class VersionCollector(object):
  """
  Collects Proxmox VE build information. E.g.:

    # HELP pve_version_info Proxmox VE version info
    # TYPE pve_version_info gauge
    pve_version_info{release="15",repoid="7599e35a",version="4.4"} 1.0

  """

  def __init__(self, pve):
    self._pve = pve

  def collect(self):
    version = self._pve.version.get()

    del version['keyboard']

    labels, label_values = zip(*version.items())
    metric = GaugeMetricFamily(
        'pve_version_info',
        'Proxmox VE version info',
        labels=labels
    )
    metric.add_metric(label_values, 1)

    yield metric

class ClusterNodeCollector(object):
  """
  Collects Proxmox VE cluster node information. E.g.:

    # HELP pve_node_info Node info
    # TYPE pve_node_info gauge
    pve_node_info{id="node/proxmox-host",ip="10.20.30.40",level="c",local="1",name="proxmox-host",nodeid="0"} 1.0
  """

  def __init__(self, pve):
    self._pve = pve

  def collect(self):
    nodes = self._pve.cluster.status.get()<commit_msg>Fix crash when pointed at cluster, closes #1 (#2)<commit_after>import itertools
from proxmoxer import ProxmoxAPI

from prometheus_client import CollectorRegistry, generate_latest
from prometheus_client.core import GaugeMetricFamily

class StatusCollector(object):
  """
  Collects Proxmox VE Node/VM/CT-Status

    # HELP pve_up Node/VM/CT-Status is online/running
    # TYPE pve_up gauge
    pve_up{id="node/proxmox-host"} 1.0
    pve_up{id="lxc/101"} 1.0
    pve_up{id="qemu/102"} 1.0
  """

  def __init__(self, pve):
    self._pve = pve

  def collect(self):
    status_metrics = GaugeMetricFamily(
      'pve_up',
      'Node/VM/CT-Status is online/running',
      labels=['id'])

    nodes = [entry for entry in self._pve.cluster.status.get() if entry['type'] == 'node']
    for node in nodes:
      label_values = [node['id']]
      status_metrics.add_metric(label_values, node['online'])

    for resource in self._pve.cluster.resources.get(type='vm'):
      label_values = [resource['id']]
      status_metrics.add_metric(label_values, resource['status'] == 'running')

    yield status_metrics

class VersionCollector(object):
  """
  Collects Proxmox VE build information. E.g.:

    # HELP pve_version_info Proxmox VE version info
    # TYPE pve_version_info gauge
    pve_version_info{release="15",repoid="7599e35a",version="4.4"} 1.0

  """

  def __init__(self, pve):
    self._pve = pve

  def collect(self):
    version = self._pve.version.get()

    del version['keyboard']

    labels, label_values = zip(*version.items())
    metric = GaugeMetricFamily(
        'pve_version_info',
        'Proxmox VE version info',
        labels=labels
    )
    metric.add_metric(label_values, 1)

    yield metric

class ClusterNodeCollector(object):
  """
  Collects Proxmox VE cluster node information. E.g.:

    # HELP pve_node_info Node info
    # TYPE pve_node_info gauge
    pve_node_info{id="node/proxmox-host",ip="10.20.30.40",level="c",local="1",name="proxmox-host",nodeid="0"} 1.0
  """

  def __init__(self, pve):
    self._pve = pve

  def collect(self):
    nodes = [entry for entry in self._pve.cluster.status.get() if entry['type'] == 'node']
{'fraction_non_alphanumeric': 0.1291248206599713, 'fraction_numerical': 0.018173122907699665, 'mean_word_length': 3.596703296703297, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6927712', 'n_tokens_mistral': 1547, 'n_tokens_neox': 1453, 'n_words': 303}