2004-01-11 21:42:57 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_ctl.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Purpose : Ejabberd admin tool
|
|
|
|
%%% Created : 11 Jan 2004 by Alexey Shchepin <alex@alex.sevcom.net>
|
|
|
|
%%% Id : $Id$
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_ctl).
|
|
|
|
-author('alexey@sevcom.net').
|
|
|
|
|
|
|
|
-export([start/0]).
|
|
|
|
|
2004-09-10 22:57:00 +02:00
|
|
|
-define(STATUS_SUCCESS, 0).
|
|
|
|
-define(STATUS_ERROR, 1).
|
|
|
|
-define(STATUS_USAGE, 2).
|
|
|
|
-define(STATUS_BADRPC, 3).
|
|
|
|
|
2004-01-11 21:42:57 +01:00
|
|
|
start() ->
|
|
|
|
case init:get_plain_arguments() of
|
|
|
|
[SNode | Args] ->
|
|
|
|
Node = list_to_atom(SNode),
|
2004-09-10 22:57:00 +02:00
|
|
|
Status = process(Node, Args),
|
|
|
|
halt(Status);
|
2004-01-11 21:42:57 +01:00
|
|
|
_ ->
|
2004-09-10 22:57:00 +02:00
|
|
|
print_usage(),
|
|
|
|
halt(?STATUS_USAGE)
|
|
|
|
end.
|
|
|
|
|
2004-01-11 21:42:57 +01:00
|
|
|
|
2004-09-10 22:57:00 +02:00
|
|
|
process(Node, ["status"]) ->
|
|
|
|
case rpc:call(Node, init, get_status, []) of
|
|
|
|
{badrpc, Reason} ->
|
|
|
|
io:format("Can't get node ~p status: ~p~n",
|
|
|
|
[Node, Reason]),
|
|
|
|
?STATUS_BADRPC;
|
|
|
|
{InternalStatus, ProvidedStatus} ->
|
|
|
|
io:format("Node ~p is ~p. Status: ~p~n",
|
|
|
|
[Node, InternalStatus, ProvidedStatus]),
|
|
|
|
?STATUS_SUCCESS
|
|
|
|
end;
|
2004-01-11 21:42:57 +01:00
|
|
|
|
|
|
|
process(Node, ["stop"]) ->
|
|
|
|
case rpc:call(Node, init, stop, []) of
|
|
|
|
{badrpc, Reason} ->
|
|
|
|
io:format("Can't stop node ~p: ~p~n",
|
2004-09-10 22:57:00 +02:00
|
|
|
[Node, Reason]),
|
|
|
|
?STATUS_BADRPC;
|
2004-01-11 21:42:57 +01:00
|
|
|
_ ->
|
2004-09-10 22:57:00 +02:00
|
|
|
?STATUS_SUCCESS
|
2004-01-11 21:42:57 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
process(Node, ["restart"]) ->
|
|
|
|
case rpc:call(Node, init, restart, []) of
|
|
|
|
{badrpc, Reason} ->
|
|
|
|
io:format("Can't restart node ~p: ~p~n",
|
2004-09-10 22:57:00 +02:00
|
|
|
[Node, Reason]),
|
|
|
|
?STATUS_BADRPC;
|
2004-01-11 21:42:57 +01:00
|
|
|
_ ->
|
2004-09-10 22:57:00 +02:00
|
|
|
?STATUS_SUCCESS
|
2004-01-11 21:42:57 +01:00
|
|
|
end;
|
|
|
|
|
2004-01-27 20:14:49 +01:00
|
|
|
process(Node, ["reopen-log"]) ->
|
2004-09-10 22:57:00 +02:00
|
|
|
case rpc:call(Node, ejabberd_logger_h, reopen_log, []) of
|
|
|
|
{badrpc, Reason} ->
|
|
|
|
io:format("Can't reopen node ~p log: ~p~n",
|
|
|
|
[Node, Reason]),
|
|
|
|
?STATUS_BADRPC;
|
|
|
|
_ ->
|
|
|
|
?STATUS_SUCCESS
|
|
|
|
end;
|
2004-01-27 20:14:49 +01:00
|
|
|
|
2005-04-18 00:59:44 +02:00
|
|
|
process(Node, ["register", User, Server, Password]) ->
|
|
|
|
case rpc:call(Node, ejabberd_auth, try_register, [User, Server, Password]) of
|
2004-01-11 21:42:57 +01:00
|
|
|
{atomic, ok} ->
|
2004-09-10 22:57:00 +02:00
|
|
|
?STATUS_SUCCESS;
|
2004-01-11 21:42:57 +01:00
|
|
|
{atomic, exists} ->
|
|
|
|
io:format("User ~p already registered on node ~p~n",
|
2005-04-18 00:59:44 +02:00
|
|
|
[User ++ "@" ++ Server, Node]),
|
2004-09-10 22:57:00 +02:00
|
|
|
?STATUS_ERROR;
|
2004-01-11 21:42:57 +01:00
|
|
|
{error, Reason} ->
|
|
|
|
io:format("Can't register user ~p on node ~p: ~p~n",
|
2005-04-18 00:59:44 +02:00
|
|
|
[User ++ "@" ++ Server, Node, Reason]),
|
2004-09-10 22:57:00 +02:00
|
|
|
?STATUS_ERROR;
|
2004-01-11 21:42:57 +01:00
|
|
|
{badrpc, Reason} ->
|
|
|
|
io:format("Can't register user ~p on node ~p: ~p~n",
|
2005-04-18 00:59:44 +02:00
|
|
|
[User ++ "@" ++ Server, Node, Reason]),
|
2004-09-10 22:57:00 +02:00
|
|
|
?STATUS_BADRPC
|
2004-01-11 21:42:57 +01:00
|
|
|
end;
|
|
|
|
|
2005-04-18 00:59:44 +02:00
|
|
|
process(Node, ["unregister", User, Server]) ->
|
|
|
|
case rpc:call(Node, ejabberd_auth, remove_user, [User, Server]) of
|
2004-01-11 21:42:57 +01:00
|
|
|
{error, Reason} ->
|
|
|
|
io:format("Can't unregister user ~p on node ~p: ~p~n",
|
2005-04-18 00:59:44 +02:00
|
|
|
[User ++ "@" ++ Server, Node, Reason]),
|
2004-09-10 22:57:00 +02:00
|
|
|
?STATUS_ERROR;
|
2004-01-11 21:42:57 +01:00
|
|
|
{badrpc, Reason} ->
|
|
|
|
io:format("Can't unregister user ~p on node ~p: ~p~n",
|
2005-04-18 00:59:44 +02:00
|
|
|
[User ++ "@" ++ Server, Node, Reason]),
|
|
|
|
?STATUS_BADRPC;
|
|
|
|
_ ->
|
|
|
|
?STATUS_SUCCESS
|
2004-01-11 21:42:57 +01:00
|
|
|
end;
|
|
|
|
|
2004-01-18 21:42:09 +01:00
|
|
|
process(Node, ["backup", Path]) ->
|
|
|
|
case rpc:call(Node, mnesia, backup, [Path]) of
|
2004-05-05 22:34:54 +02:00
|
|
|
ok ->
|
2004-09-10 22:57:00 +02:00
|
|
|
?STATUS_SUCCESS;
|
2004-01-18 21:42:09 +01:00
|
|
|
{error, Reason} ->
|
|
|
|
io:format("Can't store backup in ~p on node ~p: ~p~n",
|
2004-09-10 22:57:00 +02:00
|
|
|
[Path, Node, Reason]),
|
|
|
|
?STATUS_ERROR;
|
2004-01-18 21:42:09 +01:00
|
|
|
{badrpc, Reason} ->
|
|
|
|
io:format("Can't store backup in ~p on node ~p: ~p~n",
|
2004-09-10 22:57:00 +02:00
|
|
|
[Path, Node, Reason]),
|
|
|
|
?STATUS_BADRPC
|
2004-01-18 21:42:09 +01:00
|
|
|
end;
|
|
|
|
|
2004-05-05 22:34:54 +02:00
|
|
|
process(Node, ["dump", Path]) ->
|
|
|
|
case rpc:call(Node, mnesia, dump_to_textfile, [Path]) of
|
|
|
|
ok ->
|
2004-09-10 22:57:00 +02:00
|
|
|
?STATUS_SUCCESS;
|
2004-05-05 22:34:54 +02:00
|
|
|
{error, Reason} ->
|
|
|
|
io:format("Can't store dump in ~p on node ~p: ~p~n",
|
2004-09-10 22:57:00 +02:00
|
|
|
[Path, Node, Reason]),
|
|
|
|
?STATUS_ERROR;
|
2004-05-05 22:34:54 +02:00
|
|
|
{badrpc, Reason} ->
|
|
|
|
io:format("Can't store dump in ~p on node ~p: ~p~n",
|
2004-09-10 22:57:00 +02:00
|
|
|
[Path, Node, Reason]),
|
|
|
|
?STATUS_BADRPC
|
2004-05-05 22:34:54 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
process(Node, ["load", Path]) ->
|
|
|
|
case rpc:call(Node, mnesia, load_textfile, [Path]) of
|
|
|
|
ok ->
|
2004-09-10 22:57:00 +02:00
|
|
|
?STATUS_SUCCESS;
|
2004-05-05 22:34:54 +02:00
|
|
|
{error, Reason} ->
|
|
|
|
io:format("Can't load dump in ~p on node ~p: ~p~n",
|
2004-09-10 22:57:00 +02:00
|
|
|
[Path, Node, Reason]),
|
|
|
|
?STATUS_ERROR;
|
2004-05-05 22:34:54 +02:00
|
|
|
{badrpc, Reason} ->
|
|
|
|
io:format("Can't load dump in ~p on node ~p: ~p~n",
|
2004-09-10 22:57:00 +02:00
|
|
|
[Path, Node, Reason]),
|
|
|
|
?STATUS_BADRPC
|
2004-05-05 22:34:54 +02:00
|
|
|
end;
|
|
|
|
|
2004-01-18 21:42:09 +01:00
|
|
|
process(Node, ["restore", Path]) ->
|
|
|
|
case rpc:call(Node,
|
|
|
|
mnesia, restore, [Path, [{default_op, keep_tables}]]) of
|
2005-04-17 23:39:41 +02:00
|
|
|
{atomic, _} ->
|
2004-09-10 22:57:00 +02:00
|
|
|
?STATUS_SUCCESS;
|
2004-01-18 21:42:09 +01:00
|
|
|
{error, Reason} ->
|
|
|
|
io:format("Can't restore backup from ~p on node ~p: ~p~n",
|
2004-09-10 22:57:00 +02:00
|
|
|
[Path, Node, Reason]),
|
|
|
|
?STATUS_ERROR;
|
2004-01-18 21:42:09 +01:00
|
|
|
{badrpc, Reason} ->
|
|
|
|
io:format("Can't restore backup from ~p on node ~p: ~p~n",
|
2004-09-10 22:57:00 +02:00
|
|
|
[Path, Node, Reason]),
|
|
|
|
?STATUS_BADRPC
|
2004-01-18 21:42:09 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
process(Node, ["install-fallback", Path]) ->
|
|
|
|
case rpc:call(Node, mnesia, install_fallback, [Path]) of
|
|
|
|
{atomic, ok} ->
|
2004-09-10 22:57:00 +02:00
|
|
|
?STATUS_SUCCESS;
|
2004-01-18 21:42:09 +01:00
|
|
|
{error, Reason} ->
|
|
|
|
io:format("Can't install fallback from ~p on node ~p: ~p~n",
|
2004-09-10 22:57:00 +02:00
|
|
|
[Path, Node, Reason]),
|
|
|
|
?STATUS_ERROR;
|
2004-01-18 21:42:09 +01:00
|
|
|
{badrpc, Reason} ->
|
|
|
|
io:format("Can't install fallback from ~p on node ~p: ~p~n",
|
2004-09-10 22:57:00 +02:00
|
|
|
[Path, Node, Reason]),
|
|
|
|
?STATUS_BADRPC
|
2004-01-18 21:42:09 +01:00
|
|
|
end;
|
|
|
|
|
2004-05-05 22:34:54 +02:00
|
|
|
process(Node, ["registered-users"]) ->
|
|
|
|
case rpc:call(Node, ejabberd_auth, dirty_get_registered_users, []) of
|
|
|
|
Users when is_list(Users) ->
|
|
|
|
NewLine = io_lib:format("~n", []),
|
|
|
|
SUsers = lists:sort(Users),
|
2005-04-18 00:59:44 +02:00
|
|
|
FUsers = lists:map(fun({U, S}) -> [U, $@, S, NewLine] end, SUsers),
|
2004-05-05 22:34:54 +02:00
|
|
|
io:format("~s", [FUsers]),
|
2004-09-10 22:57:00 +02:00
|
|
|
?STATUS_SUCCESS;
|
|
|
|
{error, Reason} ->
|
|
|
|
io:format("Can't get list of registered users on node ~p: ~p~n",
|
|
|
|
[Node, Reason]),
|
|
|
|
?STATUS_ERROR;
|
|
|
|
{badrpc, Reason} ->
|
2004-05-05 22:34:54 +02:00
|
|
|
io:format("Can't get list of registered users on node ~p: ~p~n",
|
2004-09-10 22:57:00 +02:00
|
|
|
[Node, Reason]),
|
|
|
|
?STATUS_BADRPC
|
|
|
|
end;
|
|
|
|
|
|
|
|
process(Node, ["delete-expired-messages"]) ->
|
|
|
|
case rpc:call(Node, mod_offline, remove_expired_messages, []) of
|
|
|
|
{badrpc, Reason} ->
|
|
|
|
io:format("Can't delete expired messages at node ~p: ~p~n",
|
|
|
|
[Node, Reason]),
|
|
|
|
?STATUS_BADRPC;
|
|
|
|
_ ->
|
|
|
|
?STATUS_SUCCESS
|
2004-05-05 22:34:54 +02:00
|
|
|
end;
|
|
|
|
|
2004-01-11 21:42:57 +01:00
|
|
|
process(_Node, _Args) ->
|
2004-09-10 22:57:00 +02:00
|
|
|
print_usage(),
|
|
|
|
?STATUS_USAGE.
|
2004-01-11 21:42:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print_usage() ->
|
2004-05-05 22:34:54 +02:00
|
|
|
io:format(
|
|
|
|
"Usage: ejabberdctl node command~n"
|
|
|
|
"~n"
|
|
|
|
"Available commands:~n"
|
2004-09-10 22:57:00 +02:00
|
|
|
" status\t\t\tget ejabberd status~n"
|
2004-05-05 22:34:54 +02:00
|
|
|
" stop\t\t\t\tstop ejabberd~n"
|
|
|
|
" restart\t\t\trestart ejabberd~n"
|
|
|
|
" reopen-log\t\t\treopen log file~n"
|
2005-04-18 00:59:44 +02:00
|
|
|
" register user server password\tregister a user~n"
|
|
|
|
" unregister user server\t\tunregister a user~n"
|
2004-05-05 22:34:54 +02:00
|
|
|
" backup file\t\t\tstore a database backup in file~n"
|
|
|
|
" restore file\t\t\trestore a database backup from file~n"
|
|
|
|
" install-fallback file\t\tinstall a database fallback from file~n"
|
|
|
|
" dump file\t\t\tdump a database in a text file~n"
|
|
|
|
" load file\t\t\trestore a database from a text file~n"
|
|
|
|
" registered-users\t\tlist all registered users~n"
|
2004-09-10 22:57:00 +02:00
|
|
|
" delete-expired-messages\tdelete expired offline messages from database~n"
|
2004-05-05 22:34:54 +02:00
|
|
|
"~n"
|
|
|
|
"Example:~n"
|
|
|
|
" ejabberdctl ejabberd@host restart~n"
|
|
|
|
).
|