-
Notifications
You must be signed in to change notification settings - Fork 283
/
client.py
438 lines (397 loc) · 16.4 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import queue
import hashlib
import logging
import os
import subprocess
import tempfile
import threading
import time
from shutil import copyfile, rmtree
from ...common.util import SecuredShell
from ..Telegraf.config import AgentConfig
from ..Telegraf.reader import MonitoringReader
logger = logging.getLogger(__name__)
logging.getLogger("paramiko.transport").setLevel(logging.WARNING)
def generate_file_md5(filename, blocksize=2 ** 20):
m = hashlib.md5()
with open(filename, "rb") as f:
while True:
buf = f.read(blocksize)
if not buf:
break
m.update(buf)
return m.hexdigest()
class LocalhostClient(object):
""" localhost client setup """
AGENT_FILENAME = 'agent.py'
def __init__(self, config, old_style_configs, kill_old):
# config
self.kill_old = '--kill-old' if kill_old else ''
self.python = config['python']
self.host = "localhost"
self.telegraf = config['telegraf']
self.config = AgentConfig(config, old_style_configs)
# connection
self.incoming_queue = queue.Queue()
self.buffer = ""
self.workdir = None
self.reader = MonitoringReader(self.incoming_queue)
self.path = {
'AGENT_LOCAL_FOLDER': os.path.join(
os.path.dirname(__file__),
'agent'),
'TELEGRAF_LOCAL_PATH': self.telegraf,
}
def install(self):
self.workdir = tempfile.mkdtemp()
logger.info("Created temp dir %s", self.workdir)
agent_config = self.config.create_collector_config(self.workdir)
startup_config = self.config.create_startup_config()
customs_script = self.config.create_custom_exec_script()
try:
copyfile(
os.path.join(
self.path['AGENT_LOCAL_FOLDER'],
self.AGENT_FILENAME),
os.path.join(
self.workdir,
self.AGENT_FILENAME))
copyfile(agent_config, os.path.join(self.workdir, 'agent.cfg'))
copyfile(startup_config, os.path.join(
self.workdir,
'agent_startup.cfg'))
copyfile(
customs_script,
os.path.join(
self.workdir,
'agent_customs.sh'))
if not os.path.isfile(self.path['TELEGRAF_LOCAL_PATH']):
logger.error(
'Telegraf binary not found at specified path: %s\n'
'You can download telegraf binaries here: https://github.com/influxdata/telegraf\n'
'or install debian package: `telegraf`',
self.path['TELEGRAF_LOCAL_PATH'])
return None, None, None
except Exception:
logger.error("Failed to copy agent to %s on localhost", self.workdir)
logger.debug("Failed to copy agent to %s on localhost", self.workdir, exc_info=True)
return None, None, None
return agent_config, startup_config, customs_script
@staticmethod
def popen(args):
return subprocess.Popen(
args,
bufsize=0,
preexec_fn=os.setsid,
close_fds=True,
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE, )
def start(self):
"""Start local agent"""
logger.info('Starting agent on localhost')
args = self.python.split() + [
os.path.join(
self.workdir,
self.AGENT_FILENAME),
'--telegraf',
self.path['TELEGRAF_LOCAL_PATH'],
'--host',
self.host]
if self.kill_old:
args.append(self.kill_old)
self.session = self.popen(args)
self.reader_thread = threading.Thread(target=self.read_buffer)
self.reader_thread.setDaemon(True)
return self.session
def read_buffer(self):
while self.session:
try:
chunk = self.session.stdout.read(4096)
if chunk:
parts = chunk.rsplit('\n', 1)
if len(parts) > 1:
ready_chunk = self.buffer + parts[0] + '\n'
self.buffer = parts[1]
self.incoming_queue.put(ready_chunk)
else:
self.buffer += parts[0]
except ValueError:
logger.debug(
'this exc most likely raised during interpreter shutdown\n'
'otherwise something really nasty happend',
exc_info=True)
def _stop_agent(self):
if self.session:
self.session.terminate()
logger.info('Waiting localhost agent to terminate...')
def uninstall(self):
"""
Remove agent's files from remote host
"""
if self.session:
self.session.wait()
self.session = None
log_filename = "agent_{host}.log".format(host="localhost")
data_filename = "agent_{host}.rawdata".format(host="localhost")
try:
logger.info('Saving monitoring artefacts from localhost')
copyfile(self.workdir + "/_agent.log", log_filename)
copyfile(self.workdir + "/monitoring.rawdata", data_filename)
logger.info('Deleting temp directory: %s', self.workdir)
rmtree(self.workdir)
except Exception:
logger.error("Exception while uninstalling agent", exc_info=True)
logger.info("Removing agent from: localhost")
return log_filename, data_filename
class SSHClient(object):
"""remote agent client setup """
AGENT_FILENAME = 'agent.py'
def __init__(self, config, old_style_configs, timeout, kill_old):
# config
self.kill_old = '--kill-old' if kill_old else ''
self.host = config['host']
self.username = config['username']
self.python = config['python']
self.port = config['port']
self.telegraf = config['telegraf']
self.config = AgentConfig(config, old_style_configs)
# connection
self.session = None
self.ssh = SecuredShell(self.host, self.port, self.username, timeout)
self.incoming_queue = queue.Queue()
self.buffer = ""
self.stop_sent = None
self.successfull_stop = None
self.reader = MonitoringReader(self.incoming_queue)
handle, cfg_path = tempfile.mkstemp('.cfg', 'agent_')
os.close(handle)
self.path = {
# Destination path on remote host
'AGENT_REMOTE_FOLDER': '/tmp/',
# Source path on tank
'AGENT_LOCAL_FOLDER': os.path.dirname(__file__) + '/agent',
'TELEGRAF_REMOTE_PATH': '/tmp/telegraf',
'TELEGRAF_LOCAL_PATH': self.telegraf,
}
self.agent_remote_folder = None
def install(self):
"""Create folder and copy agent and metrics scripts to remote host"""
logger.info(
"Installing monitoring agent at %s@%s...",
self.username,
self.host)
# create remote temp dir
cmd = self.python + ' -c "import tempfile; print tempfile.mkdtemp();"'
logger.info("Creating temp dir on %s", self.host)
try:
out, errors, err_code = self.ssh.execute(cmd)
except Exception:
logger.error(
"Failed to install monitoring agent to %s",
self.host,
exc_info=True)
return None, None, None
if errors:
logger.error("[%s] error: '%s'", self.host, errors)
logger.error("Cancelling agent installation on %s", self.host)
return None, None, None
if err_code:
logger.error(
"Failed to create remote dir via SSH at %s@%s, code %s: %s" %
(self.username, self.host, err_code, out.strip()))
return None, None, None
remote_dir = out.strip()
if remote_dir:
self.path['AGENT_REMOTE_FOLDER'] = remote_dir
self.agent_remote_folder = remote_dir
logger.debug(
"Remote dir at %s:%s", self.host, self.path['AGENT_REMOTE_FOLDER'])
# create collector config
agent_config = self.config.create_collector_config(
self.path['AGENT_REMOTE_FOLDER'])
startup_config = self.config.create_startup_config()
customs_script = self.config.create_custom_exec_script()
# trying to detect os version/architecture and get information about telegraf client
# DO NOT DELETE indices in string format below. Python 2.6 does not
# support string formatting without indices
remote_cmd = 'import os; print os.path.isfile("' + self.path[
'TELEGRAF_REMOTE_PATH'] + '")'
cmd = self.python + ' -c \'{cmd}\''.format(cmd=remote_cmd)
remote_telegraf_exists = "False"
try:
out, err, err_code = self.ssh.execute(cmd)
except Exception:
logger.error(
"SSH execute error trying to check telegraf availability on host %s",
self.host,
exc_info=True)
else:
if err:
logger.error("[%s] error: '%s'", self.host, errors)
if out.strip():
remote_telegraf_exists = out.strip()
try:
if remote_telegraf_exists in "True":
logger.debug('Found telegraf client on %s..', self.host)
else:
logger.debug(
'Not found telegraf client on %s, trying to install from tank. Copying..',
self.host)
if os.path.isfile(self.path['TELEGRAF_LOCAL_PATH']):
self.ssh.send_file(
self.path['TELEGRAF_LOCAL_PATH'],
self.path['TELEGRAF_REMOTE_PATH'])
elif os.path.isfile("/usr/bin/telegraf"):
self.ssh.send_file(
'/usr/bin/telegraf', self.path['TELEGRAF_REMOTE_PATH'])
else:
logger.error(
'Telegraf binary not found neither on %s nor on localhost at specified path: %s\n'
'You can download telegraf binaries here: https://github.com/influxdata/telegraf\n'
'or install debian package: `telegraf`', self.host, self.path['TELEGRAF_LOCAL_PATH'])
return None, None, None
self.ssh.send_file(
os.path.join(
self.path['AGENT_LOCAL_FOLDER'],
self.AGENT_FILENAME),
os.path.join(
self.path['AGENT_REMOTE_FOLDER'],
self.AGENT_FILENAME))
self.ssh.send_file(
agent_config,
os.path.join(
self.path['AGENT_REMOTE_FOLDER'],
'agent.cfg'))
self.ssh.send_file(
startup_config,
os.path.join(
self.path['AGENT_REMOTE_FOLDER'],
'agent_startup.cfg'))
self.ssh.send_file(
customs_script,
os.path.join(
self.path['AGENT_REMOTE_FOLDER'],
'agent_customs.sh'))
except Exception:
logger.error(
"Failed to install agent on %s", self.host, exc_info=True)
return None, None, None
return agent_config, startup_config, customs_script
def start(self):
"""Start remote agent"""
logger.info('Starting agent: %s', self.host)
command = "{python} {agent_path} --telegraf {telegraf_path} --host {host} {kill_old}".format(
python=self.python,
agent_path=os.path.join(
self.path['AGENT_REMOTE_FOLDER'],
self.AGENT_FILENAME),
telegraf_path=self.path['TELEGRAF_REMOTE_PATH'],
host=self.host,
kill_old=self.kill_old)
logger.debug('Command to start agent: %s', command)
self.session = self.ssh.async_session(command)
self.reader_thread = threading.Thread(target=self.read_buffer)
self.reader_thread.setDaemon(True)
return self.session
def read_buffer(self):
while self.session and not self.stop_sent:
chunk = self.session.read_maybe()
if chunk:
parts = chunk.rsplit('\n', 1)
if len(parts) > 1:
ready_chunk = self.buffer + parts[0] + '\n'
self.buffer = parts[1]
self.incoming_queue.put(ready_chunk)
else:
self.buffer += parts[0]
else:
time.sleep(1)
logger.info('Daemon reader stopped')
def _stop_agent(self):
"""
Stop data collection. Separated from uninstall to speed up multihost processing
"""
try:
if self.session:
self.session.send("stop\n")
self.stop_sent = time.time()
except BaseException:
logger.warning(
'Unable to correctly stop monitoring agent - session is broken on %s.',
self.host,
exc_info=True)
def uninstall(self):
"""
Remove agent's files from remote host
"""
log_filename = "agent_{host}.log".format(host=self.host)
data_filename = "agent_{host}.rawdata".format(host=self.host)
try:
if self.session:
self._wait_for_stop()
self.session.close()
self.session = None
except BaseException:
logger.warning(
'Unable to correctly stop monitoring agent - session is broken. Pay attention to agent log (%s).',
log_filename,
exc_info=True)
else:
try:
self.ssh.get_file(
os.path.join(
self.path['AGENT_REMOTE_FOLDER'],
"_agent.log"),
log_filename)
self.ssh.get_file(
os.path.join(
self.path['AGENT_REMOTE_FOLDER'],
"monitoring.rawdata"),
data_filename)
self.ssh.rm_r(self.path['AGENT_REMOTE_FOLDER'])
except Exception:
logger.error("Unable to get agent artefacts", exc_info=True)
if not self.successfull_stop:
self._kill_agent()
return log_filename, data_filename
def _wait_for_stop(self):
done = False
agent_stop_timeout = 5
while not done:
if not self.stop_sent:
logger.info('Session was broken on %s, switch to kill', self.host)
break
if (self.stop_sent + agent_stop_timeout) < time.time():
logger.info("Agent hasn't finished in %s sec, force quit on %s",
agent_stop_timeout,
self.host)
break
if self.session.finished():
logger.debug('Session ended with status %s with %s',
self.session.exit_status(),
self.host)
self.successfull_stop = True
done = True
agent_stderr = self.session.read_err_maybe()
if agent_stderr and 'stopped' in agent_stderr:
logger.debug('Got stopped message from %s', self.host)
done = True
def _kill_agent(self):
if self.agent_remote_folder:
tpl = ('main_p=$(pgrep -f "[p]ython.*{folder}");'
'tlgrf_p=$(pgrep -f "[t]elegraf.*{folder}");'
'descendent_pids(){{ if [ "x$1" != "x" ]; then '
'pids=$(pgrep -P $1); echo $pids;'
'for p in $pids; do descendent_pids $p; done; fi }};'
'all_p=$(descendent_pids ${{main_p}});'
'if [ "x${{main_p}}${{tlgrf_p}}${{all_p}}" != "x" ] ; then '
' kill -9 ${{main_p}} ${{tlgrf_p}} ${{all_p}}; fi')
cmd = tpl.format(folder=self.agent_remote_folder)
out, errors, err_code = self.ssh.execute(cmd)
if errors:
logger.error(
"[%s] error while killing agent: '%s'",
self.host,
errors)