bp-check/services/cloudwatch.py

56 lines
1.7 KiB
Python
Raw Normal View History

2024-08-05 02:30:34 +00:00
from models import RuleCheckResult
import boto3
2024-08-07 12:34:02 +00:00
client = boto3.client("cloudwatch")
logs_client = boto3.client("logs")
2024-08-05 02:30:34 +00:00
def cw_loggroup_retention_period_check():
2024-08-07 12:34:02 +00:00
compliant_resources = []
non_compliant_resources = []
log_groups = logs_client.describe_log_groups()["logGroups"]
for log_group in log_groups:
if "retentionInDays" in log_group and log_group["retentionInDays"] < 365:
non_compliant_resources.append(log_group["logGroupArn"])
else:
compliant_resources.append(log_group["logGroupArn"])
2024-08-05 02:30:34 +00:00
return RuleCheckResult(
2024-08-07 12:34:02 +00:00
passed=not non_compliant_resources,
compliant_resources=compliant_resources,
non_compliant_resources=non_compliant_resources,
2024-08-05 02:30:34 +00:00
)
def cloudwatch_alarm_settings_check():
2024-08-07 12:34:02 +00:00
compliant_resources = []
non_compliant_resources = []
alarms = client.describe_alarms()["MetricAlarms"]
parameters = {
"MetricName": "", # required
"Threshold": None,
"EvaluationPeriods": None,
"Period": None,
"ComparisonOperator": None,
"Statistic": None,
}
for alarm in alarms:
for check in [i for i in parameters.keys() if parameters[i] != None]:
if alarm["MetricName"] != parameters["MetricName"]:
continue
if alarm[check] != parameters[check]:
non_compliant_resources.append(alarm["AlarmArn"])
break
else:
compliant_resources.append(alarm["AlarmArn"])
2024-08-05 02:30:34 +00:00
return RuleCheckResult(
2024-08-07 12:34:02 +00:00
passed=not non_compliant_resources,
compliant_resources=compliant_resources,
non_compliant_resources=non_compliant_resources,
2024-08-05 02:30:34 +00:00
)