logo

clairvoyance

Unnamed repository; edit this file 'description' to name the repository.

clairvoyance.ex (1573B)


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