1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
%%%----------------------------------------------------------------------
%%% 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]).
start() ->
case init:get_plain_arguments() of
[SNode | Args] ->
Node = list_to_atom(SNode),
process(Node, Args);
_ ->
print_usage()
end,
halt().
process(Node, ["stop"]) ->
case rpc:call(Node, init, stop, []) of
{badrpc, Reason} ->
io:format("Can't stop node ~p: ~p~n",
[Node, Reason]);
_ ->
ok
end;
process(Node, ["restart"]) ->
case rpc:call(Node, init, restart, []) of
{badrpc, Reason} ->
io:format("Can't restart node ~p: ~p~n",
[Node, Reason]);
_ ->
ok
end;
process(Node, ["reopen-log"]) ->
{error_logger, Node} ! {emulator, noproc, reopen};
process(Node, ["register", User, Password]) ->
case rpc:call(Node, ejabberd_auth, try_register, [User, Password]) of
{atomic, ok} ->
ok;
{atomic, exists} ->
io:format("User ~p already registered on node ~p~n",
[User, Node]);
{error, Reason} ->
io:format("Can't register user ~p on node ~p: ~p~n",
[User, Node, Reason]);
{badrpc, Reason} ->
io:format("Can't register user ~p on node ~p: ~p~n",
[User, Node, Reason])
end;
process(Node, ["unregister", User]) ->
case rpc:call(Node, ejabberd_auth, remove_user, [User]) of
{atomic, ok} ->
ok;
{error, Reason} ->
io:format("Can't unregister user ~p on node ~p: ~p~n",
[User, Node, Reason]);
{badrpc, Reason} ->
io:format("Can't unregister user ~p on node ~p: ~p~n",
[User, Node, Reason])
end;
process(Node, ["backup", Path]) ->
case rpc:call(Node, mnesia, backup, [Path]) of
ok ->
ok;
{error, Reason} ->
io:format("Can't store backup in ~p on node ~p: ~p~n",
[Path, Node, Reason]);
{badrpc, Reason} ->
io:format("Can't store backup in ~p on node ~p: ~p~n",
[Path, Node, Reason])
end;
process(Node, ["dump", Path]) ->
case rpc:call(Node, mnesia, dump_to_textfile, [Path]) of
ok ->
ok;
{error, Reason} ->
io:format("Can't store dump in ~p on node ~p: ~p~n",
[Path, Node, Reason]);
{badrpc, Reason} ->
io:format("Can't store dump in ~p on node ~p: ~p~n",
[Path, Node, Reason])
end;
process(Node, ["load", Path]) ->
case rpc:call(Node, mnesia, load_textfile, [Path]) of
ok ->
ok;
{error, Reason} ->
io:format("Can't load dump in ~p on node ~p: ~p~n",
[Path, Node, Reason]);
{badrpc, Reason} ->
io:format("Can't load dump in ~p on node ~p: ~p~n",
[Path, Node, Reason])
end;
process(Node, ["restore", Path]) ->
case rpc:call(Node,
mnesia, restore, [Path, [{default_op, keep_tables}]]) of
{atomic, ok} ->
ok;
{error, Reason} ->
io:format("Can't restore backup from ~p on node ~p: ~p~n",
[Path, Node, Reason]);
{badrpc, Reason} ->
io:format("Can't restore backup from ~p on node ~p: ~p~n",
[Path, Node, Reason])
end;
process(Node, ["install-fallback", Path]) ->
case rpc:call(Node, mnesia, install_fallback, [Path]) of
{atomic, ok} ->
ok;
{error, Reason} ->
io:format("Can't install fallback from ~p on node ~p: ~p~n",
[Path, Node, Reason]);
{badrpc, Reason} ->
io:format("Can't install fallback from ~p on node ~p: ~p~n",
[Path, Node, Reason])
end;
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),
FUsers = lists:map(fun(U) -> [U, NewLine] end, SUsers),
io:format("~s", [FUsers]),
ok;
{ErrorTag, Reason} when (ErrorTag == error) or (ErrorTag == badrpc) ->
io:format("Can't get list of registered users on node ~p: ~p~n",
[Node, Reason])
end;
process(_Node, _Args) ->
print_usage().
print_usage() ->
io:format(
"Usage: ejabberdctl node command~n"
"~n"
"Available commands:~n"
" stop\t\t\t\tstop ejabberd~n"
" restart\t\t\trestart ejabberd~n"
" reopen-log\t\t\treopen log file~n"
" register user password\tregister a user~n"
" unregister user\t\tunregister a user~n"
" 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"
"~n"
"Example:~n"
" ejabberdctl ejabberd@host restart~n"
).
|