From ea043a773900a92de3958833dd5ea6e3eacdab5a Mon Sep 17 00:00:00 2001 From: Qi Zhao Date: Wed, 31 Mar 2021 17:18:28 +0800 Subject: [PATCH] feat: support collect Hadoop that turning on https --- collectors/0/hadoop_datanode.py | 2 +- collectors/0/hadoop_namenode.py | 2 +- collectors/lib/hadoop_http.py | 23 ++++++++++++++++++++--- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/collectors/0/hadoop_datanode.py b/collectors/0/hadoop_datanode.py index 45ac73187..f976b4f6f 100755 --- a/collectors/0/hadoop_datanode.py +++ b/collectors/0/hadoop_datanode.py @@ -41,7 +41,7 @@ class HadoopDataNode(HadoopHttp): """ def __init__(self): - super(HadoopDataNode, self).__init__('hadoop', 'datanode', 'localhost', 50075) + super(HadoopDataNode, self).__init__('hadoop', 'datanode', 'localhost', 50075, https_port=50475) def emit(self): current_time = int(time.time()) diff --git a/collectors/0/hadoop_namenode.py b/collectors/0/hadoop_namenode.py index 67ec9fac3..7eeb9335a 100755 --- a/collectors/0/hadoop_namenode.py +++ b/collectors/0/hadoop_namenode.py @@ -39,7 +39,7 @@ class HadoopNameNode(HadoopHttp): """ def __init__(self): - super(HadoopNameNode, self).__init__('hadoop', 'namenode', 'localhost', 50070) + super(HadoopNameNode, self).__init__('hadoop', 'namenode', 'localhost', 50070, https_port=50470) def emit(self): current_time = int(time.time()) diff --git a/collectors/lib/hadoop_http.py b/collectors/lib/hadoop_http.py index e99aee8ad..481538033 100644 --- a/collectors/lib/hadoop_http.py +++ b/collectors/lib/hadoop_http.py @@ -12,6 +12,7 @@ # of the GNU Lesser General Public License along with this program. If not, # see . +import ssl import sys try: @@ -26,10 +27,10 @@ try: # noinspection PyCompatibility - from http.client import HTTPConnection + from http.client import HTTPConnection, HTTPSConnection except ImportError: # noinspection PyUnresolvedReferences,PyCompatibility - from httplib import HTTPConnection + from httplib import HTTPConnection, HTTPSConnection EXCLUDED_KEYS = ( @@ -52,14 +53,22 @@ def recurse(key,value): yield (key,value) class HadoopHttp(object): - def __init__(self, service, daemon, host, port, uri="/jmx"): + def __init__(self, service, daemon, host, port, https_port=None, uri="/jmx"): self.service = service self.daemon = daemon self.port = port + self.https_port = https_port self.host = host self.uri = uri self.server = HTTPConnection(self.host, self.port) self.server.auto_open = True + if self.https_port: + self.https_server = HTTPSConnection( + self.host, + self.https_port, + context=ssl._create_unverified_context(), + timeout=10) + self.https_server.auto_open = True def request(self): try: @@ -67,6 +76,14 @@ def request(self): resp = self.server.getresponse().read() except: resp = '{}' + if self.https_port: + try: + self.https_server.request('GET', self.uri) + resp = self.https_server.getresponse().read() + except: + resp = '{}' + finally: + self.https_server.close() finally: self.server.close() return json.loads(resp)