add adapter_spec to adapters, basic db stuff
This commit is contained in:
parent
30da8f31b5
commit
323ebf2569
3 changed files with 51 additions and 25 deletions
|
@ -1,18 +1,12 @@
|
|||
defmodule Elstat.Adapter.Elixire do
|
||||
@behaviour Elstat.Adapter
|
||||
|
||||
use GenServer
|
||||
require Logger
|
||||
|
||||
def start_link(service_opts) do
|
||||
GenServer.start_link(__MODULE__, service_opts, [])
|
||||
def adapter_spec do
|
||||
%{
|
||||
db_columns: [:status, :latency]
|
||||
}
|
||||
end
|
||||
|
||||
def init(service_opts) do
|
||||
Logger.info("Elixire adapter started with #{inspect service_opts}")
|
||||
{:ok, %{}}
|
||||
end
|
||||
|
||||
|
||||
def check(args) do
|
||||
addr = args.base_url
|
||||
|
||||
|
|
|
@ -1,18 +1,12 @@
|
|||
defmodule Elstat.Adapter.Ping do
|
||||
@behaviour Elstat.Adapter
|
||||
|
||||
use GenServer
|
||||
require Logger
|
||||
|
||||
def start_link(service_opts) do
|
||||
GenServer.start_link(__MODULE__, service_opts, [])
|
||||
def adapter_spec do
|
||||
%{
|
||||
db_columns: [:status]
|
||||
}
|
||||
end
|
||||
|
||||
def init(service_opts) do
|
||||
Logger.info("Ping started with #{inspect service_opts}")
|
||||
{:ok, %{}}
|
||||
end
|
||||
|
||||
|
||||
def check(args) do
|
||||
{cmd_output, _} = System.cmd("ping", ["-c", "1", args.address])
|
||||
alive? = not Regex.match?(~r/100(\.0)?% packet loss/, cmd_output)
|
||||
|
|
|
@ -73,8 +73,46 @@ defmodule Elstat.Manager do
|
|||
services = Application.fetch_env!(:elstat, :services)
|
||||
|
||||
Sqlitex.with_db('elstat.db', fn(db) ->
|
||||
Enum.each(services, fn service ->
|
||||
|
||||
services
|
||||
|> Map.keys
|
||||
|> Enum.each(fn service_id ->
|
||||
service = services[service_id]
|
||||
adapter = service.adapter
|
||||
|
||||
spec = adapter.adapter_spec
|
||||
|
||||
columns = %{
|
||||
status: "status bool",
|
||||
latency: "latency bigint",
|
||||
}
|
||||
|
||||
column_res = spec.db_columns
|
||||
|> Enum.map(fn column ->
|
||||
columns[column]
|
||||
end)
|
||||
|> Enum.join(",\n")
|
||||
|
||||
timestamp_column = if Enum.count(spec.db_columns) == 0 do
|
||||
"timestamp bigint"
|
||||
else
|
||||
"timestamp bigint,"
|
||||
end
|
||||
|
||||
query = """
|
||||
CREATE TABLE IF NOT EXISTS #{Atom.to_string service_id} (
|
||||
#{timestamp_column}
|
||||
#{column_res}
|
||||
);
|
||||
"""
|
||||
|
||||
Logger.debug "query for #{inspect service_id}: #{query}"
|
||||
|
||||
case Sqlitex.query(db, query) do
|
||||
{:ok, _} ->
|
||||
Logger.info "created table for #{inspect service_id}"
|
||||
{:error, _err} ->
|
||||
Logger.error "error making table for #{inspect service_id}"
|
||||
end
|
||||
end)
|
||||
end)
|
||||
|
||||
|
@ -91,7 +129,7 @@ defmodule Elstat.Manager do
|
|||
data = state.serv_state[key]
|
||||
desc = state.services[key].description
|
||||
|
||||
result = case data do
|
||||
case data do
|
||||
{:map, data_map} ->
|
||||
{key, Map.put(data_map, :description, desc)}
|
||||
{:bool, data_bool} ->
|
||||
|
|
Loading…
Reference in a new issue