alarm: proxy_cloudfront reads max quota from config

This commit is contained in:
Iain Learmonth 2022-06-22 16:34:18 +01:00
parent 16c1d26e3b
commit 3da5abce92

View file

@ -1,4 +1,3 @@
import datetime
from typing import Tuple from typing import Tuple
import boto3 import boto3
@ -14,17 +13,26 @@ from app.terraform import BaseAutomation
def _cloudfront_quota() -> None: def _cloudfront_quota() -> None:
alarm = get_or_create_alarm( # It would be nice to learn this from the Service Quotas API, however
BRN.from_str(f"brn:{current_app.config['GLOBAL_NAMESPACE']}:0:mirror:cloudfront:quota/distributions"), # at the time of writing this comment, the current value for this quota
"quota-usage" # is not available from the API. It just doesn't return anything.
) max_count = int(current_app.config.get('AWS_CLOUDFRONT_MAX_DISTRIBUTIONS', 200))
alarm.last_updated = datetime.datetime.utcnow()
deployed_count = len(Proxy.query.filter( deployed_count = len(Proxy.query.filter(
Proxy.destroyed.is_(None)).all()) Proxy.destroyed.is_(None)).all())
message = f"{deployed_count} distributions deployed" message = f"{deployed_count} distributions deployed of {max_count} quota"
if deployed_count > 370: alarm = get_or_create_alarm(
BRN(
group_id=0,
product="mirror",
provider="cloudfront",
resource_type="quota",
resource_id="distributions"
),
"quota-usage"
)
if deployed_count > max_count * 0.9:
alarm.update_state(AlarmState.CRITICAL, message) alarm.update_state(AlarmState.CRITICAL, message)
elif deployed_count > 320: elif deployed_count > max_count * 0.75:
alarm.update_state(AlarmState.WARNING, message) alarm.update_state(AlarmState.WARNING, message)
else: else:
alarm.update_state(AlarmState.OK, message) alarm.update_state(AlarmState.OK, message)