From ac6573df36f0b3b33db2974f8ec93db78c97e614 Mon Sep 17 00:00:00 2001 From: Peter Giacomo Lombardo Date: Fri, 21 Dec 2018 19:41:35 -0500 Subject: [PATCH 1/3] Use /proc/net/route to determine default gateway --- instana/fsm.py | 18 ++---------------- instana/util.py | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/instana/fsm.py b/instana/fsm.py index e619f706..1bc2af15 100644 --- a/instana/fsm.py +++ b/instana/fsm.py @@ -12,6 +12,7 @@ from .agent_const import AGENT_DEFAULT_HOST, AGENT_DEFAULT_PORT from .log import logger +from .util import get_default_gateway class Discovery(object): @@ -86,7 +87,7 @@ def lookup_agent_host(self, e): self.fsm.announce() return True elif os.path.exists("/proc/"): - host = self.get_default_gateway() + host = get_default_gateway() if host: if self.agent.is_agent_listening(host, port): self.agent.host = host @@ -101,21 +102,6 @@ def lookup_agent_host(self, e): self.schedule_retry(self.lookup_agent_host, e, "agent_lookup") return False - def get_default_gateway(self): - logger.debug("checking default gateway") - - try: - proc = subprocess.Popen( - "/sbin/ip route | awk '/default/' | cut -d ' ' -f 3 | tr -d '\n'", - shell=True, stdout=subprocess.PIPE) - - addr = proc.stdout.read() - return addr.decode("UTF-8") - except Exception as e: - logger.error(e) - - return None - def announce_sensor(self, e): logger.debug("announcing sensor to the agent") sock = None diff --git a/instana/util.py b/instana/util.py index f828bae2..cef0d7f0 100644 --- a/instana/util.py +++ b/instana/util.py @@ -170,6 +170,26 @@ def strip_secrets(qp, matcher, kwlist): except: logger.debug("strip_secrets", exc_info=True) + +def get_default_gateway(): + try: + # The first line is the header line + # We look for the line where the Destination is 00000000 - that is the default route + # The Gateway IP is encoded backwards in hex. + with open("/proc/self/net/route") as routes: + for line in routes: + parts = line.split('\t') + if '00000000' == parts[1]: + hip = parts[2] + + if hip is not None: + # Reverse order, convert hex to int + return "%i.%i.%i.%i" % (int(hip[6:8], 16), int(hip[4:6], 16), int(hip[2:4], 16), int(hip[0:2], 16)) + + except: + logger.warn("get_default_gateway: ", exc_info=True) + + def get_py_source(file): """ Retrieves and returns the source code for any Python From b5cb7a40a46c7bd950f083ab9ec5f7752616933b Mon Sep 17 00:00:00 2001 From: Peter Giacomo Lombardo Date: Fri, 21 Dec 2018 19:49:08 -0500 Subject: [PATCH 2/3] Code docs and validate hex ip len --- instana/util.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/instana/util.py b/instana/util.py index cef0d7f0..ab532737 100644 --- a/instana/util.py +++ b/instana/util.py @@ -172,6 +172,11 @@ def strip_secrets(qp, matcher, kwlist): def get_default_gateway(): + """ + Attempts to read /proc/self/net/route to determine the default gateway in use. + + :return: String - the ip address of the default gateway or None if not found/possible/non-existant + """ try: # The first line is the header line # We look for the line where the Destination is 00000000 - that is the default route @@ -182,7 +187,7 @@ def get_default_gateway(): if '00000000' == parts[1]: hip = parts[2] - if hip is not None: + if hip is not None and len(hip) is 8: # Reverse order, convert hex to int return "%i.%i.%i.%i" % (int(hip[6:8], 16), int(hip[4:6], 16), int(hip[2:4], 16), int(hip[0:2], 16)) From a6bb054c806e47d52e9d8f97b8cd6abee44d422e Mon Sep 17 00:00:00 2001 From: Peter Giacomo Lombardo Date: Fri, 21 Dec 2018 20:04:47 -0500 Subject: [PATCH 3/3] Limit latest spyne package: https://github.com/instana/python-sensor/issues/118 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index c81a14d3..baa7aa48 100644 --- a/setup.py +++ b/setup.py @@ -66,7 +66,7 @@ def check_setuptools(): 'redis<3.0.0', 'requests>=2.17.1', 'sqlalchemy>=1.1.15', - 'spyne>=2.9', + 'spyne>=2.9,<=2.12.14', 'suds-jurko>=0.6', 'urllib3[secure]>=1.15' ],