AWSTemplateFormatVersion: "2010-09-09"

# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this
# software and associated documentation files (the "Software"), to deal in the Software
# without restriction, including without limitation the rights to use, copy, modify,
# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Description: >
  Builds a CloudWatch dashboard for a DocumentDB cluster.

Parameters:
  DbClusterIdentifier:
    Type: String
    Description: "DocumentDB cluster identifier"

Resources: 
  Dashboard:
    Type: AWS::CloudWatch::Dashboard
    Properties: 
      DashboardName: !Sub "DocumentDB-Metrics-${DbClusterIdentifier}"
      DashboardBody: !Sub |
        {
            "widgets": [
                {
                    "type": "text",
                    "x": 0,
                    "y": 0,
                    "width": 24,
                    "height": 1,
                    "properties": {
                        "markdown": "\n# Cluster\n"
                    }
                },
                {
                    "type": "metric",
                    "x": 0,
                    "y": 1,
                    "width": 6,
                    "height": 6,
                    "properties": {
                        "view": "timeSeries",
                        "stacked": false,
                        "metrics": [
                            [ "AWS/DocDB", "DBClusterReplicaLagMinimum", "DBClusterIdentifier", "${DbClusterIdentifier}" ]
                        ],
                        "region": "${AWS::Region}",
                        "title": "DBClusterReplicaLagMaximum"
                    }
                },
                {
                    "type": "metric",
                    "x": 6,
                    "y": 1,
                    "width": 6,
                    "height": 6,
                    "properties": {
                        "view": "timeSeries",
                        "stacked": false,
                        "metrics": [
                            [ "AWS/DocDB", "DatabaseCursorsTimedOut", "DBClusterIdentifier", "${DbClusterIdentifier}" ]
                        ],
                        "region": "${AWS::Region}"
                    }
                },
                {
                    "type": "metric",
                    "x": 12,
                    "y": 1,
                    "width": 6,
                    "height": 6,
                    "properties": {
                        "view": "timeSeries",
                        "stacked": false,
                        "metrics": [
                            [ "AWS/DocDB", "VolumeWriteIOPs", "DBClusterIdentifier", "${DbClusterIdentifier}" ],
                            [ ".", "VolumeReadIOPs", ".", "." ]
                        ],
                        "region": "${AWS::Region}",
                        "title": "Volume Read/Write IOPS"
                    }
                },
                {
                    "type": "metric",
                    "x": 18,
                    "y": 1,
                    "width": 6,
                    "height": 6,
                    "properties": {
                        "view": "timeSeries",
                        "stacked": false,
                        "metrics": [
                            [ "AWS/DocDB", "OpcountersCommand", "DBClusterIdentifier", "${DbClusterIdentifier}" ],
                            [ ".", "OpcountersDelete", ".", "." ],
                            [ ".", "OpcountersGetmore", ".", "." ],
                            [ ".", "OpcountersInsert", ".", "." ],
                            [ ".", "OpcountersQuery", ".", "." ],
                            [ ".", "OpcountersUpdate", ".", "." ]
                        ],
                        "region": "${AWS::Region}",
                        "title": "Opcounters"
                    }
                },
                {
                    "type": "text",
                    "x": 0,
                    "y": 7,
                    "width": 24,
                    "height": 1,
                    "properties": {
                        "markdown": "\n# Writer\n"
                    }
                },
                {
                    "type": "metric",
                    "x": 0,
                    "y": 8,
                    "width": 6,
                    "height": 6,
                    "properties": {
                        "view": "timeSeries",
                        "stacked": false,
                        "metrics": [
                            [ "AWS/DocDB", "BufferCacheHitRatio", "Role", "WRITER", "DBClusterIdentifier", "${DbClusterIdentifier}" ],
                            [ ".", "IndexBufferCacheHitRatio", ".", ".", ".", "." ]
                        ],
                        "region": "${AWS::Region}",
                        "title": "BufferCacheHitRatio"
                    }
                },
                {
                    "type": "metric",
                    "x": 6,
                    "y": 8,
                    "width": 6,
                    "height": 6,
                    "properties": {
                        "view": "timeSeries",
                        "stacked": false,
                        "metrics": [
                            [ "AWS/DocDB", "DatabaseConnections", "Role", "WRITER", "DBClusterIdentifier", "${DbClusterIdentifier}" ]
                        ],
                        "region": "${AWS::Region}"
                    }
                },
                {
                    "type": "metric",
                    "x": 12,
                    "y": 8,
                    "width": 6,
                    "height": 6,
                    "properties": {
                        "view": "timeSeries",
                        "stacked": false,
                        "metrics": [
                            [ "AWS/DocDB", "DatabaseCursors", "Role", "WRITER", "DBClusterIdentifier", "${DbClusterIdentifier}" ]
                        ],
                        "region": "${AWS::Region}"
                    }
                },
                {
                    "type": "metric",
                    "x": 18,
                    "y": 8,
                    "width": 6,
                    "height": 6,
                    "properties": {
                        "view": "timeSeries",
                        "stacked": false,
                        "metrics": [
                            [ "AWS/DocDB", "CPUUtilization", "Role", "WRITER", "DBClusterIdentifier", "${DbClusterIdentifier}" ]
                        ],
                        "region": "${AWS::Region}"
                    }
                },
                {
                    "type": "metric",
                    "x": 0,
                    "y": 14,
                    "width": 6,
                    "height": 6,
                    "properties": {
                        "view": "timeSeries",
                        "stacked": false,
                        "metrics": [
                            [ "AWS/DocDB", "FreeableMemory", "Role", "WRITER", "DBClusterIdentifier", "${DbClusterIdentifier}" ]
                        ],
                        "region": "${AWS::Region}"
                    }
                },
                {
                    "type": "text",
                    "x": 0,
                    "y": 20,
                    "width": 24,
                    "height": 1,
                    "properties": {
                        "markdown": "\n# Readers\n"
                    }
                },
                {
                    "type": "metric",
                    "x": 0,
                    "y": 21,
                    "width": 6,
                    "height": 6,
                    "properties": {
                        "view": "timeSeries",
                        "stacked": false,
                        "metrics": [
                            [ "AWS/DocDB", "BufferCacheHitRatio", "Role", "READER", "DBClusterIdentifier", "${DbClusterIdentifier}" ],
                            [ ".", "IndexBufferCacheHitRatio", ".", ".", ".", "." ]
                        ],
                        "region": "${AWS::Region}"
                    }
                },
                {
                    "type": "metric",
                    "x": 6,
                    "y": 21,
                    "width": 6,
                    "height": 6,
                    "properties": {
                        "metrics": [
                            [ "AWS/DocDB", "DatabaseConnections", "Role", "READER", "DBClusterIdentifier", "${DbClusterIdentifier}", { "stat": "Average" } ],
                            [ "..." ],
                            [ "...", { "stat": "Maximum" } ]
                        ],
                        "view": "timeSeries",
                        "stacked": false,
                        "region": "${AWS::Region}",
                        "stat": "Minimum",
                        "period": 300
                    }
                },
                {
                    "type": "metric",
                    "x": 12,
                    "y": 21,
                    "width": 6,
                    "height": 6,
                    "properties": {
                        "metrics": [
                            [ "AWS/DocDB", "DatabaseCursors", "Role", "READER", "DBClusterIdentifier", "${DbClusterIdentifier}" ],
                            [ "...", { "stat": "Minimum" } ],
                            [ "...", { "stat": "Maximum" } ]
                        ],
                        "view": "timeSeries",
                        "stacked": false,
                        "region": "${AWS::Region}",
                        "stat": "Average",
                        "period": 300
                    }
                },
                {
                    "type": "metric",
                    "x": 18,
                    "y": 21,
                    "width": 6,
                    "height": 6,
                    "properties": {
                        "metrics": [
                            [ "AWS/DocDB", "CPUUtilization", "Role", "READER", "DBClusterIdentifier", "${DbClusterIdentifier}" ],
                            [ "...", { "stat": "Minimum" } ],
                            [ "...", { "stat": "Maximum" } ]
                        ],
                        "view": "timeSeries",
                        "stacked": false,
                        "region": "${AWS::Region}",
                        "stat": "Average",
                        "period": 300
                    }
                },
                {
                    "type": "metric",
                    "x": 0,
                    "y": 27,
                    "width": 6,
                    "height": 6,
                    "properties": {
                        "metrics": [
                            [ "AWS/DocDB", "FreeableMemory", "Role", "READER", "DBClusterIdentifier", "${DbClusterIdentifier}" ],
                            [ "...", { "stat": "Minimum" } ],
                            [ "...", { "stat": "Maximum" } ]
                        ],
                        "view": "timeSeries",
                        "stacked": false,
                        "region": "${AWS::Region}",
                        "stat": "Average",
                        "period": 300
                    }
                }
            ]
        }

Outputs:
  DashboardName:
    Value: !Ref Dashboard
