Added Rebar3 project files
This commit is contained in:
17
apps/log_monitor/src/log_monitor.app.src
Normal file
17
apps/log_monitor/src/log_monitor.app.src
Normal 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, []}
|
||||
]}.
|
||||
26
apps/log_monitor/src/log_monitor_app.erl
Normal file
26
apps/log_monitor/src/log_monitor_app.erl
Normal 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
|
||||
%%====================================================================
|
||||
37
apps/log_monitor/src/log_monitor_sup.erl
Normal file
37
apps/log_monitor/src/log_monitor_sup.erl
Normal 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
|
||||
%%====================================================================
|
||||
63
apps/log_monitor/src/monitor.erl
Normal file
63
apps/log_monitor/src/monitor.erl
Normal 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}.
|
||||
1
apps/log_monitor/src/rebar.lock
Normal file
1
apps/log_monitor/src/rebar.lock
Normal file
@@ -0,0 +1 @@
|
||||
[].
|
||||
19
apps/log_monitor/src/watcher.erl
Normal file
19
apps/log_monitor/src/watcher.erl
Normal 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.
|
||||
Reference in New Issue
Block a user