emqx_observer_cli.erl 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2023 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. Module = list_to_existing_atom(Mod),
  38. Nodes = nodes(),
  39. Res = remote_load(Nodes, Module),
  40. emqx_ctl:print("Loaded ~p module on ~p: ~p~n", [Module, Nodes, Res]);
  41. cmd(_) ->
  42. emqx_ctl:usage([
  43. {"observer status", "Start observer in the current console"},
  44. {"observer bin_leak",
  45. "Force all processes to perform garbage collection "
  46. "and prints the top-100 processes that freed the "
  47. "biggest amount of binaries, potentially highlighting leaks."},
  48. {"observer load Mod", "Ensure a module is loaded in all EMQX nodes in the cluster"}
  49. ]).
  50. %% recon:remote_load/1 has a bug, when nodes() returns [], it is
  51. %% taken by recon as a node name.
  52. %% before OTP 23, the call returns a 'badrpc' tuple
  53. %% after OTP 23, it crashes with 'badarg' error
  54. remote_load([], _Module) -> ok;
  55. remote_load(Nodes, Module) -> recon:remote_load(Nodes, Module).