def update_ec2_alarms(verbose=0): alarm_keys = AlarmsManager.get_alarm_keys() for alarm_key in alarm_keys: alarm_group = AlarmsManager.get_alarm_group(alarm_key) tag_key = alarm_group.get('TagKey') tag_value = alarm_group.get('TagValue') AWSCredentialsManager.load_saved_environment_state() if alarm_group.get('AWSProfile'): AWSCredentialsManager.load_aws_profile(alarm_group['AWSProfile']) ec2_tags = get_all_ec2_tags(tag_key) for instance_id, key in ec2_tags.items(): if key != alarm_key: continue if verbose: click.echo('\nUpdating instance {} tagged with {}: {}'.format( instance_id, tag_key, tag_value)) alarms = alarm_group.get('Alarms', []) for alarm in alarms: if verbose: click.echo('Updating {} alarm on instance {}'.format( alarm['MetricName'], instance_id)) if alarm['MetricName'] in plugin_alarms: alarm = plugin_alarms.get( alarm['MetricName']).get_updated_alarm_kwargs( identifier=instance_id, **alarm) set_cloudwatch_alarm(instance_id, **alarm)
def test_load_yaml_file_loads_alarm_groups_into_the_AlarmsManager(self): expected_group = { 'AWSProfile': 'fake-profile', 'TagKey': 'fake-key', 'TagValue': 'fake-value', 'Alarms': [ { 'MetricName': 'fake-metric', 'Statistic': 'Average', 'Period': 60, 'EvaluationPeriods': 1, 'Threshold': 1, 'ComparisonOperator': 'GreaterThanOrEqualToThreshold', 'AlarmActions': [ 'fake-alarm-action', ], 'Namespace': 'AWS/EC2', }, ], } load_yaml_file(self.tmp_file.name) self.assertEqual(AlarmsManager.get_alarm_group('fake-key:fake-value'), expected_group)
def parse_data(data): for profile in data.get('AWSProfiles', []): AWSCredentialsManager.add_aws_profile( aws_profile=profile['AWS_PROFILE'], aws_access_key_id=profile['AWS_ACCESS_KEY_ID'], aws_secret_access_key=profile['AWS_SECRET_ACCESS_KEY'], aws_default_region=profile['AWS_DEFAULT_REGION'], ) for alarm_group in data.get('AlarmGroups', []): AlarmsManager.add_alarm_group(**alarm_group) for tag_group in data.get('TagGroups', []): TagsManager.add_tag_group(**tag_group) for directory_path in data.get('Configuration').get('Plugins', []): ConfigurationManager.add_plugins_directory(directory_path)
def update_ec2_alarms(verbose=0): plugin_alarms = load_plugins() alarm_keys = AlarmsManager.get_alarm_keys() for alarm_key in alarm_keys: alarm_group = AlarmsManager.get_alarm_group(alarm_key) tag_key = alarm_group.get('TagKey') tag_value = alarm_group.get('TagValue') AWSCredentialsManager.load_saved_environment_state() if alarm_group.get('AWSProfile'): AWSCredentialsManager.load_aws_profile(alarm_group['AWSProfile']) ec2_tags = get_all_ec2_tags(tag_key) for instance_id, key in ec2_tags.items(): if key != alarm_key: continue if verbose: click.echo('\nUpdating instance {} tagged with {}: {}' .format(instance_id, tag_key, tag_value)) alarms = alarm_group.get('Alarms', []) for alarm in alarms: if verbose: click.echo('Updating {} alarm on instance {}' .format(alarm['MetricName'], instance_id)) if alarm['MetricName'] in plugin_alarms: alarm = plugin_alarms.get(alarm['MetricName']).get_updated_alarm_kwargs( identifier=instance_id, **alarm ) set_cloudwatch_alarm(instance_id, **alarm)
def test_get_alarm_keys(self): AlarmsManager.add_alarm_group(**self.alarm_group) self.assertEqual(list(AlarmsManager.get_alarm_keys()), ['fake-key:fake-value'])
def test_get_alarm_group(self): AlarmsManager.add_alarm_group(**self.alarm_group) self.assertEqual(AlarmsManager.get_alarm_group('fake-key:fake-value'), self.alarm_group)