{
  "$schema": "./.schemas/page.schema.json",
  "title": "Backups",
  "icon": "externaldrive.badge.timemachine",
  "sidebar": false,
  "sources": {
    "pve": {
      "auth": {
        "name": "Authorization",
        "type": "header"
      },
      "derive": [
        {
          "name": "host_status",
          "value": {
            "first": "status",
            "of": "resources.data",
            "where": {
              "equals": "node",
              "path": "type"
            }
          }
        },
        {
          "name": "up_days",
          "value": {
            "divide": [
              "node.data.uptime",
              86400
            ],
            "round": 0
          }
        },
        {
          "name": "cpu_pct",
          "value": {
            "path": "node.data.cpu",
            "round": 0,
            "scale": 100
          }
        },
        {
          "name": "mem_pct",
          "value": {
            "divide": [
              "node.data.memory.used",
              "node.data.memory.total"
            ],
            "round": 0,
            "scale": 100
          }
        },
        {
          "name": "mem_used_gb",
          "value": {
            "path": "node.data.memory.used",
            "round": 1,
            "scale": 1e-09
          }
        },
        {
          "name": "mem_total_gb",
          "value": {
            "path": "node.data.memory.total",
            "round": 0,
            "scale": 1e-09
          }
        },
        {
          "name": "guests",
          "value": {
            "each": "resources.data",
            "fields": {
              "cpu_pct": {
                "path": "cpu",
                "round": 1,
                "scale": 100
              },
              "max_gb": {
                "path": "maxmem",
                "round": 0,
                "scale": 1e-09
              },
              "mem_gb": {
                "path": "mem",
                "round": 1,
                "scale": 1e-09
              },
              "name": "name",
              "status": "status",
              "tags": "tags",
              "up_days": {
                "divide": [
                  "uptime",
                  86400
                ],
                "round": 0
              },
              "vmid": "vmid"
            },
            "sort": "vmid",
            "where": {
              "equals": "qemu",
              "path": "type"
            }
          }
        },
        {
          "name": "running",
          "value": {
            "count": "guests",
            "where": {
              "equals": "running",
              "path": "status"
            }
          }
        },
        {
          "name": "guest_count",
          "value": {
            "count": "guests"
          }
        },
        {
          "name": "guest_rows",
          "value": {
            "each": "guests",
            "fields": {
              "cpu": {
                "else": {
                  "template": "—"
                },
                "if": {
                  "equals": "running",
                  "path": "status"
                },
                "then": {
                  "template": "{cpu_pct}%"
                }
              },
              "memory": {
                "else": {
                  "template": "{max_gb} GB given"
                },
                "if": {
                  "equals": "running",
                  "path": "status"
                },
                "then": {
                  "template": "{mem_gb} of {max_gb} GB"
                }
              },
              "name": "name",
              "state": {
                "else": {
                  "template": "Stopped"
                },
                "if": {
                  "equals": "running",
                  "path": "status"
                },
                "then": {
                  "template": "Running"
                }
              },
              "up": {
                "else": {
                  "template": "—"
                },
                "if": {
                  "equals": "running",
                  "path": "status"
                },
                "then": {
                  "else": {
                    "template": "{up_days} days"
                  },
                  "if": {
                    "equals": 0,
                    "path": "up_days"
                  },
                  "then": {
                    "template": "Started today"
                  }
                }
              },
              "vmid": "vmid"
            }
          }
        }
      ],
      "every": "1m",
      "requests": {
        "node": "/api2/json/nodes/pve1/status",
        "resources": "/api2/json/cluster/resources",
        "time": "/api2/json/nodes/pve1/time",
        "version": "/api2/json/version"
      },
      "secret": "proxmox",
      "url": "https://proxmox.example.com:8006"
    },
    "pve-backup": {
      "auth": {
        "name": "Authorization",
        "type": "header"
      },
      "derive": [
        {
          "name": "backup_age",
          "value": {
            "subtract": [
              "time.data.time",
              "backup.data[0].endtime"
            ]
          }
        },
        {
          "name": "backup_min",
          "value": {
            "divide": [
              {
                "subtract": [
                  "backup.data[0].endtime",
                  "backup.data[0].starttime"
                ]
              },
              60
            ],
            "round": 0
          }
        },
        {
          "name": "backup_state",
          "value": {
            "else": {
              "template": "warning"
            },
            "if": {
              "all": [
                {
                  "equals": "OK",
                  "path": "backup.data[0].status"
                },
                {
                  "lt": 93600,
                  "path": "backup_age"
                }
              ]
            },
            "then": {
              "template": "ok"
            }
          }
        },
        {
          "name": "backing_up",
          "value": {
            "count": "active.data",
            "where": {
              "equals": "vzdump",
              "path": "type"
            }
          }
        }
      ],
      "every": "1m",
      "requests": {
        "active": "/api2/json/nodes/pve1/tasks?source=active",
        "backup": "/api2/json/nodes/pve1/tasks?typefilter=vzdump&limit=1",
        "time": "/api2/json/nodes/pve1/time"
      },
      "secret": "proxmox",
      "url": "https://proxmox.example.com:8006"
    },
    "pve-slow": {
      "auth": {
        "name": "Authorization",
        "type": "header"
      },
      "derive": [
        {
          "name": "unas_pct",
          "value": {
            "first": "used_fraction",
            "of": "storage.data",
            "round": 0,
            "scale": 100,
            "where": {
              "equals": "backup-nas",
              "path": "storage"
            }
          }
        },
        {
          "name": "lvm_pct",
          "value": {
            "first": "used_fraction",
            "of": "storage.data",
            "round": 0,
            "scale": 100,
            "where": {
              "equals": "local-lvm",
              "path": "storage"
            }
          }
        },
        {
          "name": "newest",
          "value": {
            "each": "backups.data",
            "fields": {
              "at": {
                "max": "ctime",
                "of": "rows"
              },
              "count": "count",
              "gb": {
                "max": "size",
                "of": "rows",
                "round": 1,
                "scale": 1e-09
              },
              "name": {
                "first": "notes",
                "of": "rows"
              },
              "vmid": "key"
            },
            "group": "vmid"
          }
        },
        {
          "name": "uncovered",
          "value": {
            "count": "notbacked.data"
          }
        }
      ],
      "every": "15m",
      "requests": {
        "backups": "/api2/json/nodes/pve1/storage/backup-nas/content?content=backup",
        "jobs": "/api2/json/cluster/backup",
        "notbacked": "/api2/json/cluster/backup-info/not-backed-up",
        "storage": "/api2/json/nodes/pve1/storage"
      },
      "secret": "proxmox",
      "url": "https://proxmox.example.com:8006"
    }
  },
  "cards": [
    {
      "id": "last",
      "label": "Last run",
      "source": {
        "map": {
          "delta": {
            "template": "{backup.data[0].status} · {backup.data[0].endtime|ago}"
          },
          "value": "backup_min"
        },
        "ref": "pve-backup"
      },
      "type": "stat",
      "unit": "min"
    },
    {
      "id": "schedule",
      "label": "Schedule",
      "source": {
        "map": {
          "delta": {
            "template": "Keep {jobs.data[0].prune-backups.keep-last} on {jobs.data[0].storage}"
          },
          "value": {
            "template": "{jobs.data[0].schedule}"
          }
        },
        "ref": "pve-slow"
      },
      "type": "stat"
    },
    {
      "bands": [
        {
          "state": "ok",
          "to": 80
        },
        {
          "from": 80,
          "state": "warning",
          "to": 92
        },
        {
          "from": 92,
          "state": "critical"
        }
      ],
      "id": "unas",
      "label": "backup-nas",
      "max": 100,
      "source": {
        "map": {
          "value": "unas_pct"
        },
        "ref": "pve-slow"
      },
      "type": "gauge",
      "unit": "%"
    },
    {
      "bands": [
        {
          "state": "ok",
          "to": 80
        },
        {
          "from": 80,
          "state": "warning",
          "to": 92
        },
        {
          "from": 92,
          "state": "critical"
        }
      ],
      "id": "lvm",
      "label": "local-lvm",
      "max": 100,
      "source": {
        "map": {
          "value": "lvm_pct"
        },
        "ref": "pve-slow"
      },
      "type": "gauge",
      "unit": "%"
    },
    {
      "id": "newest",
      "items": [],
      "label": "Newest backup of each guest",
      "source": {
        "map": {
          "items": {
            "each": "newest",
            "fields": {
              "detail": {
                "template": "{at|ago} · {gb} GB"
              },
              "text": "name"
            },
            "sort": "vmid"
          }
        },
        "ref": "pve-slow"
      },
      "type": "list"
    },
    {
      "id": "uncovered",
      "items": [],
      "label": "Not in a backup job",
      "source": {
        "empty": {
          "text": "Every guest is in a backup job.",
          "when": {
            "equals": 0,
            "path": "uncovered"
          }
        },
        "map": {
          "items": {
            "each": "notbacked.data",
            "fields": {
              "detail": {
                "template": "{vmid|raw}"
              },
              "text": "name"
            }
          }
        },
        "ref": "pve-slow"
      },
      "type": "list"
    }
  ]
}
