[checkmk-commits] mk_jolokia: refactoring I

Moritz Kiemer mo at mathias-kettner.de
Thu Nov 15 13:31:37 CET 2018


Module: check_mk
Branch: master
Commit: 9b7e9020fa87af694b239794a46111f8731e3a7b
URL:    http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=9b7e9020fa87af694b239794a46111f8731e3a7b

Author: Moritz Kiemer <mo at mathias-kettner.de>
Date:   Thu Nov 15 09:53:24 2018 +0100

mk_jolokia: refactoring I

Change-Id: I5bac0d390b3e9ad0ba9026e2dd46cc25d4f2a999

---

 agents/plugins/mk_jolokia | 38 ++++++++++++--------------------------
 1 file changed, 12 insertions(+), 26 deletions(-)

diff --git a/agents/plugins/mk_jolokia b/agents/plugins/mk_jolokia
index b45ac17..c20b6c3 100755
--- a/agents/plugins/mk_jolokia
+++ b/agents/plugins/mk_jolokia
@@ -153,25 +153,7 @@ def cached(function):
     return cached_function
 
 
-def fetch_url_get(base_url, path, function):
-    if path:
-        url = "%s/%s/%s" % (base_url, function, path)
-    else:
-        url = base_url + "/"
-
-    if VERBOSE:
-        sys.stderr.write("DEBUG: Fetching: %s\n" % url)
-    try:
-        json_data = urllib2.urlopen(url).read()
-        if VERBOSE:
-            sys.stderr.write("DEBUG: Result: %s\n\n" % json_data)
-    except () if DEBUG else Exception, exc:
-        sys.stderr.write("ERROR: %s\n" % exc)
-        return []
-    return json_data
-
-
-def fetch_url_post(base_url, path, service_url, service_user, service_password, function):
+def _get_post_data(path, service_url, service_user, service_password, function):
     segments = path.split("/")
 
     data = {
@@ -184,19 +166,22 @@ def fetch_url_post(base_url, path, service_url, service_user, service_password,
     }
     if len(segments) > 2:
         data["path"] = segments[2]
-
     if service_user:
         data["target"]["user"] = service_user
         data["target"]["password"] = service_password
 
+    return json.dumps(data)
+
+
+def fetch_url(request_url, post_data=None):
     if VERBOSE:
-        sys.stderr.write("DEBUG: Fetching: %s\n" % base_url)
+        sys.stderr.write("DEBUG: Fetching: %s\n" % request_url)
     try:
-        json_data = urllib2.urlopen(base_url, data=json.dumps(data)).read()
+        json_data = urllib2.urlopen(request_url, data=post_data).read()
         if VERBOSE:
             sys.stderr.write("DEBUG: Result: %s\n\n" % json_data)
     except () if DEBUG else Exception, exc:
-        sys.stderr.write("ERROR: %s\n" % exc)
+        sys.stderr.write("ERROR: %s\n\n" % exc)
         return []
     return json_data
 
@@ -214,10 +199,11 @@ def fetch_var(protocol,
     base_url = "%s://%s:%d/%s" % (protocol, server, port, suburi)
 
     if service_url is not None:
-        json_data = fetch_url_post(base_url, path, service_url, service_user, service_password,
-                                   function)
+        post_data = _get_post_data(path, service_url, service_user, service_password, function)
+        json_data = fetch_url(base_url, post_data)
     else:
-        json_data = fetch_url_get(base_url, path, function)
+        request_url = "%s/%s/%s" % (base_url, function, path) if path else base_url + "/"
+        json_data = fetch_url(request_url)
 
     try:
         obj = json.loads(json_data)



More information about the checkmk-commits mailing list