{
  "$schema": "./.schemas/page.schema.json",
  "title": "Box",
  "icon": "cpu",
  "sources": {
    "glances": {
      "hosts": {
        "list": "glances-hosts",
        "only": "{param.box}"
      },
      "secret": "glances",
      "secret_optional": true,
      "auth": {
        "type": "basic"
      },
      "every": "1m",
      "requests": {
        "system": "/api/4/system",
        "uptime": "/api/4/uptime",
        "fs": "/api/4/fs",
        "sensors": "/api/4/sensors",
        "quick": "/api/4/quicklook",
        "cpu": "/api/4/quicklook/cpu/history/1200"
      },
      "derive": [
        {
          "name": "mounts",
          "value": {
            "each": "fs",
            "where": {
              "path": "mnt_point",
              "not_in": [
                "/etc/hostname",
                "/etc/hosts",
                "/etc/resolv.conf",
                "/etc/os-release",
                "/usr/lib/os-release",
                "/etc/localtime",
                "/etc/timezone",
                "/run/.containerenv"
              ]
            },
            "sort": [
              "device_name",
              "mnt_point"
            ],
            "collapse": "device_name",
            "fields": {
              "name": "mnt_point",
              "pct": "percent",
              "free": "free",
              "size": "size"
            }
          }
        },
        {
          "name": "disks",
          "value": {
            "each": "mounts",
            "sort": {
              "by": "pct",
              "order": "desc"
            },
            "fields": {
              "name": "name",
              "pct": "pct",
              "free": "free",
              "size": "size"
            }
          }
        },
        {
          "name": "disk_count",
          "value": {
            "count": "disks"
          }
        },
        {
          "name": "disk_warn",
          "value": {
            "count": "disks",
            "where": {
              "path": "pct",
              "gte": 90
            }
          }
        },
        {
          "name": "disk_crit",
          "value": {
            "count": "disks",
            "where": {
              "path": "pct",
              "gte": 95
            }
          }
        },
        {
          "name": "fullest_name",
          "value": {
            "first": "name",
            "of": "disks"
          }
        },
        {
          "name": "fullest_raw",
          "value": {
            "first": "pct",
            "of": "disks"
          }
        },
        {
          "name": "fullest_pct",
          "value": {
            "path": "fullest_raw",
            "round": 0
          }
        },
        {
          "name": "temps",
          "value": {
            "each": "sensors",
            "where": {
              "all": [
                {
                  "path": "type",
                  "in": [
                    "temperature_core",
                    "temperature_hdd"
                  ]
                },
                {
                  "path": "value",
                  "gte": -273
                }
              ]
            },
            "sort": {
              "by": "value",
              "order": "desc"
            },
            "fields": {
              "label": "label",
              "value": "value",
              "shown": {
                "path": "value",
                "round": 0
              },
              "unit": "unit",
              "warning": "warning",
              "critical": "critical"
            }
          }
        },
        {
          "name": "temp_count",
          "value": {
            "count": "temps",
            "default": 0
          }
        },
        {
          "name": "hot_crit",
          "value": {
            "count": "temps",
            "where": {
              "all": [
                {
                  "path": "critical",
                  "gt": 0
                },
                {
                  "path": "value",
                  "gte": {
                    "path": "critical"
                  }
                }
              ]
            }
          }
        },
        {
          "name": "hot",
          "value": {
            "each": "temps",
            "where": {
              "any": [
                {
                  "all": [
                    {
                      "path": "critical",
                      "gt": 0
                    },
                    {
                      "path": "value",
                      "gte": {
                        "path": "critical"
                      }
                    }
                  ]
                },
                {
                  "all": [
                    {
                      "path": "warning",
                      "gt": 0
                    },
                    {
                      "path": "value",
                      "gte": {
                        "path": "warning"
                      }
                    }
                  ]
                }
              ]
            },
            "fields": {
              "label": "label",
              "shown": "shown",
              "unit": "unit"
            }
          }
        },
        {
          "name": "hot_warn",
          "value": {
            "count": "hot"
          }
        },
        {
          "name": "hot_label",
          "value": {
            "first": "label",
            "of": "hot"
          }
        },
        {
          "name": "hot_value",
          "value": {
            "first": "shown",
            "of": "hot"
          }
        },
        {
          "name": "hot_unit",
          "value": {
            "first": "unit",
            "of": "hot"
          }
        },
        {
          "name": "mem_pct",
          "value": "quick.mem"
        },
        {
          "name": "mem_shown",
          "value": {
            "path": "mem_pct",
            "round": 0
          }
        },
        {
          "name": "cpu_2h",
          "value": {
            "each": "cpu",
            "where": {
              "all": [
                {
                  "path": "[1]",
                  "gte": 0
                },
                {
                  "value": {
                    "since": "[0]"
                  },
                  "lte": 7200
                }
              ]
            },
            "fields": {
              "t": "[0]",
              "v": "[1]",
              "age": {
                "since": "[0]"
              }
            }
          }
        },
        {
          "name": "cpu_recent",
          "value": {
            "each": "cpu_2h",
            "where": {
              "path": "age",
              "lte": 900
            },
            "fields": {
              "v": "v",
              "age": "age"
            }
          }
        },
        {
          "name": "cpu_third1",
          "value": {
            "avg": "v",
            "of": "cpu_recent",
            "where": {
              "path": "age",
              "lte": 300.0
            }
          }
        },
        {
          "name": "cpu_third2",
          "value": {
            "avg": "v",
            "of": "cpu_recent",
            "where": {
              "all": [
                {
                  "path": "age",
                  "gt": 300.0
                },
                {
                  "path": "age",
                  "lte": 600.0
                }
              ]
            }
          }
        },
        {
          "name": "cpu_third3",
          "value": {
            "avg": "v",
            "of": "cpu_recent",
            "where": {
              "path": "age",
              "gt": 600.0
            }
          }
        },
        {
          "name": "cpu_avg",
          "value": {
            "divide": [
              {
                "add": [
                  "cpu_third1",
                  "cpu_third2",
                  "cpu_third3"
                ]
              },
              3
            ]
          }
        },
        {
          "name": "cpu_avg_shown",
          "value": {
            "path": "cpu_avg",
            "round": 0
          }
        },
        {
          "name": "cpu_reach",
          "value": {
            "max": "age",
            "of": "cpu_recent"
          }
        },
        {
          "name": "cpu_busy",
          "value": {
            "if": {
              "all": [
                {
                  "path": "cpu_avg",
                  "gte": 90
                },
                {
                  "path": "cpu_third1",
                  "gte": 75
                },
                {
                  "path": "cpu_third2",
                  "gte": 75
                },
                {
                  "path": "cpu_third3",
                  "gte": 75
                },
                {
                  "path": "cpu_reach",
                  "gte": 720
                }
              ]
            },
            "then": {
              "template": "yes"
            },
            "else": {
              "template": "no"
            }
          }
        },
        {
          "name": "disk_more",
          "value": {
            "subtract": [
              "disk_warn",
              1
            ]
          }
        },
        {
          "name": "seg_sensor",
          "value": {
            "if": {
              "path": "hot_warn",
              "gt": 0
            },
            "then": {
              "template": " · {hot_label} {hot_value}°{hot_unit}"
            },
            "else": {
              "template": ""
            }
          }
        },
        {
          "name": "seg_disk",
          "value": {
            "if": {
              "path": "disk_warn",
              "gt": 0
            },
            "then": {
              "if": {
                "path": "disk_warn",
                "gt": 1
              },
              "then": {
                "template": " · {fullest_name} {fullest_pct}% full and {disk_more} more"
              },
              "else": {
                "template": " · {fullest_name} {fullest_pct}% full"
              }
            },
            "else": {
              "template": ""
            }
          }
        },
        {
          "name": "seg_mem",
          "value": {
            "if": {
              "path": "mem_pct",
              "gte": 90
            },
            "then": {
              "template": " · memory {mem_shown}%"
            },
            "else": {
              "template": ""
            }
          }
        },
        {
          "name": "seg_cpu",
          "value": {
            "if": {
              "path": "cpu_busy",
              "equals": "yes"
            },
            "then": {
              "template": " · CPU {cpu_avg_shown}% for 15 minutes"
            },
            "else": {
              "template": ""
            }
          }
        },
        {
          "name": "reasons_raw",
          "value": {
            "template": "{seg_sensor}{seg_disk}{seg_mem}{seg_cpu}"
          }
        },
        {
          "name": "reasons",
          "value": {
            "path": "reasons_raw",
            "match": "^ · (.+)$"
          }
        },
        {
          "name": "up_days",
          "value": {
            "path": "uptime",
            "match": "^([0-9]+ days?)"
          }
        },
        {
          "name": "up_hours",
          "value": {
            "path": "uptime",
            "match": "^([0-9]+):[0-9]+:[0-9]+"
          }
        },
        {
          "name": "up_minutes",
          "value": {
            "path": "uptime",
            "match": "^0:([0-9]+):[0-9]+"
          }
        },
        {
          "name": "up",
          "value": {
            "if": {
              "path": "up_days",
              "exists": true
            },
            "then": {
              "template": "Up {up_days}"
            },
            "else": {
              "if": {
                "path": "up_hours",
                "gt": 0
              },
              "then": {
                "template": "Up {up_hours}h"
              },
              "else": {
                "if": {
                  "path": "up_minutes",
                  "exists": true
                },
                "then": {
                  "template": "Up {up_minutes}m"
                },
                "else": {
                  "template": ""
                }
              }
            }
          }
        },
        {
          "name": "cpu_count",
          "value": {
            "count": "cpu_2h",
            "default": 0
          }
        }
      ]
    },
    "readings": {
      "hosts": {
        "list": "glances-hosts",
        "only": "{param.box}"
      },
      "secret": "glances",
      "secret_optional": true,
      "auth": {
        "type": "basic"
      },
      "every": "1m",
      "requests": {
        "quick": "/api/4/quicklook",
        "system": "/api/4/system",
        "load": "/api/4/load",
        "swap": "/api/4/memswap",
        "procs": "/api/4/processcount",
        "uptime": "/api/4/uptime"
      },
      "derive": [
        {
          "name": "load1",
          "value": {
            "path": "load.min1",
            "round": 2
          }
        },
        {
          "name": "load5",
          "value": {
            "path": "load.min5",
            "round": 2
          }
        },
        {
          "name": "load15",
          "value": {
            "path": "load.min15",
            "round": 2
          }
        },
        {
          "name": "swap_pct",
          "value": {
            "path": "swap.percent",
            "round": 0
          }
        },
        {
          "name": "os",
          "value": {
            "if": {
              "path": "system.linux_distro",
              "exists": true
            },
            "then": "system.linux_distro",
            "else": {
              "template": "{system.os_name} {system.os_version}"
            }
          }
        },
        {
          "name": "up_days",
          "value": {
            "path": "uptime",
            "match": "^([0-9]+ days?)"
          }
        },
        {
          "name": "up_hours",
          "value": {
            "path": "uptime",
            "match": "^([0-9]+):[0-9]+:[0-9]+"
          }
        },
        {
          "name": "up_minutes",
          "value": {
            "path": "uptime",
            "match": "^0:([0-9]+):[0-9]+"
          }
        },
        {
          "name": "up_for",
          "value": {
            "if": {
              "path": "up_days",
              "exists": true
            },
            "then": {
              "template": "{up_days}"
            },
            "else": {
              "if": {
                "path": "up_hours",
                "gt": 0
              },
              "then": {
                "template": "{up_hours}h"
              },
              "else": {
                "if": {
                  "path": "up_minutes",
                  "exists": true
                },
                "then": {
                  "template": "{up_minutes}m"
                },
                "else": {
                  "template": "—"
                }
              }
            }
          }
        }
      ]
    },
    "alerts": {
      "hosts": {
        "list": "glances-hosts",
        "only": "{param.box}"
      },
      "secret": "glances",
      "secret_optional": true,
      "auth": {
        "type": "basic"
      },
      "every": "1m",
      "requests": {
        "alert": "/api/4/alert",
        "cpu_now": "/api/4/cpu",
        "memory": "/api/4/mem/percent/history/1200"
      },
      "derive": [
        {
          "name": "memory_2h",
          "value": {
            "each": "memory",
            "where": {
              "all": [
                {
                  "path": "[1]",
                  "gte": 0
                },
                {
                  "value": {
                    "since": "[0]"
                  },
                  "lte": 7200
                }
              ]
            },
            "fields": {
              "t": "[0]",
              "v": "[1]",
              "age": {
                "since": "[0]"
              }
            }
          }
        },
        {
          "name": "memory_count",
          "value": {
            "count": "memory_2h",
            "default": 0
          }
        },
        {
          "name": "events",
          "value": {
            "each": "alert",
            "fields": {
              "kind": {
                "if": {
                  "path": "type",
                  "equals": "CPU_TOTAL"
                },
                "then": {
                  "template": "CPU"
                },
                "else": {
                  "if": {
                    "path": "type",
                    "equals": "CPU_USER"
                  },
                  "then": {
                    "template": "CPU, user"
                  },
                  "else": {
                    "if": {
                      "path": "type",
                      "equals": "CPU_SYSTEM"
                    },
                    "then": {
                      "template": "CPU, system"
                    },
                    "else": {
                      "if": {
                        "path": "type",
                        "equals": "CPU_IOWAIT"
                      },
                      "then": {
                        "template": "CPU waiting on disks"
                      },
                      "else": {
                        "if": {
                          "path": "type",
                          "equals": "CPU_STEAL"
                        },
                        "then": {
                          "template": "CPU steal"
                        },
                        "else": {
                          "if": {
                            "path": "type",
                            "equals": "MEM"
                          },
                          "then": {
                            "template": "Memory"
                          },
                          "else": {
                            "if": {
                              "path": "type",
                              "equals": "MEMSWAP"
                            },
                            "then": {
                              "template": "Swap"
                            },
                            "else": {
                              "if": {
                                "path": "type",
                                "equals": "LOAD"
                              },
                              "then": {
                                "template": "Load"
                              },
                              "else": "type"
                            }
                          }
                        }
                      }
                    }
                  }
                }
              },
              "level": {
                "if": {
                  "path": "state",
                  "equals": "CRITICAL"
                },
                "then": {
                  "template": "critical"
                },
                "else": {
                  "if": {
                    "path": "state",
                    "equals": "WARNING"
                  },
                  "then": {
                    "template": "warning"
                  },
                  "else": "state"
                }
              },
              "type": "type",
              "peak": {
                "path": "max",
                "round": 0
              },
              "percent": {
                "if": {
                  "path": "type",
                  "in": [
                    "CPU_TOTAL",
                    "CPU_USER",
                    "CPU_SYSTEM",
                    "CPU_IOWAIT",
                    "CPU_STEAL",
                    "MEM",
                    "MEMSWAP"
                  ]
                },
                "then": {
                  "template": "yes"
                },
                "else": {
                  "template": "no"
                }
              },
              "done": {
                "if": {
                  "path": "end",
                  "lt": 0
                },
                "then": {
                  "template": "no"
                },
                "else": {
                  "template": "yes"
                }
              },
              "top": "top[0]",
              "top_n": {
                "count": "top",
                "default": 0
              },
              "when": {
                "if": {
                  "path": "end",
                  "lt": 0
                },
                "then": {
                  "template": "Started {begin|ago}"
                },
                "else": {
                  "template": "{end|ago}"
                }
              }
            }
          }
        },
        {
          "name": "event_count",
          "value": {
            "count": "events"
          }
        },
        {
          "name": "finished_count",
          "value": {
            "count": "events",
            "where": {
              "path": "done",
              "equals": "yes"
            }
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "host",
      "type": "status",
      "placement": "page_info",
      "label": "Host",
      "state": "ok",
      "width": "full",
      "source": {
        "ref": "glances",
        "failed": {
          "after": 3,
          "state": "critical",
          "detail": "Can't read Glances"
        },
        "map": {
          "label": "system.hostname",
          "state": {
            "if": {
              "path": "mem_pct",
              "exists": false
            },
            "then": {
              "template": "unknown"
            },
            "else": {
              "if": {
                "any": [
                  {
                    "path": "hot_crit",
                    "gt": 0
                  },
                  {
                    "path": "disk_crit",
                    "gt": 0
                  },
                  {
                    "path": "mem_pct",
                    "gte": 95
                  }
                ]
              },
              "then": {
                "template": "critical"
              },
              "else": {
                "if": {
                  "any": [
                    {
                      "path": "hot_warn",
                      "gt": 0
                    },
                    {
                      "path": "disk_warn",
                      "gt": 0
                    },
                    {
                      "path": "mem_pct",
                      "gte": 90
                    },
                    {
                      "path": "cpu_busy",
                      "equals": "yes"
                    }
                  ]
                },
                "then": {
                  "template": "warning"
                },
                "else": {
                  "template": "ok"
                }
              }
            }
          },
          "detail": {
            "if": {
              "path": "mem_pct",
              "exists": false
            },
            "then": {
              "template": "Glances sent no memory reading"
            },
            "else": {
              "if": {
                "path": "reasons",
                "exists": true
              },
              "then": "reasons",
              "else": "up"
            }
          }
        }
      }
    },
    {
      "id": "cpu",
      "type": "sparkline",
      "label": "CPU",
      "unit": "%",
      "max": 100,
      "width": "half",
      "change": false,
      "points": [
        0,
        0
      ],
      "source": {
        "ref": "glances",
        "when": {
          "path": "cpu",
          "exists": true
        },
        "empty": {
          "when": {
            "path": "cpu_count",
            "lt": 2
          },
          "text": "Collecting readings"
        },
        "map": {
          "points": {
            "each": "cpu_2h",
            "buckets": 60,
            "value": {
              "path": "v",
              "round": 1
            }
          },
          "times": {
            "each": "cpu_2h",
            "buckets": 60,
            "template": "{t}"
          },
          "labels": {
            "each": "cpu_2h",
            "buckets": 60,
            "template": "{t|clock}"
          }
        }
      }
    },
    {
      "id": "memory",
      "type": "sparkline",
      "label": "Memory",
      "unit": "%",
      "max": 100,
      "width": "half",
      "change": false,
      "points": [
        0,
        0
      ],
      "source": {
        "ref": "alerts",
        "when": {
          "path": "memory",
          "exists": true
        },
        "empty": {
          "when": {
            "path": "memory_count",
            "lt": 2
          },
          "text": "Collecting readings"
        },
        "map": {
          "points": {
            "each": "memory_2h",
            "buckets": 60,
            "value": {
              "path": "v",
              "round": 1
            }
          },
          "times": {
            "each": "memory_2h",
            "buckets": 60,
            "template": "{t}"
          },
          "labels": {
            "each": "memory_2h",
            "buckets": 60,
            "template": "{t|clock}"
          }
        }
      }
    },
    {
      "id": "disks",
      "type": "bars",
      "label": "Disks",
      "unit": "%",
      "max": 100,
      "items": [
        {
          "label": "/",
          "value": 0
        }
      ],
      "source": {
        "ref": "glances",
        "empty": {
          "when": {
            "path": "disk_count",
            "equals": 0
          },
          "text": "Glances sees no disks. In Docker, mount each one read-only."
        },
        "map": {
          "items": {
            "each": "disks",
            "limit": 8,
            "fields": {
              "label": "name",
              "value": "pct"
            }
          },
          "label": {
            "if": {
              "path": "disk_count",
              "gt": 8
            },
            "then": {
              "template": "Disks, fullest 8 of {disk_count}"
            },
            "else": {
              "template": "Disks"
            }
          }
        }
      }
    },
    {
      "id": "system",
      "type": "facts",
      "label": "System",
      "items": [
        {
          "key": "Load",
          "value": "—"
        },
        {
          "key": "CPU",
          "value": "—"
        },
        {
          "key": "OS",
          "value": "—"
        },
        {
          "key": "Processes",
          "value": "—",
          "url": "legato://page/glances-box-programs?box={param.box}"
        },
        {
          "key": "Swap",
          "value": "—"
        },
        {
          "key": "Uptime",
          "value": "—"
        }
      ],
      "source": {
        "ref": "readings",
        "map": {
          "items[0].value": {
            "template": "{load1} · {load5} · {load15}"
          },
          "items[1].value": {
            "template": "{quick.cpu_name} · {quick.cpu_log_core} cores"
          },
          "items[2].value": "os",
          "items[3].value": {
            "template": "{procs.total} · {procs.thread} threads"
          },
          "items[4].value": {
            "if": {
              "path": "swap.total",
              "gt": 0
            },
            "then": {
              "template": "{swap_pct}% of {swap.total|bytes}"
            },
            "else": {
              "template": "None"
            }
          },
          "items[5].value": "up_for"
        }
      }
    },
    {
      "id": "temperatures",
      "type": "fleet",
      "label": "Temperatures",
      "max_items": 24,
      "items": [
        {
          "name": "Sensor",
          "state": "ok"
        }
      ],
      "source": {
        "ref": "glances",
        "when": {
          "path": "temp_count",
          "gt": 0
        },
        "map": {
          "items": {
            "each": "temps",
            "limit": 60,
            "fields": {
              "name": {
                "template": "{label|clip:24}"
              },
              "state": {
                "if": {
                  "all": [
                    {
                      "path": "critical",
                      "gt": 0
                    },
                    {
                      "path": "value",
                      "gte": {
                        "path": "critical"
                      }
                    }
                  ]
                },
                "then": {
                  "template": "critical"
                },
                "else": {
                  "if": {
                    "any": [
                      {
                        "all": [
                          {
                            "path": "critical",
                            "gt": 0
                          },
                          {
                            "path": "value",
                            "gte": {
                              "path": "critical"
                            }
                          }
                        ]
                      },
                      {
                        "all": [
                          {
                            "path": "warning",
                            "gt": 0
                          },
                          {
                            "path": "value",
                            "gte": {
                              "path": "warning"
                            }
                          }
                        ]
                      }
                    ]
                  },
                  "then": {
                    "template": "warning"
                  },
                  "else": {
                    "template": "ok"
                  }
                }
              },
              "detail": {
                "if": {
                  "path": "critical",
                  "gt": 0
                },
                "then": {
                  "template": "{shown}°{unit} · critical at {critical}°{unit}"
                },
                "else": {
                  "template": "{shown}°{unit}"
                }
              }
            }
          }
        }
      }
    },
    {
      "id": "alerts",
      "type": "list",
      "label": "Recent alerts",
      "max_items": 4,
      "items": [],
      "actions_placement": "corner",
      "actions": [
        {
          "label": "Clear finished alerts",
          "icon": "trash",
          "path": "/api/4/events/clear/all",
          "when": {
            "path": "finished_count",
            "gt": 0
          },
          "confirm": {
            "message": "Clear Glances' finished alerts? Its web page loses them too.",
            "button": "Clear"
          }
        }
      ],
      "source": {
        "ref": "alerts",
        "empty": {
          "when": {
            "path": "event_count",
            "equals": 0
          },
          "text": "No alerts in Glances' log"
        },
        "map": {
          "items": {
            "each": "events",
            "limit": 10,
            "fields": {
              "text": {
                "template": "{kind} {level}"
              },
              "detail": {
                "if": {
                  "path": "percent",
                  "equals": "yes"
                },
                "then": {
                  "if": {
                    "path": "top_n",
                    "gt": 0
                  },
                  "then": {
                    "template": "{when} · peak {peak}% · {top}"
                  },
                  "else": {
                    "template": "{when} · peak {peak}%"
                  }
                },
                "else": {
                  "if": {
                    "path": "top_n",
                    "gt": 0
                  },
                  "then": {
                    "template": "{when} · {top}"
                  },
                  "else": "when"
                }
              }
            }
          }
        }
      }
    }
  ],
  "sidebar": false,
  "params": {
    "box": {
      "list_key": "glances-hosts",
      "example": "h0000000000000000"
    }
  },
  "title_from": {
    "ref": "glances",
    "value": "system.hostname"
  }
}
