1313
1414import yaml
1515
16- AWS_CLOUDWATCH_CFG_PATH = ' /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json'
16+ AWS_CLOUDWATCH_CFG_PATH = " /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json"
1717
1818
1919def parse_args ():
2020 """Parse CL args and return an argparse.Namespace."""
21- parser = argparse .ArgumentParser (
22- description = 'Create the cloudwatch agent config file'
21+ parser = argparse .ArgumentParser (description = "Create the cloudwatch agent config file" )
22+ parser .add_argument ("--config" , help = "Path to JSON file describing logs that should be monitored" , required = True )
23+ parser .add_argument (
24+ "--platform" , help = "OS family of this instance" , choices = ["amazon" , "centos" , "ubuntu" ], required = True
25+ )
26+ parser .add_argument ("--log-group" , help = "Name of the log group" , required = True )
27+ parser .add_argument (
28+ "--node-role" ,
29+ required = True ,
30+ choices = ["HeadNode" , "ComputeFleet" ],
31+ help = "Role this node plays in the cluster " "(i.e., is it a compute node or the head node?)" ,
32+ )
33+ parser .add_argument ("--scheduler" , required = True , choices = ["slurm" , "awsbatch" , "plugin" ], help = "Scheduler" )
34+ parser .add_argument (
35+ "--cluster-config-path" ,
36+ required = False ,
37+ help = "Cluster configuration path" ,
2338 )
24- parser .add_argument ('--config' ,
25- help = 'Path to JSON file describing logs that should be monitored' ,
26- required = True )
27- parser .add_argument ('--platform' ,
28- help = 'OS family of this instance' ,
29- choices = ['amazon' , 'centos' , 'ubuntu' ],
30- required = True )
31- parser .add_argument ('--log-group' ,
32- help = 'Name of the log group' ,
33- required = True )
34- parser .add_argument ('--node-role' ,
35- required = True ,
36- choices = ['HeadNode' , 'ComputeFleet' ],
37- help = 'Role this node plays in the cluster '
38- '(i.e., is it a compute node or the head node?)' )
39- parser .add_argument ('--scheduler' ,
40- required = True ,
41- choices = ['slurm' , 'awsbatch' , 'plugin' ],
42- help = 'Scheduler' )
43- parser .add_argument ('--cluster-config-path' ,
44- required = False ,
45- help = 'Cluster configuration path' ,
46- )
4739 return parser .parse_args ()
4840
4941
5042def gethostname ():
5143 """Return hostname of this instance."""
52- return socket .gethostname ().split ('.' )[0 ]
44+ return socket .gethostname ().split ("." )[0 ]
5345
5446
5547def write_config (config ):
5648 """Write config to AWS_CLOUDWATCH_CFG_PATH."""
57- with open (AWS_CLOUDWATCH_CFG_PATH , 'w+' ) as output_config_file :
49+ with open (AWS_CLOUDWATCH_CFG_PATH , "w+" ) as output_config_file :
5850 json .dump (config , output_config_file , indent = 4 )
5951
6052
6153def add_log_group_name_params (log_group_name , configs ):
6254 """Add a "log_group_name": log_group_name to every config."""
6355 for config in configs :
64- config .update ({' log_group_name' : log_group_name })
56+ config .update ({" log_group_name" : log_group_name })
6557 return configs
6658
6759
6860def add_instance_log_stream_prefixes (configs ):
6961 """Prefix all log_stream_name fields with instance identifiers."""
7062 for config in configs :
71- config ['log_stream_name' ] = '{host}.{{instance_id}}.{log_stream_name}' .format (
72- host = gethostname (),
73- log_stream_name = config ['log_stream_name' ]
63+ config ["log_stream_name" ] = "{host}.{{instance_id}}.{log_stream_name}" .format (
64+ host = gethostname (), log_stream_name = config ["log_stream_name" ]
7465 )
7566 return configs
7667
@@ -83,17 +74,17 @@ def read_data(config_path):
8374
8475def select_configs_for_scheduler (configs , scheduler ):
8576 """Filter out from configs those entries whose 'schedulers' list does not contain scheduler."""
86- return [config for config in configs if scheduler in config [' schedulers' ]]
77+ return [config for config in configs if scheduler in config [" schedulers" ]]
8778
8879
8980def select_configs_for_node_role (configs , node_role ):
9081 """Filter out from configs those entries whose 'node_roles' list does not contain node_role."""
91- return [config for config in configs if node_role in config [' node_roles' ]]
82+ return [config for config in configs if node_role in config [" node_roles" ]]
9283
9384
9485def select_configs_for_platform (configs , platform ):
9586 """Filter out from configs those entries whose 'platforms' list does not contain platform."""
96- return [config for config in configs if platform in config [' platforms' ]]
87+ return [config for config in configs if platform in config [" platforms" ]]
9788
9889
9990def get_node_info ():
@@ -151,8 +142,8 @@ def add_scheduler_plugin_log(config_data, cluster_config_path):
151142 """Add custom log files to config data if log files specified in scheduler plugin."""
152143 cluster_config = load_config (cluster_config_path )
153144 if (
154- get_dict_value (cluster_config , "Scheduling.SchedulerSettings.SchedulerDefinition.Monitoring.Logs.Files" )
155- and get_dict_value (cluster_config , "Scheduling.Scheduler" ) == "plugin"
145+ get_dict_value (cluster_config , "Scheduling.SchedulerSettings.SchedulerDefinition.Monitoring.Logs.Files" )
146+ and get_dict_value (cluster_config , "Scheduling.Scheduler" ) == "plugin"
156147 ):
157148 log_files = get_dict_value (
158149 cluster_config , "Scheduling.SchedulerSettings.SchedulerDefinition.Monitoring.Logs.Files"
@@ -178,26 +169,22 @@ def add_scheduler_plugin_log(config_data, cluster_config_path):
178169def add_timestamps (configs , timestamps_dict ):
179170 """For each config, set its timestamp_format field based on its timestamp_format_key field."""
180171 for config in configs :
181- config [' timestamp_format' ] = timestamps_dict [config [' timestamp_format_key' ]]
172+ config [" timestamp_format" ] = timestamps_dict [config [" timestamp_format_key" ]]
182173 return configs
183174
184175
185176def filter_output_fields (configs ):
186177 """Remove fields that are not required by CloudWatch agent config file."""
187- desired_keys = [' log_stream_name' , ' file_path' , ' timestamp_format' , ' log_group_name' ]
178+ desired_keys = [" log_stream_name" , " file_path" , " timestamp_format" , " log_group_name" ]
188179 return [{desired_key : config [desired_key ] for desired_key in desired_keys } for config in configs ]
189180
190181
191182def create_config (log_configs ):
192183 """Return a dict representing the structure of the output JSON."""
193184 return {
194185 "logs" : {
195- "logs_collected" : {
196- "files" : {
197- "collect_list" : log_configs
198- }
199- },
200- "log_stream_name" : "{host}.{{instance_id}}.default-log-stream" .format (host = gethostname ())
186+ "logs_collected" : {"files" : {"collect_list" : log_configs }},
187+ "log_stream_name" : "{host}.{{instance_id}}.default-log-stream" .format (host = gethostname ()),
201188 }
202189 }
203190
@@ -217,13 +204,13 @@ def main():
217204 config_data = read_data (args .config )
218205 if args .cluster_config_path :
219206 config_data = add_scheduler_plugin_log (config_data , args .cluster_config_path )
220- log_configs = select_logs (config_data [' log_configs' ], args )
221- log_configs = add_timestamps (log_configs , config_data [' timestamp_formats' ])
207+ log_configs = select_logs (config_data [" log_configs" ], args )
208+ log_configs = add_timestamps (log_configs , config_data [" timestamp_formats" ])
222209 log_configs = add_log_group_name_params (args .log_group , log_configs )
223210 log_configs = add_instance_log_stream_prefixes (log_configs )
224211 log_configs = filter_output_fields (log_configs )
225212 write_config (create_config (log_configs ))
226213
227214
228- if __name__ == ' __main__' :
215+ if __name__ == " __main__" :
229216 main ()
0 commit comments