emqx_observer_cli.erl 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2024 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -module(emqx_observer_cli).
  17. -export([
  18. enable/0,
  19. disable/0
  20. ]).
  21. -export([cmd/1]).
  22. %%--------------------------------------------------------------------
  23. %% enable/disable
  24. %%--------------------------------------------------------------------
  25. enable() ->
  26. emqx_ctl:register_command(observer, {?MODULE, cmd}, []).
  27. disable() ->
  28. emqx_ctl:unregister_command(observer).
  29. cmd(["status"]) ->
  30. observer_cli:start();
  31. cmd(["bin_leak"]) ->
  32. lists:foreach(
  33. fun(Row) -> emqx_ctl:print("~p~n", [Row]) end,
  34. recon:bin_leak(100)
  35. );
  36. cmd(["load", Mod]) ->
  37. case nodes() of
  38. [] ->
  39. emqx_ctl:print("No other nodes in the cluster~n");
  40. Nodes ->
  41. case emqx_utils:safe_to_existing_atom(Mod) of
  42. {ok, Module} ->
  43. Res = recon:remote_load(Nodes, Module),
  44. emqx_ctl:print("Loaded ~p module on ~p: ~p~n", [Module, Nodes, Res]);
  45. {error, Reason} ->
  46. emqx_ctl:print("Module(~s) not found: ~p~n", [Mod, Reason])
  47. end
  48. end;
  49. cmd(_) ->
  50. emqx_ctl:usage([
  51. {"observer status", "Start observer in the current console"},
  52. {"observer bin_leak",
  53. "Force all processes to perform garbage collection "
  54. "and prints the top-100 processes that freed the "
  55. "biggest amount of binaries, potentially highlighting leaks."},
  56. {"observer load Mod", "Enhanced module synchronization across all cluster nodes"}
  57. ]).