Skip to content

Commit

Permalink
Add help. Add more params.
Browse files Browse the repository at this point in the history
  • Loading branch information
vjache committed May 15, 2012
1 parent b913d86 commit 98879db
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/error_log_tool.app.src
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
%% -*- mode: erlang; -*-
{application, error_log_tool,
[{description, "An Error Log Tool"},
{vsn, "0.1.2"},
{vsn, "0.2.0"},
{registered, []},
{build_dependencies, []},
{env, []},
Expand Down
14 changes: 12 additions & 2 deletions src/error_log_tool.erl
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ filter([]=_Options,LogZList) ->
LogZList;
filter([Opt|Tail]=_Options,LogZList) ->
case Opt of
'all' ->
Z=LogZList;
'sasl' ->
Z=zlists:filter(
fun({_N,{_, {_, _, {_, Type, _}} }})->
Expand All @@ -95,10 +97,18 @@ filter([Opt|Tail]=_Options,LogZList) ->
lists:member(Type, [error,error_report,warning_report,warning_msg]);
(_) -> false
end, LogZList);
{node, Node} ->
{nodes_inc, 'all'} ->
Z=LogZList;
{nodes_inc, Nodes} when is_list(Nodes) ->
Z=zlists:filter(
fun({_N,{_, {_, Pid, {_, _, _}} }})->
node(Pid) == Node;
lists:member(node(Pid), Nodes);
(_) -> false
end, LogZList);
{nodes_exc, Nodes} when is_list(Nodes) ->
Z=zlists:filter(
fun({_N,{_, {_, Pid, {_, _, _}} }})->
not lists:member(node(Pid), Nodes);
(_) -> false
end, LogZList);
_ ->
Expand Down
76 changes: 76 additions & 0 deletions src/error_log_tool_help_web.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
%%%-------------------------------------------------------------------
%%% @author <[email protected]>
%%% @copyright (C) 2011, Vyacheslav Vorobyov. All Rights Reserved.
%%% Licensed under the Apache License, Version 2.0 (the "License");
%%% you may not use this file except in compliance with the License.
%%% You may obtain a copy of the License at
%%%
%%% http://www.apache.org/licenses/LICENSE-2.0
%%%
%%% Unless required by applicable law or agreed to in writing, software
%%% distributed under the License is distributed on an "AS IS" BASIS,
%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%%% See the License for the specific language governing permissions and
%%% limitations under the License.
%%%
%%% @doc
%%% TODO: Document it.
%%% @end
%%% Created : May 15, 2012
%%%-------------------------------------------------------------------------------
-module(error_log_tool_help_web).

-behaviour(cowboy_http_handler).

%%
%% Include files
%%

%%
%% Exported Functions
%%
-export([init/3,handle/2]).

%%
%% API Functions
%%

init({_Any, http}, Req, []) ->
{ok, Req, undefined}.

handle(Req, State) ->
{ok, Req2} = cowboy_http_req:reply(200, [], help_page(), Req),
{ok, Req2, State}.

%%
%% Local Functions
%%

help_page() ->
<<"
Error Logger Tool Quick Start Help.
Examples:
/error_logger - show events for last half of an hour;
/error_logger?interval=2012-03-27T02:00:00.000Z - show events since specified Zulu time;
/error_logger?interval=2012-03-27T02:00:00.000Z&limit=100 - show at most 100 events since specified Zulu time;
/[email protected],[email protected] - show events only from specified nodes;
/[email protected] - show events from all nodes except specified one;
/error_logger?severity=error - show events of specified severity.
Details:
The output returned as chunked HTTP reply. To format events the code of standard error formatters is used
(form 'kernel' and 'sasl' apps). When specifying time all parts are mandatory and leading zeros are necessary,
be careful. Probably later some short cuts for time spec may be add.
Severity parameter may have the following values:
warn - error,error_report,warning_report,warning_msg
error - error,error_msg,error_report
sasl - supervisor_report,progress,crash_report
sasl-crash - crash_report
Author:
Vyacheslav Vorobyov, <[email protected]>
">>.

84 changes: 49 additions & 35 deletions src/error_log_tool_web.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,38 +35,27 @@
%% API Functions
%%

-record(state, {interval,nodes_inc,nodes_exc,severity,max_limit,min_limit}).

-define(ECHO(Msg), io:format(standard_error, "~p: ~p~n", [?LINE, Msg])).

init({_Any, http}, Req, []) ->
{ok, Req, undefined}.

handle(Req, State) ->
Options =
case cowboy_http_req:qs_val(<<"node">>, Req) of
{undefined,_} -> [];
{Val,_} -> [{node, list_to_atom(binary_to_list(Val))}]
end ++
case cowboy_http_req:qs_val(<<"severity">>, Req) of
{undefined,_} -> [];
{Val,_} -> [list_to_atom(binary_to_list(Val))]
end ++
case cowboy_http_req:qs_val(<<"limit">>, Req) of
{undefined,_} -> [];
{Val,_} -> [{limit, list_to_integer(binary_to_list(Val))}]
end,
case cowboy_http_req:qs_val(<<"interval">>, Req) of
{undefined,_} -> Timestamp= logmachine_util:millis_to_now(
logmachine_util:now_to_millis(now()) - 1800*1000);
{<<Y:4/binary,_,
M:2/binary,_,
D:2/binary,_,
H:2/binary,_,
Mn:2/binary,_,
S:2/binary,".",
Mls:3/binary,"Z">>,_} ->
Timestamp = {{to_int(Y),to_int(M),to_int(D)},
{to_int(H),to_int(Mn),to_int(S)}, to_int(Mls)}
end,
State=#state{
interval = parse_interval(qs_val(Req, <<"interval">>, <<>>)),
nodes_inc = parse_nodes( qs_val(Req, <<"nodes">>, <<>>)),
nodes_exc = parse_nodes( qs_val(Req, <<"nodes_exc">>,<<>>)),
severity = parse_severity(qs_val(Req, <<"severity">>, <<>>)),
max_limit = parse_integer( qs_val(Req, <<"limit">>, <<"10000">>)),
min_limit = parse_integer( qs_val(Req, <<"limit_min">>, <<"0">>))
},
{ok, Req, State}.

handle(Req, #state{interval=Timestamp,nodes_inc=NodesInc,nodes_exc=NodesExc,max_limit=LimitMax,min_limit=LimitMin,severity=Severity}=State) ->
Options = [{nodes_exc, NodesExc},
{nodes_inc, NodesInc},
{max_limit, LimitMax},
{min_limit, LimitMin},
Severity ],
ZList=logmachine:get_zlist(error_logger, Timestamp),
Headers = [{'Content-Type', <<"text/plain">>}],
{ok, Req2} = cowboy_http_req:chunked_reply(200, Headers, Req),
Expand All @@ -84,9 +73,34 @@ terminate(_Req, _State) ->

%%
%% Local Functions
%% %%
%%
%% id() ->
%% {Mega, Sec, Micro} = erlang:now(),
%% Id = (Mega * 1000000 + Sec) * 1000000 + Micro,
%% integer_to_list(Id, 16).
%%

qs_val(Req, Name, Default) ->
case cowboy_http_req:qs_val(Name, Req) of
{undefined,_} when is_function(Default,0) -> Default();
{undefined,_} -> Default;
{Val,_} -> Val
end.

parse_interval(<<>>) ->
logmachine_util:now_add(now(), -1800*1000*1000);
parse_interval(<<Y:4/binary,_,
M:2/binary,_,
D:2/binary,_,
H:2/binary,_,
Mn:2/binary,_,
S:2/binary,".",
Mls:3/binary,"Z">>) ->
{{to_int(Y),to_int(M),to_int(D)},
{to_int(H),to_int(Mn),to_int(S)}, to_int(Mls)}.

parse_nodes(NodesBStr) ->
[ binary_to_existing_atom(B, latin1) || B <- binary:split(NodesBStr, <<",">>, [global,trim])].

parse_severity(<<>>) ->
all;
parse_severity(BSeverity) ->
binary_to_existing_atom(BSeverity, latin1).

parse_integer(BInt) ->
list_to_integer(binary_to_list(BInt)).

0 comments on commit 98879db

Please sign in to comment.