|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import boto3 |
| 4 | +from grafana_client import GrafanaApi, HeaderAuth, TokenAuth |
| 5 | +from grafanalib._gen import DashboardEncoder |
| 6 | +from grafanalib.core import Dashboard |
| 7 | +import json |
| 8 | +from typing import Dict |
| 9 | +import urllib.request |
| 10 | + |
| 11 | +from grafana_client.knowledge import datasource_factory |
| 12 | +from grafana_client.model import DatasourceModel |
| 13 | + |
| 14 | +import os |
| 15 | +import cfnresponse |
| 16 | + |
| 17 | +PROM_DASHBOARDS_URL = [ |
| 18 | + 'https://grafana.com/api/dashboards/12239/revisions/latest/download', |
| 19 | + 'https://grafana.com/api/dashboards/1860/revisions/latest/download', |
| 20 | + 'https://grafana.com/api/dashboards/20579/revisions/latest/download' |
| 21 | +] |
| 22 | + |
| 23 | + |
| 24 | +def create_prometheus_datasource(grafana, url, aws_region): |
| 25 | + jsonData = { |
| 26 | + 'sigV4Auth': True, |
| 27 | + 'sigV4AuthType': 'ec2_iam_role', |
| 28 | + 'sigV4Region': aws_region, |
| 29 | + 'httpMethod': 'GET' |
| 30 | + } |
| 31 | + |
| 32 | + datasource = DatasourceModel(name="Prometheus", |
| 33 | + type="prometheus", |
| 34 | + url=url, |
| 35 | + access="proxy", |
| 36 | + jsonData=jsonData) |
| 37 | + datasource = datasource_factory(datasource) |
| 38 | + datasource = datasource.asdict() |
| 39 | + datasource = grafana.datasource.create_datasource(datasource)["datasource"] |
| 40 | + r = grafana.datasource.health(datasource['uid']) |
| 41 | + return datasource |
| 42 | + |
| 43 | + |
| 44 | +def encode_dashboard(entity) -> Dict: |
| 45 | + """ |
| 46 | + Encode grafanalib `Dashboard` entity to dictionary. |
| 47 | +
|
| 48 | + TODO: Optimize without going through JSON marshalling. |
| 49 | + """ |
| 50 | + return json.loads(json.dumps(entity, sort_keys=True, cls=DashboardEncoder)) |
| 51 | + |
| 52 | + |
| 53 | +def mk_dash(datasource_uid, url): |
| 54 | + url = urllib.request.urlopen(url) |
| 55 | + dashboard = json.load(url) |
| 56 | + for i in dashboard['panels']: |
| 57 | + i["datasource"] = {"type": "prometheus", "uid": datasource_uid} |
| 58 | + |
| 59 | + for i in dashboard['templating']['list']: |
| 60 | + i["datasource"] = {"type": "prometheus", "uid": datasource_uid} |
| 61 | + |
| 62 | + return {"dashboard": dashboard, "overwrite": True} |
| 63 | + |
| 64 | + |
| 65 | +def lambda_handler(event, context): |
| 66 | + |
| 67 | + if event['RequestType'] != 'Create': |
| 68 | + responseData = {} |
| 69 | + responseData['Data'] = 0 |
| 70 | + cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData, |
| 71 | + "CustomResourcePhysicalID") |
| 72 | + return {'statusCode': 200, 'body': json.dumps('Update or Delete')} |
| 73 | + |
| 74 | + aws_region = os.environ['REGION'] |
| 75 | + grafana_key_name = "CreateDashboards" |
| 76 | + grafana_url = os.environ['GRAFANA_WORKSPACE_URL'] |
| 77 | + workspace_id = os.environ['GRAFANA_WORKSPACE_ID'] |
| 78 | + prometheus_url = os.environ['PROMETHEUS_URL'] |
| 79 | + |
| 80 | + client = boto3.client('grafana') |
| 81 | + try: |
| 82 | + response = client.create_workspace_api_key(keyName=grafana_key_name, |
| 83 | + keyRole='ADMIN', |
| 84 | + secondsToLive=60, |
| 85 | + workspaceId=workspace_id) |
| 86 | + except Exception as e: |
| 87 | + responseData = {} |
| 88 | + responseData['Data'] = 123 |
| 89 | + cfnresponse.send(event, context, cfnresponse.FAILED, responseData, |
| 90 | + "CustomResourcePhysicalID") |
| 91 | + print(e) |
| 92 | + |
| 93 | + try: |
| 94 | + grafana = GrafanaApi.from_url( |
| 95 | + url=grafana_url, |
| 96 | + credential=TokenAuth(token=response['key']), |
| 97 | + ) |
| 98 | + |
| 99 | + prometheus_datasource = create_prometheus_datasource( |
| 100 | + grafana, prometheus_url, aws_region) |
| 101 | + |
| 102 | + for i in PROM_DASHBOARDS_URL: |
| 103 | + dashboard_payload = mk_dash(prometheus_datasource['uid'], i) |
| 104 | + response = grafana.dashboard.update_dashboard(dashboard_payload) |
| 105 | + |
| 106 | + responseData = {} |
| 107 | + responseData['Data'] = 123 |
| 108 | + cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData, |
| 109 | + "CustomResourcePhysicalID") |
| 110 | + |
| 111 | + except Exception as e: |
| 112 | + responseData = {} |
| 113 | + responseData['Data'] = 123 |
| 114 | + cfnresponse.send(event, context, cfnresponse.FAILED, responseData, |
| 115 | + "CustomResourcePhysicalID") |
| 116 | + print(e) |
| 117 | + |
| 118 | + response = client.delete_workspace_api_key(keyName=grafana_key_name, |
| 119 | + workspaceId=workspace_id) |
| 120 | + |
| 121 | + return {'statusCode': 200, 'body': json.dumps('Dashboards created')} |
0 commit comments