{
  "$schema": "./.schemas/page.schema.json",
  "title": "System",
  "icon": "server.rack",
  "sidebar": false,
  "params": {
    "id": {
      "example": "k3v9q2x7m1p8w4z",
      "pattern": "[a-z0-9]{15}"
    }
  },
  "title_from": {
    "ref": "sys",
    "value": {
      "first": "name",
      "of": "system.items"
    }
  },
  "sources": {
    "sys": {
      "url": "http://beszel.local:8090",
      "secret": "beszel",
      "every": "1m",
      "auth": {
        "type": "login",
        "request": {
          "method": "POST",
          "path": "/api/collections/users/auth-with-password",
          "body": {
            "identity": "legato-reader@example.net",
            "password": "{secret}"
          }
        },
        "token": "token",
        "send": {
          "header": "Authorization"
        }
      },
      "requests": {
        "system": "/api/collections/systems/records?filter=%28id%3D%22{param.id}%22%29&fields=id,name,status,updated,info",
        "details": "/api/collections/system_details/records?filter=%28system%3D%22{param.id}%22%29&fields=hostname,os_name,kernel,cpu,cores,threads,arch",
        "containers": "/api/collections/containers/records?perPage=60&sort=-cpu&filter=%28system%3D%22{param.id}%22%29&fields=id,name,status,health,cpu,memory",
        "alerts": "/api/collections/alerts/records?perPage=60&sort=name&filter=%28system%3D%22{param.id}%22%29&fields=id,name,value,min,triggered"
      },
      "derive": [
        {
          "name": "found",
          "value": {
            "count": "system.items",
            "default": 0
          }
        },
        {
          "name": "s",
          "value": {
            "each": "system.items",
            "limit": 1,
            "fields": {
              "name": "name",
              "status": "status",
              "updated": "updated",
              "info": "info"
            }
          }
        },
        {
          "name": "d",
          "value": {
            "first": "os_name",
            "of": "details.items"
          }
        },
        {
          "name": "up_days",
          "value": {
            "divide": [
              "s[0].info.u",
              86400
            ],
            "round": 0
          }
        },
        {
          "name": "uptime",
          "value": {
            "if": {
              "path": "up_days",
              "gte": 2
            },
            "then": {
              "template": "{up_days} days"
            },
            "else": {
              "template": "{s[0].info.u|duration}"
            }
          }
        },
        {
          "name": "rules",
          "value": {
            "each": "alerts.items",
            "fields": {
              "id": "id",
              "name": "name",
              "triggered": "triggered",
              "words": {
                "if": {
                  "path": "name",
                  "equals": "Status"
                },
                "then": {
                  "template": "Goes up or down"
                },
                "else": {
                  "path": "name",
                  "values": {
                    "Status": {
                      "template": "Down"
                    },
                    "CPU": {
                      "template": "CPU above {value}%"
                    },
                    "Memory": {
                      "template": "Memory above {value}%"
                    },
                    "Disk": {
                      "template": "Disk above {value}%"
                    },
                    "GPU": {
                      "template": "GPU above {value}%"
                    },
                    "Temperature": {
                      "template": "Temperature above {value} °C"
                    },
                    "Bandwidth": {
                      "template": "Network above {value} MB/s"
                    },
                    "LoadAvg1": {
                      "template": "Load (1 min) above {value}"
                    },
                    "LoadAvg5": {
                      "template": "Load (5 min) above {value}"
                    },
                    "LoadAvg15": {
                      "template": "Load (15 min) above {value}"
                    },
                    "Battery": {
                      "template": "Battery below {value}%"
                    },
                    "ContainerHealth": {
                      "template": "A container is unhealthy"
                    },
                    "SystemdFailed": {
                      "template": "A service failed"
                    }
                  },
                  "default": {
                    "template": "{name} alert"
                  }
                }
              }
            }
          }
        },
        {
          "name": "n_containers",
          "value": {
            "count": "containers.items",
            "default": 0
          }
        },
        {
          "name": "n_rules",
          "value": {
            "count": "alerts.items",
            "default": 0
          }
        },
        {
          "name": "root",
          "value": {
            "each": "system.items",
            "limit": 1,
            "fields": {
              "label": {
                "if": {
                  "all": [
                    {
                      "path": "info.rdn",
                      "exists": true
                    },
                    {
                      "path": "info.rdn",
                      "not": ""
                    }
                  ]
                },
                "then": {
                  "template": "{info.rdn}"
                },
                "else": {
                  "template": "Root"
                }
              },
              "value": {
                "path": "info.dp",
                "round": 1
              }
            }
          }
        },
        {
          "name": "extra",
          "value": {
            "each": "system.items[0].info.efs",
            "entries": true,
            "sort": "key",
            "limit": 7,
            "fields": {
              "label": {
                "template": "{key|clip:24}"
              },
              "value": {
                "path": "value",
                "round": 1
              }
            }
          }
        },
        {
          "name": "fired",
          "value": {
            "each": "rules",
            "where": {
              "all": [
                {
                  "path": "triggered",
                  "equals": true
                },
                {
                  "path": "name",
                  "not": "Status"
                }
              ]
            },
            "fields": {
              "words": "words"
            }
          }
        },
        {
          "name": "n_fired",
          "value": {
            "count": "fired",
            "default": 0
          }
        },
        {
          "name": "n_fired_more",
          "value": {
            "subtract": [
              "n_fired",
              1
            ]
          }
        },
        {
          "name": "fired_words",
          "value": {
            "first": "words",
            "of": "fired"
          }
        },
        {
          "name": "sick",
          "value": {
            "each": "containers.items",
            "where": {
              "path": "health",
              "equals": 3
            },
            "fields": {
              "id": "id",
              "name": "name",
              "cpu": {
                "path": "cpu",
                "round": 1
              },
              "memory": {
                "path": "memory",
                "round": 0
              },
              "health": "health"
            }
          }
        },
        {
          "name": "well",
          "value": {
            "each": "containers.items",
            "where": {
              "path": "health",
              "not": 3
            },
            "fields": {
              "id": "id",
              "name": "name",
              "cpu": {
                "path": "cpu",
                "round": 1
              },
              "memory": {
                "path": "memory",
                "round": 0
              },
              "health": "health"
            }
          }
        }
      ]
    },
    "stats": {
      "url": "http://beszel.local:8090",
      "secret": "beszel",
      "every": "5m",
      "auth": {
        "type": "login",
        "request": {
          "method": "POST",
          "path": "/api/collections/users/auth-with-password",
          "body": {
            "identity": "legato-reader@example.net",
            "password": "{secret}"
          }
        },
        "token": "token",
        "send": {
          "header": "Authorization"
        }
      },
      "requests": {
        "history": "/api/collections/system_stats/records?perPage=72&sort=-created&filter=%28system%3D%22{param.id}%22%20%26%26%20type%3D%2210m%22%29&fields=created,stats.cpu,stats.mp,stats.t"
      },
      "derive": [
        {
          "name": "asc",
          "value": {
            "each": "history.items",
            "sort": "created",
            "fields": {
              "at": "created",
              "cpu": {
                "path": "stats.cpu",
                "round": 1
              },
              "mem": {
                "path": "stats.mp",
                "round": 1
              }
            }
          }
        },
        {
          "name": "n",
          "value": {
            "count": "history.items",
            "default": 0
          }
        },
        {
          "name": "temps",
          "value": {
            "each": "history.items[0].stats.t",
            "entries": true,
            "sort": {
              "by": "value",
              "order": "desc"
            },
            "limit": 6,
            "fields": {
              "label": {
                "template": "{key|clip:24}"
              },
              "value": {
                "path": "value",
                "round": 0
              }
            }
          }
        },
        {
          "name": "n_temps",
          "value": {
            "count": "temps",
            "default": 0
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "now",
      "type": "status",
      "placement": "page_info",
      "width": "full",
      "label": "System",
      "state": "unknown",
      "source": {
        "ref": "sys",
        "empty": {
          "text": "Beszel no longer has this system.",
          "when": {
            "path": "found",
            "equals": 0
          }
        },
        "failed": {
          "state": "critical",
          "detail": "Beszel isn't answering",
          "after": 3
        },
        "map": {
          "label": {
            "template": "{s[0].name|clip:40}"
          },
          "state": {
            "if": {
              "path": "s[0].status",
              "equals": "paused"
            },
            "then": {
              "template": "off"
            },
            "else": {
              "if": {
                "path": "s[0].status",
                "equals": "down"
              },
              "then": {
                "template": "critical"
              },
              "else": {
                "if": {
                  "path": "s[0].status",
                  "equals": "up"
                },
                "then": {
                  "if": {
                    "path": "n_fired",
                    "gt": 0
                  },
                  "then": {
                    "template": "warning"
                  },
                  "else": {
                    "template": "ok"
                  }
                },
                "else": {
                  "template": "unknown"
                }
              }
            }
          },
          "detail": {
            "if": {
              "path": "s[0].status",
              "equals": "paused"
            },
            "then": {
              "template": "Paused"
            },
            "else": {
              "if": {
                "path": "s[0].status",
                "equals": "down"
              },
              "then": {
                "template": "Down since {s[0].updated|ago}"
              },
              "else": {
                "if": {
                  "path": "s[0].status",
                  "equals": "pending"
                },
                "then": {
                  "template": "Waiting for its agent"
                },
                "else": {
                  "if": {
                    "path": "n_fired",
                    "equals": 1
                  },
                  "then": {
                    "template": "{fired_words}"
                  },
                  "else": {
                    "if": {
                      "path": "n_fired",
                      "gt": 1
                    },
                    "then": {
                      "template": "{fired_words} and {n_fired_more} more"
                    },
                    "else": {
                      "template": "Up {uptime}"
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    {
      "id": "cpu",
      "type": "sparkline",
      "label": "CPU",
      "unit": "%",
      "max": 100,
      "width": "half",
      "change": false,
      "points": [
        0,
        0
      ],
      "source": {
        "ref": "stats",
        "empty": {
          "when": {
            "path": "n",
            "lt": 2
          },
          "text": "Waiting for Beszel’s next 10-minute reading"
        },
        "map": {
          "points": {
            "each": "asc",
            "value": "cpu"
          },
          "times": {
            "each": "asc",
            "template": "{at}"
          },
          "labels": {
            "each": "asc",
            "template": "{at|clock}"
          }
        }
      }
    },
    {
      "id": "memory",
      "type": "sparkline",
      "label": "Memory",
      "unit": "%",
      "max": 100,
      "width": "half",
      "change": false,
      "points": [
        0,
        0
      ],
      "source": {
        "ref": "stats",
        "empty": {
          "when": {
            "path": "n",
            "lt": 2
          },
          "text": "Waiting for Beszel’s next 10-minute reading"
        },
        "map": {
          "points": {
            "each": "asc",
            "value": "mem"
          },
          "times": {
            "each": "asc",
            "template": "{at}"
          },
          "labels": {
            "each": "asc",
            "template": "{at|clock}"
          }
        }
      }
    },
    {
      "id": "disks",
      "type": "bars",
      "label": "Disks",
      "unit": "%",
      "max": 100,
      "items": [
        {
          "label": "Root",
          "value": 0
        }
      ],
      "source": {
        "ref": "sys",
        "when": {
          "path": "found",
          "gt": 0
        },
        "map": {
          "items": {
            "each": [
              "root",
              "extra"
            ],
            "fields": {
              "label": "label",
              "value": "value"
            }
          }
        }
      }
    },
    {
      "id": "temps",
      "type": "bars",
      "label": "Temperatures",
      "unit": "°C",
      "items": [
        {
          "label": "CPU",
          "value": 0
        }
      ],
      "source": {
        "ref": "stats",
        "when": {
          "path": "n_temps",
          "gt": 0
        },
        "map": {
          "items": {
            "each": "temps",
            "fields": {
              "label": "label",
              "value": "value"
            }
          }
        }
      }
    },
    {
      "id": "facts",
      "type": "facts",
      "label": "System",
      "items": [
        {
          "key": "Load",
          "value": "—"
        },
        {
          "key": "CPU",
          "value": "—"
        },
        {
          "key": "OS",
          "value": "—"
        },
        {
          "key": "Network",
          "value": "—"
        },
        {
          "key": "Uptime",
          "value": "—"
        },
        {
          "key": "Agent",
          "value": "—"
        }
      ],
      "source": {
        "ref": "sys",
        "when": {
          "path": "found",
          "gt": 0
        },
        "map": {
          "items[0].value": {
            "if": {
              "path": "s[0].info.la",
              "exists": true
            },
            "then": {
              "template": "{s[0].info.la[0]} · {s[0].info.la[1]} · {s[0].info.la[2]}"
            },
            "else": {
              "template": "—"
            }
          },
          "items[1].value": {
            "if": {
              "path": "details.items[0].cpu",
              "exists": true
            },
            "then": {
              "template": "{details.items[0].cpu|clip:40} · {details.items[0].cores} cores"
            },
            "else": {
              "template": "—"
            }
          },
          "items[2].value": {
            "if": {
              "all": [
                {
                  "path": "d",
                  "exists": true
                },
                {
                  "path": "d",
                  "not": ""
                }
              ]
            },
            "then": {
              "template": "{d|clip:48}"
            },
            "else": {
              "if": {
                "path": "details.items[0].kernel",
                "exists": true
              },
              "then": {
                "template": "{details.items[0].kernel|clip:40}"
              },
              "else": {
                "template": "—"
              }
            }
          },
          "items[3].value": {
            "if": {
              "path": "s[0].info.bb",
              "exists": true
            },
            "then": {
              "template": "{s[0].info.bb|bytes}/s"
            },
            "else": {
              "template": "—"
            }
          },
          "items[4].value": {
            "if": {
              "path": "s[0].info.u",
              "exists": true
            },
            "then": {
              "template": "{uptime}"
            },
            "else": {
              "template": "—"
            }
          },
          "items[5].value": {
            "template": "{s[0].info.v}"
          }
        }
      }
    },
    {
      "id": "containers",
      "type": "list",
      "label": "Containers",
      "max_items": 7,
      "items": [],
      "source": {
        "ref": "sys",
        "when": {
          "path": "n_containers",
          "gt": 0
        },
        "map": {
          "label": {
            "if": {
              "path": "containers.totalItems",
              "gt": 60
            },
            "then": {
              "template": "Containers, 60 busiest of {containers.totalItems}"
            },
            "else": {
              "template": "Containers"
            }
          },
          "items": {
            "each": [
              "sick",
              "well"
            ],
            "fields": {
              "key": {
                "template": "{id|raw}"
              },
              "icon": {
                "if": {
                  "path": "health",
                  "equals": 3
                },
                "then": {
                  "template": "exclamationmark.triangle.fill"
                },
                "else": {
                  "template": "shippingbox"
                }
              },
              "color": {
                "if": {
                  "path": "health",
                  "equals": 3
                },
                "then": {
                  "template": "orange"
                }
              },
              "text": {
                "template": "{name|clip:40}"
              },
              "detail": {
                "if": {
                  "path": "health",
                  "equals": 3
                },
                "then": {
                  "template": "Unhealthy"
                },
                "else": {
                  "template": "{cpu}% · {memory} MB"
                }
              }
            }
          }
        }
      }
    },
    {
      "id": "alerts",
      "type": "list",
      "label": "Alerts",
      "max_items": 7,
      "items": [],
      "source": {
        "ref": "sys",
        "empty": {
          "text": "No alerts set for this system.",
          "when": {
            "path": "n_rules",
            "equals": 0
          }
        },
        "map": {
          "items": {
            "each": "rules",
            "fields": {
              "key": {
                "template": "{id|raw}"
              },
              "icon": {
                "if": {
                  "path": "triggered",
                  "equals": true
                },
                "then": {
                  "if": {
                    "path": "name",
                    "equals": "Status"
                  },
                  "then": {
                    "template": "xmark.octagon.fill"
                  },
                  "else": {
                    "template": "exclamationmark.triangle.fill"
                  }
                },
                "else": {
                  "template": "bell"
                }
              },
              "color": {
                "if": {
                  "path": "triggered",
                  "equals": true
                },
                "then": {
                  "if": {
                    "path": "name",
                    "equals": "Status"
                  },
                  "then": {
                    "template": "red"
                  },
                  "else": {
                    "template": "orange"
                  }
                }
              },
              "text": "words",
              "detail": {
                "if": {
                  "path": "triggered",
                  "equals": true
                },
                "then": {
                  "template": "Firing"
                }
              }
            }
          }
        }
      }
    }
  ]
}
