---
AWSTemplateFormatVersion: '2010-09-09'
Description: |

  **WARNING** This template creates an Amazon CloudWatch Log group metric filters and you will be billed for the AWS resources used if you create a stack from this template.
Metadata:
  AWS::CloudFormation::Interface:
    ParameterGroups:
      - Label:
          default: RDS OS metrics
        Parameters:
          - CPUUtilization
          - Memory
          - LoadAverageMinute
          - Tasks
          - Swap
          - System
      - Label:
          default: DataBase and NameSpace Identifier
        Parameters:
          - DataBaseinstanceID
          - Namespace
          - DashBoard
      - Label:
          default: DataBase Engine
        Parameters:
          - EngineType


Parameters:
  DataBaseinstanceID:
    Type: String
    Description: Enter the InstanceID for the DataBase
  DashBoard:
    Type: String
    Default: RDOS-Dashboard
  Namespace:
    Type: String
    Description: Enter the CloudWatch NameSpace for the metrics
  EngineType:
    Default: SqlServer
    Type: String
    AllowedValues:
      - SqlServer
      - other
  CPUUtilization:
    Default: yes
    Type: String
    AllowedValues:
      - yes
      - no
    Description: select yes to populate all CPU Utilization metrics
  System:
    Default: yes
    Type: String
    AllowedValues:
      - yes
      - no
    Description: select yes to populate all System metrics
  Memory:
    Default: yes
    Type: String
    AllowedValues:
      - yes
      - no
    Description: select yes to populate all Memory metrics
  LoadAverageMinute:
    Default: yes
    Type: String
    AllowedValues:
      - yes
      - no
    Description: select yes to populate all LoadAverageMinute metrics
  Tasks:
    Default: yes
    Type: String
    AllowedValues:
      - yes
      - no
    Description: select yes to populate all Task metrics
  Swap:
    Default: yes
    Type: String
    AllowedValues:
      - yes
      - no
    Description: select yes to populate all Swap metrics
Conditions:
  CPUUtilizationOtherCondition: !And
  - !Equals ["other", !Ref EngineType]
  - !Equals ["true", !Ref CPUUtilization]
  CPUUtilizationSqlCondition: !And
  - !Equals ["SqlServer", !Ref EngineType]
  - !Equals ["true", !Ref CPUUtilization]
  CPUUtilizationCondition: !Equals 
  - !Ref CPUUtilization
  - yes
  SystemCondition: !And
  - !Equals ["SqlServer", !Ref EngineType]
  - !Equals ["true", !Ref System]
  MemorySqlCondition: !And
  - !Equals ["SqlServer", !Ref EngineType]
  - !Equals ["true", !Ref Memory]
  MemoryOtherCondition: !And
  - !Equals ["other", !Ref EngineType]
  - !Equals ["true", !Ref Memory]
  LoadAverageMinuteCondition: !And
  - !Equals ["other", !Ref EngineType]
  - !Equals ["true", !Ref LoadAverageMinute]
  TasksCondition: !And
  - !Equals ["other", !Ref EngineType]
  - !Equals ["true", !Ref Tasks]
  SwapCondition: !And
  - !Equals ["other", !Ref EngineType]
  - !Equals ["true", !Ref Swap]
  SQLEngineCondition: !Equals 
  - !Ref EngineType
  - SqlServer
  OtherEngineCondition: !Equals 
  - !Ref EngineType
  - other
