clairvoyance.ex (1573B)
- defmodule Clairvoyance do
- @moduledoc """
- Documentation for Clairvoyance.
- """
- @doc """
- Outputs a Map of the system’s status
- """
- def scan() do
- %{
- uptime: uptime(),
- services: services(),
- filesystems: filesystems(),
- memory: memory(),
- timestamp: timestamp(),
- load_average: load_average(),
- hostname: hostname()
- }
- end
- @doc """
- Outputs how long the system have been up in seconds
- """
- def uptime() do
- {uptime, _} = System.cmd("cat", ["/proc/uptime"])
- uptime
- |> String.split(" ")
- |> Enum.at(1)
- |> String.trim()
- |> String.to_float()
- end
- @doc """
- Outputs a Map containing the services status
- ``%{started: [], crashed: [], runlevel: ""}``
- """
- def services() do
- %{
- started: File.ls!("/run/openrc/started/") || [],
- crashed: File.ls!("/run/openrc/failed/") || [],
- runlevel: File.read!("/run/openrc/softlevel") || "default"
- }
- end
- @doc """
- Outputs filesystems disk space usage
- ``[[mountpoint, kilo_bytes_used, percentage_used], …]``
- """
- def filesystems() do
- :disksup.get_disk_data()
- |> Enum.map(fn {x, y, z} -> [List.to_string(x), y, z] end)
- end
- def memory() do
- :memsup.get_system_memory_data()
- |> Enum.into(%{})
- end
- def timestamp() do
- DateTime.utc_now()
- |> DateTime.to_unix()
- end
- def load_average() do
- [:cpu_sup.avg1(), :cpu_sup.avg5(), :cpu_sup.avg15()]
- |> Enum.map(fn avg -> avg / 256 * 100 end)
- end
- def hostname() do
- :net_adm.localhost()
- |> List.to_string()
- end
- end