summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHaw Loeung <[email protected]>2025-07-22 23:23:21 +0000
committerCanonical IS Mergebot <[email protected]>2025-07-22 23:23:21 +0000
commit0a5879f2e2d99f2c8e579af25b3c8c5e39e34e19 (patch)
tree269330e53972565de0195b43f62bd76c7cb61be8
parentd22eeaf4be7531b8af49feb057a30043a3f9218e (diff)
parent43e2d7c9ab36623af08e1baa32e5f2d77a7a2795 (diff)
Better handle NRPE short names
Reviewed-on: https://code.launchpad.net/~hloeung/content-cache-charm/+git/content-cache-charm/+merge/489472 Reviewed-by: 🤖 Canonical IS Review Bot <[email protected]>
-rw-r--r--lib/utils.py14
-rw-r--r--tests/unit/test_utils.py6
2 files changed, 19 insertions, 1 deletions
diff --git a/lib/utils.py b/lib/utils.py
index e4ac33f..09bccf3 100644
--- a/lib/utils.py
+++ b/lib/utils.py
@@ -67,7 +67,19 @@ def next_port_pair(
def _nagios_check_name_strip(name):
- return name.replace('.', '_').replace('-', '_').replace('/', '').replace('__', '_').strip('_')
+ return (
+ name.replace('.', '_')
+ .replace('-', '_')
+ .replace('/', '')
+ .replace('__', '_')
+ .replace('(', '')
+ .replace(')', '')
+ .replace('$', '')
+ .replace('[', '_')
+ .replace(']', '_')
+ .replace('*', 'wildcard')
+ .strip('_')
+ )
def generate_nagios_check_name(name, prefix='', suffix=''):
diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py
index 62fe1e1..94e4d05 100644
--- a/tests/unit/test_utils.py
+++ b/tests/unit/test_utils.py
@@ -2,11 +2,13 @@ import collections
import datetime
import os
import pathlib
+import re
import shutil
import sys
import tempfile
import textwrap
import unittest
+
from ipaddress import IPv4Network, IPv6Network
from unittest import mock
@@ -69,6 +71,10 @@ class TestLibUtils(unittest.TestCase):
self.assertEqual(
utils.generate_nagios_check_name('site-1.local', 'site', '/somepath'), 'site_site_1_local_somepath'
)
+ # charmhelpers/contrib/charmsupport/nrpe.py Check() uses this regex
+ shortname_re = '[A-Za-z0-9-_.@]+$'
+ shortname = utils.generate_nagios_check_name('my-cloud.io', 'site', '/docs/exp/(.*)$')
+ self.assertTrue(re.match(shortname_re, shortname))
@freezegun.freeze_time("2019-03-22", tz_offset=0)
def test_generate_token(self):