emqx_observer_cli.erl 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2022 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([ enable/0
  18. , disable/0
  19. ]).
  20. -export([cmd/1]).
  21. %%--------------------------------------------------------------------
  22. %% enable/disable
  23. %%--------------------------------------------------------------------
  24. enable() ->
  25. emqx_ctl:register_command(observer, {?MODULE, cmd}, []).
  26. disable() ->
  27. emqx_ctl:unregister_command(observer).
  28. cmd(["status"]) ->
  29. observer_cli:start();
  30. cmd(["bin_leak"]) ->
  31. [emqx_ctl:print("~p~n", [Row]) || Row <- recon:bin_leak(100)];
  32. cmd(["load", Mod]) ->
  33. Module = list_to_existing_atom(Mod),
  34. Nodes = nodes(),
  35. Res = remote_load(Nodes, Module),
  36. emqx_ctl:print("Loaded ~p module on ~p on ~n", [Mod, Nodes, Res]);
  37. cmd(_) ->
  38. emqx_ctl:usage([{"observer status", "observer_cli:start()"},
  39. {"observer bin_leak", "recon:bin_leak(100)"},
  40. {"observer load Mod", "recon:remote_load(Mod) to all nodes"}]).
  41. %% recon:remote_load/1 has a bug, when nodes() returns [], it is
  42. %% taken by recon as a node name.
  43. %% before OTP 23, the call returns a 'badrpc' tuple
  44. %% after OTP 23, it crashes with 'badarg' error
  45. remote_load([], _Module) -> ok;
  46. remote_load(Nodes, Module) -> recon:remote_load(Nodes, Module).