Skip to content

Commit 1ac9b51

Browse files
authored
Use /proc/net/route to determine default gateway (#117)
* Use /proc/net/route to determine default gateway * Code docs and validate hex ip len * Limit latest spyne package: #118
1 parent 2c8b0a6 commit 1ac9b51

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

instana/fsm.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from .agent_const import AGENT_DEFAULT_HOST, AGENT_DEFAULT_PORT
1414
from .log import logger
15+
from .util import get_default_gateway
1516

1617

1718
class Discovery(object):
@@ -86,7 +87,7 @@ def lookup_agent_host(self, e):
8687
self.fsm.announce()
8788
return True
8889
elif os.path.exists("/proc/"):
89-
host = self.get_default_gateway()
90+
host = get_default_gateway()
9091
if host:
9192
if self.agent.is_agent_listening(host, port):
9293
self.agent.host = host
@@ -101,21 +102,6 @@ def lookup_agent_host(self, e):
101102
self.schedule_retry(self.lookup_agent_host, e, "agent_lookup")
102103
return False
103104

104-
def get_default_gateway(self):
105-
logger.debug("checking default gateway")
106-
107-
try:
108-
proc = subprocess.Popen(
109-
"/sbin/ip route | awk '/default/' | cut -d ' ' -f 3 | tr -d '\n'",
110-
shell=True, stdout=subprocess.PIPE)
111-
112-
addr = proc.stdout.read()
113-
return addr.decode("UTF-8")
114-
except Exception as e:
115-
logger.error(e)
116-
117-
return None
118-
119105
def announce_sensor(self, e):
120106
logger.debug("announcing sensor to the agent")
121107
sock = None

instana/util.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,31 @@ def strip_secrets(qp, matcher, kwlist):
170170
except:
171171
logger.debug("strip_secrets", exc_info=True)
172172

173+
174+
def get_default_gateway():
175+
"""
176+
Attempts to read /proc/self/net/route to determine the default gateway in use.
177+
178+
:return: String - the ip address of the default gateway or None if not found/possible/non-existant
179+
"""
180+
try:
181+
# The first line is the header line
182+
# We look for the line where the Destination is 00000000 - that is the default route
183+
# The Gateway IP is encoded backwards in hex.
184+
with open("/proc/self/net/route") as routes:
185+
for line in routes:
186+
parts = line.split('\t')
187+
if '00000000' == parts[1]:
188+
hip = parts[2]
189+
190+
if hip is not None and len(hip) is 8:
191+
# Reverse order, convert hex to int
192+
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))
193+
194+
except:
195+
logger.warn("get_default_gateway: ", exc_info=True)
196+
197+
173198
def get_py_source(file):
174199
"""
175200
Retrieves and returns the source code for any Python

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def check_setuptools():
6666
'redis<3.0.0',
6767
'requests>=2.17.1',
6868
'sqlalchemy>=1.1.15',
69-
'spyne>=2.9',
69+
'spyne>=2.9,<=2.12.14',
7070
'suds-jurko>=0.6',
7171
'urllib3[secure]>=1.15'
7272
],

0 commit comments

Comments
 (0)