diff --git a/src/eradius_client.erl b/src/eradius_client.erl index 7a1ebb3..afccff3 100644 --- a/src/eradius_client.erl +++ b/src/eradius_client.erl @@ -62,17 +62,13 @@ send_request(NAS, #{cmd := _, payload := _} = Req) -> {ok, eradius_req:req()} | {error, 'timeout' | 'socket_down'}. send_request(ServerName, #{cmd := Cmd} = Req, Opts) when ?GOOD_CMD(Cmd), is_map(Opts), is_list(ServerName) -> - ct:pal("about to send-#1"), do_send_request(ServerName, [], Req, Opts); send_request(ServerName, Req, Opts) when not is_list(ServerName) -> - ct:pal("about to send-#2"), send_request([ServerName], Req, Opts). do_send_request([], _Tried, _Req, _Opts) -> - ct:pal("about to send-#3"), {error, no_active_servers}; do_send_request(Peers, Tried, Req0, Opts0) -> - ct:pal("about to send-#4"), TS1 = erlang:monotonic_time(), case eradius_client_mngr:wanna_send(Peers, Tried) of @@ -84,7 +80,6 @@ do_send_request(Peers, Tried, Req0, Opts0) -> Opts = maps:merge(ServerOpts, Opts0), Req1 = maps:merge(Req0, ReqInfo), - ct:pal("Client-M#1: ~p", [Req1]), Req2 = eradius_req:record_metric(request, #{}, Req1), {Response, Req} = @@ -116,9 +111,7 @@ maybe_failover(_, Req, Response, _) -> %% send_request_loop/4 send_request_loop(Socket, ReqId, Req0, Opts) -> - ct:pal("Packet-#0: ~p", [Req0]), {Packet, Req} = eradius_req:packet(Req0), - ct:pal("Packet-#1: ~p", [Packet]), send_request_loop(Socket, ReqId, Packet, Opts, Req). %% send_request_loop/8 diff --git a/src/eradius_client_mngr.erl b/src/eradius_client_mngr.erl index fe5b27a..020124b 100644 --- a/src/eradius_client_mngr.erl +++ b/src/eradius_client_mngr.erl @@ -97,7 +97,6 @@ start_client(ServerName, Opts) -> maybe ok ?= check_already_started(ServerName), {ok, Config} = client_config(maps:merge(default_client_opts(), Opts)), - ct:pal("Mngr Opts: ~p~nConfig: ~p", [Opts, Config]), eradius_client_top_sup:start_client([ServerName, Config]) end. diff --git a/src/eradius_counter.erl b/src/eradius_counter.erl deleted file mode 100644 index d6ef940..0000000 --- a/src/eradius_counter.erl +++ /dev/null @@ -1,323 +0,0 @@ -%% Copyright (c) 2011, Travelping GmbH -%% -%% SPDX-License-Identifier: MIT -%% - -%% @doc -%% This module implements the statitics counter for RADIUS servers and clients - --module(eradius_counter). - --behaviour(gen_server). - -%% API --export([init_counter/1, inc_counter/2, dec_counter/2, reset_counter/1, - inc_request_counter/2, inc_reply_counter/2, observe/4, - set_boolean_metric/3]). --export([start_link/0, reset/0, pull/0, read/0, aggregate/1]). --export([collect/2]). - -%% gen_server callbacks --export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). - --ignore_xref([?MODULE]). - --include("eradius_lib.hrl"). - --record(state, { - reset :: erlang:timestamp() - }). - --type srv_counters() :: [#server_counter{}]. --type nas_counters() :: {erlang:timestamp(), [#nas_counter{}]}. --type stats() :: {srv_counters(), nas_counters()}. - -%% ------------------------------------------------------------------------------------------ -%% API -%% @doc initialize a counter structure -init_counter({ServerIP, ServerPort, ServerName}) when is_integer(ServerPort) -> - #server_counter{key = {ServerIP, ServerPort}, - startTime = eradius_lib:timestamp(), - resetTime = eradius_lib:timestamp(), - server_name = ServerName}; -init_counter({{ClientName, ClientIP, ClientPort}, {ServerName, ServerIp, ServerPort}}) -> - #client_counter{key = {{ClientName, ClientIP, ClientPort}, - {ServerName, ServerIp, ServerPort}}, - server_name = ServerName}; -init_counter(#{server_name := ServerName, server := Server, - nas := {NasIP, _}, nas_id := NasId}) -> - #nas_counter{key = {Server, NasIP, NasId}, server_name = ServerName}. - -%% @doc reset counters -reset_counter(#server_counter{startTime = Up}) -> - #server_counter{startTime = Up, resetTime = eradius_lib:timestamp()}; -reset_counter(Req) when is_map(Req) -> - init_counter(Req). - -%% @doc increment requests counters -inc_request_counter(Counter, Nas) -> - inc_counter(Counter, Nas). - -%% @doc increment reply counters -inc_reply_counter(Counter, Nas) -> - inc_counter(Counter, Nas). - -%% @doc increment a specific counter value -inc_counter(invalidRequests, Counters = #server_counter{invalidRequests = Value}) -> - Counters#server_counter{invalidRequests = Value + 1}; -inc_counter(discardNoHandler, Counters = #server_counter{discardNoHandler = Value}) -> - Counters#server_counter{discardNoHandler = Value + 1}; -inc_counter(Counter, Req) when is_map(Req) -> - gen_server:cast(?MODULE, {inc_counter, Counter, Req}); -inc_counter(Counter, {{ClientName, ClientIP, ClientPort}, {ServerName, ServerIp, ServerPort}}) -> - gen_server:cast(?MODULE, {inc_counter, Counter, {{ClientName, ClientIP, ClientPort}, {ServerName, ServerIp, ServerPort}}}). - -dec_counter(Counter, Req) when is_map(Req) -> - gen_server:cast(?MODULE, {dec_counter, Counter, Req}); -dec_counter(Counter, {{ClientName, ClientIP, ClientPort}, {ServerName, ServerIp, ServerPort}}) -> - gen_server:cast(?MODULE, {dec_counter, Counter, {{ClientName, ClientIP, ClientPort}, {ServerName, ServerIp, ServerPort}}}). - -%% @doc reset all counters to zero -reset() -> - gen_server:call(?MODULE, reset). - -%% @doc read counters and reset to zero --spec pull() -> stats(). -pull() -> - gen_server:call(?MODULE, pull). - -%% @doc read counters --spec read() -> stats(). -read() -> - gen_server:call(?MODULE, read). - -%% @doc calculate the per server sum of all counters of a per NAS list of counters --spec aggregate(stats()) -> stats(). -aggregate({Servers, {ResetTS, Nass}}) -> - NSums = lists:foldl(fun(Nas = #nas_counter{key = {ServerId, _}}, Acc) -> - orddict:update(ServerId, fun(Value) -> add_counter(Value, Nas) end, Nas#nas_counter{key = ServerId}, Acc) - end, - orddict:new(), Nass), - NSum1 = [Value || {_Key, Value} <- orddict:to_list(NSums)], - {Servers, {ResetTS, NSum1}}. - -%% @doc Set Value for the given prometheus boolean metric by the given Name with -%% the given values -set_boolean_metric(Name, Labels, Value) -> - case code:is_loaded(prometheus) of - {file, _} -> - try - prometheus_boolean:set(Name, Labels, Value) - catch _:_ -> - prometheus_boolean:declare([{name, server_status}, {labels, [server_ip, server_port]}, - {help, "Status of an upstream RADIUS Server"}]), - prometheus_boolean:set(Name, Labels, Value) - end; - _ -> - ok - end. - -%% @doc Update the given histogram metric value -%% NOTE: We use prometheus_histogram collector here instead of eradius_counter ets table because -%% it is much easy to use histograms in this way. As we don't need to manage buckets and do -%% the other histogram things in eradius, but prometheus.erl will do it for us -observe(Name, {{ClientName, ClientIP, _}, {ServerName, ServerIP, ServerPort}} = MetricsInfo, Value, Help) -> - case code:is_loaded(prometheus) of - {file, _} -> - try - prometheus_histogram:observe(Name, [ServerIP, ServerPort, ServerName, ClientName, ClientIP], Value) - catch _:_ -> - Buckets = application:get_env(eradius, histogram_buckets, [10, 30, 50, 75, 100, 1000, 2000]), - prometheus_histogram:declare([{name, Name}, {labels, [server_ip, server_port, server_name, client_name, client_ip]}, - {duration_unit, milliseconds}, - {buckets, Buckets}, {help, Help}]), - observe(Name, MetricsInfo, Value, Help) - end; - _ -> - ok - end; - -observe(Name, #{server_name := ServerName, server := {ServerIP, ServerPort}, - nas := {NasIP, _}, nas_id := NasId} = Req, Value, Help) -> - case code:is_loaded(prometheus) of - {file, _} -> - try - prometheus_histogram:observe( - Name, [inet:ntoa(ServerIP), ServerPort, ServerName, inet:ntoa(NasIP), NasId], Value) - catch _:_ -> - Buckets = application:get_env(eradius, histogram_buckets, [10, 30, 50, 75, 100, 1000, 2000]), - prometheus_histogram:declare( - [{name, Name}, {labels, [server_ip, server_port, server_name, nas_ip, nas_id]}, - {duration_unit, milliseconds}, - {buckets, Buckets}, {help, Help}]), - observe(Name, Req, Value, Help) - end; - _ -> - ok - end. - -%% helper to be called from the aggregator to fetch this nodes values -%% @private -collect(Ref, Process) -> - gen_server:cast(?MODULE, {collect, Ref, Process}). - -%% @private --spec start_link() -> {ok, pid()} | {error, term()}. -start_link() -> - gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). - -%% ------------------------------------------------------------------------------------------ -%% -- gen_server Callbacks -%% @private -init([]) -> - ets:new(?MODULE, [ordered_set, protected, named_table, {keypos, #nas_counter.key}, {write_concurrency,true}]), - {ok, #state{reset = eradius_lib:timestamp()}}. - -%% @private -handle_call(pull, _From, State) -> - NassAndClients = read_stats(State), - Servers = server_stats(pull), - ets:delete_all_objects(?MODULE), - {reply, {Servers, NassAndClients}, State#state{reset = eradius_lib:timestamp()}}; -handle_call(read, _From, State) -> - NassAndClients = read_stats(State), - Servers = server_stats(read), - {reply, {Servers, NassAndClients}, State}; -handle_call(reset, _From, State) -> - server_stats(reset), - ets:delete_all_objects(?MODULE), - {reply, ok, State#state{reset = eradius_lib:timestamp()}}. - -%% @private -handle_cast({inc_counter, Counter, Key = {{_ClientName, _ClientIP, _ClientPort}, - {_ServerName, _ServerIp, _ServerPort}}}, State) -> - ets:update_counter(?MODULE, Key, {counter_idx(Counter, client), 1}, init_counter(Key)), - {noreply, State}; - -handle_cast({inc_counter, Counter, - #{server := Server, nas := {NasIP, _}, nas_id := NasId} = Req}, State) -> - Key = {Server, NasIP, NasId}, - ets:update_counter(?MODULE, Key, {counter_idx(Counter, nas), 1}, init_counter(Req)), - {noreply, State}; - -handle_cast({dec_counter, Counter, Key = {{_ClientName, _ClientIP, _ClientPort}, - {_ServerName, _ServerIp, _ServerPort}}}, State) -> - ets:update_counter(?MODULE, Key, {counter_idx(Counter, client), -1}, init_counter(Key)), - {noreply, State}; - -handle_cast({dec_counter, Counter, - #{server := Server, nas := {NasIP, _}, nas_id := NasId} = Req}, State) -> - Key = {Server, NasIP, NasId}, - ets:update_counter(?MODULE, Key, {counter_idx(Counter, nas), -1}, init_counter(Req)), - {noreply, State}; - -handle_cast({collect, Ref, Process}, State) -> - Process ! {collect, Ref, ets:tab2list(?MODULE)}, - ets:delete_all_objects(?MODULE), - {noreply, State#state{reset = eradius_lib:timestamp()}}; - -handle_cast(_Msg, State) -> - {noreply, State}. - -%% -- unused callbacks -%% @private -handle_info(_Info, State) -> {noreply, State}. -%% @private -code_change(_OldVsn, State, _Extra) -> {ok, State}. -%% @private -terminate(_Reason, _State) -> ok. - -%% ------------------------------------------------------------------------------------------ -%% -- helper functions -%% @private - -read_stats(State) -> - {State#state.reset, ets:tab2list(?MODULE)}. - -server_stats(Func) -> - lists:foldl(fun(S, Acc) -> [eradius_server:stats(S, Func)|Acc] end, [], eradius_server_sup:all()). - -%% @private -counter_idx(requests, nas) -> #nas_counter.requests; -counter_idx(replies, nas) -> #nas_counter.replies; -counter_idx(dupRequests, nas) -> #nas_counter.dupRequests; -counter_idx(malformedRequests, nas) -> #nas_counter.malformedRequests; -counter_idx(accessRequests, nas) -> #nas_counter.accessRequests; -counter_idx(accessAccepts, nas) -> #nas_counter.accessAccepts; -counter_idx(accessRejects, nas) -> #nas_counter.accessRejects; -counter_idx(accessChallenges, nas) -> #nas_counter.accessChallenges; -counter_idx(badAuthenticators, nas) -> #nas_counter.badAuthenticators; -counter_idx(packetsDropped, nas) -> #nas_counter.packetsDropped; -counter_idx(unknownTypes, nas) -> #nas_counter.unknownTypes; -counter_idx(handlerFailure, nas) -> #nas_counter.handlerFailure; -counter_idx(coaRequests, nas) -> #nas_counter.coaRequests; -counter_idx(coaAcks, nas) -> #nas_counter.coaAcks; -counter_idx(coaNaks, nas) -> #nas_counter.coaNaks; -counter_idx(discRequests, nas) -> #nas_counter.discRequests; -counter_idx(discAcks, nas) -> #nas_counter.discAcks; -counter_idx(discNaks, nas) -> #nas_counter.discNaks; -counter_idx(retransmissions, nas) -> #nas_counter.retransmissions; -counter_idx(pending, nas) -> #nas_counter.pending; -counter_idx(accountRequestsStart, nas) -> #nas_counter.accountRequestsStart; -counter_idx(accountRequestsStop, nas) -> #nas_counter.accountRequestsStop; -counter_idx(accountRequestsUpdate, nas) -> #nas_counter.accountRequestsUpdate; -counter_idx(accountResponsesStart, nas) -> #nas_counter.accountResponsesStart; -counter_idx(accountResponsesStop, nas) -> #nas_counter.accountResponsesStop; -counter_idx(accountResponsesUpdate, nas) -> #nas_counter.accountResponsesUpdate; - -counter_idx(requests, client) -> #client_counter.requests; -counter_idx(replies, client) -> #client_counter.replies; -counter_idx(accessRequests, client) -> #client_counter.accessRequests; -counter_idx(coaRequests, client) -> #client_counter.coaRequests; -counter_idx(discRequests, client) -> #client_counter.discRequests; -counter_idx(retransmissions, client) -> #client_counter.retransmissions; -counter_idx(accessAccepts, client) -> #client_counter.accessAccepts; -counter_idx(accessRejects, client) -> #client_counter.accessRejects; -counter_idx(accessChallenges, client) -> #client_counter.accessChallenges; -counter_idx(coaNaks, client) -> #client_counter.coaNaks; -counter_idx(coaAcks, client) -> #client_counter.coaAcks; -counter_idx(discNaks, client) -> #client_counter.discNaks; -counter_idx(discAcks, client) -> #client_counter.discAcks; -counter_idx(badAuthenticators, client) -> #client_counter.badAuthenticators; -counter_idx(packetsDropped, client) -> #client_counter.packetsDropped; -counter_idx(unknownTypes, client) -> #client_counter.unknownTypes; -counter_idx(pending, client) -> #client_counter.pending; -counter_idx(timeouts, client) -> #client_counter.timeouts; -counter_idx(accountRequestsStart, client) -> #client_counter.accountRequestsStart; -counter_idx(accountRequestsStop, client) -> #client_counter.accountRequestsStop; -counter_idx(accountRequestsUpdate, client) -> #client_counter.accountRequestsUpdate; -counter_idx(accountResponsesStart, client) -> #client_counter.accountResponsesStart; -counter_idx(accountResponsesStop, client) -> #client_counter.accountResponsesStop; -counter_idx(accountResponsesUpdate, client) -> #client_counter.accountResponsesUpdate. - -add_counter(Cnt1 = #nas_counter{}, Cnt2 = #nas_counter{}) -> - #nas_counter{ - key = Cnt1#nas_counter.key, - requests = Cnt1#nas_counter.requests + Cnt2#nas_counter.requests, - replies = Cnt1#nas_counter.replies + Cnt2#nas_counter.replies, - dupRequests = Cnt1#nas_counter.dupRequests + Cnt2#nas_counter.dupRequests, - malformedRequests = Cnt1#nas_counter.malformedRequests + Cnt2#nas_counter.malformedRequests, - accessRequests = Cnt1#nas_counter.accessRequests + Cnt2#nas_counter.accessRequests, - accessAccepts = Cnt1#nas_counter.accessAccepts + Cnt2#nas_counter.accessAccepts, - accessRejects = Cnt1#nas_counter.accessRejects + Cnt2#nas_counter.accessRejects, - accessChallenges = Cnt1#nas_counter.accessChallenges + Cnt2#nas_counter.accessChallenges, - accountRequestsStart = Cnt1#nas_counter.accountRequestsStart + Cnt2#nas_counter.accountRequestsStart, - accountRequestsStop = Cnt1#nas_counter.accountRequestsStop + Cnt2#nas_counter.accountRequestsStop, - accountRequestsUpdate = Cnt1#nas_counter.accountRequestsUpdate + Cnt2#nas_counter.accountRequestsUpdate, - accountResponsesStart = Cnt1#nas_counter.accountResponsesStart + Cnt2#nas_counter.accountResponsesStart, - accountResponsesStop = Cnt1#nas_counter.accountResponsesStop + Cnt2#nas_counter.accountResponsesStop, - accountResponsesUpdate = Cnt1#nas_counter.accountResponsesUpdate + Cnt2#nas_counter.accountResponsesUpdate, - noRecords = Cnt1#nas_counter.noRecords + Cnt2#nas_counter.noRecords, - badAuthenticators = Cnt1#nas_counter.badAuthenticators + Cnt2#nas_counter.badAuthenticators, - packetsDropped = Cnt1#nas_counter.packetsDropped + Cnt2#nas_counter.packetsDropped, - unknownTypes = Cnt1#nas_counter.unknownTypes + Cnt2#nas_counter.unknownTypes, - handlerFailure = Cnt1#nas_counter.handlerFailure + Cnt2#nas_counter.handlerFailure, - coaRequests = Cnt1#nas_counter.coaRequests + Cnt2#nas_counter.coaRequests, - coaAcks = Cnt1#nas_counter.coaAcks + Cnt2#nas_counter.coaAcks, - coaNaks = Cnt1#nas_counter.coaNaks + Cnt2#nas_counter.coaNaks, - discRequests = Cnt1#nas_counter.discRequests + Cnt2#nas_counter.discRequests, - discAcks = Cnt1#nas_counter.discAcks + Cnt2#nas_counter.discAcks, - discNaks = Cnt1#nas_counter.discNaks + Cnt2#nas_counter.discNaks, - retransmissions = Cnt1#nas_counter.retransmissions + Cnt2#nas_counter.retransmissions, - pending = Cnt1#nas_counter.pending + Cnt2#nas_counter.pending - }. diff --git a/src/eradius_lib.erl b/src/eradius_lib.erl index ae349f5..42c145a 100644 --- a/src/eradius_lib.erl +++ b/src/eradius_lib.erl @@ -28,7 +28,11 @@ pad_to(Width, Binary) -> timestamp() -> erlang:system_time(milli_seconds). --spec printable_peer({inet:ip_address() | any, eradius_server:port_number()}) -> io_lib:chars(). +-spec printable_peer(server_name() | {inet:ip_address() | any, eradius_server:port_number()}) -> io_lib:chars(). +printable_peer(Atom) when is_atom(Atom) -> + [atom_to_list(Atom)]; +printable_peer(Binary) when is_binary(Binary) -> + [Binary]; printable_peer({IP, Port}) -> printable_peer(IP, Port). diff --git a/src/eradius_metrics_prometheus.erl b/src/eradius_metrics_prometheus.erl index a36646c..74a1a8a 100644 --- a/src/eradius_metrics_prometheus.erl +++ b/src/eradius_metrics_prometheus.erl @@ -9,6 +9,7 @@ -ignore_xref([init/1, reset/0]). -ignore_xref([client_metrics_callback/3, server_metrics_callback/3]). +-include_lib("kernel/include/logger.hrl"). -include("dictionary.hrl"). -include("eradius_lib.hrl"). -include("eradius_dict.hrl"). @@ -36,7 +37,6 @@ init_client_metrics(#{histogram_buckets := Buckets, client_metrics := true}) -> %% %% Client Side Metrics %% - ct:pal("Doing Client Metrics"), %% Server Status prometheus_boolean:declare( @@ -159,14 +159,12 @@ init_client_metrics(#{histogram_buckets := Buckets, client_metrics := true}) -> {help, "Disconnect execution time"}]), ok; init_client_metrics(_Config) -> - ct:pal("NOT doing Client Metrics: ~p", [_Config]), ok. init_server_metrics(#{histogram_buckets := Buckets, server_metrics := true}) -> %% %% Server Side Metrics %% - ct:pal("Doing Server Metrics"), %% this need a collector... %% {uptime_milliseconds, gauge, "RADIUS server uptime"}, @@ -296,7 +294,6 @@ init_server_metrics(#{histogram_buckets := Buckets, server_metrics := true}) -> {help, "Disconnect-Request execution time"}]), ok; init_server_metrics(_Config) -> - ct:pal("NOT doing Server Metrics ~p", [_Config]), ok. %%%========================================================================= @@ -312,8 +309,8 @@ client_metrics_callback(Event, MetaData, server := Server, server_addr := {ServerIP, ServerPort}, client := Client, client_addr := ClientIP } = Req) -> - ct:pal("Client-Metric:~nEvent: ~p~nMetaData: ~p~nReq: ~p~n", - [Event, MetaData, Req]), + ?LOG(debug, "Client-Metric:~nEvent: ~p~nMetaData: ~p~nReq: ~p~n", + [Event, MetaData, Req]), Labels = [ServerIP, ServerPort, Server, Client, ClientIP], case Event of @@ -329,8 +326,8 @@ client_metrics_callback(Event, MetaData, end, Req; client_metrics_callback(Event, MetaData, Req) -> - ct:pal("BROKEN Client-Metric:~nEvent: ~p~nMetaData: ~p~nReq: ~p~n", - [Event, MetaData, Req]), + ?LOG(error, "BROKEN Client-Metric:~nEvent: ~p~nMetaData: ~p~nReq: ~p~n", + [Event, MetaData, Req]), Req. @@ -385,8 +382,8 @@ client_reply_metrics(_MetaData, Labels, Req :: eradius_req:req()) -> eradius_req:req(). server_metrics_callback(Event, MetaData, #{cmd := Cmd, server := Server} = Req) -> - ct:pal("Server-Metric:~nEvent: ~p~nMetaData: ~p~nReq: ~p~n", - [Event, MetaData, Req]), + ?LOG(debug, "Server-Metric:~nEvent: ~p~nMetaData: ~p~nReq: ~p~n", + [Event, MetaData, Req]), case Event of request -> prometheus_counter:inc(eradius_requests_total, [Server], 1), @@ -454,16 +451,14 @@ server_metrics_callback(invalid_request, #{server := Server} = _MetaData, _) -> prometheus_counter:inc(eradius_invalid_requests_total, [Server], 1), ok; server_metrics_callback(Event, MetaData, Req) -> - ct:pal("BROKEN Server-Metric:~nEvent: ~p~nMetaData: ~p~nReq: ~p~n", - [Event, MetaData, Req]), + ?LOG(error, "BROKEN Server-Metric:~nEvent: ~p~nMetaData: ~p~nReq: ~p~n", + [Event, MetaData, Req]), Req. acct_status_type(#{attrs := Attrs}) when is_list(Attrs) -> - ct:pal("AcctStatusTypeList: ~p", [Attrs]), acct_status_type_list(Attrs); acct_status_type(#{body := Body}) when is_binary(Body) -> - ct:pal("AcctStatusTypeBin: ~p", [Body]), acct_status_type_scan(Body); acct_status_type(_) -> invalid. @@ -478,10 +473,8 @@ acct_status_type_list([_|Next]) -> acct_status_type_list(Next). acct_status_type_scan(<>) -> - ct:pal("acct_status_type_scan-#1: ~p", [Type]), acct_status_type_label(Type); acct_status_type_scan(<<_, Len, Rest/binary>>) -> - ct:pal("acct_status_type_scan-#2: ~p, ~p", [Len, Rest]), case Rest of <<_:(Len-2)/bytes, Next/binary>> -> acct_status_type_scan(Next); diff --git a/src/eradius_req.erl b/src/eradius_req.erl index cc0c1fd..9022bfd 100644 --- a/src/eradius_req.erl +++ b/src/eradius_req.erl @@ -149,15 +149,14 @@ packet(Req) -> erlang:error(badarg, [Req]). -spec attrs(req()) -> {eradius_lib:attribute_list(), req()} | no_return(). -attrs(#{attrs := Attrs, is_valid := true} = Req) - when is_list(Attrs) -> +attrs(#{attrs := Attrs, is_valid := IsValid} = Req) + when is_list(Attrs), IsValid =/= false -> {Attrs, Req}; -attrs(#{attrs := Attrs, is_valid := IsValid}) - when is_list(Attrs), IsValid =/= true -> - throw({bad_pdu, invalid}); -attrs(#{body := Body, secret := _} = Req) +attrs(#{body := Body, secret := _} = Req0) when is_binary(Body) -> - try decode_body(Body, Req) + try + #{attrs := Attrs} = Req = decode_body(Body, Req0), + {Attrs, Req} catch exit:_ -> throw({bad_pdu, decoder_error}) @@ -165,12 +164,22 @@ attrs(#{body := Body, secret := _} = Req) attrs(Req) -> erlang:error(badarg, [Req]). -attr(Id, #{attrs := Attrs, is_valid := true}) - when is_list(Attrs) -> - proplists:get_value(Id, Attrs, undefined); +attr(Id, #{attrs := Attrs, is_valid := IsValid}) + when is_list(Attrs), IsValid =/= false -> + get_attr(Id, Attrs); attr(_, _) -> undefined. +get_attr(_Id, []) -> + undefined; +get_attr(Id, [Head|Tail]) -> + case Head of + {#attribute{id = Id}, Value} -> Value; + {Id, Value} -> Value; + _ -> get_attr(Id, Tail) + end. + + -spec new(command()) -> req(). new(Command) -> new(Command, undefined). @@ -198,10 +207,14 @@ request(<> = Header, Body, Req#{req_id => ReqId, request_authenticator => Authenticator, nas_id => NasId, secret => Secret}). --spec response(binary(), binary(), req()) -> req() | no_return(). +-spec response(binary(), binary(), req()) -> req() | no_return(); + (command(), undefined | eradius_lib:attribute_list(), req()) -> req(). response(<> = Header, Body, #{req_id := ReqId, secret := _} = Req) -> - mk_req(Cmd, ReqId, Len, Authenticator, Header, Body, Req). + mk_req(Cmd, ReqId, Len, Authenticator, Header, Body, Req); + +response(Response, Attrs, Req) when is_atom(Response) -> + Req#{cmd := Response, body := undefined, attrs := Attrs, is_valid := undefined}. -spec set_secret(req(), eradius_lib:secret()) -> req(). set_secret(Req, Secret) -> diff --git a/src/eradius_server.erl b/src/eradius_server.erl index 0bffa10..fa11bf7 100644 --- a/src/eradius_server.erl +++ b/src/eradius_server.erl @@ -69,7 +69,6 @@ server :: {inet:ip_address() | any, inet:port_number()}, % IP and port to which this socket is bound active_n :: non_neg_integer(), transacts :: ets:tid(), % ETS table containing current transactions - counter :: #server_counter{}, % statistics counter, handler :: eradius_server_mon:handler(), metrics_callback :: eradius_req:metrics_callback(), clients :: #{inet:ip_address() => eradius_client()} @@ -146,7 +145,6 @@ init([#{server_name := ServerName, handler = Handler, clients = Clients, transacts = ets:new(transacts, []), - counter = eradius_counter:init_counter({IP, Port, ServerName}), metrics_callback = MetricsCallback }, {ok, State}; @@ -157,12 +155,8 @@ init([#{server_name := ServerName, end. %% @private -handle_call({stats, pull}, _From, State = #state{counter = Counter}) -> - {reply, Counter, State#state{counter = eradius_counter:reset_counter(Counter)}}; -handle_call({stats, read}, _From, State = #state{counter = Counter}) -> - {reply, Counter, State}; -handle_call({stats, reset}, _From, State = #state{counter = Counter}) -> - {reply, ok, State#state{counter = eradius_counter:reset_counter(Counter)}}. +handle_call(_Call, _From, State) -> + {reply, ok, State}. %% @private handle_cast(_Msg, State) -> @@ -178,7 +172,6 @@ handle_info({udp, Socket, FromIP, FromPortNo, <>} handler = Handler, clients = Clients, metrics_callback = MetricsCallback} = State) when is_map_key(FromIP, Clients) -> - ct:pal("Good-UDP: ~p", [Msg]), NAS = maps:get(FromIP, Clients), <<_, ReqId:8, _/binary>> = Header, Req0 = eradius_req:request(Header, Body, NAS, MetricsCallback), @@ -192,7 +185,6 @@ handle_info({udp, Socket, FromIP, FromPortNo, <>} [] -> HandlerPid = proc_lib:spawn_link(?MODULE, do_radius, [self(), Handler, ReqKey, Req1]), - ct:pal("do_radius-pid: ~p", [HandlerPid]), ets:insert(Transacts, {ReqKey, {handling, HandlerPid}}), ets:insert(Transacts, {HandlerPid, ReqKey}), %% eradius_counter:inc_counter(pending, Req); @@ -221,7 +213,6 @@ handle_info({udp, Socket, FromIP, FromPortNo, <>} handle_info({udp, _Socket, _FromIP, _FromPortNo, _Packet} = Msg, #state{name = ServerName, metrics_callback = MetricsCallback} = State) -> %% TBD: this should go into a malformed counter - ct:pal("Invalid-UDP: ~p", [Msg]), eradius_req:metrics_callback(MetricsCallback, invalid_request, #{server => ServerName}), flow_control(State), {noreply, State}; @@ -234,7 +225,6 @@ handle_info({replied, ReqKey, HandlerPid}, State = #state{transacts = Transacts} {noreply, State}; handle_info({'EXIT', HandlerPid, _Reason}, State = #state{transacts = Transacts}) -> - ct:pal("do_radius_exit: ~p", [_Reason]), [ets:delete(Transacts, ReqKey) || {_, ReqKey} <- ets:take(Transacts, HandlerPid)], {noreply, State}; @@ -243,7 +233,6 @@ handle_info(_Info, State) -> %% @private terminate(_Reason, State) -> - ct:pal("Terminate: ~p", [_Reason]), gen_udp:close(State#state.socket), ok. @@ -259,7 +248,6 @@ code_change(_OldVsn, State, _Extra) -> {ok, State}. do_radius(ServerPid, {HandlerMod, HandlerArg}, ReqKey, #{arrival_time := TS1, socket := Socket, server := Server, nas := {NasIP, NasPort} = Nas} = Req0) -> - ct:pal("do_radius: ~p", [Req0]), case apply_handler_mod(HandlerMod, HandlerArg, Req0) of {reply, Packet, Resp0, Req} -> ?LOG(debug, "~s From: ~s INF: Sending response for request ~0p", @@ -313,22 +301,22 @@ apply_handler_mod(HandlerMod, HandlerArg, {Packet, Resp} = eradius_req:packet(Resp0), {reply, Packet, Resp, Req}; noreply -> - ?LOG(error, "~s INF: Noreply for request ~p from handler ~p: returned value: ~p", + ?LOG(error, "~ts INF: Noreply for request ~tp from handler ~tp: returned value: ~tp", [printable_peer(Server), ReqId, HandlerArg, noreply]), {discard, handler_returned_noreply}; {error, timeout} -> ReqType = eradius_log:format_cmd(Cmd), ReqId = integer_to_list(ReqId), - ?LOG(error, "~s INF: Timeout after waiting for response to ~s(~s) from RADIUS NAS: ~s", + ?LOG(error, "~ts INF: Timeout after waiting for response to ~ts(~ts) from RADIUS NAS: ~s", [printable_peer(Server), ReqType, ReqId, inet:ntoa(NasIP)]), {discard, {bad_return, {error, timeout}}}; OtherReturn -> - ?LOG(error, "~s INF: Unexpected return for request ~0p from handler ~p: returned value: ~p", + ?LOG(error, "~ts INF: Unexpected return for request ~0tp from handler ~tp: returned value: ~tp", [printable_peer(Server), ReqId, HandlerArg, OtherReturn]), {discard, {bad_return, OtherReturn}} catch Class:Reason:S -> - ?LOG(error, "~s INF: Handler crashed after request ~p, radius handler class: ~p, reason of crash: ~p, stacktrace: ~p", + ?LOG(error, "~ts INF: Handler crashed after request ~0tp, radius handler class: ~0tp, reason of crash: ~0tp, stacktrace: ~0tp", [printable_peer(Server), ReqId, Class, Reason, S]), {exit, {Class, Reason}} end. diff --git a/src/eradius_sup.erl b/src/eradius_sup.erl index cfe8620..0321d86 100644 --- a/src/eradius_sup.erl +++ b/src/eradius_sup.erl @@ -20,8 +20,6 @@ init([]) -> SupFlags = {RestartStrategy, MaxRestarts, MaxSecondsBetweenRestarts}, DictServer = {dict, {eradius_dict, start_link, []}, permanent, brutal_kill, worker, [eradius_dict]}, - StatsServer = {counter, {eradius_counter, start_link, []}, permanent, brutal_kill, worker, [eradius_counter]}, - StatsCollect = {aggregator, {eradius_counter_aggregator, start_link, []}, permanent, brutal_kill, worker, [eradius_counter_aggregator]}, RadiusLog = {radius_log, {eradius_log, start_link, []}, permanent, brutal_kill, worker, [eradius_log]}, ServerSup = #{id => server_sup, @@ -38,5 +36,4 @@ init([]) -> type => supervisor, modules => [eradius_client_top_sup]}, - {ok, {SupFlags, [DictServer, StatsServer, StatsCollect, RadiusLog, - ServerSup, ClientTopSup]}}. + {ok, {SupFlags, [DictServer, RadiusLog, ServerSup, ClientTopSup]}}.