This commit is contained in:
niamtokik
2022-04-11 20:28:09 +00:00
parent fa906a7027
commit fe1bc4c756

View File

@@ -1,5 +1,14 @@
%%%===================================================================
%%%
%%%===================================================================
-module(rfc5424_parser).
-export([priority/1]).
-export([version/1]).
-export([hostname/1]).
-export([application_name/1]).
-export([message_id/1]).
-export([process_id/1]).
-include_lib("eunit/include/eunit.hrl").
-include("rfc5424.hrl").
%%--------------------------------------------------------------------
@@ -32,7 +41,7 @@ priority(Data) ->
%%--------------------------------------------------------------------
priority_converter(Data) ->
try
{ok, Priority} = priority_to_integer(Data),
{ok, Priority} = bitstring_to_integer(Data),
Facility = erlang:trunc(Priority/8),
FacilityAtom = facility(Facility),
Severity = Priority-(Facility*8),
@@ -49,7 +58,7 @@ priority_converter(Data) ->
%%--------------------------------------------------------------------
%%
%%--------------------------------------------------------------------
priority_to_integer(Data) ->
bitstring_to_integer(Data) ->
try erlang:binary_to_integer(Data) of
Integer -> {ok, Integer}
catch
@@ -99,4 +108,101 @@ severity(7) -> debug.
%%--------------------------------------------------------------------
%%
%%--------------------------------------------------------------------
version(<<"1", Rest/bitstring>>) -> ok.
version(<<"0", Rest/bitstring>> = Data) ->
{error, {version, Data}};
version(<<I0:8/bitstring, Rest/bitstring>>) ->
{ok, Value} = bitstring_to_integer(I0),
{ok, #{ <<"version">> => Value }, Rest};
version(<<"0", _:8/bitstring, Rest/bitstring>> = Data) ->
{error, {version, Data}};
version(<<I0:8/bitstring, I1:8/bitstring, Rest/bitstring>>) ->
{ok, Value} = bitstring_to_integer(<<I0/bitstring, I1/bitstring>>),
{ok, #{ <<"version">> => Value }, Rest}.
%%--------------------------------------------------------------------
%%
%%--------------------------------------------------------------------
hostname_regex() ->
re:compilse(<<"^([[:graph:]]{1,255})(.*)">>).
hostname(Data) ->
{ok, Regex} = hostname_regex(),
case re:run(Data, Regex, [{capture, all_but_first, binary}]) of
{match, [Hostname, Rest]} -> {ok, #{ <<"hostname">> => Hostname }, Rest};
nomatch -> {error, {hostname, Data}}
end.
%%--------------------------------------------------------------------
%%
%%--------------------------------------------------------------------
application_name_regex() ->
re:compile(<<"^([[:graph:]]{1,48})(.*)">>).
application_name(Data) ->
{ok, Regex} = application_name_regex(),
case re:run(Data, Regex, [{capture, all_but_first, binary}]) of
{match, [AppName, Rest]} -> {ok, #{ <<"application_name">> => AppName }, Rest};
nomatch -> {error, {application_name, Data}}
end.
%%--------------------------------------------------------------------
%%
%%--------------------------------------------------------------------
process_id_regex() ->
re:compile(<<"^([[:graph:]]{1,128})(.*)">>).
process_id(Data) ->
{ok, Regex} = process_id_regex(),
case re:run(Data, Regex, [{capture, all_but_first, binary}]) of
{match, [ProcessId, Rest]} -> {ok, #{ <<"process_id">> => ProcessId }, Rest};
nomatch -> {error, {process_id, Data}}
end.
%%--------------------------------------------------------------------
%%
%%--------------------------------------------------------------------
message_id_regex() ->
re:compile(<<"^([[:graph:]]{1,32})(.*)">>).
message_id(Data) ->
{ok, Regex} = message_id_regex(),
case re:run(Data, Regex, [{capture, all_but_first, binary}]) of
{match, [MessageId, Rest]} -> {ok, #{ <<"message_id">> => MessageId }, Rest};
nomatch -> {error, {message_id, Data}}
end.
%%--------------------------------------------------------------------
%%
%%--------------------------------------------------------------------
timestamp() ->
ok.
timestamp_test() ->
<<"1985-04-12T23:20:50.52Z">>,
#{ <<"timestamp">> =>
#{
<<"date">> => {1985, 04, 12}
, <<"time">> => {23, 20, 50, 52}
, <<"offset">> => <<"Z">>
}
}.
%%--------------------------------------------------------------------
%%
%%--------------------------------------------------------------------
year(<<Y:32/bitstring, "-", M:16/bitstring, "-", D:16/bitstring, Rest/bitstring>> = Data) ->
{ok, Year} = bitstring_to_integer(Y),
{ok, Month} = bitstring_to_integer(M),
{ok, Day} = bitstring_to_integer(D),
case calendar:valid_date({Year, Month, Day}) of
true -> {ok, {Year, Month, Day}, Rest};
false -> {error, {year, Data}}
end.
%%--------------------------------------------------------------------
%%
%%--------------------------------------------------------------------
time(<<"T", H:16/bitstring, ":", M:16/bitstring, ":", S:16/bitstring, ".", Rest/bitstring>>) ->
ok;
time(<<"T", H:16/bitstring, ":", M:16/bitstring, ":", S:16/bitstring, Rest/bitstring>>) ->
ok.