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

3
.gitignore vendored
View File

@ -10,3 +10,6 @@ rel/example_project
.concrete/DEV_MODE
.rebar
_build/
# ---> Intellij
.idea/
*.iml

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}.

View File

@ -1,3 +1,16 @@
[
{ log_monitor, []}
{ log_monitor,
[
{logfiles, ["/tmp/lines.log", "/tmp/lines2.log"]},
{email_config,
[
{sender, "me@example.com"},
{default_receiver, "salvini.fabio001@gmail.com"},
{relay, "smtp.fabiosalvinii.com"},
{username, "salvini.fabio001@gmail.com"},
{password, ""}
]
}
]
}
].

View File

@ -1,6 +1,7 @@
{erl_opts, [debug_info]}.
{deps, [
{gen_smtp, ".*", {git,"https://github.com/Vagabond/gen_smtp", {tag, "0.11.0"}}}
{gen_smtp, ".*", {git,"https://github.com/Vagabond/gen_smtp", {tag, "0.12.0"}}},
{erlexec, ".*", {git,"https://github.com/saleyn/erlexec", {tag, "1.6.4"}}}
]}.
{relx, [{release, { log_monitor, "0.1.0" },

View File

@ -1,4 +1,8 @@
[{<<"gen_smtp">>,
[{<<"erlexec">>,
{git,"https://github.com/saleyn/erlexec",
{ref,"e13dfe7f6bb0e5a37436da6ada4b1729593c196c"}},
0},
{<<"gen_smtp">>,
{git,"https://github.com/Vagabond/gen_smtp",
{ref,"2ea8bb995adf32102f523cef93ae98e287ac77d1"}},
0}].