|
| 1 | +#!/bin/bash |
| 2 | +# Script to export CloudWatch dashboard widgets as individual images |
| 3 | + |
| 4 | +set -e |
| 5 | + |
| 6 | +DASHBOARD_NAME="${1:-Demand_And_Capacity_Prod}" |
| 7 | +OUTPUT_DIR="dashboard_exports" |
| 8 | +TIMESTAMP=$(date +%Y%m%d_%H%M%S) |
| 9 | +REGION="${AWS_REGION:-eu-west-2}" |
| 10 | + |
| 11 | +# Create output directory |
| 12 | +mkdir -p "$OUTPUT_DIR" |
| 13 | + |
| 14 | +echo "=========================================" |
| 15 | +echo "CloudWatch Dashboard Image Export" |
| 16 | +echo "=========================================" |
| 17 | +echo "Dashboard: $DASHBOARD_NAME" |
| 18 | +echo "Region: $REGION" |
| 19 | +echo "Output: $OUTPUT_DIR" |
| 20 | +echo "" |
| 21 | + |
| 22 | +# Get dashboard definition |
| 23 | +echo "Fetching dashboard definition..." |
| 24 | +aws cloudwatch get-dashboard \ |
| 25 | + --dashboard-name "$DASHBOARD_NAME" \ |
| 26 | + --region "$REGION" \ |
| 27 | + --output json > "${OUTPUT_DIR}/dashboard_definition_${TIMESTAMP}.json" |
| 28 | + |
| 29 | +if [ $? -ne 0 ]; then |
| 30 | + echo "Error: Failed to fetch dashboard. Check dashboard name and AWS credentials." |
| 31 | + exit 1 |
| 32 | +fi |
| 33 | + |
| 34 | +echo "✓ Dashboard definition saved" |
| 35 | +echo "" |
| 36 | + |
| 37 | +# Extract and process widgets using Python |
| 38 | +echo "Extracting and capturing widget images..." |
| 39 | + |
| 40 | +export TIMESTAMP |
| 41 | +export OUTPUT_DIR |
| 42 | +export REGION |
| 43 | + |
| 44 | +python3 - <<'PYTHON_SCRIPT' |
| 45 | +import json |
| 46 | +import base64 |
| 47 | +import subprocess |
| 48 | +import sys |
| 49 | +import os |
| 50 | +
|
| 51 | +# Read dashboard definition |
| 52 | +with open(f"dashboard_exports/dashboard_definition_{os.environ['TIMESTAMP']}.json") as f: |
| 53 | + dashboard_data = json.load(f) |
| 54 | +
|
| 55 | +# Parse the dashboard body |
| 56 | +dashboard_body = json.loads(dashboard_data['DashboardBody']) |
| 57 | +widgets = dashboard_body.get('widgets', []) |
| 58 | +
|
| 59 | +print(f"Found {len(widgets)} widgets in dashboard\n") |
| 60 | +
|
| 61 | +output_dir = os.environ['OUTPUT_DIR'] |
| 62 | +region = os.environ['REGION'] |
| 63 | +
|
| 64 | +for idx, widget in enumerate(widgets, 1): |
| 65 | + properties = widget.get('properties', {}) |
| 66 | +
|
| 67 | + # Get widget title for filename |
| 68 | + title = properties.get('title', f'widget_{idx}') |
| 69 | + safe_title = "".join(c if c.isalnum() or c in ('-', '_') else '_' for c in title) |
| 70 | +
|
| 71 | + # Create metric widget JSON for API call |
| 72 | + metric_widget = { |
| 73 | + 'width': widget.get('width', 6) * 100, |
| 74 | + 'height': widget.get('height', 6) * 100, |
| 75 | + } |
| 76 | +
|
| 77 | + # Copy relevant properties |
| 78 | + for key in ['metrics', 'view', 'stacked', 'region', 'title', 'period', 'stat', 'yAxis', 'annotations']: |
| 79 | + if key in properties: |
| 80 | + metric_widget[key] = properties[key] |
| 81 | +
|
| 82 | + # Set region if not in widget |
| 83 | + if 'region' not in metric_widget: |
| 84 | + metric_widget['region'] = region |
| 85 | +
|
| 86 | + # Override time range to show last 8 weeks (56 days) |
| 87 | + from datetime import datetime, timedelta |
| 88 | + end_time = datetime.utcnow() |
| 89 | + start_time = end_time - timedelta(weeks=8) |
| 90 | +
|
| 91 | + metric_widget['start'] = start_time.strftime('%Y-%m-%dT%H:%M:%S.000Z') |
| 92 | + metric_widget['end'] = end_time.strftime('%Y-%m-%dT%H:%M:%S.000Z') |
| 93 | +
|
| 94 | + widget_json = json.dumps(metric_widget) |
| 95 | + output_file = f"{output_dir}/{idx:02d}_{safe_title}.png" |
| 96 | +
|
| 97 | + print(f"[{idx}/{len(widgets)}] Capturing: {title}") |
| 98 | +
|
| 99 | + try: |
| 100 | + # Call AWS CLI to get widget image |
| 101 | + result = subprocess.run( |
| 102 | + [ |
| 103 | + 'aws', 'cloudwatch', 'get-metric-widget-image', |
| 104 | + '--metric-widget', widget_json, |
| 105 | + '--output-format', 'png', |
| 106 | + '--region', region, |
| 107 | + '--output', 'text' |
| 108 | + ], |
| 109 | + capture_output=True, |
| 110 | + text=True, |
| 111 | + check=True |
| 112 | + ) |
| 113 | +
|
| 114 | + # Decode base64 and save |
| 115 | + image_data = base64.b64decode(result.stdout) |
| 116 | + with open(output_file, 'wb') as f: |
| 117 | + f.write(image_data) |
| 118 | +
|
| 119 | + print(f" ✓ Saved to: {output_file}") |
| 120 | +
|
| 121 | + except subprocess.CalledProcessError as e: |
| 122 | + print(f" ✗ Failed: {e.stderr}") |
| 123 | + except Exception as e: |
| 124 | + print(f" ✗ Error: {str(e)}") |
| 125 | +
|
| 126 | + print() |
| 127 | +
|
| 128 | +print("========================================") |
| 129 | +print("Export complete!") |
| 130 | +print(f"Images saved to: {output_dir}/") |
| 131 | +print("========================================") |
| 132 | +
|
| 133 | +PYTHON_SCRIPT |
| 134 | + |
| 135 | +echo "" |
| 136 | +echo "Generating HTML report..." |
| 137 | +python3 scripts/generate_dashboard_report.py --input "${OUTPUT_DIR}" |
| 138 | + |
| 139 | +echo "" |
| 140 | +echo "✓ Complete! Check the ${OUTPUT_DIR}/ directory for:" |
| 141 | +echo " - Individual widget images (PNG files)" |
| 142 | +echo " - Combined HTML report (dashboard_report_*.html)" |
0 commit comments