Coordinator & Mailer
This commit is contained in:
parent
f08e5f3e77
commit
c7a9e6ccb5
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -10,3 +10,6 @@ rel/example_project
|
||||||
.concrete/DEV_MODE
|
.concrete/DEV_MODE
|
||||||
.rebar
|
.rebar
|
||||||
_build/
|
_build/
|
||||||
|
# ---> Intellij
|
||||||
|
.idea/
|
||||||
|
*.iml
|
18
apps/log_monitor/src/coordinator.erl
Normal file
18
apps/log_monitor/src/coordinator.erl
Normal 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.
|
|
@ -29,7 +29,7 @@ start_link() ->
|
||||||
%% Child :: {Id,StartFunc,Restart,Shutdown,Type,Modules}
|
%% Child :: {Id,StartFunc,Restart,Shutdown,Type,Modules}
|
||||||
init([]) ->
|
init([]) ->
|
||||||
{ok, { {one_for_all, 0, 1}, [{console,
|
{ok, { {one_for_all, 0, 1}, [{console,
|
||||||
{monitor, start, []},
|
{coordinator, start, []},
|
||||||
permanent, 5000, worker, [monitor]}]} }.
|
permanent, 5000, worker, [monitor]}]} }.
|
||||||
|
|
||||||
%%====================================================================
|
%%====================================================================
|
||||||
|
|
34
apps/log_monitor/src/mailer.erl
Normal file
34
apps/log_monitor/src/mailer.erl
Normal 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}]).
|
|
@ -1,11 +1,10 @@
|
||||||
-module(monitor).
|
-module(monitor).
|
||||||
-compile(export_all).
|
-compile(export_all).
|
||||||
|
|
||||||
start() ->
|
|
||||||
init("/tmp/lines.log").
|
|
||||||
|
|
||||||
start(File) ->
|
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) ->
|
init(File) ->
|
||||||
process_flag(trap_exit, true),
|
process_flag(trap_exit, true),
|
||||||
|
@ -15,9 +14,9 @@ init(File) ->
|
||||||
loop(File) ->
|
loop(File) ->
|
||||||
receive
|
receive
|
||||||
{log_line, Text} ->
|
{log_line, Text} ->
|
||||||
case re:run(Text, "ERROR") of
|
case isError(Text) of
|
||||||
{match, _} -> errorGathering(File, Text);
|
true -> errorGathering(File, Text);
|
||||||
nomatch -> loop(File)
|
false -> loop(File)
|
||||||
end,
|
end,
|
||||||
io:format("Received line: ~s~n", [Text]),
|
io:format("Received line: ~s~n", [Text]),
|
||||||
loop(File);
|
loop(File);
|
||||||
|
@ -28,18 +27,22 @@ loop(File) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
errorGathering(File, Error) ->
|
errorGathering(File, Error) ->
|
||||||
|
{ok, Timer} = timer:send_after(1000, {timeout}),
|
||||||
|
errorGathering(File, Error, Timer).
|
||||||
|
errorGathering(File, Error, Timer) ->
|
||||||
receive
|
receive
|
||||||
{log_line, Text} ->
|
{log_line, Text} ->
|
||||||
IsLog = isLogLine(Text),
|
case isError(Text) of
|
||||||
if IsLog ->
|
|
||||||
io:format("Send email with text: ~s~n", [Error]),
|
|
||||||
IsError = isError(Text),
|
|
||||||
if IsError -> errorGathering(File, Text);
|
|
||||||
true -> loop(File)
|
|
||||||
end;
|
|
||||||
true ->
|
true ->
|
||||||
errorGathering(File, Error ++ "\n" ++ Text)
|
timer:clean(Timer),
|
||||||
end
|
{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.
|
end.
|
||||||
|
|
||||||
isError(Text) ->
|
isError(Text) ->
|
||||||
|
@ -50,14 +53,6 @@ isError(Text) ->
|
||||||
false
|
false
|
||||||
end.
|
end.
|
||||||
|
|
||||||
isLogLine(Text) ->
|
|
||||||
case re:run(Text, "2017") of %% TODO
|
|
||||||
{match, _} ->
|
|
||||||
true;
|
|
||||||
nomatch ->
|
|
||||||
false
|
|
||||||
end.
|
|
||||||
|
|
||||||
startWatcher(File) ->
|
startWatcher(File) ->
|
||||||
WatcherPid = spawn_link(watcher, init, [self(), File]),
|
WatcherPid = spawn_link(watcher, init, [self(), File]),
|
||||||
{ok, WatcherPid}.
|
{ok, WatcherPid}.
|
||||||
|
|
|
@ -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, ""}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
].
|
].
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
{erl_opts, [debug_info]}.
|
{erl_opts, [debug_info]}.
|
||||||
{deps, [
|
{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" },
|
{relx, [{release, { log_monitor, "0.1.0" },
|
||||||
|
|
|
@ -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",
|
{git,"https://github.com/Vagabond/gen_smtp",
|
||||||
{ref,"2ea8bb995adf32102f523cef93ae98e287ac77d1"}},
|
{ref,"2ea8bb995adf32102f523cef93ae98e287ac77d1"}},
|
||||||
0}].
|
0}].
|
||||||
|
|
Loading…
Reference in New Issue
Block a user