Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 2 additions & 16 deletions instana/fsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
25 changes: 25 additions & 0 deletions instana/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,31 @@ def strip_secrets(qp, matcher, kwlist):
except:
logger.debug("strip_secrets", exc_info=True)


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
# 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 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))

except:
logger.warn("get_default_gateway: ", exc_info=True)


def get_py_source(file):
"""
Retrieves and returns the source code for any Python
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
],
Expand Down