logo

clairvoyance

Unnamed repository; edit this file 'description' to name the repository.
commit: 2980cbbc95f8ea2dab667c508af5eed183297337
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sat,  8 Sep 2018 20:32:48 +0200

Initial Commit

Diffstat:

A.formatter.exs4++++
A.gitignore25+++++++++++++++++++++++++
AREADME.md15+++++++++++++++
Aconfig/config.exs3+++
Alib/clairvoyance.ex64++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Alib/mix/tasks/scan.ex12++++++++++++
Amix.exs29+++++++++++++++++++++++++++++
Amix.lock3+++
Atest/test_helper.exs1+
9 files changed, 156 insertions(+), 0 deletions(-)

diff --git a/.formatter.exs b/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/.gitignore b/.gitignore @@ -0,0 +1,25 @@ +# The directory Mix will write compiled artifacts to. +/_build/ + +# If you run "mix test --cover", coverage assets end up here. +/cover/ + +# The directory Mix downloads your dependencies sources to. +/deps/ + +# Where 3rd-party dependencies like ExDoc output generated docs. +/doc/ + +# Ignore .fetch files in case you like to edit your project deps locally. +/.fetch + +# If the VM crashes, it generates a dump, let's ignore it too. +erl_crash.dump + +# Also ignore archive artifacts (built via "mix archive.build"). +*.ez + +# Ignore package tarball (built via "mix hex.build"). +clairvoyance-*.tar + +clairvoyance_scan.json diff --git a/README.md b/README.md @@ -0,0 +1,15 @@ +# Clairvoyance + +A server monitoring software + +Made in Elixir because Shell is awful at doing a formatted output. + +## Installation + +```elixir +def deps do + [ + {:clairvoyance, "~> 0.1.0"} + ] +end +``` diff --git a/config/config.exs b/config/config.exs @@ -0,0 +1,3 @@ +use Mix.Config + +# import_config "#{Mix.env()}.exs" diff --git a/lib/clairvoyance.ex b/lib/clairvoyance.ex @@ -0,0 +1,64 @@ +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() + } + 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, blocks_used, percentage], …]`` + """ + 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 +end diff --git a/lib/mix/tasks/scan.ex b/lib/mix/tasks/scan.ex @@ -0,0 +1,12 @@ +defmodule Mix.Tasks.Scan do + use Mix.Task + + def run(_) do + Application.ensure_all_started(:clairvoyance) + + result = Clairvoyance.scan() + |> Jason.encode!() + + File.write("clairvoyance_scan.json", result) + end +end diff --git a/mix.exs b/mix.exs @@ -0,0 +1,29 @@ +defmodule Clairvoyance.MixProject do + use Mix.Project + + def project do + [ + app: :clairvoyance, + version: "0.1.0", + elixir: "~> 1.7", + start_permanent: Mix.env() == :prod, + deps: deps() + ] + end + + # Run "mix help compile.app" to learn about applications. + def application do + [ + extra_applications: [:os_mon, :logger] + ] + end + + # Run "mix help deps" to learn about dependencies. + defp deps do + [ + # {:dep_from_hexpm, "~> 0.3.0"}, + # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}, + {:jason, "~> 1.1"} + ] + end +end diff --git a/mix.lock b/mix.lock @@ -0,0 +1,3 @@ +%{ + "jason": {:hex, :jason, "1.1.1", "d3ccb840dfb06f2f90a6d335b536dd074db748b3e7f5b11ab61d239506585eb2", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"}, +} diff --git a/test/test_helper.exs b/test/test_helper.exs @@ -0,0 +1 @@ +ExUnit.start()