logo

clairvoyance

Unnamed repository; edit this file 'description' to name the repository. git clone https://anongit.hacktivis.me/git/clairvoyance.git/

clairvoyance.ex (1573B)


  1. defmodule Clairvoyance do
  2. @moduledoc """
  3. Documentation for Clairvoyance.
  4. """
  5. @doc """
  6. Outputs a Map of the system’s status
  7. """
  8. def scan() do
  9. %{
  10. uptime: uptime(),
  11. services: services(),
  12. filesystems: filesystems(),
  13. memory: memory(),
  14. timestamp: timestamp(),
  15. load_average: load_average(),
  16. hostname: hostname()
  17. }
  18. end
  19. @doc """
  20. Outputs how long the system have been up in seconds
  21. """
  22. def uptime() do
  23. {uptime, _} = System.cmd("cat", ["/proc/uptime"])
  24. uptime
  25. |> String.split(" ")
  26. |> Enum.at(1)
  27. |> String.trim()
  28. |> String.to_float()
  29. end
  30. @doc """
  31. Outputs a Map containing the services status
  32. ``%{started: [], crashed: [], runlevel: ""}``
  33. """
  34. def services() do
  35. %{
  36. started: File.ls!("/run/openrc/started/") || [],
  37. crashed: File.ls!("/run/openrc/failed/") || [],
  38. runlevel: File.read!("/run/openrc/softlevel") || "default"
  39. }
  40. end
  41. @doc """
  42. Outputs filesystems disk space usage
  43. ``[[mountpoint, kilo_bytes_used, percentage_used], …]``
  44. """
  45. def filesystems() do
  46. :disksup.get_disk_data()
  47. |> Enum.map(fn {x, y, z} -> [List.to_string(x), y, z] end)
  48. end
  49. def memory() do
  50. :memsup.get_system_memory_data()
  51. |> Enum.into(%{})
  52. end
  53. def timestamp() do
  54. DateTime.utc_now()
  55. |> DateTime.to_unix()
  56. end
  57. def load_average() do
  58. [:cpu_sup.avg1(), :cpu_sup.avg5(), :cpu_sup.avg15()]
  59. |> Enum.map(fn avg -> avg / 256 * 100 end)
  60. end
  61. def hostname() do
  62. :net_adm.localhost()
  63. |> List.to_string()
  64. end
  65. end