From d99bc700570c92a1eb525922c3fa745644b17229 Mon Sep 17 00:00:00 2001 From: Peter Giacomo Lombardo Date: Sun, 21 Jan 2018 10:33:39 +0100 Subject: [PATCH 1/3] More reliable way to determine true command line --- instana/fsm.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/instana/fsm.py b/instana/fsm.py index 3f66f006..d1d9c1cf 100644 --- a/instana/fsm.py +++ b/instana/fsm.py @@ -1,4 +1,3 @@ -import sys import os import socket import subprocess @@ -134,8 +133,15 @@ def announce_sensor(self, e): cmdinfo = cmd.read() cmdline = cmdinfo.split('\x00') else: - cmdline = [sys.executable] - cmdline += sys.argv + # OSX doesn't provide a reliable method to determine what + # the OS process command line may be. Here we are forced to + # rely on ps rather than adding a dependency on something like + # psutil which requires dev packages, gcc etc... + proc = subprocess.Popen(["ps", "-p", str(pid), "-o", "command"], + stdout=subprocess.PIPE) + (out, err) = proc.communicate() + parts = out.split(b'\n') + cmdline = [parts[1].decode("utf-8")] d = Discovery(pid=pid, name=cmdline[0], From ac415f4a9cb5cb85a33a39d01546af954ca900a1 Mon Sep 17 00:00:00 2001 From: Peter Giacomo Lombardo Date: Sun, 21 Jan 2018 10:56:18 +0100 Subject: [PATCH 2/3] Fix problem description --- instana/fsm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/instana/fsm.py b/instana/fsm.py index d1d9c1cf..da4aa8bf 100644 --- a/instana/fsm.py +++ b/instana/fsm.py @@ -133,7 +133,7 @@ def announce_sensor(self, e): cmdinfo = cmd.read() cmdline = cmdinfo.split('\x00') else: - # OSX doesn't provide a reliable method to determine what + # Python doesn't provide a reliable method to determine what # the OS process command line may be. Here we are forced to # rely on ps rather than adding a dependency on something like # psutil which requires dev packages, gcc etc... From 442ae6461a4e06d1acc263ec4685705bf8e09e92 Mon Sep 17 00:00:00 2001 From: Peter Giacomo Lombardo Date: Sun, 21 Jan 2018 11:16:10 +0100 Subject: [PATCH 3/3] Add safeties; exception handling --- instana/fsm.py | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/instana/fsm.py b/instana/fsm.py index da4aa8bf..935837fd 100644 --- a/instana/fsm.py +++ b/instana/fsm.py @@ -1,4 +1,5 @@ import os +import sys import socket import subprocess import threading as t @@ -128,20 +129,24 @@ def announce_sensor(self, e): pid = os.getpid() cmdline = [] - if os.path.isfile("/proc/self/cmdline"): - with open("/proc/self/cmdline") as cmd: - cmdinfo = cmd.read() - cmdline = cmdinfo.split('\x00') - else: - # Python doesn't provide a reliable method to determine what - # the OS process command line may be. Here we are forced to - # rely on ps rather than adding a dependency on something like - # psutil which requires dev packages, gcc etc... - proc = subprocess.Popen(["ps", "-p", str(pid), "-o", "command"], - stdout=subprocess.PIPE) - (out, err) = proc.communicate() - parts = out.split(b'\n') - cmdline = [parts[1].decode("utf-8")] + try: + if os.path.isfile("/proc/self/cmdline"): + with open("/proc/self/cmdline") as cmd: + cmdinfo = cmd.read() + cmdline = cmdinfo.split('\x00') + else: + # Python doesn't provide a reliable method to determine what + # the OS process command line may be. Here we are forced to + # rely on ps rather than adding a dependency on something like + # psutil which requires dev packages, gcc etc... + proc = subprocess.Popen(["ps", "-p", str(pid), "-o", "command"], + stdout=subprocess.PIPE) + (out, err) = proc.communicate() + parts = out.split(b'\n') + cmdline = [parts[1].decode("utf-8")] + except Exception as err: + cmdline = sys.argv + log.debug(err) d = Discovery(pid=pid, name=cmdline[0],