Skip to content

Commit

Permalink
add prometheus manual targets relation
Browse files Browse the repository at this point in the history
  • Loading branch information
matuskosut committed Jun 15, 2023
1 parent 0acdc2a commit 9ce9baf
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
5 changes: 5 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
1 change: 1 addition & 0 deletions layer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 4 additions & 0 deletions metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
"interface": "juju-info"
"scope": "container"
"provides":
"prometheus-manual-job":
"interface": "prometheus-manual"
"prometheus-target":
"interface": "http"
"scrape":
"interface": "prometheus"
"resources":
Expand Down
58 changes: 58 additions & 0 deletions reactive/prometheus_node_exporter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import socket
from shutil import copyfile
from subprocess import call

Expand Down Expand Up @@ -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")
Expand All @@ -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

0 comments on commit 9ce9baf

Please sign in to comment.