# you can disable an alarm notification by setting the 'to' line to: silent

# --- Availability ---

 template: am_sql_database_availability
       on: azure_monitor.sql_database.availability
    class: Availability
     type: Database
component: Azure SQL
   lookup: average -10m unaligned of average
    units: percentage
    every: 1m
     warn: $this < (($status >= $WARNING)  ? (99.99) : (99.9))
     crit: $this < (($status == $CRITICAL) ? (99.9)  : (99))
    delay: down 5m multiplier 1.5 max 1h
  summary: SQL Database availability on ${label:resource_name}
     info: Database availability of Azure SQL Database ${label:resource_name} \
           in ${label:resource_group} (${label:region})
       to: dba

# --- CPU Utilization ---

 template: am_sql_database_cpu
       on: azure_monitor.sql_database.cpu
    class: Utilization
     type: Database
component: Azure SQL
   lookup: average -5m unaligned of average
    units: percentage
    every: 1m
     warn: $this > (($status >= $WARNING)  ? (75) : (85))
     crit: $this > (($status == $CRITICAL) ? (85) : (95))
    delay: down 5m multiplier 1.5 max 1h
  summary: SQL Database CPU on ${label:resource_name}
     info: Average CPU utilization of Azure SQL Database ${label:resource_name} \
           in ${label:resource_group} (${label:region})
       to: dba

 template: am_sql_database_instance_cpu
       on: azure_monitor.sql_database.instance_cpu
    class: Utilization
     type: Database
component: Azure SQL
   lookup: average -5m unaligned of average
    units: percentage
    every: 1m
     warn: $this > (($status >= $WARNING)  ? (75) : (85))
     crit: $this > (($status == $CRITICAL) ? (85) : (95))
    delay: down 5m multiplier 1.5 max 1h
  summary: SQL Database instance CPU on ${label:resource_name}
     info: SQL process CPU utilization (including background tasks) on Azure SQL Database ${label:resource_name} \
           in ${label:resource_group} (${label:region})
       to: dba

# --- Memory Utilization ---

 template: am_sql_database_instance_memory
       on: azure_monitor.sql_database.instance_memory
    class: Utilization
     type: Database
component: Azure SQL
   lookup: average -5m unaligned of average
    units: percentage
    every: 1m
     warn: $this > (($status >= $WARNING)  ? (80) : (90))
     crit: $this > (($status == $CRITICAL) ? (90) : (95))
    delay: down 5m multiplier 1.5 max 1h
  summary: SQL Database instance memory on ${label:resource_name}
     info: SQL process memory utilization on Azure SQL Database ${label:resource_name} \
           in ${label:resource_group} (${label:region})
       to: dba

# --- DTU Consumption ---

 template: am_sql_database_dtu_consumption
       on: azure_monitor.sql_database.dtu_consumption
    class: Utilization
     type: Database
component: Azure SQL
   lookup: average -5m unaligned of average
    units: percentage
    every: 1m
     warn: $this != nan AND $this > (($status >= $WARNING)  ? (80) : (90))
     crit: $this != nan AND $this > (($status == $CRITICAL) ? (90) : (95))
    delay: down 5m multiplier 1.5 max 1h
  summary: SQL Database DTU consumption on ${label:resource_name}
     info: DTU consumption on Azure SQL Database ${label:resource_name} \
           in ${label:resource_group} (${label:region}). \
           Only relevant for DTU-based service tiers
       to: dba

# --- I/O Utilization ---

 template: am_sql_database_data_io
       on: azure_monitor.sql_database.io_utilization
    class: Utilization
     type: Database
component: Azure SQL
   lookup: average -5m unaligned of data_read
    units: percentage
    every: 1m
     warn: $this > (($status >= $WARNING)  ? (80) : (90))
     crit: $this > (($status == $CRITICAL) ? (90) : (95))
    delay: down 5m multiplier 1.5 max 1h
  summary: SQL Database data I/O on ${label:resource_name}
     info: Physical data read I/O utilization on Azure SQL Database ${label:resource_name} \
           in ${label:resource_group} (${label:region})
       to: dba

 template: am_sql_database_log_write
       on: azure_monitor.sql_database.io_utilization
    class: Utilization
     type: Database
