From e239fb07cdd9521bf76db6405009ed5c97d55db0 Mon Sep 17 00:00:00 2001 From: JianBo He Date: Wed, 25 Aug 2021 10:40:52 +0800 Subject: [PATCH] chore(gw): add http-api for gateway summary lists --- apps/emqx_gateway/src/coap/emqx_coap_impl.erl | 2 +- apps/emqx_gateway/src/emqx_gateway_api.erl | 9 ++- apps/emqx_gateway/src/emqx_gateway_intr.erl | 72 +++++++++++++++++++ .../src/exproto/emqx_exproto_impl.erl | 2 +- .../src/lwm2m/emqx_lwm2m_impl.erl | 2 +- apps/emqx_gateway/src/mqttsn/emqx_sn_impl.erl | 2 +- .../src/stomp/emqx_stomp_impl.erl | 2 +- 7 files changed, 84 insertions(+), 7 deletions(-) create mode 100644 apps/emqx_gateway/src/emqx_gateway_intr.erl diff --git a/apps/emqx_gateway/src/coap/emqx_coap_impl.erl b/apps/emqx_gateway/src/coap/emqx_coap_impl.erl index cf181f07e..f150f6f81 100644 --- a/apps/emqx_gateway/src/coap/emqx_coap_impl.erl +++ b/apps/emqx_gateway/src/coap/emqx_coap_impl.erl @@ -115,7 +115,7 @@ do_start_listener(dtls, Name, ListenOn, SocketOpts, MFA) -> esockd:open_dtls(Name, ListenOn, SocketOpts, MFA). name(GwName, LisName, Type) -> - list_to_atom(lists:concat([GwName, ":", LisName, ":", Type])). + list_to_atom(lists:concat([GwName, ":", Type, ":", LisName])). stop_listener(GwName, {Type, LisName, ListenOn, SocketOpts, Cfg}) -> StopRet = stop_listener(GwName, Type, LisName, ListenOn, SocketOpts, Cfg), diff --git a/apps/emqx_gateway/src/emqx_gateway_api.erl b/apps/emqx_gateway/src/emqx_gateway_api.erl index 9b77cc643..e617af65e 100644 --- a/apps/emqx_gateway/src/emqx_gateway_api.erl +++ b/apps/emqx_gateway/src/emqx_gateway_api.erl @@ -374,8 +374,13 @@ schema_for_gateway_stats() -> %%-------------------------------------------------------------------- %% http handlers -gateway(get, _Request) -> - {200, ok}. +gateway(get, Request) -> + Params = cowboy_req:parse_qs(Request), + Status = case proplists:get_value(<<"status">>, Params) of + undefined -> all; + S0 -> binary_to_existing_atom(S0, utf8) + end, + {200, emqx_gateway_intr:gateways(Status)}. gateway_insta(delete, _Request) -> {200, ok}; diff --git a/apps/emqx_gateway/src/emqx_gateway_intr.erl b/apps/emqx_gateway/src/emqx_gateway_intr.erl new file mode 100644 index 000000000..bafdb233e --- /dev/null +++ b/apps/emqx_gateway/src/emqx_gateway_intr.erl @@ -0,0 +1,72 @@ +%%-------------------------------------------------------------------- +%% Copyright (c) 2021 EMQ Technologies Co., Ltd. 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 Gateway Interface Module for HTTP-APIs +-module(emqx_gateway_intr). + +-export([ gateways/1 + ]). + +-type gateway_summary() :: + #{ name := binary() + , status := running | stopped | unloaded + , started_at => binary() + , max_connection => integer() + , current_connect => integer() + , listeners => [] + }. + +%%-------------------------------------------------------------------- +%% APIs +%%-------------------------------------------------------------------- + +-spec gateways(Status :: all | running | stopped | unloaded) + -> [gateway_summary()]. +gateways(Status) -> + Gateways = lists:map(fun({GwName, _}) -> + case emqx_gateway:lookup(GwName) of + undefined -> #{name => GwName, status => unloaded}; + GwInfo = #{rawconf := RawConf} -> + GwInfo1 = maps:with( + [name, started_at, craeted_at, status], GwInfo), + GwInfo1#{listeners => get_listeners_status(GwName, RawConf)} + + end + end, emqx_gateway_registry:list()), + case Status of + all -> Gateways; + _ -> + [Gw || Gw = #{status := S} <- Gateways, S == Status] + end. + +%% @private +get_listeners_status(GwName, RawConf) -> + Listeners = emqx_gateway_utils:normalize_rawconf(RawConf), + lists:map(fun({Type, LisName, ListenOn, _, _}) -> + Name0 = listener_name(GwName, Type, LisName), + Name = {Name0, ListenOn}, + case catch esockd:listener(Name) of + _Pid when is_pid(_Pid) -> + #{Name0 => <<"activing">>}; + _ -> + #{Name0 => <<"inactived">>} + + end + end, Listeners). + +%% @private +listener_name(GwName, Type, LisName) -> + list_to_atom(lists:concat([GwName, ":", Type, ":", LisName])). diff --git a/apps/emqx_gateway/src/exproto/emqx_exproto_impl.erl b/apps/emqx_gateway/src/exproto/emqx_exproto_impl.erl index c17a6e766..a3b484fad 100644 --- a/apps/emqx_gateway/src/exproto/emqx_exproto_impl.erl +++ b/apps/emqx_gateway/src/exproto/emqx_exproto_impl.erl @@ -173,7 +173,7 @@ do_start_listener(dtls, Name, ListenOn, Opts, MFA) -> esockd:open_dtls(Name, ListenOn, Opts, MFA). name(GwName, LisName, Type) -> - list_to_atom(lists:concat([GwName, ":", LisName, ":", Type])). + list_to_atom(lists:concat([GwName, ":", Type, ":", LisName])). merge_default_by_type(Type, Options) when Type =:= tcp; Type =:= ssl -> diff --git a/apps/emqx_gateway/src/lwm2m/emqx_lwm2m_impl.erl b/apps/emqx_gateway/src/lwm2m/emqx_lwm2m_impl.erl index d1afbc6b2..66a7bd9e2 100644 --- a/apps/emqx_gateway/src/lwm2m/emqx_lwm2m_impl.erl +++ b/apps/emqx_gateway/src/lwm2m/emqx_lwm2m_impl.erl @@ -129,7 +129,7 @@ start_listener(GwName, Ctx, Type, LisName, ListenOn, SocketOpts, Cfg) -> end. name(GwName, LisName, Type) -> - list_to_atom(lists:concat([GwName, ":", LisName, ":", Type])). + list_to_atom(lists:concat([GwName, ":", Type, ":", LisName])). merge_default(Options) -> Default = emqx_gateway_utils:default_udp_options(), diff --git a/apps/emqx_gateway/src/mqttsn/emqx_sn_impl.erl b/apps/emqx_gateway/src/mqttsn/emqx_sn_impl.erl index 9ea3ca3ae..0eeb1b952 100644 --- a/apps/emqx_gateway/src/mqttsn/emqx_sn_impl.erl +++ b/apps/emqx_gateway/src/mqttsn/emqx_sn_impl.erl @@ -128,7 +128,7 @@ start_listener(GwName, Ctx, Type, LisName, ListenOn, SocketOpts, Cfg) -> {emqx_gateway_conn, start_link, [NCfg]}). name(GwName, LisName, Type) -> - list_to_atom(lists:concat([GwName, ":", LisName, ":", Type])). + list_to_atom(lists:concat([GwName, ":", Type, ":", LisName])). merge_default(Options) -> Default = emqx_gateway_utils:default_udp_options(), diff --git a/apps/emqx_gateway/src/stomp/emqx_stomp_impl.erl b/apps/emqx_gateway/src/stomp/emqx_stomp_impl.erl index d8b4bfcff..28dedac26 100644 --- a/apps/emqx_gateway/src/stomp/emqx_stomp_impl.erl +++ b/apps/emqx_gateway/src/stomp/emqx_stomp_impl.erl @@ -113,7 +113,7 @@ start_listener(GwName, Ctx, Type, LisName, ListenOn, SocketOpts, Cfg) -> {emqx_gateway_conn, start_link, [NCfg]}). name(GwName, LisName, Type) -> - list_to_atom(lists:concat([GwName, ":", LisName, ":", Type])). + list_to_atom(lists:concat([GwName, ":", Type, ":", LisName])). merge_default(Options) -> Default = emqx_gateway_utils:default_tcp_options(),