###############################################################################
#                            GENERAL
###############################################################################

[agent]
  ## Global collection frequency
  interval = "30s"
  ## How often to flush data to outputs
  flush_interval = "30s"
  ## Random jitter to avoid all agents syncing at once
  collection_jitter = "2s"
  flush_jitter = "2s"
  ## Precision of timestamps
  precision = "1s"
  ## Enable for more verbose logging
  # debug = true
  ## If true, processors do not run again after aggregators. Default is false.
  skip_processors_after_aggregators=true

###############################################################################
#                            SYSTEM
###############################################################################

# CPU usage (user, system, idle)
# [[inputs.cpu]]
#   percpu = false
#   totalcpu = true
#   fielddrop = ["usage_nice", "usage_iowait", "usage_irq", "usage_softirq", "usage_steal", "usage_guest", "usage_guest_nice"]
#   interval = "30s"

# Memory (used, free)
# [[inputs.mem]]
#   fieldpass = ["used", "free", "used_percent"]
#   interval = "30s"

# Swap (used, free)
# [[inputs.swap]]
#   fieldpass = ["used", "free", "used_percent"]
#   interval = "30s"

# Disk usage (like df)
# [[inputs.disk]]
#   mount_points = ["/media/docker"]
#   fieldpass = ["used", "free", "used_percent"]
#   ignore_fs = ["tmpfs", "devtmpfs"]
#   interval = "30s"

# Disk I/O
# [[inputs.diskio]]
#   devices = ["sda", "sdb", "nvme0n1"]
#   fieldpass = ["reads", "writes", "read_bytes", "write_bytes"]
#   interval = "30s"

# Load and uptime
# [[inputs.system]]
#   fieldpass = ["load1", "load5", "load15", "uptime"]
#   interval = "1m"

###############################################################################
#                            MQTT
###############################################################################

[[inputs.mqtt_consumer]]
  servers     = ["tcp://host:1883"]
  username    = "${MQTT_USER}"
  password    = "${MQTT_PASS}"
  topics      = [
    "borgmatic/stats/#",
  ]
  data_format = "value"
  data_type = "string"
  name_override = "borgmatic_stats"

[[inputs.mqtt_consumer]]
  servers     = ["tcp://host:1883"]
  username    = "${MQTT_USER}"
  password    = "${MQTT_PASS}"
  topics      = [
    "borgmatic/split/#"
  ]
  data_format = "value"
  data_type = "string"
  name_override = "borgmatic_split"

###############################################################################
#                            DATABASES
###############################################################################

[[inputs.postgresql_extensible]]
  address = "host=host port=11111 user=postgres password=postgres dbname=postgres sslmode=disable"
  interval = "1m"

  [[inputs.postgresql_extensible.query]]
    sqlquery = """
      SELECT
        datname,
        pg_database_size(datname) AS size_bytes
      FROM pg_database WHERE datname IN ('dagster', 'gitea', 'teslamate')
    """
    measurement = "postgres_db_size"

  [[inputs.postgresql_extensible.query]]
    sqlquery = """
      SELECT
          schemaname AS schema_name,
          tablename AS table_name,
          pg_total_relation_size(schemaname || '.' || tablename) AS size_bytes
      FROM pg_tables
      WHERE schemaname NOT IN ('pg_catalog', 'information_schema') AND schemaname NOT LIKE '_timescaledb_%'
    """
    measurement = "postgres_table_size"

[[inputs.postgresql_extensible]]
  address = "host=${DATABASE_HOST} port=${DATABASE_PORT} user=${DATABASE_USER} password=${DATABASE_PASS} dbname=${DATABASE_NAME} sslmode=disable"
  interval = "1m"

  [[inputs.postgresql_extensible.query]]
    sqlquery = """
      SELECT
        datname,
        pg_database_size(datname) AS size_bytes
      FROM pg_database WHERE datname IN ('homeassistant', 'fluentbit', 'telegraf')
    """
    measurement = "postgres_db_size"

  [[inputs.postgresql_extensible.query]]
    sqlquery = """
      SELECT
          schemaname AS schema_name,
          tablename AS table_name,
          pg_total_relation_size(schemaname || '.' || tablename) AS size_bytes
      FROM pg_tables
      WHERE schemaname NOT IN ('pg_catalog', 'information_schema') AND schemaname NOT LIKE '_timescaledb_%'
    """
    measurement = "postgres_table_size"

###############################################################################
#                            OUTPUTS
###############################################################################

[[outputs.sql]]
  driver = "pgx"
  data_source_name = "host=${DATABASE_HOST} port=${DATABASE_PORT} user=${DATABASE_USER} password=${DATABASE_PASS} dbname=${DATABASE_NAME} sslmode=disable"

  # Only include specific measurements
  # include_metrics = ["cpu", "mem", "swap", "disk", "diskio", "system"]

  [outputs.sql.convert]
    conversion_style     = "literal"
    integer              = "BIGINT"
    real                 = "DOUBLE PRECISION"
    text                 = "TEXT"
    timestamp            = "TIMESTAMPTZ"
    defaultvalue         = "TEXT"
    bool                 = "BOOLEAN"
    unsigned             = "BIGINT"

[[outputs.loki]]
  domain = "http://host:3100"
  timeout = "10s"
  # TODO: configure which metrics to include

[[outputs.mqtt]]
  servers     = ["tcp://host:1883"]
  username    = "${MQTT_USER}"
  password    = "${MQTT_PASS}"
  topic       = "telegraf/metrics"
  qos         = 0
  retain      = false
  client_id   = "telegraf"
  data_format = "json"
  # TODO: configure which metrics to include
