41 lines
1.2 KiB
Elixir
41 lines
1.2 KiB
Elixir
defmodule Server.Status do
|
|
@moduledoc """
|
|
Derive a status level for a host from its latest fast sample.
|
|
:offline host has no active agent connection
|
|
:critical pool DEGRADED/FAULTED or capacity > 90
|
|
:warning capacity 80..90 or pending OS updates
|
|
:ok everything nominal
|
|
"""
|
|
|
|
@bad_pool_states ~w(DEGRADED FAULTED SUSPENDED UNAVAIL)
|
|
|
|
@spec compute(String.t(), map() | nil) :: :offline | :critical | :warning | :ok
|
|
def compute(host_status, _payload) when host_status in ~w(offline never_connected),
|
|
do: :offline
|
|
|
|
def compute(_host_status, nil), do: :ok
|
|
|
|
def compute(_host_status, %{} = payload) do
|
|
pools = get_in(payload, ["zfs_pools", "pools"]) || []
|
|
pending = get_in(payload, ["system_info", "pending_updates"]) || 0
|
|
|
|
cond do
|
|
Enum.any?(pools, &critical_pool?/1) -> :critical
|
|
Enum.any?(pools, &warning_pool?/1) -> :warning
|
|
pending > 0 -> :warning
|
|
true -> :ok
|
|
end
|
|
end
|
|
|
|
defp critical_pool?(pool) do
|
|
health = pool["health"]
|
|
cap = pool["capacity_percent"] || 0
|
|
|
|
health in @bad_pool_states or cap > 90
|
|
end
|
|
|
|
defp warning_pool?(pool) do
|
|
cap = pool["capacity_percent"] || 0
|
|
cap >= 80 and cap <= 90
|
|
end
|
|
end
|