From e76b0877d6281e9e23984bd5e946bf43091875b8 Mon Sep 17 00:00:00 2001 From: Badlop Date: Thu, 21 Jul 2022 15:48:53 +0200 Subject: [PATCH] Add mod_status/0 to show its current status in WebAdmin --- .../src/ejabberd_observer_cli.erl | 5 ++- mod_isolation/src/mod_isolation.erl | 6 +++- mod_log_chat/src/mod_log_chat.erl | 13 ++++++- mod_logsession/src/mod_logsession.erl | 15 +++++++- mod_message_log/src/mod_message_log.erl | 8 +++++ mod_muc_log_http/src/mod_muc_log_http.erl | 35 ++++++++++++++++++- mod_rest/src/mod_rest.erl | 34 ++++++++++++++++++ mod_s2s_log/src/mod_s2s_log.erl | 9 +++++ mod_shcommands/src/mod_shcommands.erl | 5 ++- mod_statsdx/src/mod_statsdx.erl | 5 ++- mod_webpresence/src/mod_webpresence.erl | 34 ++++++++++++++++++ 11 files changed, 162 insertions(+), 7 deletions(-) diff --git a/ejabberd_observer_cli/src/ejabberd_observer_cli.erl b/ejabberd_observer_cli/src/ejabberd_observer_cli.erl index 915ed80..2aaa150 100644 --- a/ejabberd_observer_cli/src/ejabberd_observer_cli.erl +++ b/ejabberd_observer_cli/src/ejabberd_observer_cli.erl @@ -1,11 +1,14 @@ -module(ejabberd_observer_cli). --export([start/0]). +-export([start/0, mod_status/0]). start() -> application:set_env(observer_cli, plugins, plugins(), [{persistent, true}]), observer_cli:start_plugin(). +mod_status() -> + "In an erlang shell run: ejabberd_observer_cli:start().". + plugins() -> [ #{ diff --git a/mod_isolation/src/mod_isolation.erl b/mod_isolation/src/mod_isolation.erl index d5d54f9..69b5e0a 100644 --- a/mod_isolation/src/mod_isolation.erl +++ b/mod_isolation/src/mod_isolation.erl @@ -19,7 +19,8 @@ -behaviour(gen_mod). %% gen_mod callbacks --export([start/2, stop/1, reload/3, mod_options/1, depends/2, mod_doc/0]). +-export([start/2, stop/1, reload/3, mod_options/1, depends/2, mod_doc/0, + mod_status/0]). %% hooks -export([filter_packet/1]). @@ -50,6 +51,9 @@ depends(_Host, _Opts) -> mod_doc() -> #{}. +mod_status() -> + "Isolation enabled". + %%%=================================================================== %%% Internal functions %%%=================================================================== diff --git a/mod_log_chat/src/mod_log_chat.erl b/mod_log_chat/src/mod_log_chat.erl index 4d6fd25..8fad81b 100644 --- a/mod_log_chat/src/mod_log_chat.erl +++ b/mod_log_chat/src/mod_log_chat.erl @@ -10,7 +10,7 @@ -behaviour(gen_mod). --export([start/2, stop/1, depends/2, mod_opt_type/1, mod_options/1, mod_doc/0]). +-export([start/2, stop/1, depends/2, mod_opt_type/1, mod_options/1, mod_doc/0, mod_status/0]). -export([init/1, log_packet_send/1, log_packet_receive/1]). @@ -289,3 +289,14 @@ mod_options(_Host) -> {format, text}]. mod_doc() -> #{}. + +mod_status() -> + Host = ejabberd_config:get_myname(), + gen_mod:get_module_proc(Host, ?PROCNAME) ! {call, self(), get_config}, + Config = receive + {config, Result} -> + Result + end, + Format = Config#config.format, + Path = Config#config.path, + io_lib:format("Logging with format '~p' to path: ~s", [Format, Path]). diff --git a/mod_logsession/src/mod_logsession.erl b/mod_logsession/src/mod_logsession.erl index d0811ac..b9ea83c 100644 --- a/mod_logsession/src/mod_logsession.erl +++ b/mod_logsession/src/mod_logsession.erl @@ -29,7 +29,7 @@ -behaviour(gen_mod). --export([start/2, stop/1, depends/2, mod_options/1, mod_opt_type/1, mod_doc/0]). +-export([start/2, stop/1, depends/2, mod_options/1, mod_opt_type/1, mod_doc/0, mod_status/0]). -export([loop/3, reopen_log/1, failed_auth/3, @@ -80,6 +80,16 @@ mod_options(_Host) -> mod_doc() -> #{}. +mod_status() -> + Host = ejabberd_config:get_myname(), + Pid = get_process_name(Host), + Pid ! {get_filename, self()}, + Filename = receive + {filename, F} -> + F + end, + io_lib:format("Logging ~p to: ~s", [binary_to_list(Host), Filename]). + %%%---------------------------------------------------------------------- %%% REQUEST HANDLERS %%%---------------------------------------------------------------------- @@ -116,6 +126,9 @@ loop(Filename, File, Host) -> reopenlog -> File2 = reopen_file(File, Filename), loop(Filename, File2, Host); + {get_filename, Pid} -> + Pid ! {filename, Filename}, + loop(Filename, File, Host); stop -> close_file(File) end. diff --git a/mod_message_log/src/mod_message_log.erl b/mod_message_log/src/mod_message_log.erl index efc6f6e..db84d67 100644 --- a/mod_message_log/src/mod_message_log.erl +++ b/mod_message_log/src/mod_message_log.erl @@ -35,6 +35,7 @@ mod_opt_type/1, mod_options/1, depends/2, + mod_status/0, mod_doc/0]). %% gen_server callbacks. @@ -108,6 +109,11 @@ depends(_Host, _Opts) -> mod_doc() -> #{}. +mod_status() -> + Proc = gen_mod:get_module_proc(global, ?MODULE), + {filename, Filename} = gen_server:call(Proc, get_filename, timer:seconds(15)), + io_lib:format("Logging to: ~s", [Filename]). + %% ------------------------------------------------------------------- %% gen_server callbacks. %% ------------------------------------------------------------------- @@ -125,6 +131,8 @@ init([_Host, Opts]) -> {ok, #state{filename = Filename, iodevice = IoDevice}}. -spec handle_call(_, {pid(), _}, state()) -> {noreply, state()}. +handle_call(get_filename, From, State) -> + {reply, {filename, State#state.filename}, State}; handle_call(_Request, _From, State) -> {noreply, State}. diff --git a/mod_muc_log_http/src/mod_muc_log_http.erl b/mod_muc_log_http/src/mod_muc_log_http.erl index bd6036b..ed4c6e8 100644 --- a/mod_muc_log_http/src/mod_muc_log_http.erl +++ b/mod_muc_log_http/src/mod_muc_log_http.erl @@ -10,7 +10,7 @@ -behaviour(gen_mod). --export([start/2, stop/1, depends/2, mod_options/1, mod_doc/0]). +-export([start/2, stop/1, depends/2, mod_options/1, mod_doc/0, mod_status/0]). -export([process/2]). @@ -237,3 +237,36 @@ mod_options(_Host) -> []. mod_doc() -> #{}. + +mod_status() -> + Host = ejabberd_config:get_myname(), + Url = case find_handler_port_path(any, ?MODULE) of + [] -> undefined; + [{ThisTls, Port, Path} | _] -> + Protocol = case ThisTls of + false -> <<"http">>; + true -> <<"https">> + end, + <>))/binary, + "/">> + end, + io_lib:format("Serving MUC logs in: ~s", [binary_to_list(Url)]). + +find_handler_port_path(Tls, Module) -> + lists:filtermap( + fun({{Port, _, _}, + ejabberd_http, + #{tls := ThisTls, request_handlers := Handlers}}) + when (Tls == any) or (Tls == ThisTls) -> + case lists:keyfind(Module, 2, Handlers) of + false -> false; + {Path, Module} -> {true, {ThisTls, Port, Path}} + end; + (_) -> false + end, ets:tab2list(ejabberd_listener)). diff --git a/mod_rest/src/mod_rest.erl b/mod_rest/src/mod_rest.erl index a6d63b0..d412433 100644 --- a/mod_rest/src/mod_rest.erl +++ b/mod_rest/src/mod_rest.erl @@ -31,6 +31,7 @@ depends/2, split_line/1, process/2, + mod_status/0, mod_opt_type/1, mod_options/1, mod_doc/0]). -include("logger.hrl"). @@ -189,3 +190,36 @@ mod_options(_Host) -> {access_commands, []}]. mod_doc() -> #{}. + +mod_status() -> + Host = ejabberd_config:get_myname(), + Url = case find_handler_port_path(any, ?MODULE) of + [] -> undefined; + [{ThisTls, Port, Path} | _] -> + Protocol = case ThisTls of + false -> <<"http">>; + true -> <<"https">> + end, + <>))/binary, + "/">> + end, + io_lib:format("Awaiting HTTP POSTs in: ~s", [binary_to_list(Url)]). + +find_handler_port_path(Tls, Module) -> + lists:filtermap( + fun({{Port, _, _}, + ejabberd_http, + #{tls := ThisTls, request_handlers := Handlers}}) + when (Tls == any) or (Tls == ThisTls) -> + case lists:keyfind(Module, 2, Handlers) of + false -> false; + {Path, Module} -> {true, {ThisTls, Port, Path}} + end; + (_) -> false + end, ets:tab2list(ejabberd_listener)). diff --git a/mod_s2s_log/src/mod_s2s_log.erl b/mod_s2s_log/src/mod_s2s_log.erl index 7b05cee..aa6217d 100644 --- a/mod_s2s_log/src/mod_s2s_log.erl +++ b/mod_s2s_log/src/mod_s2s_log.erl @@ -34,6 +34,7 @@ depends/2, mod_doc/0, mod_opt_type/1, + mod_status/0, mod_options/1]). %% Hooks: -export([reopen_log/0, @@ -79,6 +80,9 @@ loop(Config) -> file:close(Config#config.iodevice), {ok, IOD} = file:open(Config#config.filename, ?FILE_OPTS), loop(Config#config{iodevice = IOD}); + {get_filename, Pid} -> + Pid ! {filename, Config#config.filename}, + loop(Config); stop -> file:close(Config#config.iodevice), exit(normal) @@ -120,6 +124,11 @@ mod_options(_Host) -> mod_doc() -> #{}. +mod_status() -> + ?PROCNAME ! {get_filename, self()}, + Filename = receive {filename, F} -> F end, + io_lib:format("Logging to: ~s", [binary_to_list(Filename)]). + %% --- %% Internal functions diff --git a/mod_shcommands/src/mod_shcommands.erl b/mod_shcommands/src/mod_shcommands.erl index 835e1b1..8aa40db 100644 --- a/mod_shcommands/src/mod_shcommands.erl +++ b/mod_shcommands/src/mod_shcommands.erl @@ -11,7 +11,7 @@ -behaviour(gen_mod). --export([start/2, stop/1, depends/2, mod_options/1, mod_doc/0]). +-export([start/2, stop/1, depends/2, mod_options/1, mod_doc/0, mod_status/0]). -export([execute_system/1, execute_erlang/1]). -export([web_menu_node/3, web_page_node/5]). @@ -49,6 +49,9 @@ mod_options(_Host) -> mod_doc() -> #{}. +mod_status() -> + "Page available in WebAdmin -> Nodes -> your node -> Shell Commands". + %%------------------- %% Commands %%------------------- diff --git a/mod_statsdx/src/mod_statsdx.erl b/mod_statsdx/src/mod_statsdx.erl index d9c2b90..8a63179 100644 --- a/mod_statsdx/src/mod_statsdx.erl +++ b/mod_statsdx/src/mod_statsdx.erl @@ -13,7 +13,7 @@ -behaviour(gen_mod). --export([start/2, stop/1, depends/2, mod_opt_type/1, mod_options/1, mod_doc/0]). +-export([start/2, stop/1, depends/2, mod_opt_type/1, mod_options/1, mod_doc/0, mod_status/0]). -export([loop/1, get_statistic/2, pre_uninstall/0, received_response/3, @@ -100,6 +100,9 @@ mod_options(_Host) -> mod_doc() -> #{}. +mod_status() -> + "Pages 'Statistics Dx' available in WebAdmin, your Virtual Hosts and your Nodes". + %%%================================== %%%% Stats Server diff --git a/mod_webpresence/src/mod_webpresence.erl b/mod_webpresence/src/mod_webpresence.erl index ed0a47b..7cc0c42 100644 --- a/mod_webpresence/src/mod_webpresence.erl +++ b/mod_webpresence/src/mod_webpresence.erl @@ -26,6 +26,7 @@ %% gen_server callbacks -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3, + mod_status/0, mod_opt_type/1, mod_options/1, depends/2, mod_doc/0]). %% API @@ -105,6 +106,39 @@ depends(_Host, _Opts) -> mod_doc() -> #{}. +mod_status() -> + Host = ejabberd_config:get_myname(), + Url = case find_handler_port_path(any, ?MODULE) of + [] -> undefined; + [{ThisTls, Port, Path} | _] -> + Protocol = case ThisTls of + false -> <<"http">>; + true -> <<"https">> + end, + <>))/binary, + "/">> + end, + io_lib:format("Serving Webpresence in: ~s", [binary_to_list(Url)]). + +find_handler_port_path(Tls, Module) -> + lists:filtermap( + fun({{Port, _, _}, + ejabberd_http, + #{tls := ThisTls, request_handlers := Handlers}}) + when (Tls == any) or (Tls == ThisTls) -> + case lists:keyfind(Module, 2, Handlers) of + false -> false; + {Path, Module} -> {true, {ThisTls, Port, Path}} + end; + (_) -> false + end, ets:tab2list(ejabberd_listener)). + %%==================================================================== %% gen_server callbacks %%====================================================================