Added Rebar3 project files

This commit is contained in:
Fabio Salvini 2017-03-11 01:31:41 +01:00
parent a2346a343f
commit f08e5f3e77
13 changed files with 233 additions and 8 deletions

2
.gitignore vendored
View File

@ -9,4 +9,4 @@ ebin
rel/example_project
.concrete/DEV_MODE
.rebar
_build/

31
LICENSE
View File

@ -1,8 +1,29 @@
MIT License
Copyright (c) <year> <copyright holders>
Copyright (c) 2017, Fabio Salvini <salvini.fabio001@gmail.com>.
All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* The names of its contributors may not be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@ -1,3 +1,9 @@
# log_monitor
log_monitor
=====
Erlang application to monitor multiple log files and be notified when errors occur.
Erlang OTP application to monitor multiple log files and be notified when errors occur.
Build
-----
$ rebar3 compile

View File

@ -0,0 +1,17 @@
{application, log_monitor,
[{description, "An OTP application"},
{vsn, "0.1.0"},
{registered, []},
{mod, { log_monitor_app, []}},
{applications,
[kernel,
stdlib,
gen_smtp
]},
{env,[]},
{modules, []},
{maintainers, []},
{licenses, []},
{links, []}
]}.

View File

@ -0,0 +1,26 @@
%%%-------------------------------------------------------------------
%% @doc log_monitor public API
%% @end
%%%-------------------------------------------------------------------
-module(log_monitor_app).
-behaviour(application).
%% Application callbacks
-export([start/2, stop/1]).
%%====================================================================
%% API
%%====================================================================
start(_StartType, _StartArgs) ->
log_monitor_sup:start_link().
%%--------------------------------------------------------------------
stop(_State) ->
ok.
%%====================================================================
%% Internal functions
%%====================================================================

View File

@ -0,0 +1,37 @@
%%%-------------------------------------------------------------------
%% @doc log_monitor top level supervisor.
%% @end
%%%-------------------------------------------------------------------
-module(log_monitor_sup).
-behaviour(supervisor).
%% API
-export([start_link/0]).
%% Supervisor callbacks
-export([init/1]).
-define(SERVER, ?MODULE).
%%====================================================================
%% API functions
%%====================================================================
start_link() ->
supervisor:start_link({local, ?SERVER}, ?MODULE, []).
%%====================================================================
%% Supervisor callbacks
%%====================================================================
%% Child :: {Id,StartFunc,Restart,Shutdown,Type,Modules}
init([]) ->
{ok, { {one_for_all, 0, 1}, [{console,
{monitor, start, []},
permanent, 5000, worker, [monitor]}]} }.
%%====================================================================
%% Internal functions
%%====================================================================

View File

@ -0,0 +1,63 @@
-module(monitor).
-compile(export_all).
start() ->
init("/tmp/lines.log").
start(File) ->
spawn_link(?MODULE, init, [File]).
init(File) ->
process_flag(trap_exit, true),
startWatcher(File),
loop(File).
loop(File) ->
receive
{log_line, Text} ->
case re:run(Text, "ERROR") of
{match, _} -> errorGathering(File, Text);
nomatch -> loop(File)
end,
io:format("Received line: ~s~n", [Text]),
loop(File);
{'EXIT', _FromPid, Reason} ->
io:format("Watcher process terminated: ~s~n", [Reason]),
startWatcher(File),
loop(File)
end.
errorGathering(File, Error) ->
receive
{log_line, Text} ->
IsLog = isLogLine(Text),
if IsLog ->
io:format("Send email with text: ~s~n", [Error]),
IsError = isError(Text),
if IsError -> errorGathering(File, Text);
true -> loop(File)
end;
true ->
errorGathering(File, Error ++ "\n" ++ Text)
end
end.
isError(Text) ->
case re:run(Text, "ERROR") of %% TODO
{match, _} ->
true;
nomatch ->
false
end.
isLogLine(Text) ->
case re:run(Text, "2017") of %% TODO
{match, _} ->
true;
nomatch ->
false
end.
startWatcher(File) ->
WatcherPid = spawn_link(watcher, init, [self(), File]),
{ok, WatcherPid}.

View File

@ -0,0 +1 @@
[].

View File

@ -0,0 +1,19 @@
-module(watcher).
-compile(export_all).
start(MonitorPid, File) ->
spawn_link(?MODULE, init, [MonitorPid, File]).
init(MonitorPid, File) ->
Cmd = "/usr/bin/tail -n0 --follow=name " ++ File,
Port = open_port({spawn, Cmd}, [stderr_to_stdout, {line, 256}, exit_status, binary]),
loop(MonitorPid, Port).
loop(MonitorPid, Port) ->
receive
{Port, {data, {eol, Bin}}} ->
%% io:format("~s~n", [iolist_to_binary(Bin)]),
Text = binary_to_list(iolist_to_binary(Bin)),
MonitorPid ! {log_line, Text},
loop(MonitorPid, Port)
end.

3
config/sys.config Normal file
View File

@ -0,0 +1,3 @@
[
{ log_monitor, []}
].

6
config/vm.args Normal file
View File

@ -0,0 +1,6 @@
-sname log_monitor
-setcookie log_monitor_cookie
+K true
+A30

22
rebar.config Normal file
View File

@ -0,0 +1,22 @@
{erl_opts, [debug_info]}.
{deps, [
{gen_smtp, ".*", {git,"https://github.com/Vagabond/gen_smtp", {tag, "0.11.0"}}}
]}.
{relx, [{release, { log_monitor, "0.1.0" },
[log_monitor,
sasl]},
{sys_config, "./config/sys.config"},
{vm_args, "./config/vm.args"},
{dev_mode, true},
{include_erts, false},
{extended_start_script, true}]
}.
{profiles, [{prod, [{relx, [{dev_mode, false},
{include_erts, true}]}]
}]
}.

4
rebar.lock Normal file
View File

@ -0,0 +1,4 @@
[{<<"gen_smtp">>,
{git,"https://github.com/Vagabond/gen_smtp",
{ref,"2ea8bb995adf32102f523cef93ae98e287ac77d1"}},
0}].