Custom subject for groups
This commit is contained in:
parent
34948d7319
commit
bf31928336
2
TODO.md
2
TODO.md
|
@ -1,3 +1,5 @@
|
||||||
TODO
|
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.
|
- Limit number of emails that can be sent in a period of time.
|
||||||
|
|
|
@ -97,9 +97,10 @@ 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} || {{group, Name}, {email_receivers, EmailReceivers}, _} <- MonitoredLogs],
|
Groups = [#log_monitor_group{name = Name, email_receivers = EmailReceivers, email_subject = EmailSubject}
|
||||||
|
|| {{group, Name}, {email_receivers, EmailReceivers}, {email_subject, EmailSubject}, _, _} <- 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, _}, {logfiles, Logfiles}} <- MonitoredLogs]),
|
|| {{group, Name}, {email_receivers, _}, {email_subject, _}, {default_error_regex, _}, {logfiles, Logfiles}} <- MonitoredLogs]),
|
||||||
GroupsPartitions = compare_groups(Groups),
|
GroupsPartitions = compare_groups(Groups),
|
||||||
manage_deleted_groups(proplists:get_value(deleted, GroupsPartitions)),
|
manage_deleted_groups(proplists:get_value(deleted, GroupsPartitions)),
|
||||||
manage_existing_groups(proplists:get_value(existing, GroupsPartitions)),
|
manage_existing_groups(proplists:get_value(existing, GroupsPartitions)),
|
||||||
|
@ -185,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 = _}) -> N == Name end, Groups) end, Old),
|
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 = _}) -> lists:member(Name, Old) end, Groups),
|
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 = _}) -> not 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, Deleted},
|
{deleted, Deleted},
|
||||||
{existing, Existing},
|
{existing, Existing},
|
||||||
|
|
|
@ -175,7 +175,10 @@ send_email(File, Text) ->
|
||||||
{ok, EmailConfig} = application:get_env(log_monitor, email_config),
|
{ok, EmailConfig} = application:get_env(log_monitor, email_config),
|
||||||
Sender = proplists:get_value(sender, EmailConfig),
|
Sender = proplists:get_value(sender, EmailConfig),
|
||||||
DefaultReceiver = proplists:get_value(default_receiver, EmailConfig),
|
DefaultReceiver = proplists:get_value(default_receiver, EmailConfig),
|
||||||
RawSubject = proplists:get_value(subject, EmailConfig),
|
RawSubject = case group_subject(File) of
|
||||||
|
"" -> proplists:get_value(subject, EmailConfig);
|
||||||
|
GroupSubject -> GroupSubject
|
||||||
|
end,
|
||||||
Connection = proplists:get_value(connection, EmailConfig),
|
Connection = proplists:get_value(connection, EmailConfig),
|
||||||
try receivers(File) of
|
try receivers(File) of
|
||||||
GroupReceivers ->
|
GroupReceivers ->
|
||||||
|
@ -184,6 +187,7 @@ send_email(File, Text) ->
|
||||||
true -> GroupReceivers
|
true -> GroupReceivers
|
||||||
end,
|
end,
|
||||||
Subject = email_subject(RawSubject, File),
|
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]),
|
||||||
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])},
|
||||||
|
@ -207,16 +211,30 @@ receivers(File) ->
|
||||||
transaction,
|
transaction,
|
||||||
fun() ->
|
fun() ->
|
||||||
case mnesia:read(log_monitor_file, File) of
|
case mnesia:read(log_monitor_file, File) of
|
||||||
[{log_monitor_file, File, _, Group}] ->
|
[{log_monitor_file, File, _, GroupName}] ->
|
||||||
case mnesia:read(log_monitor_group, Group) of
|
case mnesia:read(log_monitor_group, GroupName) of
|
||||||
[{log_monitor_group, Group, EmailReceivers}] ->
|
[Group] ->
|
||||||
EmailReceivers;
|
Group#log_monitor_group.email_receivers;
|
||||||
_ -> throw(file_group_not_found)
|
_ -> throw(file_group_not_found)
|
||||||
end;
|
end;
|
||||||
_ -> throw(file_not_found)
|
_ -> throw(file_not_found)
|
||||||
end
|
end
|
||||||
end).
|
end).
|
||||||
|
|
||||||
|
group_subject(File) ->
|
||||||
|
mnesia:activity(
|
||||||
|
transaction,
|
||||||
|
fun() ->
|
||||||
|
case mnesia:read(log_monitor_file, File) of
|
||||||
|
[Logfile] ->
|
||||||
|
case mnesia:read(log_monitor_group, Logfile#log_monitor_file.group) of
|
||||||
|
[Group] -> Group#log_monitor_group.email_subject;
|
||||||
|
_ -> ""
|
||||||
|
end;
|
||||||
|
_ -> ""
|
||||||
|
end
|
||||||
|
end).
|
||||||
|
|
||||||
email_subject(Text, File) ->
|
email_subject(Text, File) ->
|
||||||
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}]).
|
re:replace(Text1, "%F", file_name_from_path(File), [global, {return, list}]).
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
-define(MNESIA_HRL, 1).
|
-define(MNESIA_HRL, 1).
|
||||||
|
|
||||||
%% Mnesia tables
|
%% Mnesia tables
|
||||||
-record(log_monitor_group, {name, email_receivers=[]}).
|
-record(log_monitor_group, {name, email_receivers=[], email_subject=""}).
|
||||||
-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}).
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
{
|
{
|
||||||
{group, "Test"},
|
{group, "Test"},
|
||||||
{email_receivers, ["user@example.com"]},
|
{email_receivers, ["user@example.com"]},
|
||||||
|
{email_subject, "[%F] Error notification"},
|
||||||
|
{default_error_regex, "Error"},
|
||||||
{logfiles, [
|
{logfiles, [
|
||||||
{
|
{
|
||||||
{file, "/tmp/lines.log"},
|
{file, "/tmp/lines.log"},
|
||||||
|
|
Loading…
Reference in New Issue
Block a user