From dbeec61590da16b25dfa8d7e187d9e55edcb2c2d Mon Sep 17 00:00:00 2001 From: Fabio Salvini Date: Tue, 4 Jul 2017 21:21:15 +0200 Subject: [PATCH] Subject placeholder for first error line --- TODO.md | 1 - apps/log_monitor/src/config.erl | 14 +++++++------- apps/log_monitor/src/mailer.erl | 25 ++++++++++++++++++------- apps/log_monitor/src/mnesia_tables.hrl | 2 +- config/log_monitor.config | 5 +++-- config/logfiles.config | 5 +++-- 6 files changed, 32 insertions(+), 20 deletions(-) diff --git a/TODO.md b/TODO.md index 2f5af33..30b41f9 100644 --- a/TODO.md +++ b/TODO.md @@ -1,5 +1,4 @@ TODO ===== - Default error_regex for group. - - Subject placeholder for first error line. - Limit number of emails that can be sent in a period of time. diff --git a/apps/log_monitor/src/config.erl b/apps/log_monitor/src/config.erl index a450a72..a2ee1c5 100644 --- a/apps/log_monitor/src/config.erl +++ b/apps/log_monitor/src/config.erl @@ -97,8 +97,8 @@ handle_call({reload}, _From, State = #state{statuses = Statuses}) -> {ok, File} = application:get_env(log_monitor, logfiles_config), {ok, Terms} = file:consult(File), MonitoredLogs = proplists:get_value(monitored_logs, Terms), - Groups = [#log_monitor_group{name = Name, email_receivers = EmailReceivers, email_subject = EmailSubject} - || {{group, Name}, {email_receivers, EmailReceivers}, {email_subject, EmailSubject}, _, _} <- MonitoredLogs], + Groups = [#log_monitor_group{name = Name, email_receivers = EmailReceivers, email_subject = EmailSubject, default_error_regex = DefaultErrorRegex} + || {{group, Name}, {email_receivers, EmailReceivers}, {email_subject, EmailSubject}, {default_error_regex, DefaultErrorRegex}, _} <- MonitoredLogs], Logfiles = utils:flatten([[#log_monitor_file{file = F, error_regex = ErrorRegex, group = Name} || {{file, F}, {error_regex, ErrorRegex}} <- Logfiles] || {{group, Name}, {email_receivers, _}, {email_subject, _}, {default_error_regex, _}, {logfiles, Logfiles}} <- MonitoredLogs]), GroupsPartitions = compare_groups(Groups), @@ -186,9 +186,9 @@ code_change(_OldVsn, State, _Extra) -> %%%=================================================================== compare_groups(Groups) -> Old = list_groups(), - Deleted = lists:filter(fun(Name) -> not lists:any(fun(#log_monitor_group{name = N, email_receivers = _, email_subject = _}) -> N == Name end, Groups) end, Old), - Existing = lists:filter(fun(#log_monitor_group{name = Name, email_receivers = _, email_subject = _}) -> lists:member(Name, Old) end, Groups), - New = lists:filter(fun(#log_monitor_group{name = Name, email_receivers = _, email_subject = _}) -> not lists:member(Name, Old) end, Groups), + Deleted = lists:filter(fun(Name) -> not lists:any(fun(Group) -> Group#log_monitor_group.name == Name end, Groups) end, Old), + Existing = lists:filter(fun(Group) -> lists:member(Group#log_monitor_group.name, Old) end, Groups), + New = lists:filter(fun(Group) -> not lists:member(Group#log_monitor_group.name, Old) end, Groups), [ {deleted, Deleted}, {existing, Existing}, @@ -227,8 +227,8 @@ list_groups() -> transaction, fun() -> mnesia:foldl( - fun(#log_monitor_group{name = Name, email_receivers = _}, Acc) -> - [Name | Acc] + fun(Group, Acc) -> + [Group#log_monitor_group.name | Acc] end, [], log_monitor_group) end). diff --git a/apps/log_monitor/src/mailer.erl b/apps/log_monitor/src/mailer.erl index 99cc391..9c917ca 100644 --- a/apps/log_monitor/src/mailer.erl +++ b/apps/log_monitor/src/mailer.erl @@ -186,11 +186,17 @@ send_email(File, Text) -> -> [DefaultReceiver]; true -> GroupReceivers end, - Subject = email_subject(RawSubject, File), - error_logger:info_msg("Sending email [~n\tSender: ~s,~n\tReceivers: ~s,~n\tSubject: ~s,~n\tFile: ~s~n]~n", [Sender, lists:join(",", Receivers), Subject, File]), + Subject = email_subject(RawSubject, File, lists:takewhile(fun(X) -> X =/= "\n" end, Text)), + error_logger:info_msg( + "Sending email [~n\tSender: ~s,~n\tReceivers: ~s,~n\tSubject: ~s,~n\tFile: ~s~n]~n", + [Sender, lists:join(",", Receivers), Subject, File] + ), gen_smtp_client:send_blocking( {Sender, Receivers, - io_lib:format("Subject: ~s\r\nFrom: ~s\r\n\r\nLogfile: ~s~nError: ~n~s~n", [Subject, Sender, File, Text])}, + io_lib:format( + "Subject: ~s\r\nFrom: ~s\r\n\r\nLogfile: ~s~nError: ~n~s~n", + [Subject, Sender, File, Text] + )}, Connection ) catch @@ -235,9 +241,10 @@ group_subject(File) -> end end). -email_subject(Text, File) -> +email_subject(Text, File, FirstErrorLine) -> Text1 = re:replace(Text, "%f", File, [global, {return, list}]), - re:replace(Text1, "%F", file_name_from_path(File), [global, {return, list}]). + Text2 = re:replace(Text1, "%F", file_name_from_path(File), [global, {return, list}]), + re:replace(Text2, "%l", FirstErrorLine, [global, {return, list}]). file_name_from_path(File) -> {match, [_, _, Basename]} = re:run(File, "^(.*\/)?(.*)\\..*$", [{capture, all, list}]), @@ -252,10 +259,14 @@ start_test() -> {ok, _Pid} = start_link(). email_subject_test() -> - ?assertEqual("Error", email_subject("Error", "/var/log/myApp.log")), + ?assertEqual("Error", email_subject("Error", "/var/log/myApp.log", "Line")), ?assertEqual( "[myApp] Error: /var/log/myApp.log", - email_subject("[%F] Error: %f", "/var/log/myApp.log") + email_subject("[%F] Error: %f", "/var/log/myApp.log", "Line") + ), + ?assertEqual( + "[myApp] Line", + email_subject("[%F] %l", "/var/log/myApp.log", "Line") ). file_name_from_path_test() -> diff --git a/apps/log_monitor/src/mnesia_tables.hrl b/apps/log_monitor/src/mnesia_tables.hrl index d3f86ee..c20d0c9 100644 --- a/apps/log_monitor/src/mnesia_tables.hrl +++ b/apps/log_monitor/src/mnesia_tables.hrl @@ -10,7 +10,7 @@ -define(MNESIA_HRL, 1). %% Mnesia tables --record(log_monitor_group, {name, email_receivers=[], email_subject=""}). +-record(log_monitor_group, {name, email_receivers=[], email_subject="", default_error_regex=""}). -record(log_monitor_file, {file, error_regex, group}). -record(log_monitor_error, {id, file, text}). diff --git a/config/log_monitor.config b/config/log_monitor.config index a396883..6afc9b5 100644 --- a/config/log_monitor.config +++ b/config/log_monitor.config @@ -30,11 +30,12 @@ %% Placeholders: %% %f: the full path of the log file (ex. /var/log/myapp.log). %% %F: the basename of the log file (ex. myapp). + %% %l: the first error line. {subject, "[%F] Error notification"}, {connection, [ - {relay, "smtp.fabiosalvinii.com"}, - {username, "salvini.fabio001@gmail.com"}, + {relay, "smtp.nodomain.com"}, + {username, ""}, {password, ""}] } ] diff --git a/config/logfiles.config b/config/logfiles.config index 1216909..f9a3a8e 100644 --- a/config/logfiles.config +++ b/config/logfiles.config @@ -1,8 +1,9 @@ {monitored_logs, [ { {group, "Test"}, - {email_receivers, ["user@example.com"]}, - {email_subject, "[%F] Error notification"}, + {email_receivers, []}, + {email_subject, "[%F] %l"}, + %% Currently ignored {default_error_regex, "Error"}, {logfiles, [ {