External configuration and supervisor child modules

This commit is contained in:
Fabio Salvini 2017-06-12 19:44:06 +02:00
parent 9a6464aa31
commit af8fad987d
6 changed files with 44 additions and 30 deletions

View File

@ -13,20 +13,23 @@ init([]) ->
id => mailer, id => mailer,
start => {mailer, start_link, []}, start => {mailer, start_link, []},
restart => permanent, restart => permanent,
shutdown => 5000 shutdown => 5000,
modules => [mailer]
}, },
#{ #{
id => logfiles_sup, id => logfiles_sup,
start => {logfiles_sup, start_link, []}, start => {logfiles_sup, start_link, []},
restart => permanent, restart => permanent,
type => supervisor, type => supervisor,
shutdown => 5000 shutdown => 5000,
modules => [logfiles_sup]
}, },
#{ #{
id => config, id => config,
start => {config, start_link, []}, start => {config, start_link, []},
restart => permanent, restart => permanent,
shutdown => 5000 shutdown => 5000,
modules => [config]
} }
], ],
{ok, {SupFlags, ChildSpecs}}. {ok, {SupFlags, ChildSpecs}}.

View File

@ -13,12 +13,14 @@ init([File, ErrorRegex]) ->
id => gatherer, id => gatherer,
start => {gatherer, start_link, [File, ErrorRegex]}, start => {gatherer, start_link, [File, ErrorRegex]},
restart => permanent, restart => permanent,
shutdown => 5000 shutdown => 5000,
modules => [gatherer]
}, },
#{ #{
id => watcher, id => watcher,
start => {watcher, start_link, [self(), File]}, start => {watcher, start_link, [self(), File]},
restart => permanent, restart => permanent,
shutdown => 5000 shutdown => 5000,
modules => [watcher]
}], }],
{ok, {SupFlags, ChildSpecs}}. {ok, {SupFlags, ChildSpecs}}.

View File

@ -21,10 +21,11 @@ add_child(Args = [File, _ErrorRegex]) ->
supervisor:start_child( supervisor:start_child(
?MODULE, ?MODULE,
#{ #{
id => File, id => File,
start => {log_sup, start_link, Args}, start => {log_sup, start_link, Args},
restart => temporary, restart => temporary,
shutdown => 5000 shutdown => 5000,
modules => [log_sup]
} }
). ).

View File

@ -34,12 +34,12 @@ code_change(_OldVsn, State, _Extra) ->
send_email(File, Text) -> 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, "log@monitor.com"), Sender = proplists:get_value(sender, EmailConfig),
DefaultReceiver = proplists:get_value(default_receiver, EmailConfig), DefaultReceiver = proplists:get_value(default_receiver, EmailConfig),
Relay = proplists:get_value(relay, EmailConfig), Connection = proplists:get_value(connection, EmailConfig),
Username = proplists:get_value(username, EmailConfig),
Password = proplists:get_value(password, EmailConfig),
error_logger:info_report("Sending email for file ~s with text: ~n~s~n", [File, Text]), error_logger:info_report("Sending email for file ~s with text: ~n~s~n", [File, Text]),
gen_smtp_client:send_blocking({Sender, [DefaultReceiver], gen_smtp_client:send_blocking(
io_lib:format("Subject: Error notification\r\nFrom: Fabio\r\n\r\nLogfile: ~s~nError: ~n~s~n", [File, Text])}, {Sender, [DefaultReceiver],
[{relay, Relay}, {username, Username}, {password, Password}]). io_lib:format("Subject: Error notification\r\nFrom: Fabio\r\n\r\nLogfile: ~s~nError: ~n~s~n", [File, Text])},
Connection
).

20
config/log_monitor.config Normal file
View File

@ -0,0 +1,20 @@
[
{ log_monitor,
[
%% File where the list of monitored log files is stored.
%% WARNING: If you leave it inside /tmp, every time the node is restarted the log files will be lost.
{storage, "/tmp/logfiles"},
{email_config,
[
{sender, "log@monitor.com"},
{default_receiver, "salvini.fabio001@gmail.com"},
{connection, [
{relay, "smtp.fabiosalvinii.com"},
{username, "salvini.fabio001@gmail.com"},
{password, ""}]
}
]
}
]
}
].

View File

@ -1,16 +1,4 @@
[ [
{ log_monitor, { log_monitor, []
[ }, "/home/fsalvini/gitRepos/log_monitor/config/log_monitor.config"
{storage, "/tmp/logfiles"},
{email_config,
[
{sender, "me@example.com"},
{default_receiver, "salvini.fabio001@gmail.com"},
{relay, "smtp.fabiosalvinii.com"},
{username, "salvini.fabio001@gmail.com"},
{password, ""}
]
}
]
}
]. ].