{
  "$schema": "./.schemas/page.schema.json",
  "title": "Glances",
  "icon": "cpu",
  "sources": {
    "boxes": {
      "hosts": {
        "list": "glances-hosts",
        "max": 20
      },
      "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",
        "mem": "/api/4/mem",
        "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": "mem.percent"
        },
        {
          "name": "mem_shown",
          "value": {
            "path": "mem_pct",
            "round": 0
          }
        },
        {
          "name": "cpu_recent",
          "value": {
            "each": "cpu",
            "where": {
              "all": [
                {
                  "path": "[1]",
                  "gte": 0
                },
                {
                  "value": {
                    "since": "[0]"
                  },
                  "lte": 900
                }
              ]
            },
            "fields": {
              "v": "[1]",
              "age": {
                "since": "[0]"
              }
            }
          }
        },
        {
          "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": "hostname",
          "value": "system.hostname"
        },
        {
          "name": "light",
          "value": {
            "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"
                }
              }
            }
          }
        },
        {
          "name": "words",
          "value": {
            "if": {
              "path": "mem_pct",
              "exists": false
            },
            "then": {
              "template": "Glances sent no memory reading"
            },
            "else": {
              "if": {
                "path": "reasons",
                "exists": true
              },
              "then": "reasons",
              "else": "up"
            }
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "boxes",
      "type": "fleet",
      "label": "Boxes",
      "max_items": 24,
      "items": [
        {
          "name": "Box",
          "state": "ok"
        }
      ],
      "source": {
        "ref": "boxes",
        "empty": {
          "when": {
            "value": {
              "count": "hosts"
            },
            "equals": 0
          },
          "text": "Add each box's Glances address to the list glances-hosts in Settings → Lists."
        },
        "map": {
          "items": {
            "each": "hosts",
            "fields": {
              "name": {
                "if": {
                  "value": {
                    "path": "origin",
                    "match": "^[a-z]+://(.{1,24}?)(?::61208)?$"
                  },
                  "exists": true
                },
                "then": {
                  "path": "origin",
                  "match": "^[a-z]+://(.{1,24}?)(?::61208)?$"
                },
                "else": {
                  "if": {
                    "path": "host",
                    "exists": true
                  },
                  "then": {
                    "template": "{host|clip:24}"
                  },
                  "else": {
                    "template": "{entry|clip:24}"
                  }
                }
              },
              "state": {
                "if": {
                  "path": "fails",
                  "gte": 3
                },
                "then": {
                  "template": "critical"
                },
                "else": {
                  "if": {
                    "all": [
                      {
                        "path": "state",
                        "in": [
                          "answered",
                          "failed"
                        ]
                      },
                      {
                        "path": "answer.light",
                        "exists": true
                      }
                    ]
                  },
                  "then": "answer.light",
                  "else": {
                    "template": "unknown"
                  }
                }
              },
              "detail": {
                "if": {
                  "path": "fails",
                  "gte": 3
                },
                "then": {
                  "template": "Can't read Glances · {error|clip:58}"
                },
                "else": {
                  "if": {
                    "path": "answer.words",
                    "exists": true
                  },
                  "then": {
                    "if": {
                      "path": "answer.hostname",
                      "exists": true
                    },
                    "then": {
                      "template": "{answer.hostname|clip:24} · {answer.words|clip:53}"
                    },
                    "else": {
                      "template": "{answer.words|clip:80}"
                    }
                  },
                  "else": {
                    "if": {
                      "path": "error",
                      "exists": true
                    },
                    "then": {
                      "template": "{error|clip:80}"
                    },
                    "else": {
                      "template": "Not read yet"
                    }
                  }
                }
              },
              "url": {
                "if": {
                  "path": "state",
                  "in": [
                    "answered",
                    "failed"
                  ]
                },
                "then": {
                  "template": "legato://page/glances-box?box={key}"
                }
              }
            }
          }
        }
      }
    }
  ]
}
