Added Rebar3 project files

This commit is contained in:
Fabio Salvini
2017-03-11 01:31:41 +01:00
parent a2346a343f
commit f08e5f3e77
13 changed files with 233 additions and 8 deletions

View File

@@ -0,0 +1,17 @@
{application, log_monitor,
[{description, "An OTP application"},
{vsn, "0.1.0"},
{registered, []},
{mod, { log_monitor_app, []}},
{applications,
[kernel,
stdlib,
gen_smtp
]},
{env,[]},
{modules, []},
{maintainers, []},
{licenses, []},
{links, []}
]}.

View File

@@ -0,0 +1,26 @@
%%%-------------------------------------------------------------------
%% @doc log_monitor public API
%% @end
%%%-------------------------------------------------------------------
-module(log_monitor_app).
-behaviour(application).
%% Application callbacks
-export([start/2, stop/1]).
%%====================================================================
%% API
%%====================================================================
start(_StartType, _StartArgs) ->
log_monitor_sup:start_link().
%%--------------------------------------------------------------------
stop(_State) ->
ok.
%%====================================================================
%% Internal functions
%%====================================================================

View File

@@ -0,0 +1,37 @@
%%%-------------------------------------------------------------------
%% @doc log_monitor top level supervisor.
%% @end
%%%-------------------------------------------------------------------
-module(log_monitor_sup).
-behaviour(supervisor).
%% API
-export([start_link/0]).
%% Supervisor callbacks
-export([init/1]).
-define(SERVER, ?MODULE).
%%====================================================================
%% API functions
%%====================================================================
start_link() ->
supervisor:start_link({local, ?SERVER}, ?MODULE, []).
%%====================================================================
%% Supervisor callbacks
%%====================================================================
%% Child :: {Id,StartFunc,Restart,Shutdown,Type,Modules}
init([]) ->
{ok, { {one_for_all, 0, 1}, [{console,
{monitor, start, []},
permanent, 5000, worker, [monitor]}]} }.
%%====================================================================
%% Internal functions
%%====================================================================

View File

@@ -0,0 +1,63 @@
-module(monitor).
-compile(export_all).
start() ->
init("/tmp/lines.log").
start(File) ->
spawn_link(?MODULE, init, [File]).
init(File) ->
process_flag(trap_exit, true),
startWatcher(File),
loop(File).
loop(File) ->
receive
{log_line, Text} ->
case re:run(Text, "ERROR") of
{match, _} -> errorGathering(File, Text);
nomatch -> loop(File)
end,
io:format("Received line: ~s~n", [Text]),
loop(File);
{'EXIT', _FromPid, Reason} ->
io:format("Watcher process terminated: ~s~n", [Reason]),
startWatcher(File),
loop(File)
end.
errorGathering(File, Error) ->
receive
{log_line, Text} ->
IsLog = isLogLine(Text),
if IsLog ->
io:format("Send email with text: ~s~n", [Error]),
IsError = isError(Text),
if IsError -> errorGathering(File, Text);
true -> loop(File)
end;
true ->
errorGathering(File, Error ++ "\n" ++ Text)
end
end.
isError(Text) ->
case re:run(Text, "ERROR") of %% TODO
{match, _} ->
true;
nomatch ->
false
end.
isLogLine(Text) ->
case re:run(Text, "2017") of %% TODO
{match, _} ->
true;
nomatch ->
false
end.
startWatcher(File) ->
WatcherPid = spawn_link(watcher, init, [self(), File]),
{ok, WatcherPid}.

View File

@@ -0,0 +1 @@
[].

View File

@@ -0,0 +1,19 @@
-module(watcher).
-compile(export_all).
start(MonitorPid, File) ->
spawn_link(?MODULE, init, [MonitorPid, File]).
init(MonitorPid, File) ->
Cmd = "/usr/bin/tail -n0 --follow=name " ++ File,
Port = open_port({spawn, Cmd}, [stderr_to_stdout, {line, 256}, exit_status, binary]),
loop(MonitorPid, Port).
loop(MonitorPid, Port) ->
receive
{Port, {data, {eol, Bin}}} ->
%% io:format("~s~n", [iolist_to_binary(Bin)]),
Text = binary_to_list(iolist_to_binary(Bin)),
MonitorPid ! {log_line, Text},
loop(MonitorPid, Port)
end.