Пример #1
0
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)
Пример #2
0
    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)
Пример #3
0
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_group(self):
        AlarmsManager.add_alarm_group(**self.alarm_group)

        self.assertEqual(AlarmsManager.get_alarm_group('fake-key:fake-value'),
                         self.alarm_group)
Пример #5
0
    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)