Skip to content

Commit

Permalink
Allow hostnames without unit id (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
sed-i authored Feb 8, 2024
1 parent c4bd1c5 commit 98319c8
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
10 changes: 3 additions & 7 deletions lib/charms/nrpe_exporter/v0/nrpe_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,13 +453,9 @@ def _generate_data(self, relation) -> Tuple[list, list]:

def _generate_alert(self, relation, cmd, id, unit) -> dict:
"""Generate an on-the-fly Alert rule."""
pattern = r"^(.*?)[-_](\d+)$"
if match := re.match(pattern, id.replace("_", "-")):
app_name, unit_num = match.groups()
else:
raise ValueError(f"Invalid unit identifier '{id}': expected a string like 'unit-0'")
unit_label = re.sub(r"^(.*?)[-_](\d+)$", r"\1/\2", id.replace("_", "-"))
app_label = re.sub(r"^(.*?)[-_]\d+$", r"\1", id.replace("_", "-"))

unit_label = f"{app_name}/{unit_num}"
return {
"alert": "{}NrpeAlert".format("".join([x.title() for x in cmd.split("_")])),
# Average over 5 minutes considering a 60-second scrape interval
Expand All @@ -470,7 +466,7 @@ def _generate_alert(self, relation, cmd, id, unit) -> dict:
"labels": {
"severity": "{{ if eq $value 0.0 -}} info {{- else if eq $value 1.0 -}} warning {{- else if eq $value 2.0 -}} critical {{- else if eq $value 3.0 -}} error {{- end }}",
"juju_model": self.model.name,
"juju_application": app_name,
"juju_application": app_label,
"juju_unit": unit_label,
"nrpe_application": relation.app.name,
"nrpe_unit": unit.name,
Expand Down
2 changes: 1 addition & 1 deletion tests/scenario/test_alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_relation(ctx, n_remote_units):
"monitors",
remote_app_name="remote",
remote_units_data={
i: {"monitors": yaml.safe_dump(monitors_raw), "target-id": "juju-ubuntu-0"}
i: {"monitors": yaml.safe_dump(monitors_raw), "target-id": "hostname-without-unit-id"}
for i in range(n_remote_units)
},
)
Expand Down

0 comments on commit 98319c8

Please sign in to comment.