krz/aws-summary-report
Automated AWS summary reports, straight to your inbox.
clone: git clone https://gitbay.org/krz/aws-summary-report.git
main: sections/cloudwatch.py · raw
1# cloudwatch.py
2import boto3
3import datetime
4from tabulate import tabulate
5
6
7def get_section(config):
8 profile = config["aws"].get("profile")
9 region = config["aws"]["region"]
10 session = boto3.Session(
11 profile_name=profile if profile else None, region_name=region
12 )
13 client = session.client("cloudwatch")
14
15 now = datetime.datetime.utcnow()
16 yesterday = now - datetime.timedelta(days=1)
17
18 alarms = client.describe_alarms(StateValue="ALARM")["MetricAlarms"]
19 rows = []
20
21 for alarm in alarms:
22 last_updated = alarm["StateUpdatedTimestamp"]
23 if last_updated >= yesterday:
24 rows.append(
25 [
26 alarm["AlarmName"],
27 alarm["MetricName"],
28 alarm["StateValue"],
29 last_updated.strftime("%Y-%m-%d %H:%M UTC"),
30 ]
31 )
32
33 if not rows:
34 return "CloudWatch Alarms:\nNo alarms triggered in the last 24h."
35
36 table = tabulate(
37 rows, headers=["Name", "Metric", "State", "Updated"], tablefmt="simple_grid"
38 )
39 lines = [
40 "CloudWatch Alarms (Last 24h):",
41 f"[https://{config['aws'].get('region')}.console.aws.amazon.com/cloudwatch/home#alarmsV2:]",
42 table,
43 ]
44
45 return "\n".join(lines)