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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
%%%----------------------------------------------------------------------
%%% File : mod_stats.erl
%%% Author : Alexey Shchepin <alexey@process-one.net>
%%% Purpose : Basic statistics.
%%% Created : 11 Jan 2003 by Alexey Shchepin <alexey@process-one.net>
%%%
%%%
%%% ejabberd, Copyright (C) 2002-2021 ProcessOne
%%%
%%% This program is free software; you can redistribute it and/or
%%% modify it under the terms of the GNU General Public License as
%%% published by the Free Software Foundation; either version 2 of the
%%% License, or (at your option) any later version.
%%%
%%% This program is distributed in the hope that it will be useful,
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
%%% General Public License for more details.
%%%
%%% You should have received a copy of the GNU General Public License along
%%% with this program; if not, write to the Free Software Foundation, Inc.,
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
%%%
%%%----------------------------------------------------------------------
-module(mod_stats).
-author('alexey@process-one.net').
-protocol({xep, 39, '0.6.0'}).
-behaviour(gen_mod).
-export([start/2, stop/1, reload/3, process_iq/1,
mod_options/1, depends/2, mod_doc/0]).
-include("logger.hrl").
-include_lib("xmpp/include/xmpp.hrl").
-include("translate.hrl").
start(Host, _Opts) ->
gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_STATS,
?MODULE, process_iq).
stop(Host) ->
gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_STATS).
reload(Host, NewOpts, _OldOpts) ->
start(Host, NewOpts).
depends(_Host, _Opts) ->
[].
process_iq(#iq{type = set, lang = Lang} = IQ) ->
Txt = ?T("Value 'set' of 'type' attribute is not allowed"),
xmpp:make_error(IQ, xmpp:err_not_allowed(Txt, Lang));
process_iq(#iq{type = get, to = To, lang = Lang,
sub_els = [#stats{} = Stats]} = IQ) ->
Node = str:tokens(Stats#stats.node, <<"/">>),
Names = [Name || #stat{name = Name} <- Stats#stats.list],
case get_local_stats(To#jid.server, Node, Names, Lang) of
{result, List} ->
xmpp:make_iq_result(IQ, Stats#stats{list = List});
{error, Error} ->
xmpp:make_error(IQ, Error)
end.
-define(STAT(Name), #stat{name = Name}).
get_local_stats(_Server, [], [], _Lang) ->
{result,
[?STAT(<<"users/online">>), ?STAT(<<"users/total">>),
?STAT(<<"users/all-hosts/online">>),
?STAT(<<"users/all-hosts/total">>)]};
get_local_stats(Server, [], Names, _Lang) ->
{result,
lists:map(fun (Name) -> get_local_stat(Server, [], Name)
end,
Names)};
get_local_stats(_Server, [<<"running nodes">>, _],
[], _Lang) ->
{result,
[?STAT(<<"time/uptime">>), ?STAT(<<"time/cputime">>),
?STAT(<<"users/online">>),
?STAT(<<"transactions/committed">>),
?STAT(<<"transactions/aborted">>),
?STAT(<<"transactions/restarted">>),
?STAT(<<"transactions/logged">>)]};
get_local_stats(_Server, [<<"running nodes">>, ENode],
Names, Lang) ->
case search_running_node(ENode) of
false ->
Txt = ?T("No running node found"),
{error, xmpp:err_item_not_found(Txt, Lang)};
Node ->
{result,
lists:map(fun (Name) -> get_node_stat(Node, Name) end,
Names)}
end;
get_local_stats(_Server, _, _, Lang) ->
Txt = ?T("No statistics found for this item"),
{error, xmpp:err_feature_not_implemented(Txt, Lang)}.
-define(STATVAL(Val, Unit), #stat{name = Name, units = Unit, value = Val}).
-define(STATERR(Code, Desc),
#stat{name = Name,
error = #stat_error{code = Code, reason = Desc}}).
get_local_stat(Server, [], Name)
when Name == <<"users/online">> ->
case catch ejabberd_sm:get_vh_session_list(Server) of
{'EXIT', _Reason} ->
?STATERR(500, <<"Internal Server Error">>);
Users ->
?STATVAL((integer_to_binary(length(Users))),
<<"users">>)
end;
get_local_stat(Server, [], Name)
when Name == <<"users/total">> ->
case catch
ejabberd_auth:count_users(Server)
of
{'EXIT', _Reason} ->
?STATERR(500, <<"Internal Server Error">>);
NUsers ->
?STATVAL((integer_to_binary(NUsers)),
<<"users">>)
end;
get_local_stat(_Server, [], Name)
when Name == <<"users/all-hosts/online">> ->
Users = ejabberd_sm:connected_users_number(),
?STATVAL((integer_to_binary(Users)), <<"users">>);
get_local_stat(_Server, [], Name)
when Name == <<"users/all-hosts/total">> ->
NumUsers = lists:foldl(fun (Host, Total) ->
ejabberd_auth:count_users(Host)
+ Total
end,
0, ejabberd_option:hosts()),
?STATVAL((integer_to_binary(NumUsers)),
<<"users">>);
get_local_stat(_Server, _, Name) ->
?STATERR(404, <<"Not Found">>).
get_node_stat(Node, Name)
when Name == <<"time/uptime">> ->
case catch ejabberd_cluster:call(Node, erlang, statistics,
[wall_clock])
of
{badrpc, _Reason} ->
?STATERR(500, <<"Internal Server Error">>);
CPUTime ->
?STATVAL(str:format("~.3f", [element(1, CPUTime) / 1000]),
<<"seconds">>)
end;
get_node_stat(Node, Name)
when Name == <<"time/cputime">> ->
case catch ejabberd_cluster:call(Node, erlang, statistics, [runtime])
of
{badrpc, _Reason} ->
?STATERR(500, <<"Internal Server Error">>);
RunTime ->
?STATVAL(str:format("~.3f", [element(1, RunTime) / 1000]),
<<"seconds">>)
end;
get_node_stat(Node, Name)
when Name == <<"users/online">> ->
case catch ejabberd_cluster:call(Node, ejabberd_sm,
dirty_get_my_sessions_list, [])
of
{badrpc, _Reason} ->
?STATERR(500, <<"Internal Server Error">>);
Users ->
?STATVAL((integer_to_binary(length(Users))),
<<"users">>)
end;
get_node_stat(Node, Name)
when Name == <<"transactions/committed">> ->
case catch ejabberd_cluster:call(Node, mnesia, system_info,
[transaction_commits])
of
{badrpc, _Reason} ->
?STATERR(500, <<"Internal Server Error">>);
Transactions ->
?STATVAL((integer_to_binary(Transactions)),
<<"transactions">>)
end;
get_node_stat(Node, Name)
when Name == <<"transactions/aborted">> ->
case catch ejabberd_cluster:call(Node, mnesia, system_info,
[transaction_failures])
of
{badrpc, _Reason} ->
?STATERR(500, <<"Internal Server Error">>);
Transactions ->
?STATVAL((integer_to_binary(Transactions)),
<<"transactions">>)
end;
get_node_stat(Node, Name)
when Name == <<"transactions/restarted">> ->
case catch ejabberd_cluster:call(Node, mnesia, system_info,
[transaction_restarts])
of
{badrpc, _Reason} ->
?STATERR(500, <<"Internal Server Error">>);
Transactions ->
?STATVAL((integer_to_binary(Transactions)),
<<"transactions">>)
end;
get_node_stat(Node, Name)
when Name == <<"transactions/logged">> ->
case catch ejabberd_cluster:call(Node, mnesia, system_info,
[transaction_log_writes])
of
{badrpc, _Reason} ->
?STATERR(500, <<"Internal Server Error">>);
Transactions ->
?STATVAL((integer_to_binary(Transactions)),
<<"transactions">>)
end;
get_node_stat(_, Name) ->
?STATERR(404, <<"Not Found">>).
search_running_node(SNode) ->
search_running_node(SNode,
mnesia:system_info(running_db_nodes)).
search_running_node(_, []) -> false;
search_running_node(SNode, [Node | Nodes]) ->
case iolist_to_binary(atom_to_list(Node)) of
SNode -> Node;
_ -> search_running_node(SNode, Nodes)
end.
mod_options(_Host) ->
[].
mod_doc() ->
#{desc =>
[?T("This module adds support for "
"https://xmpp.org/extensions/xep-0039.html"
"[XEP-0039: Statistics Gathering]. This protocol "
"allows you to retrieve the following statistics "
"from your ejabberd server:"), "",
?T("- Total number of registered users on the current "
"virtual host (users/total)."), "",
?T("- Total number of registered users on all virtual "
"hosts (users/all-hosts/total)."), "",
?T("- Total number of online users on the current "
"virtual host (users/online)."), "",
?T("- Total number of online users on all virtual "
"hosts (users/all-hosts/online)."), "",
?T("NOTE: The protocol extension is deferred and seems "
"like even a few clients that were supporting it "
"are now abandoned. So using this module makes "
"very little sense.")]}.
|