|
1 | 1 | from __future__ import absolute_import |
2 | 2 |
|
3 | 3 | import os |
| 4 | +import re |
4 | 5 | import socket |
5 | 6 | import subprocess |
6 | 7 | import sys |
@@ -74,25 +75,7 @@ def start_metric_reporting(self, e): |
74 | 75 | self.agent.sensor.meter.run() |
75 | 76 |
|
76 | 77 | def lookup_agent_host(self, e): |
77 | | - host = a.AGENT_DEFAULT_HOST |
78 | | - port = a.AGENT_DEFAULT_PORT |
79 | | - |
80 | | - if "INSTANA_AGENT_HOST" in os.environ: |
81 | | - host = os.environ["INSTANA_AGENT_HOST"] |
82 | | - if "INSTANA_AGENT_PORT" in os.environ: |
83 | | - port = int(os.environ["INSTANA_AGENT_PORT"]) |
84 | | - |
85 | | - elif "INSTANA_AGENT_IP" in os.environ: |
86 | | - # Deprecated: INSTANA_AGENT_IP environment variable |
87 | | - # To be removed in a future version |
88 | | - host = os.environ["INSTANA_AGENT_IP"] |
89 | | - if "INSTANA_AGENT_PORT" in os.environ: |
90 | | - port = int(os.environ["INSTANA_AGENT_PORT"]) |
91 | | - |
92 | | - elif self.agent.sensor.options.agent_host != "": |
93 | | - host = self.agent.sensor.options.agent_host |
94 | | - if self.agent.sensor.options.agent_port != 0: |
95 | | - port = self.agent.sensor.options.agent_port |
| 78 | + host, port = self.__get_agent_host_port() |
96 | 79 |
|
97 | 80 | h = self.check_host(host, port) |
98 | 81 | if h == a.AGENT_HEADER: |
@@ -165,7 +148,7 @@ def announce_sensor(self, e): |
165 | 148 | cmdline = sys.argv |
166 | 149 | log.debug(err) |
167 | 150 |
|
168 | | - d = Discovery(pid=pid, |
| 151 | + d = Discovery(pid=self.__get_real_pid(), |
169 | 152 | name=cmdline[0], |
170 | 153 | args=cmdline[1:]) |
171 | 154 |
|
@@ -206,3 +189,57 @@ def test_agent(self, e): |
206 | 189 | self.schedule_retry(self.test_agent, e, "agent test") |
207 | 190 | else: |
208 | 191 | self.fsm.test() |
| 192 | + |
| 193 | + def __get_real_pid(self): |
| 194 | + """ |
| 195 | + Attempts to determine the true process ID by querying the |
| 196 | + /proc/<pid>/sched file. This works on systems with a proc filesystem. |
| 197 | + Otherwise default to os default. |
| 198 | + """ |
| 199 | + pid = None |
| 200 | + |
| 201 | + if os.path.exists("/proc/"): |
| 202 | + sched_file = "/proc/%d/sched" % os.getpid() |
| 203 | + |
| 204 | + if os.path.isfile(sched_file): |
| 205 | + try: |
| 206 | + file = open(sched_file) |
| 207 | + line = file.readline() |
| 208 | + g = re.search(r'\((\d+),', line) |
| 209 | + if len(g.groups()) == 1: |
| 210 | + pid = int(g.groups()[0]) |
| 211 | + except Exception: |
| 212 | + log.debug("parsing sched file failed", exc_info=True) |
| 213 | + pass |
| 214 | + |
| 215 | + if pid is None: |
| 216 | + pid = os.getpid() |
| 217 | + |
| 218 | + return pid |
| 219 | + |
| 220 | + def __get_agent_host_port(self): |
| 221 | + """ |
| 222 | + Iterates the the various ways the host and port of the Instana host |
| 223 | + agent may be configured: default, env vars, sensor options... |
| 224 | + """ |
| 225 | + host = a.AGENT_DEFAULT_HOST |
| 226 | + port = a.AGENT_DEFAULT_PORT |
| 227 | + |
| 228 | + if "INSTANA_AGENT_HOST" in os.environ: |
| 229 | + host = os.environ["INSTANA_AGENT_HOST"] |
| 230 | + if "INSTANA_AGENT_PORT" in os.environ: |
| 231 | + port = int(os.environ["INSTANA_AGENT_PORT"]) |
| 232 | + |
| 233 | + elif "INSTANA_AGENT_IP" in os.environ: |
| 234 | + # Deprecated: INSTANA_AGENT_IP environment variable |
| 235 | + # To be removed in a future version |
| 236 | + host = os.environ["INSTANA_AGENT_IP"] |
| 237 | + if "INSTANA_AGENT_PORT" in os.environ: |
| 238 | + port = int(os.environ["INSTANA_AGENT_PORT"]) |
| 239 | + |
| 240 | + elif self.agent.sensor.options.agent_host != "": |
| 241 | + host = self.agent.sensor.options.agent_host |
| 242 | + if self.agent.sensor.options.agent_port != 0: |
| 243 | + port = self.agent.sensor.options.agent_port |
| 244 | + |
| 245 | + return host, port |
0 commit comments