groups:
  - name: pbs_backups
    interval: 120s
    rules:
      - alert: PBSBackupStale
        # Exclude destroyed/non-existent guests: 200 (never existed in PVE), 207, 208 (destroyed 2026-04-10)
        expr: >
          (time() - pbs_backup_last_success_timestamp{guest!~"200|207|208"}) > 93600
          and pbs_backup_last_success_timestamp{guest!~"200|207|208"} > 0
        for: 5m
        labels:
          severity: high
        annotations:
          summary: "PBS backup for guest {{ $labels.guest }} older than 26 hours"
          description: "Last successful backup for guest {{ $labels.guest }} on datastore {{ $labels.datastore }} was {{ with printf `(time() - pbs_backup_last_success_timestamp{datastore=\"%s\",guest=\"%s\"})` $labels.datastore $labels.guest | query }}{{ . | first | value | humanizeDuration }}{{ end }} ago. Daily backup schedule may have failed."

      - alert: PBSBackupMissing
        # Exclude destroyed/non-existent guests: 200 (never existed in PVE), 207, 208 (destroyed 2026-04-10)
        expr: >
          (time() - pbs_backup_last_success_timestamp{guest!~"200|207|208"}) > 172800
          and pbs_backup_last_success_timestamp{guest!~"200|207|208"} > 0
        for: 5m
        labels:
          severity: critical
        annotations:
          summary: "PBS backup for guest {{ $labels.guest }} missing for > 48 hours"
          description: "No successful backup for guest {{ $labels.guest }} on {{ $labels.datastore }} in over 48 hours. Investigate PBS task logs and storage availability."

      - alert: PBSVerifyFailure
        expr: >
          pbs_verify_errors_total > 0
          and (time() - pbs_verify_last_success_timestamp > 86400
               or pbs_verify_last_success_timestamp == 0)
        for: 10m
        labels:
          severity: high
        annotations:
          summary: "PBS verify errors on datastore {{ $labels.datastore }}"
          description: "Verify errors detected on datastore {{ $labels.datastore }} with no successful verify in 24 hours. Check PBS task logs for affected snapshots."

      - alert: PBSSyncStale
        expr: >
          (time() - pbs_sync_last_success_timestamp) > 93600
          and pbs_sync_last_success_timestamp > 0
        for: 10m
        labels:
          severity: high
        annotations:
          summary: "PBS sync {{ $labels.source }} -> {{ $labels.target }} stale > 26h"
          description: "Sync from {{ $labels.source }} to {{ $labels.target }} has not succeeded in over 26 hours. Daily sync runs at 03:00 — check PBS task logs and iSCSI connectivity."

      - alert: PBSSyncTaskError
        expr: >
          pbs_backup_task_status{task_type="sync", status="error"}
          > ignoring(status)
          pbs_backup_task_status{task_type="sync", status="ok"}
        for: 5m
        labels:
          severity: high
        annotations:
          summary: "PBS sync task errors exceed successes on {{ $labels.datastore }}"
          description: "Sync error count exceeds success count in recent task history on {{ $labels.datastore }}. Check PBS task logs — pbs-backup-1 may not be receiving updates."
