diff --git a/dcache_exporter.spec b/dcache_exporter.spec index 00d695f..e9a8561 100644 --- a/dcache_exporter.spec +++ b/dcache_exporter.spec @@ -1,10 +1,11 @@ Summary: Prometheus exporter for dcache metrics Name: dcache_exporter Version: %{_version} -Release: 1 +Release: 2 License: GPLv3 Source0: %{_source} Requires: python2-prometheus_client +BuildArch: noarch %{?systemd_requires} %description diff --git a/src/dcache_exporter.py b/src/dcache_exporter.py index 0bdefb6..284b4c0 100755 --- a/src/dcache_exporter.py +++ b/src/dcache_exporter.py @@ -1,5 +1,6 @@ #!/usr/bin/python +import argparse import copy import httplib import re @@ -12,7 +13,11 @@ from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer from SocketServer import ThreadingMixIn -VERSION = '0.5' +VERSION = '0.6' +PROM_PORT = 9310 +PROM_ADDRESS = '::' +INFO_PORT = 22112 +INFO_ADDRESS = 'localhost' def start_http6_server(port, addr=''): """Starts an HTTP server for prometheus metrics as a daemon thread""" @@ -91,9 +96,10 @@ class DcacheCollector(object): ExportTag('pools', 'pool', False, [ 'active', 'queued', 'total', 'precious', 'removable', 'used', 'free' ], [], None, None), ExportTag('poolgroups', 'poolgroup', False, [ 'active', 'queued', 'total', 'precious', 'removable', 'used', 'free' ], [], None, None) ] - def __init__(self, host, port): + def __init__(self, host, port, cluster): self._info_host = host self._info_port = port + self._cluster = cluster self._tree = None self._ns = None self._metrics = {} @@ -140,14 +146,14 @@ def _collect_metrics_set(self, element, export): name = element.attrib.get('name') if '@' in name: name = name[:name.find('@')] - labels = [ (export.prefix, name) ] + labels = [ ('dcache_cluster', self._cluster), (export.prefix, name) ] for child in element: self._collect_metric(child, export, labels) def _collect_all_metrics(self): self._metrics = {} for export in DcacheCollector.ExportTags: - elements = self._tree.findall("{0}{1}".format(self._ns, export.name)) + elements = self._tree.findall('{0}{1}'.format(self._ns, export.name)) if len(elements) > 0: for elem in elements[0]: self._collect_metrics_set(elem, export) @@ -159,9 +165,22 @@ def collect(self): for metric_name in sorted(self._metrics.keys()): yield self._metrics[metric_name] + +def create_parser(): + parser = argparse.ArgumentParser() + parser.add_argument('--metrics-port', dest='metrics_port', type=int, default=PROM_PORT, help='port to export metrics on') + parser.add_argument('--metrics-address', dest='metrics_address', type=str, default=PROM_ADDRESS, help='address to export metrics on') + parser.add_argument('-c', '--cluster', dest='cluster', type=str, default='dcache_cluster', help='cluster prometheus label') + parser.add_argument('--info-port', dest='info_port', type=int, default=INFO_PORT, help='port to export metrics on') + parser.add_argument('--info-address', dest='info_address', type=str, default=INFO_ADDRESS, help='address to export metrics on') + return parser + + def main(): - pclient.REGISTRY.register(DcacheCollector("localhost", 22112)) - start_http6_server(9310, '::') + parser = create_parser() + args = parser.parse_args() + pclient.REGISTRY.register(DcacheCollector(args.info_address, args.info_port, args.cluster)) + start_http6_server(args.metrics_port, args.metrics_address) while True: time.sleep(10)