Moved tail cmd to config file

This commit is contained in:
Fabio Salvini 2017-07-03 15:35:43 +02:00
parent 87930f7c3b
commit 90314e4080
3 changed files with 25 additions and 20 deletions

View File

@ -82,8 +82,8 @@ init([File, ErrorRegex]) ->
state_off({log_line, Text}, State = #state_off{log = Log}) -> state_off({log_line, Text}, State = #state_off{log = Log}) ->
case isError(Text, Log#log.error_regex) of case isError(Text, Log#log.error_regex) of
true -> true ->
Timeout = timer_time(), Timeout = gathering_time(),
MaxTimeout = safe_timer_time(), MaxTimeout = max_gathering_time(),
Now = to_milliseconds(os:timestamp()), Now = to_milliseconds(os:timestamp()),
Until = Now + Timeout, Until = Now + Timeout,
MaxUntil = Now + MaxTimeout, MaxUntil = Now + MaxTimeout,
@ -112,7 +112,7 @@ state_off({log_line, Text}, State = #state_off{log = Log}) ->
state_on({log_line, Text}, #state_on{log = Log, error = Error, until = Until, max_until = MaxUntil}) -> state_on({log_line, Text}, #state_on{log = Log, error = Error, until = Until, max_until = MaxUntil}) ->
case isError(Text, Log#log.error_regex) of case isError(Text, Log#log.error_regex) of
true -> true ->
Timeout = timer_time(), Timeout = gathering_time(),
Now = to_milliseconds(os:timestamp()), Now = to_milliseconds(os:timestamp()),
NewUntil = min(Now + Timeout, MaxUntil), NewUntil = min(Now + Timeout, MaxUntil),
{next_state, {next_state,
@ -220,12 +220,12 @@ isError(Text, ErrorRegex) ->
false false
end. end.
timer_time() -> gathering_time() ->
{ok, ProcessingConfig} = application:get_env(log_monitor, processing_config), {ok, ProcessingConfig} = application:get_env(log_monitor, general),
proplists:get_value(gathering_time, ProcessingConfig, ?DEFAULT_TIMER_TIME). proplists:get_value(gathering_time, ProcessingConfig, ?DEFAULT_TIMER_TIME).
safe_timer_time() -> max_gathering_time() ->
{ok, ProcessingConfig} = application:get_env(log_monitor, processing_config), {ok, ProcessingConfig} = application:get_env(log_monitor, general),
proplists:get_value(max_gathering_time, ProcessingConfig, ?DEFAULT_TIMER_TIME). proplists:get_value(max_gathering_time, ProcessingConfig, ?DEFAULT_TIMER_TIME).
to_milliseconds({Me, S, Mu}) -> to_milliseconds({Me, S, Mu}) ->

View File

@ -18,7 +18,6 @@
terminate/2, code_change/3]). terminate/2, code_change/3]).
-define(SERVER, ?MODULE). -define(SERVER, ?MODULE).
-define(TAIL_CMD, "/usr/bin/tail -n0 --follow=name ").
-record(state, {file, supervisor, port}). -record(state, {file, supervisor, port}).
@ -54,7 +53,9 @@ start_link(SupPid, File) ->
init([SupPid, File]) -> init([SupPid, File]) ->
timer:sleep(1000), timer:sleep(1000),
config ! {watcher_init, File}, config ! {watcher_init, File},
Cmd = ?TAIL_CMD ++ File, {ok, GeneralConfig} = application:get_env(log_monitor, general),
TailCmd = proplists:get_value(tail_cmd, GeneralConfig),
Cmd = TailCmd ++ " " ++ File,
Port = open_port({spawn, Cmd}, [stderr_to_stdout, exit_status, binary]), Port = open_port({spawn, Cmd}, [stderr_to_stdout, exit_status, binary]),
{ok, #state{file = File, supervisor = SupPid, port = Port}}. {ok, #state{file = File, supervisor = SupPid, port = Port}}.

View File

@ -1,6 +1,21 @@
[ [
{ log_monitor, { log_monitor,
[ [
{general,
[
%% Command to watch the log files.
{tail_cmd, "/usr/bin/tail -n0 --follow=name"},
%% How long to gather log lines after an error occurs.
%% If another error occurs, the time is reset.
{gathering_time, 1000},
%% Max time for gathering log lines.
%% Precaution in case of infinite consecutive errors.
{max_gathering_time, 30000}
]
},
{email_config, {email_config,
[ [
{sender, "log@monitor.com"}, {sender, "log@monitor.com"},
@ -24,17 +39,6 @@
} }
] ]
}, },
{processing_config,
[
%% How long to gather log lines after an error occurs.
%% If another error occurs, the time is reset.
{gathering_time, 1000},
%% Max time for gathering log lines.
%% Precaution in case of infinite consecutive errors.
{max_gathering_time, 30000}
]
},
%% Location of the file that contains the logs to monitor. %% Location of the file that contains the logs to monitor.
{logfiles_config, "/home/fsalvini/gitRepos/log_monitor/config/logfiles.config"} {logfiles_config, "/home/fsalvini/gitRepos/log_monitor/config/logfiles.config"}