Subject placeholder for first error line

This commit is contained in:
Fabio Salvini 2017-07-04 21:21:15 +02:00
parent bf31928336
commit dbeec61590
6 changed files with 32 additions and 20 deletions

View File

@ -1,5 +1,4 @@
TODO TODO
===== =====
- Default error_regex for group. - Default error_regex for group.
- Subject placeholder for first error line.
- Limit number of emails that can be sent in a period of time. - Limit number of emails that can be sent in a period of time.

View File

@ -97,8 +97,8 @@ handle_call({reload}, _From, State = #state{statuses = Statuses}) ->
{ok, File} = application:get_env(log_monitor, logfiles_config), {ok, File} = application:get_env(log_monitor, logfiles_config),
{ok, Terms} = file:consult(File), {ok, Terms} = file:consult(File),
MonitoredLogs = proplists:get_value(monitored_logs, Terms), MonitoredLogs = proplists:get_value(monitored_logs, Terms),
Groups = [#log_monitor_group{name = Name, email_receivers = EmailReceivers, email_subject = EmailSubject} Groups = [#log_monitor_group{name = Name, email_receivers = EmailReceivers, email_subject = EmailSubject, default_error_regex = DefaultErrorRegex}
|| {{group, Name}, {email_receivers, EmailReceivers}, {email_subject, EmailSubject}, _, _} <- MonitoredLogs], || {{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] 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]), || {{group, Name}, {email_receivers, _}, {email_subject, _}, {default_error_regex, _}, {logfiles, Logfiles}} <- MonitoredLogs]),
GroupsPartitions = compare_groups(Groups), GroupsPartitions = compare_groups(Groups),
@ -186,9 +186,9 @@ code_change(_OldVsn, State, _Extra) ->
%%%=================================================================== %%%===================================================================
compare_groups(Groups) -> compare_groups(Groups) ->
Old = list_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), Deleted = lists:filter(fun(Name) -> not lists:any(fun(Group) -> Group#log_monitor_group.name == Name end, Groups) end, Old),
Existing = lists:filter(fun(#log_monitor_group{name = Name, email_receivers = _, email_subject = _}) -> lists:member(Name, Old) end, Groups), Existing = lists:filter(fun(Group) -> lists:member(Group#log_monitor_group.name, Old) end, Groups),
New = lists:filter(fun(#log_monitor_group{name = Name, email_receivers = _, email_subject = _}) -> not lists:member(Name, Old) end, Groups), New = lists:filter(fun(Group) -> not lists:member(Group#log_monitor_group.name, Old) end, Groups),
[ [
{deleted, Deleted}, {deleted, Deleted},
{existing, Existing}, {existing, Existing},
@ -227,8 +227,8 @@ list_groups() ->
transaction, transaction,
fun() -> fun() ->
mnesia:foldl( mnesia:foldl(
fun(#log_monitor_group{name = Name, email_receivers = _}, Acc) -> fun(Group, Acc) ->
[Name | Acc] [Group#log_monitor_group.name | Acc]
end, [], log_monitor_group) end, [], log_monitor_group)
end). end).

View File

@ -186,11 +186,17 @@ send_email(File, Text) ->
-> [DefaultReceiver]; -> [DefaultReceiver];
true -> GroupReceivers true -> GroupReceivers
end, end,
Subject = email_subject(RawSubject, 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]), 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( gen_smtp_client:send_blocking(
{Sender, Receivers, {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 Connection
) )
catch catch
@ -235,9 +241,10 @@ group_subject(File) ->
end end
end). end).
email_subject(Text, File) -> email_subject(Text, File, FirstErrorLine) ->
Text1 = re:replace(Text, "%f", File, [global, {return, list}]), 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) -> file_name_from_path(File) ->
{match, [_, _, Basename]} = re:run(File, "^(.*\/)?(.*)\\..*$", [{capture, all, list}]), {match, [_, _, Basename]} = re:run(File, "^(.*\/)?(.*)\\..*$", [{capture, all, list}]),
@ -252,10 +259,14 @@ start_test() ->
{ok, _Pid} = start_link(). {ok, _Pid} = start_link().
email_subject_test() -> email_subject_test() ->
?assertEqual("Error", email_subject("Error", "/var/log/myApp.log")), ?assertEqual("Error", email_subject("Error", "/var/log/myApp.log", "Line")),
?assertEqual( ?assertEqual(
"[myApp] Error: /var/log/myApp.log", "[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() -> file_name_from_path_test() ->

View File

@ -10,7 +10,7 @@
-define(MNESIA_HRL, 1). -define(MNESIA_HRL, 1).
%% Mnesia tables %% 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_file, {file, error_regex, group}).
-record(log_monitor_error, {id, file, text}). -record(log_monitor_error, {id, file, text}).

View File

@ -30,11 +30,12 @@
%% Placeholders: %% Placeholders:
%% %f: the full path of the log file (ex. /var/log/myapp.log). %% %f: the full path of the log file (ex. /var/log/myapp.log).
%% %F: the basename of the log file (ex. myapp). %% %F: the basename of the log file (ex. myapp).
%% %l: the first error line.
{subject, "[%F] Error notification"}, {subject, "[%F] Error notification"},
{connection, [ {connection, [
{relay, "smtp.fabiosalvinii.com"}, {relay, "smtp.nodomain.com"},
{username, "salvini.fabio001@gmail.com"}, {username, ""},
{password, ""}] {password, ""}]
} }
] ]

View File

@ -1,8 +1,9 @@
{monitored_logs, [ {monitored_logs, [
{ {
{group, "Test"}, {group, "Test"},
{email_receivers, ["user@example.com"]}, {email_receivers, []},
{email_subject, "[%F] Error notification"}, {email_subject, "[%F] %l"},
%% Currently ignored
{default_error_regex, "Error"}, {default_error_regex, "Error"},
{logfiles, [ {logfiles, [
{ {