forked from maplelabs/agent-installer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install-agent.py
822 lines (737 loc) · 34.1 KB
/
install-agent.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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
#!/usr/bin/env python
import argparse
import os
import platform
import shutil
import signal
import socket
import subprocess
import sys
import tarfile
import zipfile
from time import sleep
COLLCTD_SOURCE_URL = "https://github.com/maplelabs/collectd/releases/download/" \
"collectd-custom-5.6.1/collectd-custom-5.6.1.tar.bz2"
# collectd_source_url = "https://github.com/upendrasahu/collectd/releases/download/" \
# "collectd-custom-5.6.1/collectd-custom-5.6.1.tar.bz2"
COLLCTD_SOURCE_FILE = "collectd-custom-5.6.1"
# configurator_source_url = "http://10.81.1.134:8000/configurator.tar.gz"
CONFIGURATOR_SOURCE_REPO = "https://github.com/maplelabs/configurator-exporter"
CONFIGURATOR_DIR = "/opt/configurator-exporter"
# collectd_plugins_source_url = "http://10.81.1.134:8000/plugins.tar.gz"
COLLECTD_PLUGINS_REPO = "https://github.com/maplelabs/collectd-plugins"
COLLECTD_PLUGINS_ZIP = "https://github.com/maplelabs/collectd-plugins/archive/master.zip"
CONFIGURATOR_ZIP = "https://github.com/maplelabs/configurator-exporter/archive/master.zip"
COLLECTD_PLUGINS_DIR = "/opt/collectd/plugins"
COLLECTD_X86_64 = "https://github.com/maplelabs/collectd/releases/download/collectd-custom-5.6.1/collectd_x86_64.tar.bz2"
DEFAULT_RETRIES = 3
DEFAULT_CONFIGURATOR_PORT = 8585
# check output function for python 2.6
if "check_output" not in dir(subprocess):
def f(*popenargs, **kwargs):
if 'stdout' in kwargs:
raise ValueError('stdout argument not allowed, it will be overridden.')
process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
output, unused_err = process.communicate()
retcode = process.poll()
if retcode:
cmd = kwargs.get("args")
if cmd is None:
cmd = popenargs[0]
raise subprocess.CalledProcessError(retcode, cmd)
return output
subprocess.check_output = f
def set_env(**kwargs):
for key, value in kwargs.iteritems():
os.environ[key] = value
def kill_process(pid):
if pid:
print "Kill process ID {0}".format(pid)
try:
os.kill(int(pid), signal.SIGKILL)
except:
print "Failed to kill the process with pid {0}".format(pid)
def run_call(cmd, shell):
"""
run a command don't check output
:param cmd:
:param shell:
:return:
"""
try:
subprocess.call(cmd, shell=shell)
except subprocess.CalledProcessError as error:
print >> sys.stderr, "Error: {0}".format(error)
print "error ignored"
return
def download_file(url, local_path, proxy=None):
if proxy:
cmd = "wget -e use_proxy=on -e http_proxy={0} -O {1} {2}".format(proxy, local_path, url)
else:
cmd = "wget -O {0} {1}".format(local_path, url)
print cmd
run_call(cmd, shell=True)
def download_and_extract_tar(tarfile_url, local_file_name, tarfile_type=None, extract_dir=None, proxy=None):
if extract_dir is None:
extract_dir = '/tmp'
if tarfile_type is None:
tarfile_type = "r:gz"
# try:
# urllib.urlretrieve(tarfile_url, local_file_name)
# except IOError as err:
# print >> sys.stderr, err
download_file(tarfile_url, local_file_name, proxy)
print "untar " + local_file_name
try:
tar = tarfile.open(local_file_name, tarfile_type)
tar.extractall(extract_dir)
tar.close()
except tarfile.TarError as err:
print >> sys.stderr, err
def unzip_file(zip_file, target_dir="/tmp"):
zip_ref = zipfile.ZipFile(zip_file, 'r')
zip_ref.extractall(target_dir)
zip_ref.close()
def clone_git_repo(REPO_URL, LOCAL_DIR, proxy=None):
if proxy:
cmd = "git config --global http.proxy {0}".format(proxy)
run_call(cmd, shell=True)
command = "git clone {0} {1}".format(REPO_URL, LOCAL_DIR)
print command
run_call(command, shell=True)
def update_hostfile():
hosts_file = "/etc/hosts"
hostname = platform.node()
hostname = hostname.strip()
IP = "127.0.1.1"
try:
f = open(hosts_file, "r")
data = f.readlines()
new_data = []
found = False
for line in data:
if IP in line and not line.startswith("#"):
if hostname not in line:
line = "{0} {1}".format(line, hostname)
new_data.append(line)
else:
new_data.append(line)
found = True
else:
new_data.append(line)
if not found:
hostname = hostname + '\n'
line = "{0} {1}".format(IP, hostname)
new_data.append(line)
f.close()
f = open(hosts_file, 'w')
f.write(''.join(new_data))
f.close()
except:
print "FAILED to update hostname"
def check_open_port_available(port, address="127.0.0.1"):
# Create a TCP socket
port = int(port)
s = socket.socket()
print "Attempting to connect to %s on port %s" % (address, port)
try:
s.connect((address, port))
print "Port {0} already in use".format(port)
return False
except socket.error, e:
print "Port {0} is available".format(port)
return True
class DeployAgent:
def __init__(self, host, port, proxy=None, retries=None):
self.host = host
self.port = port
self.proxy = proxy
self.retries = retries
if self.retries is None:
self.retries = DEFAULT_RETRIES
self.os = get_os()
self.pip = 'pip'
self.python = 'python'
if os.path.isfile("/usr/bin/python"):
self.python = "/usr/bin/python"
def _run_cmd(self, cmd, shell, ignore_err=False, print_output=False):
"""
return output and status after runing a shell command
:param cmd:
:param shell:
:param ignore_err:
:param print_output:
:return:
"""
print cmd
for i in xrange(self.retries):
try:
output = subprocess.check_output(cmd, shell=shell)
if print_output:
print output
return output
return
except subprocess.CalledProcessError as error:
if not ignore_err:
print >> sys.stderr, "ERROR: {0}".format(error)
sleep(0.05)
continue
else:
print >> sys.stdout, "WARNING: {0}".format(error)
return
sys.exit(1)
def _add_proxy_for_curl_in_file(self, proxy, file_name):
cmd = 'sed -i "s|curl|curl -x {0}|g" {1}'.format(proxy, file_name)
print cmd
self._run_cmd(cmd, shell=True, ignore_err=True)
def _add_proxy_for_rpm_in_file(self, proxy, file_name):
proxy_url = str(proxy).replace('http://', '')
proxy_url = str(proxy_url).replace('https://', '')
proxy_url = proxy_url.split(':')
if len(proxy_url) > 1:
result = ''.join([i for i in proxy_url[1] if i.isdigit()])
cmd = 'sed -i "s|rpm|rpm --httpproxy {0} --httpport {1}|g" {2}'.format(proxy_url[0],
result, file_name)
print cmd
self._run_cmd(cmd, shell=True, ignore_err=True)
def install_dev_tools(self):
"""
install development tools and dependencies required to compile collectd
:return:
"""
if self.os == "ubuntu" or self.os == "debian":
print "found ubuntu installing development tools and dependencies..."
cmd1 = "DEBIAN_FRONTEND='noninteractive' apt-get -y -o Dpkg::Options::='--force-confdef' " \
"-o Dpkg::Options::='--force-confold' update"
# cmd2 = "apt-get install -y pkg-config build-essential libpthread-stubs0-dev curl " \
# "zlib1g-dev python-dev python-pip libcurl4-openssl-dev libvirt-dev sudo libmysqlclient-dev git wget"
# cmd2 = "apt-get install -y gcc make curl python-dev sudo wget libmysqlclient-dev libcurl4-openssl-dev"
cmd2 = "DEBIAN_FRONTEND='noninteractive' apt-get -y -o Dpkg::Options::='--force-confdef' " \
"-o Dpkg::Options::='--force-confold' install gcc make curl python-dev sudo wget " \
"libmysqlclient-dev libcurl4-openssl-dev"
self._run_cmd(cmd1, shell=True)
self._run_cmd(cmd2, shell=True)
elif self.os == "centos" or self.os == "redhat":
print "found centos/redhat installing developments tools and dependencies..."
# cmd1 = "yum groupinstall -y 'Development Tools'"
# cmd1 = "yum -y install libcurl libcurl-devel rrdtool rrdtool-devel rrdtool-prel libgcrypt-devel gcc make gcc-c++"
# cmd2 = "yum install -y curl python-devel libcurl libvirt-devel perl-ExtUtils-Embed sudo mysql-devel git wget"
cmd1 = "yum install -y gcc gcc-c++ curl python-devel sudo mysql-devel wget bzip2 perf"
# cmd3 = "yum update -y"
# self._run_cmd(cmd3, shell=True)
self._run_cmd(cmd1, shell=True)
# self._run_cmd(cmd2, shell=True)
# def install_pip():
# """
# install latest version of pip
# :return:
# """
# output = self._run_cmd("pip -V", shell=True, print_output=True)
# try:
# if output.split(" ")[1] == "9.0.1":
# return
# else:
# print "install latest version of pip"
# pip_install_url = "https://bootstrap.pypa.io/get-pip.py"
# try:
# urllib.urlretrieve(pip_install_url, "/tmp/get-pip.py")
# except IOError as err:
# print >> sys.stderr, err
# self._run_cmd("python /tmp/get-pip.py", shell=True)
# except Exception as err:
# print >> sys.stderr, err
# def install_pip():
# print "install latest version of pip"
# pip_install_url = "https://bootstrap.pypa.io/get-pip.py"
# try:
# urllib.urlretrieve(pip_install_url, "/tmp/get-pip.py")
# except IOError as err:
# print >> sys.stderr, err
# self._run_cmd("python /tmp/get-pip.py", shell=True)
def install_pip(self):
print "install latest version of pip"
pip_install_url = "https://bootstrap.pypa.io/get-pip.py"
local_file = "/tmp/get-pip.py"
download_file(pip_install_url, local_file, self.proxy)
# if proxy:
# cmd = "curl -x {0} -o /tmp/get-pip.py {1}".format(proxy, pip_install_url)
# else:
# cmd = "curl -o /tmp/get-pip.py {0}".format(pip_install_url)
# print cmd
# run_call(cmd, shell=True)
self._run_cmd("{0} {1}".format(self.python, local_file), shell=True)
if os.path.isfile("/usr/local/bin/pip"):
self.pip = "/usr/local/bin/pip"
elif os.path.isfile("/usr/bin/pip"):
self.pip = "/usr/bin/pip"
print "pip binary {0}".format(self.pip)
def install_python_packages(self):
"""
install required python packages
:return:
"""
print "install python packages using pip"
if self.proxy:
cmd2 = "{0} install --upgrade setuptools collectd psutil argparse pyyaml requests " \
"mako web.py pyopenssl --proxy {1}".format(self.pip, self.proxy)
else:
cmd2 = "{0} install --upgrade setuptools collectd psutil argparse pyyaml mako " \
"requests web.py pyopenssl".format(self.pip)
self._run_cmd(cmd2, shell=True)
def build_collectd(self):
"""
install a custoum collectd from source
:return:
"""
# download and extract collectd
print "downloading collectd..."
download_and_extract_tar(COLLCTD_SOURCE_URL, "/tmp/{0}.tar.bz2".format(COLLCTD_SOURCE_FILE),
tarfile_type="r:bz2",
proxy=self.proxy)
try:
shutil.rmtree("/opt/collectd", ignore_errors=True)
except shutil.Error:
pass
print "setup collectd..."
if os.path.isdir("/tmp/{0}".format(COLLCTD_SOURCE_FILE)):
cmd = "cd /tmp/{0} && ./configure && make all install".format(COLLCTD_SOURCE_FILE)
self._run_cmd(cmd, shell=True)
try:
shutil.copyfile("/tmp/{0}/src/my_types.db".format(COLLCTD_SOURCE_FILE), "/opt/collectd/my_types.db")
except Exception as err:
print err
def setup_collectd(self):
"""
install a custoum collectd from source
:return:
"""
# download and extract collectd
try:
shutil.rmtree("/opt/collectd", ignore_errors=True)
except shutil.Error:
pass
print "downloading collectd..."
if platform.machine() == "x86_64":
download_and_extract_tar(tarfile_url=COLLECTD_X86_64, local_file_name="/tmp/collectd-prebuilt.tar",
proxy=self.proxy, extract_dir="/opt/", tarfile_type="r:bz2")
else:
self.build_collectd()
# self.create_collectd_service()
def create_collectd_service(self):
"""
create a service for collectd installed
:return:
"""
if self.os == "ubuntu":
print "found ubuntu ..."
version = platform.dist()[1]
print "ubuntu version: {0}".format(version)
if version < "16.04":
try:
shutil.copyfile("/opt/collectd/init_scripts/ubuntu14.init",
"/etc/init.d/collectd")
except shutil.Error as err:
print >> sys.stderr, err
self._run_cmd("chmod +x /etc/init.d/collectd", shell=True)
else:
try:
shutil.copyfile("/opt/collectd/init_scripts/ubuntu16.init",
"/etc/systemd/system/collectd.service")
if os.path.isfile("/opt/collectd/init_scripts/collectd_default") and not os.path.isfile(
"/etc/default/collectd"):
shutil.copyfile("/opt/collectd/init_scripts/collectd_default",
"/etc/default/collectd")
except shutil.Error as err:
print >> sys.stderr, err
self._run_cmd("systemctl daemon-reload", shell=True, ignore_err=True)
self._run_cmd("systemctl enable collectd", shell=True, ignore_err=True)
elif self.os == "centos" or self.os == "redhat":
print "found centos ..."
version = platform.dist()[1]
print "centos version: {0}".format(version)
if version < "7.0":
try:
shutil.copyfile("/opt/collectd/init_scripts/centos6.init",
"/etc/init.d/collectd")
except shutil.Error as err:
print >> sys.stderr, err
self._run_cmd("chmod +x /etc/init.d/collectd", shell=True)
else:
try:
shutil.copyfile("/opt/collectd/init_scripts/centos7.init",
"/etc/systemd/system/collectd.service")
if os.path.isfile("/opt/collectd/init_scripts/collectd_default") and not os.path.isfile(
"/etc/default/collectd"):
shutil.copyfile("/opt/collectd/init_scripts/collectd_default",
"/etc/default/collectd")
except shutil.Error as err:
print >> sys.stderr, err
self._run_cmd("systemctl daemon-reload", shell=True, ignore_err=True)
self._run_cmd("systemctl enable collectd", shell=True, ignore_err=True)
print "terminate any old instance of collectd if available"
self._run_cmd("kill $(ps aux | grep -v grep | grep 'collectd' | awk '{print $2}')", shell=True, ignore_err=True)
print "start collectd ..."
# self._run_cmd("systemctl daemon-reload", shell=True, ignore_err=True)
if self.os in ["ubuntu", "centos", "redhat"]:
self._run_cmd("service collectd start", shell=True, print_output=True)
self._run_cmd("service collectd status", shell=True, print_output=True)
else:
bin_file = "/opt/collectd/sbin/collectd"
config_file = "/opt/collectd/etc/collectd.conf"
pid_file = "/opt/collectd/var/run/collectd.pid"
cmd = "nohup {0} -C {1} -P {2} &> /dev/null &".format(bin_file, config_file, pid_file)
print cmd
run_call(cmd, shell=True)
# / opt / collectd / sbin / collectd - C / opt / collectd / etc / collectd.conf - P / opt / collectd / var / run / collectd.pid
def start_collectd(self):
print "terminate any old instance of collectd if available"
self._run_cmd("kill $(ps aux | grep -v grep | grep 'collectd' | awk '{print $2}')", shell=True, ignore_err=True)
bin_file = "/opt/collectd/sbin/collectd"
config_file = "/opt/collectd/etc/collectd.conf"
cmd = "{0} -C {1}".format(bin_file, config_file)
print cmd
run_call(cmd, shell=True)
sleep(1)
pid = self._get_collectd_pid()
if not pid:
run_call(cmd, shell=True)
sleep(1)
def install_fluentd(self):
"""
install fluentd and start the service
:return:
"""
distro, version, name = platform.dist()
fluentd_file_name = "/tmp/install-fluentd.sh"
if self.os == "ubuntu":
print "install fluentd for ubuntu {0} {1}".format(version, name)
fluentd_install_url_ubuntu = "https://toolbelt.treasuredata.com/sh/install-ubuntu-{0}-td-agent3.sh".format(
name)
# urllib.urlretrieve(fluentd_install_url_ubuntu.format(name), "/tmp/install-ubuntu-{0}-td-agent2.sh".format(name))
# self._run_cmd("sh /tmp/install-ubuntu-{0}-td-agent2.sh".format(name), shell=True)
download_file(fluentd_install_url_ubuntu, fluentd_file_name, self.proxy)
if self.proxy:
self._add_proxy_for_curl_in_file(self.proxy, fluentd_file_name)
self._run_cmd("sh {0}".format(fluentd_file_name), shell=True)
self._run_cmd("echo '*.* @127.0.0.1:42186' >> /etc/rsyslog.conf", shell=True, ignore_err=True)
elif self.os in ["centos", "redhat"]:
print "install fluentd for centos/redhat {0} {1}".format(version, name)
# fluentd_install_url_centos = "https://toolbelt.treasuredata.com/sh/install-redhat-td-agent2.sh"
fluentd_install_url_centos = "https://toolbelt.treasuredata.com/sh/install-redhat-td-agent3.sh"
# urllib.urlretrieve(fluentd_install_url_centos, "/tmp/install-redhat-td-agent2.sh")
#
download_file(fluentd_install_url_centos, fluentd_file_name, self.proxy)
if self.proxy:
self._add_proxy_for_curl_in_file(self.proxy, fluentd_file_name)
self._add_proxy_for_rpm_in_file(self.proxy, fluentd_file_name)
self._run_cmd("sh {0}".format(fluentd_file_name), shell=True)
cmd = "usermod -a -G adm td-agent"
print "Adding user td-agent to the group adm"
self._run_cmd(cmd, ignore_err=True, shell=True)
print "Install fluentd gems..."
print "Install fluentd fluent-plugin-elasticsearch..."
self._run_cmd("/usr/sbin/td-agent-gem install fluent-plugin-elasticsearch", shell=True)
print "Install fluentd fluent-plugin-multi-format-parser..."
self._run_cmd("/usr/sbin/td-agent-gem install fluent-plugin-multi-format-parser", shell=True)
# print "Install fluentd fluent-plugin-mysqlslowquery..."
# self._run_cmd("/usr/sbin/td-agent-gem install fluent-plugin-mysqlslowquery", shell=True)
# print "Install fluentd fluent-plugin-kafka..."
# self._run_cmd("/usr/sbin/td-agent-gem install fluent-plugin-kafka", shell=True)
print "start fluentd ..."
self._run_cmd("/etc/init.d/td-agent start", shell=True)
print "Get fluentd status..."
self._run_cmd("/etc/init.d/td-agent status", shell=True, print_output=True)
self._run_cmd("systemctl enable td-agent", shell=True, ignore_err=True)
def add_collectd_plugins(self):
"""
add plugins to collectd installed
:return:
"""
# download_and_extract_tar(collectd_plugins_source_url, "/tmp/plugins.tar.gz")
download_file(COLLECTD_PLUGINS_ZIP, local_path="/tmp/collectd-plugins.zip", proxy=self.proxy)
unzip_file("/tmp/collectd-plugins.zip")
# clone_git_repo(COLLECTD_PLUGINS_REPO, COLLECTD_PLUGINS_DIR, proxy=self.proxy)
try:
shutil.copytree("/tmp/collectd-plugins-master", "/opt/collectd/plugins")
except shutil.Error as err:
print >> sys.stderr, err
if os.path.isfile("{0}/requirements.txt".format(COLLECTD_PLUGINS_DIR)):
if self.proxy:
cmd = "{0} install -r {1}/requirements.txt --proxy {2}".format(self.pip, COLLECTD_PLUGINS_DIR, self.proxy)
else:
cmd = "{0} install -r {1}/requirements.txt".format(self.pip, COLLECTD_PLUGINS_DIR)
self._run_cmd(cmd, shell=True, ignore_err=True)
try:
shutil.move("{0}/collectd.conf".format(COLLECTD_PLUGINS_DIR), "/opt/collectd/etc/collectd.conf")
except shutil.Error as err:
print >> sys.stderr, err
if not os.path.isfile("/opt/collectd/my_types.db"):
try:
shutil.move("{0}/my_types.db".format(COLLECTD_PLUGINS_DIR), "/opt/collectd/my_types.db")
except shutil.Error as err:
print err
# self._run_cmd("service collectd restart", shell=True)
def _check_configurator_status(self, port=DEFAULT_CONFIGURATOR_PORT):
try:
import urllib2
url = "http://127.0.0.1:%s" % (port)
# resp = urllib.urlopen(url)
proxy_handler = urllib2.ProxyHandler({})
opener = urllib2.build_opener(proxy_handler)
req = urllib2.Request(url)
resp = opener.open(req)
return resp.code
except Exception:
return 404
def verify_configurator(self):
print "verify configurator"
code = self._check_configurator_status(self.port)
count = 0
while code != 200:
if count == 6:
print >> sys.stderr, "Error: Configurator-exporter not running"
sys.exit(128)
count += 1
sleep(5)
code = self._check_configurator_status(self.port)
print "verified"
def _get_configurator_pid(self):
pid = self._run_cmd("ps -face | grep -v grep | grep 'api_server' | awk '{print $2}'",
shell=True, print_output=True)
return pid
def _get_collectd_pid(self):
pid = self._run_cmd("ps -face | grep -v grep | grep 'collectd' | awk '{print $2}'",
shell=True, print_output=True)
return pid
def stop_configurator_process(self):
print "Stopping configurator"
kill_process(self._get_configurator_pid())
def install_configurator(self):
"""
install and start configurator
:return:
"""
# kill existing configurator service
self.stop_configurator_process()
# sleep(0.5)
if os.path.isdir(CONFIGURATOR_DIR):
shutil.rmtree(CONFIGURATOR_DIR, ignore_errors=True)
print "downloading configurator..."
# download_and_extract_tar(configurator_source_url, "/tmp/configurator.tar.gz", extract_dir="/opt")
# clone_git_repo(CONFIGURATOR_SOURCE_REPO, CONFIGURATOR_DIR, proxy=self.proxy)
download_file(CONFIGURATOR_ZIP, local_path="/tmp/configurator.zip", proxy=self.proxy)
unzip_file("/tmp/configurator.zip")
try:
shutil.copytree("/tmp/configurator-exporter-master", "/opt/configurator-exporter")
except shutil.Error as err:
print >> sys.stderr, err
print "setup configurator..."
if os.path.isdir(CONFIGURATOR_DIR):
print "starting configurator ..."
self.create_configurator_service()
# if not check_open_port_available(port=self.port):
# sys.exit(98)
#
# if self.os == "ubuntu":
# cmd2 = "cd {0}; nohup python api_server.py -i {1} -p {2} &".format(CONFIGURATOR_DIR, self.host,
# self.port)
# print cmd2
# run_call(cmd2, shell=True)
# sleep(1)
# elif self.os in ["centos", "redhat"]:
# cmd2 = "cd {0}; nohup python api_server.py -i {1} -p {2} &> /dev/null &".format(CONFIGURATOR_DIR,
# self.host,
# self.port)
# print cmd2
# run_call(cmd2, shell=True)
# sleep(5)
# status = self._get_configurator_pid()
# if not status:
# print >> sys.stderr, "Error: Configurator-exporter failed to start"
# sys.exit(128)
def create_configurator_service(self):
"""
create a service for collectd installed
:return:
"""
if self.os == "ubuntu":
print "found ubuntu ..."
version = platform.dist()[1]
print "ubuntu version: {0}".format(version)
if version < "16.04":
try:
shutil.copyfile("/opt/configurator-exporter/init_scripts/configurator.conf",
"/etc/init/configurator.conf")
except shutil.Error as err:
print >> sys.stderr, err
# self._run_cmd("chmod +x /etc/init/configurator.conf", shell=True)
else:
try:
shutil.copyfile("/opt/configurator-exporter/init_scripts/configurator.service",
"/etc/systemd/system/configurator.service")
except shutil.Error as err:
print >> sys.stderr, err
self._run_cmd("systemctl daemon-reload", shell=True, ignore_err=True)
self._run_cmd("systemctl enable configurator", shell=True, ignore_err=True)
elif self.os == "centos" or self.os == "redhat":
print "found centos ..."
version = platform.dist()[1]
print "centos version: {0}".format(version)
if version < "7.0":
try:
shutil.copyfile("/opt/configurator-exporter/init_scripts/configurator_centos6",
"/etc/init.d/configurator")
except shutil.Error as err:
print >> sys.stderr, err
self._run_cmd("chmod +x /etc/init.d/configurator", shell=True)
else:
try:
shutil.copyfile("/opt/configurator-exporter/init_scripts/configurator.service",
"/etc/systemd/system/configurator.service")
except shutil.Error as err:
print >> sys.stderr, err
self._run_cmd("systemctl daemon-reload", shell=True, ignore_err=True)
self._run_cmd("systemctl enable configurator", shell=True, ignore_err=True)
print "terminate any old instance of configurator if available"
self._run_cmd("kill $(ps aux | grep -v grep | grep 'api_server' | awk '{print $2}')", shell=True,
ignore_err=True)
print "start configurator ..."
# self._run_cmd("systemctl daemon-reload", shell=True, ignore_err=True)
self._run_cmd("service configurator start", shell=True, print_output=True)
self._run_cmd("service configurator status", shell=True, print_output=True)
def remove_iptables_rule(self):
"""
clear any previously added iptable rule on port_number
:param port_number:
:return:
"""
clean_rule = "iptables -D INPUT -p tcp -m tcp --dport {0} -j ACCEPT".format(self.port)
self._run_cmd(clean_rule, shell=True, ignore_err=True)
def configure_iptables(self):
"""
add rule to accept traffic on configurator port
:param port_number
:return:
"""
add_rule = "iptables -I INPUT 1 -p tcp -m tcp --dport {0} -j ACCEPT".format(self.port)
save_rule = "iptables-save"
if self.os == "ubuntu":
restart_iptables = "service ufw restart"
elif self.os in ["centos", "redhat"]:
save_rule = "iptables-save | sudo tee /etc/sysconfig/iptables"
restart_iptables = "service iptables restart"
else:
restart_iptables = "service iptables restart"
self.remove_iptables_rule()
self._run_cmd(add_rule, shell=True, ignore_err=True)
self._run_cmd(save_rule, shell=True, ignore_err=True)
self._run_cmd(restart_iptables, shell=True, ignore_err=True)
def install(collectd=True, setup=True, fluentd=True, configurator=True, configurator_host="0.0.0.0",
configurator_port=DEFAULT_CONFIGURATOR_PORT,
http_proxy=None, https_proxy=None, retries=None):
"""
use this function to controll installation process
:param collectd:
:param fluentd:
:param configurator:
:param configurator_host:
:param configurator_port:
:param http_proxy:
:param https_proxy:
:return:
"""
import time
begin = time.time()
# if not collectd and not fluentd and not configurator:
# print >> sys.stderr, "you cannot skip all collectd and fluentd installation"
# sys.exit(128)
if http_proxy and not os.environ.get("http_proxy"):
set_env(http_proxy=http_proxy)
if https_proxy and not os.environ.get("https_proxy"):
set_env(http_proxy=https_proxy)
# noproxy = os.environ.get("no_proxy", "")
# if "127.0.0.1" not in noproxy and (os.environ.get("http_proxy") or os.environ.get("https_proxy")):
# noproxy = "127.0.0.1"
# set_env(no_proxy=noproxy)
proxy = https_proxy
if not proxy:
proxy = http_proxy
obj = DeployAgent(host=configurator_host, port=configurator_port, retries=retries)
if setup:
start = time.time()
update_hostfile()
obj.install_dev_tools()
obj.install_pip()
obj.install_python_packages()
print "=================package setup time in seconds============"
print time.time() - start
print "===================================="
if collectd:
start = time.time()
print "Started installing collectd ..."
obj.setup_collectd()
obj.add_collectd_plugins()
obj.create_collectd_service()
# obj.start_collectd()
print "=================collectd setup time in seconds============"
print time.time() - start
print "===================================="
if fluentd:
start = time.time()
print "started installing fluentd ..."
obj.install_fluentd()
print "=================fluentd setup time in seconds============"
print time.time() - start
print "===================================="
if configurator:
start = time.time()
obj.stop_configurator_process()
print "started installing configurator ..."
obj.install_configurator()
obj.configure_iptables()
obj.verify_configurator()
print "=================configurator setup time in seconds============"
print time.time() - start
print "===================================="
# create_configurator_service()
print "=================total time in seconds============"
print time.time() - begin
print "===================================="
sys.exit(0)
def get_os():
"""
return os name
:return:
"""
return platform.dist()[0].lower()
if __name__ == '__main__':
"""main function"""
parser = argparse.ArgumentParser()
parser.add_argument('-ss', '--skipsetup', action='store_false', default=True, dest='initialsetup',
help='skip collectd installation')
parser.add_argument('-sc', '--skipcollectd', action='store_false', default=True, dest='installcollectd',
help='skip collectd installation')
parser.add_argument('-sf', '--skipfluentd', action='store_false', default=True, dest='installfluentd',
help='skip fluentd installation')
parser.add_argument('-sce', '--skipconfigurator', action='store_false', default=True, dest='installconfigurator',
help='skip configurator installation')
parser.add_argument('-p', '--port', action='store', default="{0}".format(DEFAULT_CONFIGURATOR_PORT), dest='port',
help='port on which configurator will listen')
parser.add_argument('-ip', '--host', action='store', default="0.0.0.0", dest='host',
help='host ip on which configurator will listen')
parser.add_argument('--http_proxy', action='store', default="", dest='http_proxy',
help='http proxy for connecting to internet')
parser.add_argument('--https_proxy', action='store', default="", dest='https_proxy',
help='https proxy for connecting to internet')
parser.add_argument('--retries', type=int, dest='retries',
help='Retries on failure')
args = parser.parse_args()
install(collectd=args.installcollectd,
setup=args.initialsetup,
fluentd=args.installfluentd,
configurator=args.installconfigurator,
configurator_host=args.host,
configurator_port=args.port,
http_proxy=args.http_proxy,
https_proxy=args.https_proxy,
retries=args.retries)