summaryrefslogtreecommitdiff
path: root/statusserver/src/statusserver.erl
diff options
context:
space:
mode:
Diffstat (limited to 'statusserver/src/statusserver.erl')
-rw-r--r--statusserver/src/statusserver.erl71
1 files changed, 71 insertions, 0 deletions
diff --git a/statusserver/src/statusserver.erl b/statusserver/src/statusserver.erl
new file mode 100644
index 0000000..323b28d
--- /dev/null
+++ b/statusserver/src/statusserver.erl
@@ -0,0 +1,71 @@
+%%% Copyright (c) 2014-2016, NORDUnet A/S.
+%%% See LICENSE for licensing information.
+
+%%% @doc Frontend node API
+
+-module(statusserver).
+-export([init_module/0]).
+%% API (URL)
+-export([request/4]).
+
+-define(APPURL_PLOP_STATUS, "plop/v1/status").
+
+request(post, ?APPURL_PLOP_STATUS, Service, Input) ->
+ case (catch mochijson2:decode(Input)) of
+ {error, E} ->
+ html("bad input:", E);
+ Entries when is_list(Entries) ->
+ lists:foreach(fun ({struct, PropList}) ->
+ Target = proplists:get_value(<<"target">>, PropList),
+ Variable = proplists:get_value(<<"key">>, PropList),
+ Status = proplists:get_value(<<"value">>, PropList),
+ set_status(Service, Target, Variable, Status)
+ end, Entries)
+ end,
+ success({[{result, <<"ok">>}]});
+request(get, "", "status", Input) ->
+ Now = erlang:monotonic_time(millisecond),
+ Variables = [{struct, [
+ {service, list_to_binary(Service)},
+ {target, Target},
+ {variable, Variable},
+ {status, Status},
+ {age, (Now - Timestamp) / 1000}
+ ]} || {{Service, Target, Variable}, Status, Timestamp} <- get_status()],
+ success({[{result, Variables}]}).
+
+
+success(Data) ->
+ {200, [{"Content-Type", "text/json"}, {"Access-Control-Allow-Origin", "*"}], mochijson2:encode(Data)}.
+
+html(Text, Input) ->
+ {400, [{"Content-Type", "text/html"}],
+ io_lib:format(
+ "<html><body><p>~n" ++
+ "~s~n" ++
+ "~p~n" ++
+ "</body></html>~n", [Text, Input])}.
+
+-define(STATUSDB_TABLE, statusserver_statusdb).
+
+init_module() ->
+ create_statusdb().
+
+create_statusdb() ->
+ case ets:info(?STATUSDB_TABLE) of
+ undefined ->
+ ok;
+ _ ->
+ ets:delete(?STATUSDB_TABLE)
+ end,
+ ets:new(?STATUSDB_TABLE, [set, public, named_table]).
+
+get_status() ->
+ [E || [E] <- ets:match(?STATUSDB_TABLE, '$1')].
+
+set_status(Service, Target, Variable, Status) ->
+ lager:info("status: ~p ~p ~p ~p", [Service, Target, Variable, Status]),
+ Timestamp = erlang:monotonic_time(millisecond),
+ true = ets:insert(?STATUSDB_TABLE,
+ {{Service, Target, Variable}, Status, Timestamp}),
+ ok.