Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
RoadRunnr committed Apr 26, 2024
1 parent f1da28a commit 04ea746
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 495 deletions.
15 changes: 2 additions & 13 deletions src/eradius.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
-behaviour(application).

%% API
-export([load_tables/1, load_tables/2, statistics/1,
-export([load_tables/1, load_tables/2,
start_server/3, start_server/4]).
-ignore_xref([load_tables/1, load_tables/2, statistics/1,
-ignore_xref([load_tables/1, load_tables/2,
start_server/3, start_server/4]).

%% application callbacks
Expand All @@ -30,17 +30,6 @@ load_tables(Tables) ->
load_tables(Dir, Tables) ->
eradius_dict:load_tables(Dir, Tables).

%% @doc manipulate server statistics
%% * reset: reset all counters to zero
%% * pull: read counters and reset to zero
%% * read: read counters
statistics(reset) ->
eradius_counter_aggregator:reset();
statistics(pull) ->
eradius_counter_aggregator:pull();
statistics(read) ->
eradius_counter_aggregator:read().

start_server(IP, Port, #{handler := {_, _}, clients := #{}} = Opts)
when is_tuple(IP), is_integer(Port), Port >= 0, Port < 65536 ->
eradius_server_sup:start_instance(eradius_server:config(IP, Port, Opts)).
Expand Down
3 changes: 3 additions & 0 deletions src/eradius_auth.erl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
-export([nt_hash/1, v2_generate_nt_response/4, mppe_get_master_key/2,
mppe_generate_session_keys/3, mppe_get_asymetric_send_start_key/2,
v2_generate_authenticator_response/5]).
-ignore_xref([nt_hash/1, v2_generate_nt_response/4, mppe_get_master_key/2,
mppe_generate_session_keys/3, mppe_get_asymetric_send_start_key/2,
v2_generate_authenticator_response/5]).
-endif.

-include("eradius_lib.hrl").
Expand Down
143 changes: 0 additions & 143 deletions src/eradius_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,13 @@ proceed_response(_Tried, Req, {ok, Resp0}, _ServerName, _, TS1, _Opts) ->
Resp = eradius_req:record_metric(
reply, #{request => Req,
duration => erlang:monotonic_time() - TS1}, Resp0),
%% update_client_request(Req, erlang:monotonic_time() - TS1),
%% update_client_responses(MetricsInfo),
{{ok, Resp}, Req};

proceed_response(Tried, Req0, Response, ServerName, #{ip := ServerIP, port := Port}, TS1, Opts) ->
Req = eradius_req:record_metric(
Response, #{request => Req0,
duration => erlang:monotonic_time() - TS1}, Req0),
%% update_client_responses(MetricsInfo),
%% update_client_request(Req, erlang:monotonic_time() - TS1),

eradius_client_mngr:request_failed(ServerName),
%% update_server_status_metric(ServerIP, Port, false, Opts),

maybe_failover(Tried, Req, Response, Opts).

maybe_failover(Tried, Req, _Response, #{failover := [_|_] = FailOver} = Opts) ->
Expand All @@ -131,8 +124,6 @@ send_request_loop(Socket, ReqId, Req0, Opts) ->
%% send_request_loop/8
send_request_loop(_Socket, _ReqId, _Packet,
#{timeout := Timeout, retries := 0}, Req) ->
%% TS = erlang:convert_time_unit(Timeout, millisecond, native),
%% update_client_request(timeout, TS),
{{error, timeout}, Req};
send_request_loop(Socket, ReqId, Packet,
#{timeout := Timeout, retries := RetryN} = Opts,
Expand All @@ -152,143 +143,9 @@ send_request_loop(Socket, ReqId, Packet,
{error, close} ->
{{error, socket_down}, Req};
{error, timeout} ->
%% TS = erlang:convert_time_unit(Timeout, millisecond, native),
%% update_client_request(retransmission, TS),
ReqN = eradius_req:record_metric(retransmission, #{}, Req),
send_request_loop(Socket, ReqId, Packet,
Opts#{retries := RetryN - 1}, ReqN);
{error, _} = Error ->
{Error, Req}
end.

%% @private
update_client_requests(MetricsInfo) ->
eradius_counter:inc_counter(requests, MetricsInfo).

%% @private
update_client_request(pending, MetricsInfo, Pending) ->
if Pending =< 0 -> eradius_counter:dec_counter(pending, MetricsInfo);
true -> eradius_counter:inc_counter(pending, MetricsInfo)
end;
update_client_request(#{cmd := Cmd} = Req, MetricsInfo, Ms) ->
eradius_counter:observe(eradius_client_request_duration_milliseconds, MetricsInfo, Ms, "Execution time of a RADIUS request"),
update_client_request_by_type(Cmd, MetricsInfo, Ms, Req).

%% @private
update_client_request_by_type(request, MetricsInfo, Ms, _) ->
eradius_counter:observe(eradius_client_access_request_duration_milliseconds, MetricsInfo, Ms, "Access-Request execution time"),
eradius_counter:inc_counter(accessRequests, MetricsInfo);
update_client_request_by_type(accreq, MetricsInfo, Ms, Req) ->
eradius_counter:observe(eradius_client_accounting_request_duration_milliseconds, MetricsInfo, Ms, "Accounting-Request execution time"),
inc_request_counter_accounting(MetricsInfo, Req);
update_client_request_by_type(coareq, MetricsInfo, Ms, _) ->
eradius_counter:observe(eradius_client_coa_request_duration_milliseconds, MetricsInfo, Ms, "Coa request execution time"),
eradius_counter:inc_counter(coaRequests, MetricsInfo);
update_client_request_by_type(discreq, MetricsInfo, Ms, _) ->
eradius_counter:observe(eradius_client_disconnect_request_duration_milliseconds, MetricsInfo, Ms, "Disconnect execution time"),
eradius_counter:inc_counter(discRequests, MetricsInfo);
update_client_request_by_type(retransmission, MetricsInfo, _Ms, _) ->
eradius_counter:inc_counter(retransmissions, MetricsInfo);
update_client_request_by_type(timeout, MetricsInfo, _Ms, _) ->
eradius_counter:inc_counter(timeouts, MetricsInfo);
update_client_request_by_type(_, _, _, _) -> ok.

%% @private
update_client_responses(MetricsInfo) -> eradius_counter:inc_counter(replies, MetricsInfo).

%% @private
update_client_response(accept, MetricsInfo, _) -> eradius_counter:inc_counter(accessAccepts, MetricsInfo);
update_client_response(reject, MetricsInfo, _) -> eradius_counter:inc_counter(accessRejects, MetricsInfo);
update_client_response(challenge, MetricsInfo, _) -> eradius_counter:inc_counter(accessChallenges, MetricsInfo);
update_client_response(accresp, MetricsInfo, Request) -> inc_responses_counter_accounting(MetricsInfo, Request);
update_client_response(coanak, MetricsInfo, _) -> eradius_counter:inc_counter(coaNaks, MetricsInfo);
update_client_response(coaack, MetricsInfo, _) -> eradius_counter:inc_counter(coaAcks, MetricsInfo);
update_client_response(discnak, MetricsInfo, _) -> eradius_counter:inc_counter(discNaks, MetricsInfo);
update_client_response(discack, MetricsInfo, _) -> eradius_counter:inc_counter(discAcks, MetricsInfo);
update_client_response(dropped, MetricsInfo, _) -> eradius_counter:inc_counter(packetsDropped, MetricsInfo);
update_client_response(bad_authenticator, MetricsInfo, _) -> eradius_counter:inc_counter(badAuthenticators, MetricsInfo);
update_client_response(unknown_req_type, MetricsInfo, _) -> eradius_counter:inc_counter(unknownTypes, MetricsInfo);
update_client_response(_, _, _) -> ok.

%%%=========================================================================
%%% internal functions
%%%=========================================================================

make_metrics_info(ServerName, #{ip := ServerIP, port := ServerPort}, {ClientName, ClientIP}) ->
ClientAddrInfo = eradius_lib:make_addr_info({ClientName, {ClientIP, undefined}}),
ServerAddrInfo = eradius_lib:make_addr_info({ServerName, {ServerIP, ServerPort}}),
{ClientAddrInfo, ServerAddrInfo}.

inc_request_counter_accounting(MetricsInfo, #{attrs := Attrs}) ->
Requests = ets:match_spec_run(Attrs, client_request_counter_account_match_spec_compile()),
[eradius_counter:inc_counter(Type, MetricsInfo) || Type <- Requests],
ok;
inc_request_counter_accounting(_, _) ->
ok.

inc_responses_counter_accounting(MetricsInfo, #{attrs := Attrs}) ->
Responses = ets:match_spec_run(Attrs, client_response_counter_account_match_spec_compile()),
[eradius_counter:inc_counter(Type, MetricsInfo) || Type <- Responses],
ok;
inc_responses_counter_accounting(_, _) ->
ok.

update_server_status_metric(IP, Port, false, _Options) ->
eradius_counter:set_boolean_metric(server_status, [IP, Port], false);
update_server_status_metric(IP, Port, true, Options) ->
UpstreamServers = proplists:get_value(failover, Options, []),
%% set all servesr from pool as inactive
if is_list(UpstreamServers) ->
lists:foreach(
fun (Server) ->
case Server of
{ServerIP, ServerPort, _} ->
eradius_counter:set_boolean_metric(server_status, [ServerIP, ServerPort], false);
{ServerIP, ServerPort, _, _} ->
eradius_counter:set_boolean_metric(server_status, [ServerIP, ServerPort], false);
_ ->
ok
end

end, UpstreamServers);
true ->
ok
end,
%% set current service as active
eradius_counter:set_boolean_metric(server_status, [IP, Port], true).

client_request_counter_account_match_spec_compile() ->
case persistent_term:get({?MODULE, ?FUNCTION_NAME}, undefined) of
undefined ->
MatchSpecCompile =
ets:match_spec_compile(
ets:fun2ms(
fun ({?RStatus_Type, ?RStatus_Type_Start}) -> accountRequestsStart;
({?RStatus_Type, ?RStatus_Type_Stop}) -> accountRequestsStop;
({?RStatus_Type, ?RStatus_Type_Update}) -> accountRequestsUpdate;
({#attribute{id = ?RStatus_Type}, ?RStatus_Type_Start}) -> accountRequestsStart;
({#attribute{id = ?RStatus_Type}, ?RStatus_Type_Stop}) -> accountRequestsStop;
({#attribute{id = ?RStatus_Type}, ?RStatus_Type_Update}) -> accountRequestsUpdate end)),
persistent_term:put({?MODULE, ?FUNCTION_NAME}, MatchSpecCompile),
MatchSpecCompile;
MatchSpecCompile ->
MatchSpecCompile
end.

client_response_counter_account_match_spec_compile() ->
case persistent_term:get({?MODULE, ?FUNCTION_NAME}, undefined) of
undefined ->
MatchSpecCompile =
ets:match_spec_compile(
ets:fun2ms(
fun ({?RStatus_Type, ?RStatus_Type_Start}) -> accountResponsesStart;
({?RStatus_Type, ?RStatus_Type_Stop}) -> accountResponsesStop;
({?RStatus_Type, ?RStatus_Type_Update}) -> accountResponsesUpdate;
({#attribute{id = ?RStatus_Type}, ?RStatus_Type_Start}) -> accountResponsesStart;
({#attribute{id = ?RStatus_Type}, ?RStatus_Type_Stop}) -> accountResponsesStop;
({#attribute{id = ?RStatus_Type}, ?RStatus_Type_Update}) -> accountResponsesUpdate end)),
persistent_term:put({?MODULE, ?FUNCTION_NAME}, MatchSpecCompile),
MatchSpecCompile;
MatchSpecCompile ->
MatchSpecCompile
end.
17 changes: 2 additions & 15 deletions src/eradius_client_mngr.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).

-ifdef(TEST).
-export([get_state/0, servers/0, server/1, get_socket_count/0, init_server_status_metrics/0]).
-export([get_state/0, servers/0, server/1, get_socket_count/0]).
-ignore_xref([get_state/0, servers/0, server/1, get_socket_count/0]).
-endif.

-ignore_xref([start_default_client/1, start_client/1, start_client/2]).
Expand Down Expand Up @@ -474,17 +475,3 @@ find_socket_process(PortIdx, Sockets, #state{owner = Owner, config = Config}) ->
Socket ->
{Socket, Sockets}
end.

%% @private
init_server_status_metrics() ->
case application:get_env(eradius, server_status_metrics_enabled, false) of
false ->
ok;
true ->
%% That will be called at eradius startup and we must be sure that prometheus
%% application already started if server status metrics supposed to be used
application:ensure_all_started(prometheus),
ets:foldl(fun ({{Addr, Port}, _, _}, _Acc) ->
eradius_counter:set_boolean_metric(server_status, [Addr, Port], false)
end, [], ?MODULE)
end.
Loading

0 comments on commit 04ea746

Please sign in to comment.