zabbix_export:
  version: '7.4'
  template_groups:
    - uuid: 748ad4d098d447d492bb935c907f652f
      name: Templates/Databases
  templates:
    - uuid: a1d5f8c3b2e44a7c9d6b1f2e8a3c5b4d
      template: 'PostgreSQL Partitioning by Zabbix Agent 2'
      name: 'PostgreSQL Partitioning by Zabbix Agent 2'
      description: 'Monitors the custom partitions.monitoring view via the native Zabbix Agent 2 PostgreSQL plugin. Using a single master to minimize the DB connections and load.'
      vendor:
        name: Zabbix Support
        version: 7.4-0 
      groups:
        - name: Templates/Databases
      items:
        - uuid: b8c7d6e5f4a34b2c8d2e3f4a5b6c7d8e
          name: 'PostgreSQL: Get Partitioning Data'
          key: 'pgsql.custom.query["{$PG.CONNSTRING.AGENT2}",,,"{$PG.DBNAME}","partitions.get_all"]'
          history: '0'
          value_type: TEXT
          description: 'Master item that queries all partition statistics in a single bulk JSON sequence.'
          tags:
            - tag: component
              value: raw
      discovery_rules:
        - uuid: b7c2a5d8f1e44b9c8a3f6d2e1c5b4a7d
          name: 'Partitioned Tables Discovery'
          type: DEPENDENT
          key: db.partitions.discovery.dependent
          item_prototypes:
            - uuid: f1a2b3c4d5e64f7a9b8c7d6e5f4a3b2c
              name: '{#TABLE_NAME}: Time Since Last Maintenance'
              type: DEPENDENT
              key: 'db.partitions.age["{#TABLE_NAME}"]'
              units: s
              preprocessing:
                - type: JSONPATH
                  parameters:
                    - '$.[?(@.table_name == "{#TABLE_NAME}")].age_seconds.first()'
              master_item:
                key: 'pgsql.custom.query["{$PG.CONNSTRING.AGENT2}",,,"{$PG.DBNAME}","partitions.get_all"]'
              tags:
                - tag: metric
                  value: age
                - tag: table
                  value: '{#TABLE_NAME}'
              trigger_prototypes:
                - uuid: a9b8c7d6e5f44a3b8c1d2e3f4a5b6c7d
                  expression: 'last(/PostgreSQL Partitioning by Zabbix Agent 2/db.partitions.age["{#TABLE_NAME}"])>{$PARTITIONS.AGE}'
                  name: 'Table {#TABLE_NAME}: Maintenance script has not run successfully in over 48 hours'
                  priority: WARNING
            - uuid: c4b9e2a5f1d84c7a9f3b6d1e5a2c8b4d
              name: '{#TABLE_NAME}: Future Partitions Buffer'
              type: DEPENDENT
              key: 'db.partitions.future["{#TABLE_NAME}"]'
              preprocessing:
                - type: JSONPATH
                  parameters:
                    - '$.[?(@.table_name == "{#TABLE_NAME}")].future_partitions.first()'
              master_item:
                key: 'pgsql.custom.query["{$PG.CONNSTRING.AGENT2}",,,"{$PG.DBNAME}","partitions.get_all"]'
              tags:
                - tag: metric
                  value: partitions
                - tag: table
                  value: '{#TABLE_NAME}'
              trigger_prototypes:
                - uuid: d6e3a5c8b2f14d9e8a7b6c5d4e3f2a1b
                  expression: 'last(/PostgreSQL Partitioning by Zabbix Agent 2/db.partitions.future["{#TABLE_NAME}"])<{$PARTITIONS.LOW}'
                  name: 'Table {#TABLE_NAME}: Future partitions buffer is critically low (< 2)'
                  priority: HIGH
            - uuid: e8f2a1b3c4d54e6f9a8b7c6d5e4f3a2b
              name: '{#TABLE_NAME}: Total Size Bytes'
              type: DEPENDENT
              key: 'db.partitions.size["{#TABLE_NAME}"]'
              units: B
              preprocessing:
                - type: JSONPATH
                  parameters:
                    - '$.[?(@.table_name == "{#TABLE_NAME}")].total_size_bytes.first()'
              master_item:
                key: 'pgsql.custom.query["{$PG.CONNSTRING.AGENT2}",,,"{$PG.DBNAME}","partitions.get_all"]'
              tags:
                - tag: metric
                  value: size
                - tag: table
                  value: '{#TABLE_NAME}'
          master_item:
            key: 'pgsql.custom.query["{$PG.CONNSTRING.AGENT2}",,,"{$PG.DBNAME}","partitions.get_all"]'
          lld_macro_paths:
            - lld_macro: '{#TABLE_NAME}'
              path: $.table_name
      macros:
        - macro: '{$PARTITIONS.AGE}'
          value: 24h
          description: 'The maximum period during which no new partitions may be created'
        - macro: '{$PARTITIONS.LOW}'
          value: '2'
          description: 'The minimum number of partitions that must exist in the future'
        - macro: '{$PG.CONNSTRING.AGENT2}'
          value: AWS_RDS
          description: 'Session name or URI of the PostgreSQL instance'
        - macro: '{$PG.DBNAME}'
          value: zabbix
