Coordinator & Mailer

This commit is contained in:
Fabio Salvini
2017-06-08 21:22:48 +02:00
parent f08e5f3e77
commit c7a9e6ccb5
8 changed files with 97 additions and 29 deletions

View File

@@ -0,0 +1,18 @@
-module(coordinator).
-compile(export_all).
start() ->
_MailerPid = mailer:start(),
{ok, Files} = application:get_env(log_monitor, logfiles),
Monitors = [monitor:start(File) || File <- Files],
loop(Monitors).
loop(Monitors) ->
receive
{'EXIT', FromPid, Reason} ->
io:format("Monitor process terminated: ~s~n", [Reason]),
RemainingMonitors = [M || M <- Monitors, M =/= FromPid],
loop(RemainingMonitors)
after 5000 ->
loop(Monitors)
end.

View File

@@ -29,7 +29,7 @@ start_link() ->
%% Child :: {Id,StartFunc,Restart,Shutdown,Type,Modules}
init([]) ->
{ok, { {one_for_all, 0, 1}, [{console,
{monitor, start, []},
{coordinator, start, []},
permanent, 5000, worker, [monitor]}]} }.
%%====================================================================

View File

@@ -0,0 +1,34 @@
-module(mailer).
-compile(export_all).
start() ->
io:format("Starting Mailer~n", []),
Pid = spawn_link(?MODULE, init, []),
register(mailer, Pid),
Pid.
init() ->
loop().
loop() ->
receive
{error, File, Text} ->
case send_email(File, Text) of
{error, Reason, Message} ->
io:format("Error sending email: ~s ~p~n", [Reason, Message]);
_ -> io:format("Mail sent~n", [])
end,
loop()
end.
send_email(File, Text) ->
{ok, EmailConfig} = application:get_env(log_monitor, email_config),
Sender = proplists:get_value(sender, EmailConfig, "log@monitor.com"),
DefaultReceiver = proplists:get_value(default_receiver, EmailConfig),
Relay = proplists:get_value(relay, EmailConfig),
Username = proplists:get_value(username, EmailConfig),
Password = proplists:get_value(password, EmailConfig),
io:format("Send Email for file ~s with text: ~n~s~n", [File, Text]),
gen_smtp_client:send_blocking({Sender, [DefaultReceiver],
io_lib:format("Subject: Error notification\r\nFrom: Fabio\r\n\r\nLogfile: ~s~nError: ~n~s~n", [File, Text])},
[{relay, Relay}, {username, Username}, {password, Password}]).

View File

@@ -1,11 +1,10 @@
-module(monitor).
-compile(export_all).
start() ->
init("/tmp/lines.log").
start(File) ->
spawn_link(?MODULE, init, [File]).
io:format("Starting monitor for file: ~s~n", [File]),
Pid = spawn_link(?MODULE, init, [File]),
Pid.
init(File) ->
process_flag(trap_exit, true),
@@ -15,9 +14,9 @@ init(File) ->
loop(File) ->
receive
{log_line, Text} ->
case re:run(Text, "ERROR") of
{match, _} -> errorGathering(File, Text);
nomatch -> loop(File)
case isError(Text) of
true -> errorGathering(File, Text);
false -> loop(File)
end,
io:format("Received line: ~s~n", [Text]),
loop(File);
@@ -28,18 +27,22 @@ loop(File) ->
end.
errorGathering(File, Error) ->
{ok, Timer} = timer:send_after(1000, {timeout}),
errorGathering(File, Error, Timer).
errorGathering(File, Error, Timer) ->
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
case isError(Text) of
true ->
timer:clean(Timer),
{ok, NewTimer} = timer:send_after(1000, {timeout}),
errorGathering(File, Error ++ "\n" ++ Text, NewTimer);
false ->
errorGathering(File, Error ++ "\n" ++ Text, Timer)
end;
{timeout} ->
mailer ! {error, File, Error},
loop(File)
end.
isError(Text) ->
@@ -50,14 +53,6 @@ isError(Text) ->
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}.