diff --git a/.gitignore b/.gitignore index 180ef50..7253177 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,6 @@ rel/example_project .concrete/DEV_MODE .rebar _build/ +# ---> Intellij +.idea/ +*.iml \ No newline at end of file diff --git a/apps/log_monitor/src/coordinator.erl b/apps/log_monitor/src/coordinator.erl new file mode 100644 index 0000000..f897fa5 --- /dev/null +++ b/apps/log_monitor/src/coordinator.erl @@ -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. diff --git a/apps/log_monitor/src/log_monitor_sup.erl b/apps/log_monitor/src/log_monitor_sup.erl index 641e6c6..51640a9 100644 --- a/apps/log_monitor/src/log_monitor_sup.erl +++ b/apps/log_monitor/src/log_monitor_sup.erl @@ -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]}]} }. %%==================================================================== diff --git a/apps/log_monitor/src/mailer.erl b/apps/log_monitor/src/mailer.erl new file mode 100644 index 0000000..0e1f864 --- /dev/null +++ b/apps/log_monitor/src/mailer.erl @@ -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}]). diff --git a/apps/log_monitor/src/monitor.erl b/apps/log_monitor/src/monitor.erl index 75b82b8..2105f08 100644 --- a/apps/log_monitor/src/monitor.erl +++ b/apps/log_monitor/src/monitor.erl @@ -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}. diff --git a/config/sys.config b/config/sys.config index 8c9df77..889f6c9 100644 --- a/config/sys.config +++ b/config/sys.config @@ -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, ""} + ] + } + ] + } ]. diff --git a/rebar.config b/rebar.config index 865607e..705dafd 100644 --- a/rebar.config +++ b/rebar.config @@ -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" }, diff --git a/rebar.lock b/rebar.lock index b09d7ca..b5f8823 100644 --- a/rebar.lock +++ b/rebar.lock @@ -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}].