# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: MIT-0 # CloudWatch Custom Widget sample: send bitmap snapshot of a CloudWatch Dashboard by email import base64 import boto3 import datetime import json import os from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText DOCS = """ ## Send bitmap snapshot of a CloudWatch Dashboard by email Grabs bitmap snapshots of all metric widgets in the current CloudWatch Dashboard and sends them to an email address configured in [SES](/ses/home). ```""" def load_dashboard(dashboardName): cloudwatch = boto3.client('cloudwatch') dashboard = cloudwatch.get_dashboard(DashboardName=dashboardName) return json.loads(dashboard['DashboardBody']) def get_metric_bitmaps(widgets, start, end, unitWidth, unitHeight): bitmaps = [] for widget in widgets: if widget['type'] != 'metric': continue width = widget['width'] if 'width' in widget else 12 height = widget['height'] if 'height' in widget else 12 widgetProps = widget['properties'] graph = { **widgetProps, 'start': start, 'end': end, 'width': int(width * unitWidth), 'height': int(height * unitHeight) } params = { 'MetricWidget': json.dumps(graph) } region = widgetProps['region'] cloudwatch = boto3.client('cloudwatch', region_name=region) image = cloudwatch.get_metric_widget_image(**params) bitmaps.append(image['MetricWidgetImage']) return bitmaps def email_bitmaps(dashboardName, email, bitmaps, sesRegion): subject = f"Dashboard snapshot: {dashboardName}" html = f"
Snapshot was sent successfully to '{email}'" except Exception as e: msg = f"
Snapshot failed to send to '{email}': {e}" return f""" {msg}"""