Dynamically add log files to monitor

This commit is contained in:
Fabio Salvini 2017-06-12 00:09:27 +02:00
parent 2f74f322ef
commit 3ea2c5c55e
7 changed files with 52 additions and 16 deletions

View File

@ -1,6 +1,8 @@
-module(coordinator).
-behaviour(supervisor).
-compile(export_all).
-export([start_link/0]).
-export([init/1]).
start_link() ->
supervisor:start_link(?MODULE, []).
@ -12,14 +14,13 @@ init([]) ->
start => {mailer, start_link, []},
restart => permanent,
shutdown => 5000
}] ++ monitors_child_specs(),
{ok, {SupFlags, ChildSpecs}}.
monitors_child_specs() ->
{ok, Files} = application:get_env(log_monitor, logfiles),
[#{
id => list_to_atom(File),
start => {monitor, start_link, [File]},
restart => temporary,
},
#{
id => logfiles_sup,
start => {logfiles_sup, start_link, []},
restart => permanent,
type => supervisor,
shutdown => 5000
} || File <- Files].
}
],
{ok, {SupFlags, ChildSpecs}}.

View File

@ -1,6 +1,9 @@
-module(gatherer).
-behaviour(gen_server).
-compile(export_all).
-export([start_link/1, init/1, terminate/2]).
-export([handle_info/2, handle_cast/2, handle_call/3]).
-export([code_change/3]).
start_link(File) ->
gen_server:start_link(?MODULE, [File], []).

View File

@ -0,0 +1,25 @@
-module(logfiles_sup).
-behaviour(supervisor).
-export([start_link/0]).
-export([init/1]).
start_link() ->
supervisor:start_link({local, logfiles_sup}, ?MODULE, []).
init([]) ->
SupFlags = #{
strategy => simple_one_for_one,
intensity => 0,
period => 1
},
ChildSpecs = [#{
id => monitor,
start => {monitor, start_link, []},
restart => temporary,
shutdown => 5000
}],
{ok, {SupFlags, ChildSpecs}}.
%% Start monitor with:
%% supervisor:start_child(logfiles_sup, "/tmp/lines.log")

View File

@ -1,6 +1,9 @@
-module(mailer).
-behaviour(gen_server).
-compile(export_all).
-export([start_link/0, init/1, terminate/2]).
-export([handle_info/2, handle_cast/2, handle_call/3]).
-export([code_change/3]).
start_link() ->
gen_server:start_link(?MODULE, [], []).

View File

@ -1,6 +1,8 @@
-module(monitor).
-behaviour(supervisor).
-compile(export_all).
-export([start_link/1]).
-export([init/1]).
start_link(File) ->
supervisor:start_link(?MODULE, [File]).

View File

@ -1,6 +1,9 @@
-module(watcher).
-behaviour(gen_server).
-compile(export_all).
-export([start_link/2, init/1, terminate/2]).
-export([handle_info/2, handle_cast/2, handle_call/3]).
-export([code_change/3]).
start_link(SupPid, File) ->
gen_server:start_link(?MODULE, [SupPid, File], []).

View File

@ -1,7 +1,6 @@
[
{ log_monitor,
[
{logfiles, ["/tmp/lines.log", "/tmp/lines2.log"]},
{email_config,
[
{sender, "me@example.com"},