component: Azure SQL
   lookup: average -5m unaligned of log_write
    units: percentage
    every: 1m
     warn: $this > (($status >= $WARNING)  ? (80) : (90))
     crit: $this > (($status == $CRITICAL) ? (90) : (95))
    delay: down 5m multiplier 1.5 max 1h
  summary: SQL Database log write I/O on ${label:resource_name}
     info: Transaction log write I/O utilization on Azure SQL Database ${label:resource_name} \
           in ${label:resource_group} (${label:region})
       to: dba

# --- Resource Limits (Workers / Sessions) ---

 template: am_sql_database_workers
       on: azure_monitor.sql_database.resource_utilization
    class: Utilization
     type: Database
component: Azure SQL
   lookup: average -5m unaligned of workers
    units: percentage
    every: 1m
     warn: $this > (($status >= $WARNING)  ? (70) : (80))
     crit: $this > (($status == $CRITICAL) ? (80) : (90))
    delay: down 5m multiplier 1.5 max 1h
  summary: SQL Database worker utilization on ${label:resource_name}
     info: Worker thread utilization on Azure SQL Database ${label:resource_name} \
           in ${label:resource_group} (${label:region}). \
           Exhausting workers causes query failures
       to: dba

 template: am_sql_database_sessions
       on: azure_monitor.sql_database.resource_utilization
    class: Utilization
     type: Database
component: Azure SQL
   lookup: average -5m unaligned of sessions
    units: percentage
    every: 1m
     warn: $this > (($status >= $WARNING)  ? (70) : (80))
     crit: $this > (($status == $CRITICAL) ? (80) : (90))
    delay: down 5m multiplier 1.5 max 1h
  summary: SQL Database session utilization on ${label:resource_name}
     info: Session utilization on Azure SQL Database ${label:resource_name} \
           in ${label:resource_group} (${label:region}). \
           Exhausting sessions prevents new connections
       to: dba

# --- Connection Errors ---

 template: am_sql_database_connection_failures
       on: azure_monitor.sql_database.connections
    class: Errors
     type: Database
component: Azure SQL
   lookup: average -5m unaligned of failed_system
    units: connections/s
    every: 1m
     warn: $this > (($status >= $WARNING) ? (1) : (5))
    delay: down 5m multiplier 1.5 max 1h
  summary: SQL Database system connection failures on ${label:resource_name}
     info: Rate of system-caused connection failures on Azure SQL Database ${label:resource_name} \
           in ${label:resource_group} (${label:region})
       to: dba

 template: am_sql_database_firewall_blocks
       on: azure_monitor.sql_database.connections
    class: Errors
     type: Database
component: Azure SQL
   lookup: average -5m unaligned of firewall_blocked
    units: connections/s
    every: 1m
     warn: $this > (($status >= $WARNING) ? (1) : (5))
    delay: down 5m multiplier 1.5 max 1h
  summary: SQL Database firewall blocks on ${label:resource_name}
     info: Rate of connections blocked by firewall on Azure SQL Database ${label:resource_name} \
           in ${label:resource_group} (${label:region}). \
           May indicate misconfigured firewall rules or unauthorized access attempts
       to: dba

# --- Deadlocks ---

 template: am_sql_database_deadlocks
       on: azure_monitor.sql_database.deadlocks
    class: Errors
     type: Database
component: Azure SQL
   lookup: average -5m unaligned of total
    units: deadlocks/s
    every: 1m
     warn: $this > (($status >= $WARNING) ? (0) : (1))
    delay: down 5m multiplier 1.5 max 1h
  summary: SQL Database deadlocks on ${label:resource_name}
     info: Deadlock rate on Azure SQL Database ${label:resource_name} \
           in ${label:resource_group} (${label:region})
       to: dba

# --- Storage ---

 template: am_sql_database_storage_utilization
       on: azure_monitor.sql_database.storage_utilization
    class: Utilization
     type: Database
component: Azure SQL
   lookup: average -5m unaligned of average
    units: percentage
    every: 1m
     warn: $this > (($status >= $WARNING)  ? (75) : (85))
     crit: $this > (($status == $CRITICAL) ? (85) : (95))
    delay: down 5m multiplier 1.5 max 1h
  summary: SQL Database storage utilization on ${label:resource_name}
     info: Data storage utilization on Azure SQL Database ${label:resource_name} \
           in ${label:resource_group} (${label:region})
       to: dba

