apiVersion: 1

# ---------- Backups (Proxmox Backup Server) — uses contact point + policy from alerting.yaml ----------
groups:
  - orgId: 1
    name: backups
    folder: Homelab Alerts
    interval: 5m
    rules:

      - uid: pbs-backup-overdue
        title: Backup overdue
        condition: C
        for: 10m
        noDataState: OK
        execErrState: Error
        labels: {severity: critical}
        annotations:
          summary: 'Last backup of {{ $labels.vm_name }} ({{ $labels.vm_id }}) is {{ printf "%.0f" $values.A.Value }} hours old - the daily backup failed or was skipped'
        data:
          - refId: A
            relativeTimeRange: {from: 600, to: 0}
            datasourceUid: prometheus
            model: {refId: A, instant: true, expr: '(time() - pbs_snapshot_vm_last_timestamp) / 3600 and on(vm_id) max by (vm_id) (label_replace(pve_guest_info{template!="1"}, "vm_id", "$1", "id", "(?:qemu|lxc)/([0-9]+)"))'}
          - refId: C
            datasourceUid: __expr__
            model:
              refId: C
              type: threshold
              expression: A
              conditions: [{evaluator: {type: gt, params: [30]}}]

      - uid: pbs-datastore-full
        title: Backup datastore above 80%
        condition: C
        for: 30m
        noDataState: OK
        execErrState: Error
        labels: {severity: warning}
        annotations:
          summary: 'PBS datastore {{ $labels.datastore }} is {{ printf "%.0f" $values.A.Value }}% full'
        data:
          - refId: A
            relativeTimeRange: {from: 600, to: 0}
            datasourceUid: prometheus
            model: {refId: A, instant: true, expr: '100 * pbs_used / pbs_size'}
          - refId: C
            datasourceUid: __expr__
            model:
              refId: C
              type: threshold
              expression: A
              conditions: [{evaluator: {type: gt, params: [80]}}]

      - uid: pbs-not-answering
        title: Backup server not answering
        condition: C
        for: 15m
        noDataState: OK
        execErrState: Error
        labels: {severity: critical}
        annotations:
          summary: 'Proxmox Backup Server does not answer the monitoring - backups may fail'
        data:
          - refId: A
            relativeTimeRange: {from: 600, to: 0}
            datasourceUid: prometheus
            model: {refId: A, instant: true, expr: 'pbs_up'}
          - refId: C
            datasourceUid: __expr__
            model:
              refId: C
              type: threshold
              expression: A
              conditions: [{evaluator: {type: lt, params: [1]}}]
