Module: check_mk
Branch: master
Commit: 8e45211be812934324b246f542eb8f15390fdf5d
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=8e45211be81293…
Author: Moritz Kiemer <mo(a)mathias-kettner.de>
Date: Thu Jan 31 08:55:24 2019 +0100
7136 mk_jolokia: connection timeout is now configurable
Previously the connection timeout was hardcoded to 1 second, based on the assumption that
the jolokia server is most likely the local host.
This assumption has been dropped, and the timeout can now be configured using the agent
bakery or by adding (e.g.) "timeout=23.0" to your configuration file.
Change-Id: Ib95da73beb3ae7e4d62246776219615b773cc90b
---
.werks/7136 | 11 +++++++++++
agents/plugins/mk_jolokia.py | 18 ++++++++++--------
tests/unit/plugins/test_mk_jolokia.py | 20 ++++++++++++++------
3 files changed, 35 insertions(+), 14 deletions(-)
diff --git a/.werks/7136 b/.werks/7136
new file mode 100644
index 0000000..e2db581
--- /dev/null
+++ b/.werks/7136
@@ -0,0 +1,11 @@
+Title: mk_jolokia: connection timeout is now configurable
+Level: 1
+Component: checks
+Compatible: compat
+Edition: cre
+Version: 1.6.0i1
+Date: 1548921175
+Class: feature
+
+Previously the connection timeout was hardcoded to 1 second, based on the assumption that
the jolokia server is most likely the local host.
+This assumption has been dropped, and the timeout can now be configured using the agent
bakery or by adding (e.g.) "timeout=23.0" to your configuration file.
diff --git a/agents/plugins/mk_jolokia.py b/agents/plugins/mk_jolokia.py
index 252057a..648bdc9 100755
--- a/agents/plugins/mk_jolokia.py
+++ b/agents/plugins/mk_jolokia.py
@@ -60,6 +60,7 @@ DEFAULT_CONFIG = {
"protocol": "http",
"server": "localhost",
"port": 8080,
+ "timeout": 1.0,
"user": "monitoring",
"password": None,
"mode": "digest",
@@ -197,7 +198,7 @@ class JolokiaInstance(object):
if instance:
err_msg += " for %s" % instance
- required_keys = {"protocol", "server", "port",
"suburi"}
+ required_keys = {"protocol", "server", "port",
"suburi", "timeout"}
auth_mode = config.get("mode")
if auth_mode in ("digest", "basic",
"basic_preemtive"):
required_keys |= {"user", "password"}
@@ -213,11 +214,13 @@ class JolokiaInstance(object):
instance = str(config["port"])
config["instance"] = instance.replace(" ", "_")
- # port must be (or look like) an integer
- try:
- config["port"] = int(config["port"])
- except ValueError:
- raise ValueError(err_msg % ("Invalid port %r" %
config["port"]))
+ # port must be (or look like) an integer, timeout like float
+ for key, type_ in (("port", int), ("timeout", float)):
+ val = config[key]
+ try:
+ config[key] = type_(val)
+ except ValueError:
+ raise ValueError(err_msg % ("Invalid %s %r" % (key, val)))
if config.get("server") == "use fqdn":
config["server"] = socket.getfqdn()
@@ -272,8 +275,7 @@ class JolokiaInstance(object):
def _initialize_http_session(self):
session = requests.Session()
session.verify = self._config["verify"]
-
- session.timeout = 1.0
+ session.timeout = self._config["timeout"]
auth_method = self._config.get("mode")
if auth_method is None:
diff --git a/tests/unit/plugins/test_mk_jolokia.py
b/tests/unit/plugins/test_mk_jolokia.py
index b7a69b2..e119e6b 100644
--- a/tests/unit/plugins/test_mk_jolokia.py
+++ b/tests/unit/plugins/test_mk_jolokia.py
@@ -10,11 +10,10 @@ sys.path.insert(0, os.path.join(cmk_path(), 'agents',
'plugins'))
import mk_jolokia # pylint: disable=import-error,wrong-import-position
-
SANITIZE = mk_jolokia.JolokiaInstance._sanitize_config
-(a)pytest.mark.parametrize("removed"oved", ["protocol", "server",
"port", "suburi"])
+(a)pytest.mark.parametrize("removed"oved", ["protocol", "server",
"port", "suburi", "timeout"])
def test_missing_config_basic(removed):
config = copy.deepcopy(mk_jolokia.DEFAULT_CONFIG)
config.pop(removed)
@@ -29,10 +28,19 @@ def test_config_instance():
assert SANITIZE(config).get("instance") ==
"some_spaces_in_string"
-(a)pytest.mark.parametrize("config,base_url"_url", [
- ({"protocol": "sftp", "server":
"billy.theserver", "port": 42,
- "suburi": "jolo-site"},
"sftp://billy.theserver:42/jolo-site/")
-])
+def test_config_timeout():
+ config = copy.deepcopy(mk_jolokia.DEFAULT_CONFIG)
+ config["timeout"] = '23'
+ assert isinstance(SANITIZE(config).get("timeout"), float)
+
+
+(a)pytest.mark.parametrize("config,base_url"_url", [({
+ "protocol": "sftp",
+ "server": "billy.theserver",
+ "port": 42,
+ "suburi": "jolo-site",
+ "timeout": 2.0
+}, "sftp://billy.theserver:42/jolo-site/")])
def test_jolokia_instance_base_url(config, base_url):
joloi = mk_jolokia.JolokiaInstance(config)
assert joloi._get_base_url() == base_url