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
=====
- Default error_regex for group.
- Subject placeholder for first error line.
- 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, 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).

View File

@ -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() ->

View File

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

View File

@ -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, ""}]
}
]

View File

@ -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, [
{