def test_subscribers_to_alarm(self, patched_env, patched_resource, patched_client): """ Test the subscribers_to_alarm function """ from chalicelib import cloudwatch cloudwatch.subscribers_to_alarm("TestAlarm", REGION)
def update_alarms(): """ Entry point for the CloudWatch scheduled task to discover and cache services. """ try: print("update alarms") resource_cache = {} updated = int(time.time()) ddb_table_name = ALARMS_TABLE_NAME ddb_resource = boto3.resource('dynamodb') ddb_table = ddb_resource.Table(ddb_table_name) for alarm in cloudwatch_data.all_subscribed_alarms(): region = alarm["Region"] name = alarm["AlarmName"] if region in resource_cache: cloudwatch = resource_cache[region] else: cloudwatch = boto3.resource('cloudwatch', region_name=region) resource_cache[region] = cloudwatch alarm = cloudwatch.Alarm(name) region_alarm_name = "{}:{}".format(region, name) # look up the resources with this region alarm name subscribers = cloudwatch_data.subscribers_to_alarm(name, region) for resource_arn in subscribers: item = { "RegionAlarmName": region_alarm_name, "ResourceArn": resource_arn, "StateValue": alarm.state_value, "Namespace": alarm.namespace, "StateUpdated": int(alarm.state_updated_timestamp.timestamp()), "Updated": updated } ddb_table.put_item(Item=item) except ClientError as error: print(error) return True
def update_alarms(): """ Entry point for the CloudWatch scheduled task to discover and cache services. """ try: print("update alarms") resource_cache = {} updated = int(time.time()) ddb_table_name = ALARMS_TABLE_NAME ddb_resource = boto3.resource('dynamodb') ddb_table = ddb_resource.Table(ddb_table_name) for alarm in cloudwatch_data.all_subscribed_alarms(): region = alarm["Region"] name = alarm["AlarmName"] if region in resource_cache: cloudwatch = resource_cache[region] else: cloudwatch = boto3.resource('cloudwatch', region_name=region) resource_cache[region] = cloudwatch alarm = cloudwatch.Alarm(name) region_alarm_name = "{}:{}".format(region, name) # look up the resources with this region alarm name subscribers = cloudwatch_data.subscribers_to_alarm(name, region) for resource_arn in subscribers: item = { "RegionAlarmName": region_alarm_name, "ResourceArn": resource_arn, "StateValue": alarm.state_value, "Namespace": alarm.namespace, "StateUpdated": int(alarm.state_updated_timestamp.timestamp()), "Updated": updated } ddb_table.put_item(Item=item) except ClientError as error: print(error) return True
def subscribers_to_alarm(alarm_name, region): """ API entry point to return subscribed nodes of a CloudWatch alarm in a region. """ return cloudwatch_data.subscribers_to_alarm(alarm_name, region)
def subscribers_to_alarm(alarm_name, region): """ API entry point to return subscribed nodes of a CloudWatch alarm in a region. """ return cloudwatch_data.subscribers_to_alarm(alarm_name, region)