11import subprocess
22import os
33import psutil
4+ import socket
45import threading as t
56import fysom as f
67import instana .log as l
@@ -11,6 +12,8 @@ class Discovery(object):
1112 pid = 0
1213 name = None
1314 args = None
15+ fd = - 1
16+ inode = ""
1417
1518 def __init__ (self , ** kwds ):
1619 self .__dict__ .update (kwds )
@@ -20,8 +23,11 @@ def to_dict(self):
2023 kvs ['pid' ] = self .pid
2124 kvs ['name' ] = self .name
2225 kvs ['args' ] = self .args
26+ kvs ['fd' ] = self .fd
27+ kvs ['inode' ] = self .inode
2328 return kvs
2429
30+
2531class Fsm (object ):
2632 RETRY_PERIOD = 30
2733
@@ -63,18 +69,19 @@ def lookup_agent_host(self, e):
6369 if h == a .AGENT_HEADER :
6470 self .agent .set_host (host )
6571 self .fsm .announce ()
66- else :
72+ return True
73+ elif os .path .exists ("/proc/" ):
6774 host = self .get_default_gateway ()
6875 if host :
6976 h = self .check_host (host )
7077 if h == a .AGENT_HEADER :
7178 self .agent .set_host (host )
7279 self .fsm .announce ()
80+ return True
7381 else :
7482 l .error ("Cannot lookup agent host. Scheduling retry." )
7583 self .schedule_retry (self .lookup_agent_host , e , "agent_lookup" )
76- return False
77- return True
84+ return False
7885
7986 def get_default_gateway (self ):
8087 l .debug ("checking default gateway" )
@@ -83,14 +90,15 @@ def get_default_gateway(self):
8390 proc = subprocess .Popen (
8491 "/sbin/ip route | awk '/default/' | cut -d ' ' -f 3 | tr -d '\n '" , shell = True , stdout = subprocess .PIPE )
8592
86- return proc .stdout .read ()
93+ addr = proc .stdout .read ()
94+ return addr .decode ("UTF-8" )
8795 except Exception as e :
8896 l .error (e )
8997
9098 return None
9199
92100 def check_host (self , host ):
93- l .debug ("checking host" , str ( host ) )
101+ l .debug ("checking host" , host )
94102
95103 (_ , h ) = self .agent .request_header (
96104 self .agent .make_host_url (host , "/" ), "GET" , "Server" )
@@ -100,29 +108,39 @@ def check_host(self, host):
100108 def announce_sensor (self , e ):
101109 l .debug ("announcing sensor to the agent" )
102110 p = psutil .Process (os .getpid ())
111+ s = None
112+
103113 d = Discovery (pid = p .pid ,
104114 name = p .cmdline ()[0 ],
105115 args = p .cmdline ()[1 :])
106116
117+ # If we're on a system with a procfs
118+ if os .path .exists ("/proc/" ):
119+ s = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
120+ s .connect ((self .agent .host , 42699 ))
121+ path = "/proc/%d/fd/%d" % (p .pid , s .fileno ())
122+ d .fd = s .fileno ()
123+ d .inode = os .readlink (path )
124+
107125 (b , _ ) = self .agent .request_response (
108126 self .agent .make_url (a .AGENT_DISCOVERY_URL ), "PUT" , d )
109- if not b :
110- l .error ("Cannot announce sensor. Scheduling retry." )
111- self .schedule_retry (self .announce_sensor , e , "announce" )
112- return False
113- else :
127+ if b :
114128 self .agent .set_from (b )
115129 self .fsm .ready ()
116- l .warn ("Host agent available. We're in business. ( Announced pid: %i)" % p .pid )
130+ l .warn ("Host agent available. We're in business. Announced pid: %i (true pid: %i )" % ( p .pid , self . agent . from_ . pid ) )
117131 return True
132+ else :
133+ l .error ("Cannot announce sensor. Scheduling retry." )
134+ self .schedule_retry (self .announce_sensor , e , "announce" )
135+ return False
118136
119137 def schedule_retry (self , fun , e , name ):
120138 l .debug ("Scheduling: " + name )
121139 self .timer = t .Timer (self .RETRY_PERIOD , fun , [e ])
122140 self .timer .daemon = True
123141 self .timer .name = name
124142 self .timer .start ()
125- l .debug ('Threadlist: %s ' , str (t .enumerate ()))
143+ l .debug ('Threadlist: ' , str (t .enumerate ()))
126144
127145 def test_agent (self , e ):
128146 l .debug ("testing communication with the agent" )
0 commit comments