-
Notifications
You must be signed in to change notification settings - Fork 132
/
setup.py
executable file
·1035 lines (890 loc) · 44.4 KB
/
setup.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
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
"""
The installation script provides the following key functions:
1. Installation of dependencies required for the plugin.
2. Download and extraction of plugin source code into a predefined location /opt/collectd-plugins/
3. Plugin configuration using EC2 metadata as defaults if possible.
4. Configuration of Collectd in order to use this plugin.
Depending on the version of collectd installed on the host we provide the following options:
1. version >= 5.5.0 - by default we will supply a configuration and whitelist with percentage host metrics enabled.
2. version >= 5.0.0 - we will offer option to inject our plugin into the existing configuration (no metrics are whitelisted).
3. any other version of collectd is not supported.
"""
import os
import sys
import platform
import re
import shlex
import shutil
import time
import errno
import argparse
import logging
from collections import namedtuple
from distutils.version import LooseVersion
from glob import glob
from json import loads
from os import path, makedirs, chdir
from subprocess import check_output, CalledProcessError, Popen, PIPE
from tempfile import gettempdir
ROOT_UID = 0
TEMP_DIRECTORY = gettempdir() + "/collectd-cloudwatch-plugin/"
TIMESTAMP_FORMAT = "%Y-%m-%d_%H_%M"
GITHUB_USER_NAME = "awslabs"
GITHUB_REPO_BRANCH = "master"
TAR_FILE = GITHUB_USER_NAME + "-collectd-cloudwatch.tar.gz"
DOWNLOAD_PLUGIN_DIR = GITHUB_USER_NAME + "-collectd-cloudwatch*"
DEFAULT_PLUGIN_CONFIGURATION_DIR = "/opt/collectd-plugins/cloudwatch/config"
NEW_PLUGIN_FILES = DOWNLOAD_PLUGIN_DIR + "/src/*"
RECOMMENDED_COLLECTD_CONFIGURATION = DOWNLOAD_PLUGIN_DIR + "/resources/collectd.conf"
RECOMMENDED_WHITELIST = DOWNLOAD_PLUGIN_DIR + "/resources/whitelist.conf"
PLUGIN_INCLUDE_CONFIGURATION = DOWNLOAD_PLUGIN_DIR + "/resources/collectd-cloudwatch.conf"
PLUGIN_CONFIGURATION_INCLUDE_LINE = 'Include "/etc/collectd-cloudwatch.conf"\r\n'
APT_INSTALL_COMMAND = "apt-get install -y "
YUM_INSTALL_COMMAND = "yum install -y "
PIP_INSTALLATION_FLAGS = " install --quiet --upgrade --force-reinstall "
EASY_INSTALL_COMMAND = "easy_install -U --quiet "
PYTHON_DEPENDENCIES = ["requests"]
FIND_COMMAND = "which {} 2> /dev/null"
COLLECTD_HELP_ARGS = "-help"
CONFIG_FILE_REGEX = re.compile("\sConfig file\s*(.*)\s")
VERSION_REGEX = re.compile("\scollectd ([\d*].*[\d*]).*\s")
DISTRO_NAME_REGEX = re.compile("(?<!...)NAME=\"?([\w\s]*)\"?\s?")
CLOUD_WATCH_COLLECTD_DETECTION_REGEX = re.compile('^Import [\'\"]cloudwatch_writer[\"\']$|collectd-cloudwatch\.conf', re.MULTILINE | re.IGNORECASE)
COLLECTD_CONFIG_INCLUDE_REGEX = re.compile("^Include [\'\"](.*?\.conf)[\'\"]", re.MULTILINE | re.IGNORECASE)
COLLECTD_PYTHON_PLUGIN_CONFIGURATION_REGEX = re.compile("^LoadPlugin python$|^<LoadPlugin python>$", re.MULTILINE | re.IGNORECASE)
logging.basicConfig()
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
DISTRIBUTION_TO_INSTALLER = {
"Ubuntu": APT_INSTALL_COMMAND,
"Red Hat Enterprise Linux Server": YUM_INSTALL_COMMAND,
"Amazon Linux AMI": YUM_INSTALL_COMMAND,
"Amazon Linux": YUM_INSTALL_COMMAND,
"CentOS Linux": YUM_INSTALL_COMMAND,
}
def is_pip_installed():
try:
get_path_to_executable("pip")
except CalledProcessError:
return False
else:
return True
def is_setupTools_installed():
try:
import setuptools
except ImportError:
return False
else:
return True
class InstallationFailedException(Exception):
pass
class CollectdInfo(object):
MIN_SUPPORTED_VERSION = "5.0.0"
MIN_RECOMMENDED_VERSION = "5.5.0"
PLUGINS_DIR = "/opt/collectd-plugins"
DEFAULT_COMPILE_PATH = "/opt/collectd/sbin/collectd"
def __init__(self, exec_path, config_path, version):
self.exec_path = exec_path
self.config_path = config_path
self.version = version
def is_supported_version(self):
return self._is_newer_than(self.MIN_SUPPORTED_VERSION)
def is_recommended_version(self):
return self._is_newer_than(self.MIN_RECOMMENDED_VERSION)
def _is_newer_than(self, target):
return LooseVersion(target) <= LooseVersion(self.version)
class Color(object):
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
CYAN = '\033[96m'
END = '\033[0m'
@classmethod
def red(cls, string):
return cls.RED + string + cls.END
@classmethod
def green(cls, string):
return cls.GREEN + string + cls.END
@classmethod
def yellow(cls, string):
return cls.YELLOW + string + cls.END
@classmethod
def cyan(cls, string):
return cls.CYAN + string + cls.END
def get_collectd_info():
exec_path = _get_collectd_exec()
output = check_output([exec_path, COLLECTD_HELP_ARGS])
version = VERSION_REGEX.search(output).group(1)
config_path = CONFIG_FILE_REGEX.search(output).group(1)
return CollectdInfo(exec_path, config_path, version)
def _get_collectd_exec():
try:
return get_path_to_executable("collectd")
except CalledProcessError:
if path.exists(CollectdInfo.DEFAULT_COMPILE_PATH):
return CollectdInfo.DEFAULT_COMPILE_PATH
raise InstallationFailedException("Cannot detect collectd.")
def get_path_to_executable(command):
return check_output(FIND_COMMAND.format(command), shell=True).strip()
class Command(object):
SUCCESS = 0
OK = "... " + Color.green("OK")
NOT_OK = "... " + Color.red("NOT OK")
INVALID_COMMAND_ERROR_MSG = Color.red("ERROR: Could not execute the following command: {command}.")
ERROR_MSG = Color.red("Installation cancelled due to an error.\nExecuted command: '{command}'."
"\nError output: '{error_output}'.")
def __init__(self, command, message, exit_on_failure=False, shell=False):
self.message = message
self.stdout = ""
self.stderr = ""
self._command = command
self._process = None
self._shell = shell
self._exit_on_failure = exit_on_failure
@property
def was_successful(self):
return self._process and self._process.returncode is self.SUCCESS
def run(self):
print self.message, # comma stops print from adding line break at the end
try:
self._process = self._get_process()
self._capture_outputs()
except OSError:
self.stderr = self.INVALID_COMMAND_ERROR_MSG.format(command=self._command)
finally:
self._output_command_status()
if not self.was_successful and self._exit_on_failure:
raise InstallationFailedException(self.ERROR_MSG.format(command=self._command, error_output=self.stderr))
def _get_process(self):
command = self._command
if not self._shell:
command = shlex.split(self._command)
return Popen(command, shell=self._shell, stdout=PIPE, stderr=PIPE)
def _capture_outputs(self):
stdout, stderr = self._process.communicate()
self.stdout = str(stdout).strip()
self.stderr = str(stderr).strip()
def _output_command_status(self):
result = self.NOT_OK
if self.was_successful:
result = self.OK
print result
class MetadataReader(object):
"""
The metadata reader class is responsible for retrieving configuration values from the local metadata server.
"""
_METADATA_SERVICE_ADDRESS = 'http://169.254.169.254/'
_IDENTITY_DOCUMENT_REQUEST = "latest/dynamic/instance-identity/document"
_INSTANCE_ID_METADATA_REQUEST = "latest/meta-data/instance-id/"
_IAM_ROLE_CREDENTIAL_REQUEST = "latest/meta-data/iam/security-credentials/"
_MAX_RETRIES = 1
_CONNECT_TIMEOUT_IN_SECONDS = 0.3
_RESPONSE_TIMEOUT_IN_SECONDS = 0.5
_REQUEST_TIMEOUT = (_CONNECT_TIMEOUT_IN_SECONDS, _RESPONSE_TIMEOUT_IN_SECONDS)
_TOKEN_REQUEST = "latest/api/token"
_TOKEN_TTL_SECONDS = 21600 # 6 hours
_X_AWS_EC_METADATA_TOKEN = 'X-aws-ec2-metadata-token'
_TTL_SECONDS = "X-aws-ec2-metadata-token-ttl-seconds"
def __init__(self):
self.metadata_server = self._METADATA_SERVICE_ADDRESS
self.token = ""
def get_region(self):
""" Get the region value from the metadata service """
document = self._get_metadata(MetadataReader._IDENTITY_DOCUMENT_REQUEST)
return loads(document)['region']
def get_instance_id(self):
""" Get the instance id value from the metadata service """
return self._get_metadata(MetadataReader._INSTANCE_ID_METADATA_REQUEST)
def get_iam_role_name(self):
""" Get the name of IAM Role applied to the EC2 instance """
return self._get_metadata(self._IAM_ROLE_CREDENTIAL_REQUEST)
def _get_metadata(self, request):
"""
This method retrieves values from metadata service.
request -- The request part after the metadata service address, for example if full request is:
'http://169.254.169.254/latest/meta-data/placement/availability-zone/'
then the request part is 'latest/meta-data/placement/availability-zone/'.
"""
from requests import Session, codes
from requests.adapters import HTTPAdapter
try:
result = self._v2_call(request)
# fallback to v1
if not (result and result.status_code is codes.ok):
print("Fallback to IMDSV1")
result = self._v1_call(request)
except Exception as e:
raise MetadataRequestException("Cannot access metadata service. Cause: " + str(e))
if result.status_code is not codes.ok:
raise MetadataRequestException("Cannot retrieve configuration from metadata service. Status code: " + str(result.status_code))
return str(result.text)
def _get_metadata_token(self):
"""
This method retrieves token from metadata service.
"""
from requests import Session, codes
from requests.adapters import HTTPAdapter
try:
session = Session()
session.mount("http://", HTTPAdapter(max_retries=self._MAX_RETRIES))
headers = {self._TTL_SECONDS:str(self._TOKEN_TTL_SECONDS)}
result = session.put(self.metadata_server + self._TOKEN_REQUEST, timeout=self._REQUEST_TIMEOUT, headers=headers)
except Exception as e:
raise MetadataRequestException("%s cannot access metadata service. url:%s, Cause: %s " %(self._get_metadata_token.__name__, self._TOKEN_REQUEST, str(e)) )
if result.status_code is not codes.ok:
raise MetadataRequestException("%s cannot retrieve configuration from metadata service. url:%s, Status code: %s" %(self._get_metadata_token.__name__, self._TOKEN_REQUEST, str(result.status_code)))
return str(result.text)
def _v1_call(self, request):
from requests import Session, codes
from requests.adapters import HTTPAdapter
session = Session()
session.mount("http://", HTTPAdapter(max_retries=self._MAX_RETRIES))
result = session.get(self.metadata_server + request, timeout=self._REQUEST_TIMEOUT)
return result
def _v2_call(self, request):
from requests import Session, codes
from requests.adapters import HTTPAdapter
print ("Try IMDSV2")
try:
self.token = self._get_metadata_token()
session = Session()
session.mount("http://", HTTPAdapter(max_retries=self._MAX_RETRIES))
headers = {self._X_AWS_EC_METADATA_TOKEN:self.token}
result = session.get(self.metadata_server + request, timeout=self._REQUEST_TIMEOUT, headers=headers)
return result
except Exception as e:
print(Color.yellow("\n Failed to retrieve IMDSV2, switch to IMDSV1 explicitly."))
return False
class MetadataRequestException(Exception):
pass
def install_python_packages(packages):
try:
Command(detect_pip() + PIP_INSTALLATION_FLAGS + " ".join(packages), "Installing python dependencies", exit_on_failure=True).run()
except CalledProcessError:
Command(EASY_INSTALL_COMMAND + " ".join(packages), "Installing python dependencies", exit_on_failure=True).run()
def detect_pip():
try:
return get_path_to_executable("pip")
except CalledProcessError:
return get_path_to_executable("python-pip")
def install_packages(packages):
command = DISTRIBUTION_TO_INSTALLER[detect_linux_distribution()] + " ".join(packages)
Command(command, "Installing dependencies").run()
def detect_linux_distribution():
search_string = ""
for release_file in glob("/etc/*-release"):
with open(release_file) as fd:
search_string += fd.read()
return DISTRO_NAME_REGEX.search(search_string).group(1).strip()
def make_dirs(directory):
try:
makedirs(directory)
except OSError as e:
if e.errno is not errno.EEXIST:
raise InstallationFailedException("Could not create directory: {}. Cause: {}".format(directory, str(e)))
class PluginConfig(object):
CREDENTIALS_PATH_KEY = "credentials_path"
REGION_KEY = "region"
HOST_KEY = "host"
PROXY_SERVER_NAME = "proxy_server_name"
PROXY_SERVER_PORT = "proxy_server_port"
PASS_THROUGH_KEY = "whitelist_pass_through"
PUSH_ASG_KEY = "push_asg"
PUSH_CONSTANT_KEY = "push_constant"
CONSTANT_DIMENSION_VALUE_KEY = "constant_dimension_value"
DEBUG_KEY = "debug"
ACCESS_KEY = "aws_access_key"
SECRET_KEY = "aws_secret_key"
ENABLE_HIGH_DEFINITION_METRICS = "enable_high_resolution_metrics"
FLUSH_INTERVAL_IN_SECONDS = "flush_interval_in_seconds"
def __init__(self, credentials_path=None, access_key=None, secret_key=None, region=None, host=None, proxy_server_name=None, proxy_server_port=None, push_asg=None, push_constant=None, constant_dimension_value=None, enable_high_resolution_metrics=False, flush_interval_in_seconds=None):
self.credentials_path = credentials_path
self.access_key = access_key
self.secret_key = secret_key
self.region = region
self.host = host
self.use_recommended_collectd_config = False
self.only_add_plugin = False
self.debug = False
self.pass_through = False
self.credentials_file_exist = False
self.proxy_server_name = proxy_server_name
self.proxy_server_port = proxy_server_port
self.enable_high_resolution_metrics = enable_high_resolution_metrics
self.flush_interval_in_seconds = flush_interval_in_seconds
self.push_asg = push_asg
self.push_constant = push_constant
self.constant_dimension_value = constant_dimension_value
class InteractiveConfigurator(object):
DEFAULT_PROMPT = "Enter choice [" + Color.green("{default}") + "]: "
def __init__(self, plugin_config, metadata_reader, collectd_info,
non_interactive, region, host, proxy_name, proxy_port,
enable_high_resolution_metrics, flush_interval_in_seconds,
access_key, secret_key, creds_path,
installation_method, push_asg, push_constant, dimension_value,
debug_setup, debug):
self.config = plugin_config
self.metadata_reader = metadata_reader
self.collectd_info = collectd_info
self.non_interactive = non_interactive
self.region = region
self.host = host
self.proxy_name = proxy_name
self.proxy_port = proxy_port
self.enable_high_resolution_metrics = enable_high_resolution_metrics
self.flush_interval_in_seconds = flush_interval_in_seconds
self.access_key = access_key
self.secret_key = secret_key
self.creds_path = creds_path
self.installation_method = installation_method
self.push_asg = push_asg
self.push_constant = push_constant
self.dimension_value = dimension_value
self.debug = debug
self.debug_setup = debug_setup
def run(self):
if self.non_interactive:
self._configure_region_non_interactive()
self._configure_hostname_non_interactive()
self._configure_credentials_non_interactive()
self._configure_proxy_server_name_non_interactive()
self._configure_proxy_server_port_non_interactive()
self._configure_push_asg_non_interactive()
self._configure_push_constant_non_interactive()
self._configure_enable_high_resolution_metrics_non_interactive()
self._configure_flush_interval_in_seconds_non_interactive()
self._configure_plugin_installation_method_non_interactive()
self._configure_debug_non_interactive()
if self.debug_setup:
self._debug_setup()
else:
self._configure_region()
self._configure_hostname()
self._configure_credentials()
self._configure_proxy_server_name()
self._configure_proxy_server_port()
self._configure_push_asg()
self._configure_push_constant()
self._configure_enable_high_resolution_metrics()
self._configure_flush_interval_in_seconds()
self._configure_plugin_installation_method()
def _configure_push_asg_non_interactive(self):
if self.push_asg:
self.config.push_asg = True
else:
self.config.push_asg = False
def _configure_push_asg(self):
self.config.push_asg = False
choice = Prompt("\nInclude the Auto-Scaling Group name as a metric dimension:", options=["No", "Yes"], default="1").run()
if choice == "2":
self.config.push_asg = True
def _get_constant_dimension_value(self):
return Prompt(message="Enter FixedDimension value [" + Color.green("ALL") + "]: ", default="ALL").run()
def _configure_push_constant_non_interactive(self):
if self.push_constant and self.dimension_value:
self.config.push_constant = True
self.config.constant_dimension_value = self.dimension_value
else:
self.config.push_constant = False
self.config.constant_dimension_value = "ALL"
def _configure_push_constant(self):
self.config.push_constant = False
self.config.constant_dimension_value = "ALL"
choice = Prompt("\nInclude the FixedDimension as a metric dimension:", options=["No", "Yes"], default="1").run()
if choice == "2":
self.config.push_constant = True
self.config.constant_dimension_value = self._get_constant_dimension_value()
def _configure_region_non_interactive(self):
if self.region:
self.config.region = self.region
else:
self._configure_region()
def _configure_region(self):
try:
region = self.metadata_reader.get_region()
if self.non_interactive:
self.config.region = region
else:
choice = Prompt("\nChoose AWS region for published metrics:", ["Automatic [" + region + "]", "Custom"],
default="1").run()
if choice == "2":
self.config.region = self._get_region()
except MetadataRequestException as e:
print(Color.yellow("\nAWS region could not be automatically detected. Cause:" + str(e)))
self.config.region = self._get_region()
def _get_region(self):
return Prompt("Enter one of the available regions from: " +
Color.cyan("http://docs.aws.amazon.com/general/latest/gr/rande.html#cw_region"),
message="Enter region: ").run()
def _configure_hostname_non_interactive(self):
if self.host:
self.config.host = self.host
else:
self._configure_hostname()
def _configure_hostname(self):
try:
instance_id = self.metadata_reader.get_instance_id()
if self.non_interactive:
self.config.host = instance_id
else:
choice = Prompt("\nChoose hostname for published metrics:", ["EC2 instance id [" + instance_id + "]", "Custom"],
default="1").run()
if choice == "2":
self.config.host = self._get_hostname()
except MetadataRequestException:
print(Color.yellow("\nEC2 instance id could not be automatically detected."))
self.config.host = self._get_hostname()
def _get_hostname(self):
hostname = platform.node()
return Prompt(message="Enter hostname [" + Color.green(hostname) + "]: ", default=hostname).run()
def _configure_proxy_server_name_non_interactive(self):
if self.proxy_name:
self.config.proxy_server_name = self.proxy_name
else:
self.config.proxy_server_name = None
def _configure_proxy_server_name(self):
self.config.proxy_server_name = None
choice = Prompt("\nEnter proxy server name:", options=[None, "Custom"],default="1").run()
if choice == "2":
self.config.proxy_server_name = self._get_proxy_server_name()
def _get_proxy_server_name(self):
self.config.proxy_server_name = None
return Prompt("\nEnter proxy server name (e.g. http[s]://hostname):", default=None).run()
def _configure_proxy_server_port_non_interactive(self):
if self.proxy_port:
self.config.proxy_server_port = self.proxy_port
else:
self.config.proxy_server_port = None
def _configure_proxy_server_port(self):
self.config.proxy_server_port = None
choice = Prompt("\nEnter proxy server port:", options=[None, "Custom"],default="1").run()
if choice == "2":
self.config.proxy_server_port = self._get_proxy_server_port()
def _get_proxy_server_port(self):
self.config.proxy_server_port = None
return Prompt("\nEnter proxy server port (e.g. 8080):", default=None).run()
def _configure_enable_high_resolution_metrics_non_interactive(self):
if self.enable_high_resolution_metrics:
self.config.enable_high_resolution_metrics = True
else:
self.config.enable_high_resolution_metrics = False
def _configure_enable_high_resolution_metrics(self):
choice = Prompt("\nEnable high resolution:", options=["Yes", "No"], default="2").run()
if choice == "1":
self.config.enable_high_resolution_metrics = True
elif choice == "2" :
self.config.enable_high_resolution_metrics = False
def _configure_flush_interval_in_seconds_non_interactive(self):
if self.flush_interval_in_seconds:
self.config.flush_interval_in_seconds = self.flush_interval_in_seconds
else:
self.config.flush_interval_in_seconds = 60
def _configure_flush_interval_in_seconds(self):
choice = Prompt("\nEnter flush internal:", options=["Default 60s", "Custom"], default="1").run()
if choice == "1":
self.config.flush_interval_in_seconds = 60
elif choice == "2":
self.config.flush_interval_in_seconds = self._get_flush_interval_in_seconds()
def _get_flush_interval_in_seconds(self):
return Prompt("\nEnter the customized flush interval ([1, 60] s):", default="60", allowed_values=[str(x) for x in range(1, 61)]).run()
def _configure_credentials_non_interactive(self):
if self.access_key and self.secret_key:
self.config.credentials_path = self._get_credentials_path()
print "self.config.credentials_path = ", self.config.credentials_path
self.config.credentials_file_exist = path.exists(str(self.config.credentials_path))
print "self.config.credentials_file_exist = ", self.config.credentials_file_exist
if not self.config.credentials_file_exist:
self.config.access_key = self.access_key
self.config.secret_key = self.secret_key
def _configure_credentials(self):
if self._is_iam_user_required():
self.config.credentials_path = self._get_credentials_path()
self.config.credentials_file_exist = path.exists(self.config.credentials_path)
if not self.config.credentials_file_exist:
self.config.access_key = Prompt(message="Enter access key: ").run()
self.config.secret_key = Prompt(message="Enter secret key: ").run()
def _is_iam_user_required(self):
try:
iam_role = self.metadata_reader.get_iam_role_name()
if not self.non_interactive:
answer = Prompt("\nChoose authentication method:", ["IAM Role [" + iam_role + "]", "IAM User"], default="1").run()
return answer == "2"
except MetadataRequestException:
print(Color.yellow("\nIAM Role could not be automatically detected."))
return True
def _get_credentials_path(self):
recommended_path = path.expanduser('~') + '/.aws/credentials'
creds_path = ""
if not self.non_interactive:
while not path.isabs(creds_path):
creds_path = Prompt(
message="Enter absolute path to AWS credentials file [" + Color.green(recommended_path) + "]: ",
default=recommended_path).run()
else:
if self.creds_path:
if path.isabs(self.creds_path):
creds_path = self.creds_path
else:
creds_path = recommended_path
make_dirs(path.dirname(creds_path))
return creds_path
def _configure_plugin_installation_method(self):
options = ["Do not modify existing collectd configuration"]
default = "1"
if self.collectd_info.is_supported_version():
options.append("Add plugin to the existing configuration")
default = "2"
if self.collectd_info.is_recommended_version():
options.append("Use CloudWatch recommended configuration (4 metrics)")
default = "3"
answer = Prompt("\nChoose how to install CloudWatch plugin in collectd:", options, default=default).run()
if answer == "2":
self.config.only_add_plugin = True
elif answer == "3":
self.config.use_recommended_collectd_config = True
def _configure_plugin_installation_method_non_interactive(self):
# recommended|add|not_modify
if not self.installation_method:
pass
else:
if self.installation_method == 'not_modify':
pass
if self.installation_method == 'add':
self.config.only_add_plugin = True
if self.installation_method == 'recommended':
self.config.use_recommended_collectd_config = True
def _configure_debug_non_interactive(self):
if self.debug:
self.config.debug = True
def _debug_setup(self):
logger.info('-=**********DEBUG**********=-')
logger.info('PUSH ASG: {}'.format(self.config.push_asg))
logger.info('PUSH CONSTANT: {}'.format(self.config.push_constant))
logger.info('CONSTANT DIMENSION VALUE: {}'.format(self.config.constant_dimension_value))
logger.info('SECRET KEY: {}'.format(self.config.secret_key))
logger.info('ACCESS KEY: {}'.format(self.config.access_key))
logger.info('HOST: {}'.format(self.config.host))
logger.info('PROXY NAME: {}'.format(self.config.proxy_server_name))
logger.info('PROXY PORT: {}'.format(self.config.proxy_server_port))
logger.info('ENABLE HIGH RESOLUTION METRICS: {}'.format(
self.config.enable_high_resolution_metrics))
logger.info('FLUSH INTERVAL IN SECONDS: {}'.format(
self.config.flush_interval_in_seconds))
logger.info('CREDENTIALS PATH: {}'.format(self.config.credentials_path))
logger.info('METHOD ONLY ADD PLUGIN: {}'.format(self.config.only_add_plugin))
logger.info('USE RECOMMENDED CONFIG: {}'.format(self.config.use_recommended_collectd_config))
logger.info('REGION: {}'.format(self.config.region))
logger.info('IAM ROLE (metadata): {}'.format(self.metadata_reader.get_iam_role_name()))
class Prompt(object):
_DEFAULT_PROMPT = "Enter choice [" + Color.green("{}") + "]: "
def __init__(self, title=None, options=None, message=_DEFAULT_PROMPT, default=None, allowed_values=None):
self.title = title
self.options = options
self.message = message
self.default = default
self.allowed_values = allowed_values
self._prepare_prompt()
def _prepare_prompt(self):
if self.message == self._DEFAULT_PROMPT:
self.message = self.message.format(self.default)
def run(self):
if self.title:
print self.title
if self.options:
for index, option in enumerate(self.options, start=1):
print " {}. {}".format(index, option)
return self._get_answer()
def _get_answer(self):
value = raw_input(self.message).strip()
while self._is_value_invalid(value):
value = raw_input(self.message).strip()
return value or str(self.default)
def _is_value_invalid(self, value):
return (not value and not self.default) \
or (value and self.allowed_values and value not in self.allowed_values) \
or (value and self.options and value not in [str(x) for x in range(1, len(self.options)+1)])
class PluginConfigWriter(object):
TEMPLATE = """# The path to the AWS credentials file. This value has to be provided if plugin is used outside of EC2 instances
$credentials_path$
# The target region which will be used to publish metric data
# For list of valid regions visit: http://docs.aws.amazon.com/general/latest/gr/rande.html#cw_region
$region$
# The host parameter can be used to override instance-id or host information published with every metric
$host$
# The pass through option allows unsafe regular expressions such as '.*' or '.+'.
# WARNING: ENABLING THIS OPTION MAY LEAD TO PUBLISHING A LARGE NUMBER OF METRICS
# SEE https://aws.amazon.com/cloudwatch/pricing/ TO UNDERSTAND HOW TO ESTIMATE YOUR BILL.
$whitelist_pass_through$
# The debug parameter enables verbose logging of published metrics
$debug$
# Wheter or not to push the ASG as part of the dimension.
# WARNING: ENABLING THIS WILL LEAD TO CREATING A LARGE NUMBER OF METRICS.
$push_asg$
# Whether or not to push the constant value to CWM as a metric
$push_constant$
# Constant dimension value to add to CWL
$constant_dimension_value$
# This parameter contains proxy server name to connect aws, if needed. Foramt is http[s]://PROXYHOST
$proxy_server_name$
# This parameter contains proxy server port to connect aws, if needed.
$proxy_server_port$
# The enable_high_resolution_metrics is for high resolution support
$enable_high_resolution_metrics$
# The flush_interval_in_seconds is used for flush interval, it means how long plugin should flush the metrics to Cloudwatch
$flush_interval_in_seconds$
"""
DEFAULT_PLUGIN_CONFIG_FILE = path.join(DEFAULT_PLUGIN_CONFIGURATION_DIR, "plugin.conf")
def __init__(self, plugin_config):
self.plugin_config = plugin_config
def write(self):
self._write_plugin_configuration_file()
if self.plugin_config.credentials_path and not self.plugin_config.credentials_file_exist:
self._write_credentials_file()
def _write_plugin_configuration_file(self):
try:
with open(self.DEFAULT_PLUGIN_CONFIG_FILE, "w") as config_file:
config_file.write(self._prepare_config())
print(Color.green("Plugin configuration written successfully."))
except IOError as e:
raise InstallationFailedException("Could not write plugin configuration file. Cause: {}".format(str(e)))
def _write_credentials_file(self):
try:
with open(self.plugin_config.credentials_path, 'w') as credentials_file:
credentials_file.write('{} = "{}"\n'.format(self.plugin_config.ACCESS_KEY, self.plugin_config.access_key))
credentials_file.write('{} = "{}"\n'.format(self.plugin_config.SECRET_KEY, self.plugin_config.secret_key))
print(Color.green("Credentials configuration written successfully."))
except IOError as e:
raise InstallationFailedException("Could not write credentials to file. Cause: {}".format(str(e)))
def _prepare_config(self):
config = self.TEMPLATE
config = self._replace_with_value(config, self.plugin_config.CREDENTIALS_PATH_KEY, self.plugin_config.credentials_path)
config = self._replace_with_value(config, self.plugin_config.REGION_KEY, self.plugin_config.region)
config = self._replace_with_value(config, self.plugin_config.HOST_KEY, self.plugin_config.host)
config = self._replace_with_value(config, self.plugin_config.PASS_THROUGH_KEY, self.plugin_config.pass_through)
config = self._replace_with_value(config, self.plugin_config.DEBUG_KEY, self.plugin_config.debug)
config = self._replace_with_value(config, self.plugin_config.PROXY_SERVER_NAME, self.plugin_config.proxy_server_name)
config = self._replace_with_value(config, self.plugin_config.PROXY_SERVER_PORT, self.plugin_config.proxy_server_port)
config = self._replace_with_value(config, self.plugin_config.ENABLE_HIGH_DEFINITION_METRICS, self.plugin_config.enable_high_resolution_metrics)
config = self._replace_with_value(config, self.plugin_config.FLUSH_INTERVAL_IN_SECONDS, self.plugin_config.flush_interval_in_seconds)
config = self._replace_with_value(config, self.plugin_config.PUSH_ASG_KEY, self.plugin_config.push_asg)
config = self._replace_with_value(config, self.plugin_config.PUSH_CONSTANT_KEY, self.plugin_config.push_constant)
config = self._replace_with_value(config, self.plugin_config.CONSTANT_DIMENSION_VALUE_KEY, self.plugin_config.constant_dimension_value)
return config
def _replace_with_value(self, string, key, value):
template_key = "${}$".format(key)
if value is None:
return string.replace(template_key, '# {} = '.format(key))
return string.replace(template_key, '{} = "{}"'.format(key, value))
def main():
CMD = namedtuple("cmd", "cmd, msg")
COLLECTD_INFO = get_collectd_info()
STOP_COLLECTD_CMD = CMD("pkill collectd", "Stopping collectd process")
START_COLLECTD_CMD = CMD(COLLECTD_INFO.exec_path, "Starting collectd process")
DOWNLOAD_PLUGIN_CMD = CMD("curl -sL https://github.com/" + GITHUB_USER_NAME + "/collectd-cloudwatch/tarball/" + GITHUB_REPO_BRANCH + " > " + TAR_FILE, "Downloading plugin")
UNTAR_PLUGIN_CMD = CMD("tar zxf " + TAR_FILE, "Extracting plugin")
COPY_CMD = "\cp -rf {source} {target}"
COPY_PLUGIN_CMD = CMD(COPY_CMD.format(source=NEW_PLUGIN_FILES, target=CollectdInfo.PLUGINS_DIR), "Moving to collectd plugins directory")
COPY_PLUGIN_INCLUDE_FILE_CMD = CMD(COPY_CMD.format(source=PLUGIN_INCLUDE_CONFIGURATION, target="/etc/"), "Copying CloudWatch plugin include file")
COPY_RECOMMENDED_COLLECTD_CONFIG_CMD = CMD(COPY_CMD.format(source=RECOMMENDED_COLLECTD_CONFIGURATION, target=COLLECTD_INFO.config_path), "Replacing collectd configuration")
BACKUP_COLLECTD_CONFIG_CMD = CMD(COPY_CMD.format(source=COLLECTD_INFO.config_path, target=COLLECTD_INFO.config_path + "." + time.strftime(TIMESTAMP_FORMAT)),
"Creating backup of the original configuration")
REPLACE_WHITELIST_CMD = CMD(COPY_CMD.format(source=RECOMMENDED_WHITELIST, target=DEFAULT_PLUGIN_CONFIGURATION_DIR), "Replacing whitelist configuration")
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description='Script for custom installation process for collectd AWS CloudWatch plugin'
)
parser.add_argument(
'-I', '--non_interactive', required=False,
help='Non interactive mode',
default=False, action='store_true'
)
parser.add_argument(
'-H', '--host_name', required=False,
help='Manual override for EC2 Instance ID and Host information propagated by collectd',
metavar='HOST_NAME', default=None
)
parser.add_argument(
'-r', '--region', required=False,
help='Manual override for region used to publish metrics',
metavar='REGION', default=None
)
parser.add_argument(
'-n', '--proxy_name', required=False,
help='Proxy server name',
metavar='NAME', default=None
)
parser.add_argument(
'-p', '--proxy_port', required=False,
help='Proxy server port',
metavar='PORT', default=None
)
parser.add_argument(
'-e', '--enable_high_resolution_metrics', required=False,
help='Enable high resolution metrics',
default=False, action='store_true'
)
parser.add_argument(
'-f', '--flush_interval', required=False,
help='Flush interval (in seconds)',
metavar='FLUSH_INTERVAL', default=None
)
parser.add_argument(
'-a', '--access_key', required=False,
help='AWS IAM user access key',
metavar='ACCESS_KEY', default=None
)
parser.add_argument(
'-s', '--secret_key', required=False,
help='AWS IAM user secret key',
metavar='SECRET_KEY', default=None
)
parser.add_argument(
'-P', '--creds_path', required=False,
help='Absolute path to AWS credentials file',
metavar='CREDENTIALS_PATH', default=None
)
parser.add_argument(
'-m', '--installation_method', required=False,
help='Choose how to install CloudWatch plugin in collectd',
choices=['recommended', 'add', 'not_modify'],
metavar='recommended|add|not_modify', default=None
)
parser.add_argument(
'-g', '--push_asg', required=False,
help='Include the Auto-Scaling Group name as a metric dimension:',
default=False, action='store_true'
)
parser.add_argument(
'-c', '--push_constant', required=False,
help='Include the FixedDimension as a metric dimension',
default=None, action='store_true'
)
parser.add_argument(
'-v', '--dimension_value', required=False,
help='FixedDimension value',
metavar='DIMENSION_VALUE', default=None
)
parser.add_argument(
'-d', '--debug', default=False,
action='store_true', help='Provides verbose logging of metrics emitted to CloudWatch'
)
parser.add_argument(
'-D', '--debug_setup', default=False,
action='store_true', help='Provides verbose logging during setup process'
)
args = parser.parse_args()
if args.proxy_port is None and args.proxy_name or args.proxy_port and args.proxy_name is None:
parser.error('To enable proxy, use both --proxy_name and --proxy_port ')
if args.access_key is None and args.secret_key or args.access_key and args.secret_key is None:
parser.error('For IAM credentials, use both --secret_key and --access_key')
if args.push_constant is None and args.dimension_value:
parser.error('To include the FixedDimension as a metric dimension, '
'use both --push_constant and --dimension_value')
if args.creds_path and args.secret_key is None and args.access_key is None:
parser.error('Credential path (--creds_path) used only with --secret_key and --access_key arguments')
non_interactive = args.non_interactive
host = args.host_name
region = args.region
proxy_name = args.proxy_name
proxy_port = args.proxy_port
enable_high_resolution_metrics = args.enable_high_resolution_metrics
flush_interval = args.flush_interval
access_key = args.access_key
secret_key = args.secret_key
creds_path = args.creds_path
installation_method = args.installation_method
push_asg = args.push_asg
push_constant = args.push_constant
dimension_value = args.dimension_value
debug_setup = args.debug_setup
debug = args.debug
def install_plugin():
try:
# "python-pip", "python-setuptools" will pull in the python2.6 runtime
# Since python with version >= 2.7 should install pip and setuptools by default, we
# can ignore the system dependencies
if not ( is_pip_installed() and is_setupTools_installed() ):
install_packages(["python-pip", "python-setuptools"])
install_python_packages(PYTHON_DEPENDENCIES)
create_directory_structure()
chdir(TEMP_DIRECTORY)
_run_command(DOWNLOAD_PLUGIN_CMD, shell=True, exit_on_failure=True)
_run_command(UNTAR_PLUGIN_CMD, exit_on_failure=True)
_run_command(COPY_PLUGIN_CMD, shell=True, exit_on_failure=True)
supply_config()
restart_collectd()
finally:
remove_temp_dir()
def create_directory_structure():
try:
make_dirs(TEMP_DIRECTORY)
make_dirs(CollectdInfo.PLUGINS_DIR)
except IOError:
raise InstallationFailedException("Cannot create required directories.")
def supply_config():
if COLLECTD_INFO.is_supported_version():
_run_command(COPY_PLUGIN_INCLUDE_FILE_CMD, shell=True)
config = PluginConfig()
_prepare_plugin_config(config)
if config.use_recommended_collectd_config:
_copy_recommended_configs()
elif config.only_add_plugin:
_inject_plugin_configuration()
else:
print Color.yellow("Please find instructions for the manual configuration of the plugin in the readme.md file.")
else:
raise InstallationFailedException("The minimum supported version of collectd is " + CollectdInfo.MIN_SUPPORTED_VERSION + \
", and your version is " + COLLECTD_INFO.version + \
". You need to upgrade collectd before proceeding with the plugin installation.")
def _prepare_plugin_config(plugin_config):
metadata_reader = MetadataReader()
InteractiveConfigurator(plugin_config, metadata_reader, COLLECTD_INFO,
non_interactive, region, host, proxy_name,
proxy_port, enable_high_resolution_metrics,
flush_interval, access_key, secret_key,
creds_path, installation_method, push_asg,
push_constant, dimension_value, debug_setup,
debug).run()
PluginConfigWriter(plugin_config).write()
def _inject_plugin_configuration():
if _is_cloudwatch_plugin_configured():
print Color.yellow("CloudWatch collectd plugin is already configured in the existing collectd.conf file.")
elif _can_safely_add_python_plugin():
with open(COLLECTD_INFO.config_path, "a") as config:
config.write(PLUGIN_CONFIGURATION_INCLUDE_LINE)
else:
print Color.yellow("Cannot add CloudWatch collectd plugin automatically to the existing collectd configuration.\n"
"Plugin must be configured manually, please find instructions in readme.md file.")
def _copy_recommended_configs():
_run_command(BACKUP_COLLECTD_CONFIG_CMD)
_run_command(COPY_RECOMMENDED_COLLECTD_CONFIG_CMD, shell=True)
_run_command(REPLACE_WHITELIST_CMD, shell=True)
def _can_safely_add_python_plugin():
configs = [COLLECTD_INFO.config_path]