PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/rel/files/nodetool

http://github.com/AF83/ucengine
#! | 116 lines | 106 code | 10 blank | 0 comment | 0 complexity | 9082f7a09ec9fb94800c6572c31409f2 MD5 | raw file
  1. %% -*- mode:erlang;tab-width:4;erlang-indent-level:4;indent-tabs-mode:nil -*-
  2. %% ex: ft=erlang ts=4 sw=4 et
  3. %% -------------------------------------------------------------------
  4. %%
  5. %% nodetool: Helper Script for interacting with live nodes
  6. %%
  7. %% -------------------------------------------------------------------
  8. main(Args) ->
  9. %% Extract the args
  10. {RestArgs, TargetNode} = process_args(Args, [], undefined),
  11. %% See if the node is currently running -- if it's not, we'll bail
  12. case {net_kernel:hidden_connect_node(TargetNode), net_adm:ping(TargetNode)} of
  13. {true, pong} ->
  14. ok;
  15. {_, pang} ->
  16. io:format("Node ~p not responding to pings.\n", [TargetNode]),
  17. halt(1)
  18. end,
  19. case RestArgs of
  20. ["ping"] ->
  21. %% If we got this far, the node already responsed to a ping, so just dump
  22. %% a "pong"
  23. io:format("pong\n");
  24. ["stop"] ->
  25. io:format("~p\n", [rpc:call(TargetNode, init, stop, [], 60000)]);
  26. ["restart"] ->
  27. io:format("~p\n", [rpc:call(TargetNode, init, restart, [], 60000)]);
  28. ["reboot"] ->
  29. io:format("~p\n", [rpc:call(TargetNode, init, reboot, [], 60000)]);
  30. ["rpc", Module, Function | RpcArgs] ->
  31. case rpc:call(TargetNode, list_to_atom(Module), list_to_atom(Function),
  32. [RpcArgs], 60000) of
  33. ok ->
  34. ok;
  35. {badrpc, Reason} ->
  36. io:format("RPC to ~p failed: ~p\n", [TargetNode, Reason]),
  37. halt(1);
  38. _ ->
  39. halt(1)
  40. end;
  41. ["rpcterms", Module, Function, ArgsAsString] ->
  42. case rpc:call(TargetNode, list_to_atom(Module), list_to_atom(Function),
  43. consult(ArgsAsString), 60000) of
  44. {badrpc, Reason} ->
  45. io:format("RPC to ~p failed: ~p\n", [TargetNode, Reason]),
  46. halt(1);
  47. Other ->
  48. io:format("~p\n", [Other])
  49. end;
  50. Other ->
  51. io:format("Other: ~p\n", [Other]),
  52. io:format("Usage: nodetool {ping|stop|restart|reboot}\n")
  53. end,
  54. net_kernel:stop().
  55. process_args([], Acc, TargetNode) ->
  56. {lists:reverse(Acc), TargetNode};
  57. process_args(["-setcookie", Cookie | Rest], Acc, TargetNode) ->
  58. erlang:set_cookie(node(), list_to_atom(Cookie)),
  59. process_args(Rest, Acc, TargetNode);
  60. process_args(["-name", TargetName | Rest], Acc, _) ->
  61. ThisNode = append_node_suffix(TargetName, "_maint_"),
  62. {ok, _} = net_kernel:start([ThisNode, longnames]),
  63. process_args(Rest, Acc, nodename(TargetName));
  64. process_args(["-sname", TargetName | Rest], Acc, _) ->
  65. ThisNode = append_node_suffix(TargetName, "_maint_"),
  66. {ok, _} = net_kernel:start([ThisNode, shortnames]),
  67. process_args(Rest, Acc, nodename(TargetName));
  68. process_args([Arg | Rest], Acc, Opts) ->
  69. process_args(Rest, [Arg | Acc], Opts).
  70. nodename(Name) ->
  71. case string:tokens(Name, "@") of
  72. [_Node, _Host] ->
  73. list_to_atom(Name);
  74. [Node] ->
  75. [_, Host] = string:tokens(atom_to_list(node()), "@"),
  76. list_to_atom(lists:concat([Node, "@", Host]))
  77. end.
  78. append_node_suffix(Name, Suffix) ->
  79. case string:tokens(Name, "@") of
  80. [Node, Host] ->
  81. list_to_atom(lists:concat([Node, Suffix, os:getpid(), "@", Host]));
  82. [Node] ->
  83. list_to_atom(lists:concat([Node, Suffix, os:getpid()]))
  84. end.
  85. %%
  86. %% Given a string or binary, parse it into a list of terms, ala file:consult/0
  87. %%
  88. consult(Str) when is_list(Str) ->
  89. consult([], Str, []);
  90. consult(Bin) when is_binary(Bin)->
  91. consult([], binary_to_list(Bin), []).
  92. consult(Cont, Str, Acc) ->
  93. case erl_scan:tokens(Cont, Str, 0) of
  94. {done, Result, Remaining} ->
  95. case Result of
  96. {ok, Tokens, _} ->
  97. {ok, Term} = erl_parse:parse_term(Tokens),
  98. consult([], Remaining, [Term | Acc]);
  99. {eof, _Other} ->
  100. lists:reverse(Acc);
  101. {error, Info, _} ->
  102. {error, Info}
  103. end;
  104. {more, Cont1} ->
  105. consult(Cont1, eof, Acc)
  106. end.