aboutsummaryrefslogtreecommitdiff
path: root/sections/cloudwatch.py
diff options
context:
space:
mode:
Diffstat (limited to 'sections/cloudwatch.py')
-rw-r--r--sections/cloudwatch.py45
1 files changed, 45 insertions, 0 deletions
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)