You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.9 KiB

  1. %% -*- erlang -*-
  2. %% -------------------------------------------------------------------
  3. %%
  4. %% nodetool: Helper Script for interacting with live nodes
  5. %%
  6. %% -------------------------------------------------------------------
  7. main(Args) ->
  8. %% Extract the args
  9. {RestArgs, TargetNode} = process_args(Args, [], undefined),
  10. %% See if the node is currently running -- if it's not, we'll bail
  11. case {net_kernel:hidden_connect_node(TargetNode), net_adm:ping(TargetNode)} of
  12. {true, pong} ->
  13. ok;
  14. {_, pang} ->
  15. io:format("Node ~p not responding to pings.\n", [TargetNode]),
  16. halt(1)
  17. end,
  18. case RestArgs of
  19. ["ping"] ->
  20. %% If we got this far, the node already responsed to a ping, so just dump
  21. %% a "pong"
  22. io:format("pong\n");
  23. ["stop"] ->
  24. io:format("~p\n", [rpc:call(TargetNode, init, stop, [], 60000)]);
  25. ["restart"] ->
  26. io:format("~p\n", [rpc:call(TargetNode, init, restart, [], 60000)]);
  27. ["reboot"] ->
  28. io:format("~p\n", [rpc:call(TargetNode, init, reboot, [], 60000)]);
  29. ["rpc", Module, Function | RpcArgs] ->
  30. case rpc:call(TargetNode, list_to_atom(Module), list_to_atom(Function), [RpcArgs], 60000) of
  31. ok ->
  32. ok;
  33. {badrpc, Reason} ->
  34. io:format("RPC to ~p failed: ~p\n", [TargetNode, Reason]),
  35. halt(1);
  36. _ ->
  37. halt(1)
  38. end;
  39. Other ->
  40. io:format("Other: ~p\n", [Other]),
  41. io:format("Usage: nodetool {ping|stop|restart|reboot}\n")
  42. end,
  43. net_kernel:stop().
  44. process_args([], Acc, TargetNode) ->
  45. {lists:reverse(Acc), TargetNode};
  46. process_args(["-setcookie", Cookie | Rest], Acc, TargetNode) ->
  47. erlang:set_cookie(node(), list_to_atom(Cookie)),
  48. process_args(Rest, Acc, TargetNode);
  49. process_args(["-name", TargetName | Rest], Acc, _) ->
  50. ThisNode = append_node_suffix(TargetName, "_maint_"),
  51. {ok, _} = net_kernel:start([ThisNode, longnames]),
  52. process_args(Rest, Acc, nodename(TargetName));
  53. process_args(["-sname", TargetName | Rest], Acc, _) ->
  54. ThisNode = append_node_suffix(TargetName, "_maint_"),
  55. {ok, _} = net_kernel:start([ThisNode, shortnames]),
  56. process_args(Rest, Acc, nodename(TargetName));
  57. process_args([Arg | Rest], Acc, Opts) ->
  58. process_args(Rest, [Arg | Acc], Opts).
  59. nodename(Name) ->
  60. case string:tokens(Name, "@") of
  61. [_Node, _Host] ->
  62. list_to_atom(Name);
  63. [Node] ->
  64. [_, Host] = string:tokens(atom_to_list(node()), "@"),
  65. list_to_atom(lists:concat([Node, "@", Host]))
  66. end.
  67. append_node_suffix(Name, Suffix) ->
  68. case string:tokens(Name, "@") of
  69. [Node, Host] ->
  70. list_to_atom(lists:concat([Node, Suffix, os:getpid(), "@", Host]));
  71. [Node] ->
  72. list_to_atom(lists:concat([Node, Suffix, os:getpid()]))
  73. end.