Resources:
  CPUUtilizationUser: 
    Condition: CPUUtilizationCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.cpuUtilization.user = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.cpuUtilization.user
          MetricNamespace: !Ref Namespace
          MetricName: "CPU-User"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  CPUUtilizationGuest: 
    Condition: CPUUtilizationOtherCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.cpuUtilization.guest = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.cpuUtilization.guest
          MetricNamespace: !Ref Namespace
          MetricName: "CPU-Guest"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  CPUUtilizationIrq: 
    Condition: CPUUtilizationOtherCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.cpuUtilization.irq = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.cpuUtilization.irq
          MetricNamespace: !Ref Namespace
          MetricName: "CPU-Irq"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID   
  CPUUtilizationSystem: 
    Condition: CPUUtilizationOtherCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.cpuUtilization.system = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.cpuUtilization.system
          MetricNamespace: !Ref Namespace
          MetricName: "CPU-System"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID 
  CPUUtilizationWait: 
    Condition: CPUUtilizationOtherCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.cpuUtilization.wait = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.cpuUtilization.wait
          MetricNamespace: !Ref Namespace
          MetricName: "CPU-Wait"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  CPUUtilizationIdle: 
    Condition: CPUUtilizationCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.cpuUtilization.idle = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.cpuUtilization.idle
          MetricNamespace: !Ref Namespace
          MetricName: "CPU-Idle"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  CPUUtilizationTotal: 
    Condition: CPUUtilizationOtherCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.cpuUtilization.total = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.cpuUtilization.total
          MetricNamespace: !Ref Namespace
          MetricName: "CPU-Total"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  CPUUtilizationKern: 
    Condition: CPUUtilizationSqlCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.cpuUtilization.kern = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.cpuUtilization.kern
          MetricNamespace: !Ref Namespace
          MetricName: "CPU-Kern"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  CPUUtilizationSteal: 
    Condition: CPUUtilizationOtherCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.cpuUtilization.steal = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.cpuUtilization.steal
          MetricNamespace: !Ref Namespace
          MetricName: "CPU-Steal"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  CPUUtilizationNice: 
    Condition: CPUUtilizationOtherCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.cpuUtilization.nice = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.cpuUtilization.nice
          MetricNamespace: !Ref Namespace
          MetricName: "CPU-Nice"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  LoadAverageOneMinute: 
    Condition: LoadAverageMinuteCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.loadAverageMinute.one = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.loadAverageMinute.one
          MetricNamespace: !Ref Namespace
          MetricName: "LoadAverageOneMinute"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  LoadAverageFiveMinute: 
    Condition: LoadAverageMinuteCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.loadAverageMinute.five = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.loadAverageMinute.five
          MetricNamespace: !Ref Namespace
          MetricName: "LoadAverageFiveMinute"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  LoadAverageFifteenMinute: 
    Condition: LoadAverageMinuteCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.loadAverageMinute.fifteen = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.loadAverageMinute.fifteen
          MetricNamespace: !Ref Namespace
          MetricName: "LoadAverageFifteenMinute"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID      
  TasksSleeping: 
    Condition: TasksCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.tasks.sleeping = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.tasks.sleeping
          MetricNamespace: !Ref Namespace
          MetricName: "TasksSleeping"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  TasksZombie: 
    Condition: TasksCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.tasks.zombie = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.tasks.zombie
          MetricNamespace: !Ref Namespace
          MetricName: "TasksZombie"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  TasksRunning: 
    Condition: TasksCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.tasks.running = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.tasks.running
          MetricNamespace: !Ref Namespace
          MetricName: "TasksRunning"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  TasksStopped: 
    Condition: TasksCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.tasks.stopped = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.tasks.stopped
          MetricNamespace: !Ref Namespace
          MetricName: "TasksStopped"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  TasksTotal: 
    Condition: TasksCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.tasks.total = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.tasks.total
          MetricNamespace: !Ref Namespace
          MetricName: "TasksTotal"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  TasksBlocked: 
    Condition: TasksCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.tasks.blocked = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.tasks.blocked
          MetricNamespace: !Ref Namespace
          MetricName: "TasksBlocked"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  SwapCached: 
    Condition: SwapCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.swap.cached = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.swap.cached
          MetricNamespace: !Ref Namespace
          MetricName: "SwapCached"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  SwapTotal: 
    Condition: SwapCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.swap.total = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.swap.total
          MetricNamespace: !Ref Namespace
          MetricName: "SwapTotal"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  SwapFree: 
    Condition: SwapCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.swap.free = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.swap.free
          MetricNamespace: !Ref Namespace
          MetricName: "SwapFree"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  MemoryWriteback: 
    Condition: MemoryOtherCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.memory.writeback = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.memory.writeback
          MetricNamespace: !Ref Namespace
          MetricName: "MemoryWriteback"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  MemoryCommitTotKb: 
    Condition: MemorySqlCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.memory.commitTotKb = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.memory.commitTotKb
          MetricNamespace: !Ref Namespace
          MetricName: "MemoryCommitTotKb"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  MemoryCommitLimitKb: 
    Condition: MemorySqlCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.memory.commitLimitKb = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.memory.commitLimitKb
          MetricNamespace: !Ref Namespace
          MetricName: "MemoryCommitLimitKb"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  MemoryCommitPeakKb: 
    Condition: MemorySqlCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.memory.commitPeakKb = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.memory.commitPeakKb
          MetricNamespace: !Ref Namespace
          MetricName: "MemoryCommitPeakKb"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  MemoryPhysTotKb: 
    Condition: MemorySqlCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.memory.physTotKb = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.memory.physTotKb
          MetricNamespace: !Ref Namespace
          MetricName: "MemoryPhysTotKb"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  MemoryPhysAvailKb: 
    Condition: MemorySqlCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.memory.physAvailKb = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.memory.physAvailKb
          MetricNamespace: !Ref Namespace
          MetricName: "MemoryPhysAvailKb"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  MemorySysCacheKb: 
    Condition: MemorySqlCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.memory.sysCacheKb = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.memory.sysCacheKb
          MetricNamespace: !Ref Namespace
          MetricName: "MemorySysCacheKb"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  MemoryKernTotKb: 
    Condition: MemorySqlCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.memory.kernTotKb = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.memory.kernTotKb
          MetricNamespace: !Ref Namespace
          MetricName: "MemoryKernTotKb"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  MemoryKernPagedKb: 
    Condition: MemorySqlCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.memory.kernPagedKb = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.memory.kernPagedKb
          MetricNamespace: !Ref Namespace
          MetricName: "MemoryKernPagedKb"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  MemoryKernNonpagedKb: 
    Condition: MemorySqlCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.memory.kernNonpagedKb = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.memory.kernNonpagedKb
          MetricNamespace: !Ref Namespace
          MetricName: "MemoryKernNonpagedKb"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  MemorySqlServerTotKb: 
    Condition: MemorySqlCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.memory.sqlServerTotKb = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.memory.sqlServerTotKb
          MetricNamespace: !Ref Namespace
          MetricName: "MemorySqlServerTotKb"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  MemoryPageSize: 
    Condition: MemorySqlCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.memory.pageSize = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.memory.pageSize
          MetricNamespace: !Ref Namespace
          MetricName: "MemoryPageSize"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  MemoryHugePagesFree: 
    Condition: MemoryOtherCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.memory.hugePagesFree = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.memory.hugePagesFree
          MetricNamespace: !Ref Namespace
          MetricName: "MemoryHugePagesFree"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  MemoryHugePagesRsvd: 
    Condition: MemoryOtherCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.memory.hugePagesRsvd = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.memory.hugePagesRsvd
          MetricNamespace: !Ref Namespace
          MetricName: "MemoryHugePagesRsvd"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  MemoryHugePagesSurp: 
    Condition: MemoryOtherCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.memory.hugePagesSurp = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.memory.hugePagesSurp
          MetricNamespace: !Ref Namespace
          MetricName: "MemoryHugePagesSurp"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  MemoryCached: 
    Condition: MemoryOtherCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.memory.cached = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.memory.cached
          MetricNamespace: !Ref Namespace
          MetricName: "MemoryCached"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  MemoryHugePagesSize: 
    Condition: MemoryOtherCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.memory.hugePagesSize = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.memory.hugePagesSize
          MetricNamespace: !Ref Namespace
          MetricName: "MemoryHugePagesSize"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  MemoryFree: 
    Condition: MemoryOtherCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.memory.free = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.memory.free
          MetricNamespace: !Ref Namespace
          MetricName: "MemoryFree"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  MemoryHugePagesTotal: 
    Condition: MemoryOtherCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.memory.hugePagesTotal = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.memory.hugePagesTotal
          MetricNamespace: !Ref Namespace
          MetricName: "MemoryHugePagesTotal"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  MemoryInactive: 
    Condition: MemoryOtherCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.memory.inactive = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.memory.inactive
          MetricNamespace: !Ref Namespace
          MetricName: "MemoryInactive"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  MemoryPageTables: 
    Condition: MemoryOtherCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.memory.pageTables = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.memory.pageTables
          MetricNamespace: !Ref Namespace
          MetricName: "MemoryPageTables"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  MemoryDirty: 
    Condition: MemoryOtherCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.memory.dirty = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.memory.dirty
          MetricNamespace: !Ref Namespace
          MetricName: "MemoryDirty"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  MemoryMapped: 
    Condition: MemoryOtherCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.memory.mapped = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.memory.mapped
          MetricNamespace: !Ref Namespace
          MetricName: "MemoryMapped"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  MemoryActive: 
    Condition: MemoryOtherCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.memory.active = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.memory.active
          MetricNamespace: !Ref Namespace
          MetricName: "MemoryActive"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  MemoryTotal: 
    Condition: MemoryOtherCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.memory.total = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.memory.total
          MetricNamespace: !Ref Namespace
          MetricName: "MemoryTotal"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  MemorySlab: 
    Condition: MemoryOtherCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.memory.slab = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.memory.slab
          MetricNamespace: !Ref Namespace
          MetricName: "MemorySlab"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  MemoryBuffers: 
    Condition: MemoryOtherCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.memory.buffers = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.memory.buffers
          MetricNamespace: !Ref Namespace
          MetricName: "MemoryBuffer"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  SystemHandles: 
    Condition: SystemCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.system.handles = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.system.handles
          MetricNamespace: !Ref Namespace
          MetricName: "SystemHandles"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  SystemThreads: 
    Condition: SystemCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.system.threads = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.system.threads
          MetricNamespace: !Ref Namespace
          MetricName: "SystemThreads"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  SystemProcesses: 
    Condition: SystemCondition
    Type: AWS::Logs::MetricFilter
    Properties: 
      LogGroupName: RDSOSMetrics
      FilterPattern: !Sub 
        - "{ $.system.processes = \"*\"  &&  $.instanceID = ${InstanceID} }"
        -  InstanceID:  !Ref DataBaseinstanceID
      MetricTransformations: 
        - 
          MetricValue: $.system.processes
          MetricNamespace: !Ref Namespace
          MetricName: "SystemProcesses"
          Dimensions: 
            - Key: instanceID
              Value: $.instanceID
  OtherEngineTypeDashboard:
    Type: AWS::CloudWatch::Dashboard
    Condition: OtherEngineCondition
    Properties:
      DashboardName: !Ref DashBoard
      DashboardBody: !Sub
        - |
          {
                "widgets": [
              {
                  "height": 6,
                  "width": 6,
                  "y": 0,
                  "x": 0,
                  "type": "metric",
                  "properties": {
                      "metrics": [
                          [ { "expression": "SEARCH('{${namespace}, instanceID} instanceID=${database} CPU', 'Average')", "label": "CPUUtilization metrics", "id": "e1" } ]
                      ],
                      "view": "timeSeries",
                      "stacked": false,
                      "region": "${AWS::Region}",
                      "stat": "Average",
                      "period": 300,
                      "title": "CPUUtilization metrics"
                  }
              },
              {
                  "height": 6,
                  "width": 6,
                  "y": 0,
                  "x": 6,
                  "type": "metric",
                  "properties": {
                      "metrics": [
                          [ { "expression": "SEARCH('{${namespace}, instanceID} instanceID=${database} Memory', 'Average')", "label": "Memory metrics", "id": "e1", "region": "${AWS::Region}" } ]
                      ],
                      "view": "timeSeries",
                      "stacked": false,
                      "region": "${AWS::Region}",
                      "stat": "Average",
                      "period": 300,
                      "title": "Memory metrics"
                  }
              },
              {
                  "height": 6,
                  "width": 6,
                  "y": 0,
                  "x": 12,
                  "type": "metric",
                  "properties": {
                      "metrics": [
                          [ { "expression": "SEARCH('{${namespace}, instanceID} instanceID=${database} Load', 'Average')", "label": "Load Average metrics", "id": "e1", "region": "${AWS::Region}" } ]
                      ],
                      "view": "timeSeries",
                      "stacked": false,
                      "region": "${AWS::Region}",
                      "stat": "Average",
                      "period": 300,
                      "title": "Load Average metrics"
                  }
              },
              {
                  "height": 6,
                  "width": 6,
                  "y": 0,
                  "x": 18,
                  "type": "metric",
                  "properties": {
                      "metrics": [
                          [ { "expression": "SEARCH('{${namespace}, instanceID} instanceID=${database} Swap', 'Average')", "label": "Swap metrics", "id": "e1", "region": "${AWS::Region}" } ]
                      ],
                      "view": "timeSeries",
                      "stacked": false,
                      "region": "${AWS::Region}",
                      "stat": "Average",
                      "period": 300,
                      "title": "Swap metrics"
                  }
              },
              {
                  "type": "metric",
                  "x": 0,
                  "y": 6,
                  "width": 6,
                  "height": 6,
                  "properties": {
                      "metrics": [
                          [ { "expression": "SEARCH('{${namespace}, instanceID} instanceID=${database} Tasks', 'Average')", "label": "Processes metrics", "id": "e1", "region": "${AWS::Region}" } ]
                      ],
                      "view": "timeSeries",
                      "stacked": false,
                      "region": "${AWS::Region}",
                      "stat": "Average",
                      "period": 300,
                      "title": "Tasks metrics"
                  }
              }
                ]
              }  
        - namespace: !Ref Namespace
          database: !Ref DataBaseinstanceID
  SQLEngineTypeDashboard:
    Type: AWS::CloudWatch::Dashboard
    Condition: SQLEngineCondition
    Properties:
      DashboardName: !Ref DashBoard
      DashboardBody: !Sub
        - |
          {
              "widgets": [
               {
            "height": 6,
            "width": 6,
            "y": 0,
            "x": 0,
            "type": "metric",
            "properties": {
                "metrics": [
                    [ { "expression": "SEARCH('{${namespace}, instanceID} instanceID=${database} Memory', 'Average')", "label": "Memory metrics", "id": "e1", "period": 60, "region": "${AWS::Region}" } ]
                ],
                "view": "timeSeries",
                "stacked": false,
                "region": "${AWS::Region}",
                "stat": "Average",
                "period": 60,
                "title": "Memory metrics"
            }
            },
           {
            "height": 6,
            "width": 6,
            "y": 0,
            "x": 6,
            "type": "metric",
            "properties": {
                "metrics": [
                    [ { "expression": "SEARCH('{${namespace}, instanceID} instanceID=${database} CPU', 'Average')", "label": "CPUUtilization metrics", "id": "e1", "period": 60 } ]
                ],
                "view": "timeSeries",
                "stacked": false,
                "region": "${AWS::Region}",
                "stat": "Average",
                "period": 60,
                "title": "CPUUtilization metrics"
            }
           },
           {
            "height": 6,
            "width": 6,
            "y": 0,
            "x": 12,
            "type": "metric",
            "properties": {
                "metrics": [
                    [ { "expression": "SEARCH('{${namespace}, instanceID} instanceID=${database} System', 'Average')", "label": "Processes metrics", "id": "e1", "period": 60, "region": "${AWS::Region}" } ]
                ],
                "view": "timeSeries",
                "stacked": false,
                "region": "${AWS::Region}",
                "stat": "Average",
                "period": 60,
                "title": "Processes metrics"
            }
          }
           ]
            }  
        - namespace: !Ref Namespace
          database: !Ref DataBaseinstanceID