# --- In-Memory OLTP ---

 template: am_sql_database_xtp_storage
       on: azure_monitor.sql_database.xtp_storage
    class: Utilization
     type: Database
component: Azure SQL
   lookup: average -5m unaligned of average
    units: percentage
    every: 1m
     warn: $this != nan AND $this > (($status >= $WARNING)  ? (70) : (80))
     crit: $this != nan AND $this > (($status == $CRITICAL) ? (80) : (90))
    delay: down 5m multiplier 1.5 max 1h
  summary: SQL Database In-Memory OLTP storage on ${label:resource_name}
     info: In-Memory OLTP storage utilization on Azure SQL Database ${label:resource_name} \
           in ${label:resource_group} (${label:region}). \
           Only relevant when In-Memory OLTP is enabled
       to: dba

# --- Tempdb ---

 template: am_sql_database_tempdb_log_utilization
       on: azure_monitor.sql_database.tempdb_log_utilization
    class: Utilization
     type: Database
component: Azure SQL
   lookup: average -5m unaligned of average
    units: percentage
    every: 1m
     warn: $this > (($status >= $WARNING)  ? (70) : (80))
     crit: $this > (($status == $CRITICAL) ? (80) : (90))
    delay: down 5m multiplier 1.5 max 1h
  summary: SQL Database tempdb log utilization on ${label:resource_name}
     info: Tempdb transaction log utilization on Azure SQL Database ${label:resource_name} \
           in ${label:resource_group} (${label:region}). \
           High utilization may indicate long-running transactions or excessive version store usage
       to: dba

# --- Replication ---

 template: am_sql_database_replication_lag
       on: azure_monitor.sql_database.replication_lag
    class: Latency
     type: Database
component: Azure SQL
   lookup: average -5m unaligned of average
    units: seconds
    every: 1m
     warn: $this != nan AND $this > (($status >= $WARNING)  ? (5) : (10))
     crit: $this != nan AND $this > (($status == $CRITICAL) ? (10) : (30))
    delay: down 5m multiplier 1.5 max 1h
  summary: SQL Database replication lag on ${label:resource_name}
     info: Geo-replication lag on Azure SQL Database ${label:resource_name} \
           in ${label:resource_group} (${label:region}). \
           Only relevant when geo-replication is configured
       to: dba

# --- Serverless ---

 template: am_sql_database_serverless_cpu
       on: azure_monitor.sql_database.serverless_utilization
    class: Utilization
     type: Database
component: Azure SQL
   lookup: average -5m unaligned of cpu
    units: percentage
    every: 1m
     warn: $this != nan AND $this > (($status >= $WARNING)  ? (75) : (85))
     crit: $this != nan AND $this > (($status == $CRITICAL) ? (85) : (95))
    delay: down 5m multiplier 1.5 max 1h
  summary: SQL Database serverless CPU on ${label:resource_name}
     info: App-level CPU utilization on serverless Azure SQL Database ${label:resource_name} \
           in ${label:resource_group} (${label:region}). \
           Only relevant for serverless tier databases
       to: dba

 template: am_sql_database_serverless_memory
       on: azure_monitor.sql_database.serverless_utilization
    class: Utilization
     type: Database
component: Azure SQL
   lookup: average -5m unaligned of memory
    units: percentage
    every: 1m
     warn: $this != nan AND $this > (($status >= $WARNING)  ? (80) : (90))
     crit: $this != nan AND $this > (($status == $CRITICAL) ? (90) : (95))
    delay: down 5m multiplier 1.5 max 1h
  summary: SQL Database serverless memory on ${label:resource_name}
     info: App-level memory utilization on serverless Azure SQL Database ${label:resource_name} \
           in ${label:resource_group} (${label:region}). \
           Only relevant for serverless tier databases
       to: dba

# --- Ledger ---

 template: am_sql_database_ledger_digest_failures
       on: azure_monitor.sql_database.ledger_digest
    class: Errors
     type: Database
component: Azure SQL
   lookup: average -5m unaligned of failed
    units: events/s
    every: 1m
     warn: $this != nan AND $this > 0
    delay: down 5m multiplier 1.5 max 1h
  summary: SQL Database ledger digest failures on ${label:resource_name}
     info: Failed ledger digest uploads on Azure SQL Database ${label:resource_name} \
           in ${label:resource_group} (${label:region}). \
           Only relevant when database ledger is enabled
       to: dba
