From 785f42901f34aaf356f316c691e3f56138c8608d Mon Sep 17 00:00:00 2001 From: Christian Cleberg Date: Fri, 20 Jun 2025 13:55:54 -0500 Subject: initial commit --- sections/cloudwatch.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 sections/cloudwatch.py (limited to 'sections/cloudwatch.py') diff --git a/sections/cloudwatch.py b/sections/cloudwatch.py new file mode 100644 index 0000000..65a59d6 --- /dev/null +++ b/sections/cloudwatch.py @@ -0,0 +1,45 @@ +# cloudwatch.py +import boto3 +import datetime +from tabulate import tabulate + + +def get_section(config): + profile = config["aws"].get("profile") + region = config["aws"]["region"] + session = boto3.Session( + profile_name=profile if profile else None, region_name=region + ) + client = session.client("cloudwatch") + + now = datetime.datetime.utcnow() + yesterday = now - datetime.timedelta(days=1) + + alarms = client.describe_alarms(StateValue="ALARM")["MetricAlarms"] + rows = [] + + for alarm in alarms: + last_updated = alarm["StateUpdatedTimestamp"] + if last_updated >= yesterday: + rows.append( + [ + alarm["AlarmName"], + alarm["MetricName"], + alarm["StateValue"], + last_updated.strftime("%Y-%m-%d %H:%M UTC"), + ] + ) + + if not rows: + return "CloudWatch Alarms:\nNo alarms triggered in the last 24h." + + table = tabulate( + rows, headers=["Name", "Metric", "State", "Updated"], tablefmt="simple_grid" + ) + lines = [ + "CloudWatch Alarms (Last 24h):", + f"[https://{config['aws'].get('region')}.console.aws.amazon.com/cloudwatch/home#alarmsV2:]", + table, + ] + + return "\n".join(lines) -- cgit v1.2.3-70-g09d2