diff --git a/config.yaml b/config.yaml index 7e9f589..5b6bb3a 100644 --- a/config.yaml +++ b/config.yaml @@ -1,4 +1,9 @@ "options": + "job-name": + "type": "string" + "default": !!str "node-exporter" + "description": | + Default job name passed to prometheus scrape config "host": "type": "string" "default": !!str "0.0.0.0" diff --git a/layer.yaml b/layer.yaml index 4f7d641..bd3c7af 100644 --- a/layer.yaml +++ b/layer.yaml @@ -8,5 +8,6 @@ "includes": - "layer:basic" - "interface:prometheus" +- "interface:prometheus-manual" "repo": "https://github.com/CanonicalLtd/prometheus-node-exporter-charm" "is": "prometheus-node-exporter" diff --git a/metadata.yaml b/metadata.yaml index 5b63afe..d8b7bd7 100644 --- a/metadata.yaml +++ b/metadata.yaml @@ -14,6 +14,10 @@ "interface": "juju-info" "scope": "container" "provides": + "prometheus-manual-job": + "interface": "prometheus-manual" + "prometheus-target": + "interface": "http" "scrape": "interface": "prometheus" "resources": diff --git a/reactive/prometheus_node_exporter.py b/reactive/prometheus_node_exporter.py index abea6f8..292ce7f 100644 --- a/reactive/prometheus_node_exporter.py +++ b/reactive/prometheus_node_exporter.py @@ -1,4 +1,5 @@ import os +import socket from shutil import copyfile from subprocess import call @@ -162,6 +163,50 @@ def prometheus_left(): clear_flag('prometheus.node.exporter.configured_port') +@when('prometheus-target.available') +def configure_http(prometheus_target): + job_name = 'node-exporter' + log('Register target {}: {}:{}'.format( + job_name, + get_ip()[1], + config.get('port') + )) + open_port(config.get('port')) + prometheus_target.configure( + private_address=get_ip()[1], + port=config('port') + ) + + +@when('endpoint.prometheus-manual-job.joined') +def register_prometheus_jobs(): + prometheus = endpoint_from_flag('endpoint.prometheus-manual-job.joined') + job_name = config('job-name') + target = '{}:{}'.format( + get_ip()[1], + config('port') + ) + labels = {} + try: + labels['fqdn'] = socket.getfqdn() + except Exception as e: + log('Failed to read FQDN: {}'.format(str(e))) + try: + labels['hostname'] = socket.gethostname() + except Exception as e: + log('Failed to read FQDN: {}'.format(str(e))) + log('Register manual-job {}: {}'.format(job_name, target)) + open_port(config('port')) + prometheus.register_job( + job_name=job_name, + job_data={ + 'static_configs': [{ + 'targets': [target], + 'labels': labels + }] + }) + + @hook('stop') def cleanup(): status_set("maintenance", "cleaning up prometheus-node-exporter") @@ -170,3 +215,16 @@ def cleanup(): for f in [NODE_EXPORTER_BIN, NODE_EXPORTER_SERVICE]: call('rm {}'.format(f).split()) status_set("active", "cleanup complete") + + +def get_ip(): + """Get internal IP and relation IP""" + rel_ip = None + main_ip = unit_private_ip() if ( + not config('host') or (config('host') == "none") + ) else config('host') + if not main_ip or (main_ip == '0.0.0.0'): + rel_ip = unit_private_ip() + if not rel_ip: + rel_ip = main_ip + return main_ip, rel_ip