2003-01-11 21:25:11 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_stats.erl
|
2007-12-24 13:58:05 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%% Purpose : Basic statistics.
|
|
|
|
%%% Created : 11 Jan 2003 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2018-01-05 21:18:58 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2018 ProcessOne
|
2007-12-24 13:58:05 +01:00
|
|
|
%%%
|
|
|
|
%%% 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.
|
2009-01-12 15:44:42 +01:00
|
|
|
%%%
|
2014-02-22 11:27:40 +01:00
|
|
|
%%% 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.
|
2007-12-24 13:58:05 +01:00
|
|
|
%%%
|
2003-01-11 21:25:11 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_stats).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2007-12-24 13:58:05 +01:00
|
|
|
-author('alexey@process-one.net').
|
2003-01-11 21:25:11 +01:00
|
|
|
|
2015-05-21 17:02:36 +02:00
|
|
|
-protocol({xep, 39, '0.6.0'}).
|
|
|
|
|
2003-01-24 21:18:33 +01:00
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
2018-01-23 08:54:52 +01:00
|
|
|
-export([start/2, stop/1, reload/3, process_iq/1, mod_opt_type/1,
|
|
|
|
mod_options/1, depends/2]).
|
2003-01-11 21:25:11 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2016-07-31 13:51:16 +02:00
|
|
|
-include("xmpp.hrl").
|
2003-01-11 21:25:11 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
start(Host, Opts) ->
|
2018-01-23 08:54:52 +01:00
|
|
|
IQDisc = gen_mod:get_opt(iqdisc, Opts),
|
2016-07-31 13:51:16 +02:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_STATS,
|
|
|
|
?MODULE, process_iq, IQDisc).
|
2003-01-11 21:25:11 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
stop(Host) ->
|
2016-07-31 13:51:16 +02:00
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_STATS).
|
2003-01-11 21:25:11 +01:00
|
|
|
|
2017-02-22 17:46:47 +01:00
|
|
|
reload(Host, NewOpts, _OldOpts) ->
|
|
|
|
start(Host, NewOpts).
|
|
|
|
|
2016-07-06 13:58:48 +02:00
|
|
|
depends(_Host, _Opts) ->
|
|
|
|
[].
|
|
|
|
|
2016-07-31 13:51:16 +02:00
|
|
|
process_iq(#iq{type = set, lang = Lang} = IQ) ->
|
|
|
|
Txt = <<"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)
|
2003-01-11 21:25:11 +01:00
|
|
|
end.
|
|
|
|
|
2016-07-31 13:51:16 +02:00
|
|
|
-define(STAT(Name), #stat{name = Name}).
|
2003-01-18 20:42:48 +01:00
|
|
|
|
2016-03-31 10:00:29 +02:00
|
|
|
get_local_stats(_Server, [], [], _Lang) ->
|
2003-01-11 21:25:11 +01:00
|
|
|
{result,
|
2013-03-14 10:33:02 +01:00
|
|
|
[?STAT(<<"users/online">>), ?STAT(<<"users/total">>),
|
|
|
|
?STAT(<<"users/all-hosts/online">>),
|
|
|
|
?STAT(<<"users/all-hosts/total">>)]};
|
2016-03-31 10:00:29 +02:00
|
|
|
get_local_stats(Server, [], Names, _Lang) ->
|
2003-01-11 21:25:11 +01:00
|
|
|
{result,
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:map(fun (Name) -> get_local_stat(Server, [], Name)
|
|
|
|
end,
|
|
|
|
Names)};
|
|
|
|
get_local_stats(_Server, [<<"running nodes">>, _],
|
2016-03-31 10:00:29 +02:00
|
|
|
[], _Lang) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
{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],
|
2016-03-31 10:00:29 +02:00
|
|
|
Names, Lang) ->
|
2003-01-18 20:42:48 +01:00
|
|
|
case search_running_node(ENode) of
|
2016-03-31 10:00:29 +02:00
|
|
|
false ->
|
|
|
|
Txt = <<"No running node found">>,
|
2016-07-31 13:51:16 +02:00
|
|
|
{error, xmpp:err_item_not_found(Txt, Lang)};
|
2013-03-14 10:33:02 +01:00
|
|
|
Node ->
|
|
|
|
{result,
|
|
|
|
lists:map(fun (Name) -> get_node_stat(Node, Name) end,
|
|
|
|
Names)}
|
2003-01-18 20:42:48 +01:00
|
|
|
end;
|
2016-03-31 10:00:29 +02:00
|
|
|
get_local_stats(_Server, _, _, Lang) ->
|
|
|
|
Txt = <<"No statistics found for this item">>,
|
2016-07-31 13:51:16 +02:00
|
|
|
{error, xmpp:err_feature_not_implemented(Txt, Lang)}.
|
2003-01-18 20:42:48 +01:00
|
|
|
|
2016-07-31 13:51:16 +02:00
|
|
|
-define(STATVAL(Val, Unit), #stat{name = Name, units = Unit, value = Val}).
|
2003-01-11 21:25:11 +01:00
|
|
|
|
|
|
|
-define(STATERR(Code, Desc),
|
2016-07-31 13:51:16 +02:00
|
|
|
#stat{name = Name,
|
|
|
|
error = #stat_error{code = Code, reason = Desc}}).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
get_local_stat(Server, [], Name)
|
|
|
|
when Name == <<"users/online">> ->
|
2005-04-17 20:08:34 +02:00
|
|
|
case catch ejabberd_sm:get_vh_session_list(Server) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{'EXIT', _Reason} ->
|
2016-07-31 13:51:16 +02:00
|
|
|
?STATERR(500, <<"Internal Server Error">>);
|
2013-03-14 10:33:02 +01:00
|
|
|
Users ->
|
2016-11-24 13:06:06 +01:00
|
|
|
?STATVAL((integer_to_binary(length(Users))),
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"users">>)
|
2005-04-17 20:08:34 +02:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
get_local_stat(Server, [], Name)
|
|
|
|
when Name == <<"users/total">> ->
|
|
|
|
case catch
|
2017-05-11 13:37:21 +02:00
|
|
|
ejabberd_auth:count_users(Server)
|
2013-03-14 10:33:02 +01:00
|
|
|
of
|
|
|
|
{'EXIT', _Reason} ->
|
2016-07-31 13:51:16 +02:00
|
|
|
?STATERR(500, <<"Internal Server Error">>);
|
2013-03-14 10:33:02 +01:00
|
|
|
NUsers ->
|
2016-11-24 13:06:06 +01:00
|
|
|
?STATVAL((integer_to_binary(NUsers)),
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"users">>)
|
2005-04-17 20:08:34 +02:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
get_local_stat(_Server, [], Name)
|
|
|
|
when Name == <<"users/all-hosts/online">> ->
|
2016-10-11 22:20:22 +02:00
|
|
|
Users = ejabberd_sm:connected_users_number(),
|
2016-11-24 13:06:06 +01:00
|
|
|
?STATVAL((integer_to_binary(Users)), <<"users">>);
|
2013-03-14 10:33:02 +01:00
|
|
|
get_local_stat(_Server, [], Name)
|
|
|
|
when Name == <<"users/all-hosts/total">> ->
|
|
|
|
NumUsers = lists:foldl(fun (Host, Total) ->
|
2017-05-11 13:37:21 +02:00
|
|
|
ejabberd_auth:count_users(Host)
|
2013-03-14 10:33:02 +01:00
|
|
|
+ Total
|
|
|
|
end,
|
|
|
|
0, ?MYHOSTS),
|
2016-11-24 13:06:06 +01:00
|
|
|
?STATVAL((integer_to_binary(NumUsers)),
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"users">>);
|
2005-04-17 20:08:34 +02:00
|
|
|
get_local_stat(_Server, _, Name) ->
|
2016-07-31 13:51:16 +02:00
|
|
|
?STATERR(404, <<"Not Found">>).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
get_node_stat(Node, Name)
|
|
|
|
when Name == <<"time/uptime">> ->
|
2015-10-07 00:06:58 +02:00
|
|
|
case catch ejabberd_cluster:call(Node, erlang, statistics,
|
2013-03-14 10:33:02 +01:00
|
|
|
[wall_clock])
|
|
|
|
of
|
|
|
|
{badrpc, _Reason} ->
|
2016-07-31 13:51:16 +02:00
|
|
|
?STATERR(500, <<"Internal Server Error">>);
|
2013-03-14 10:33:02 +01:00
|
|
|
CPUTime ->
|
2016-11-24 13:06:06 +01:00
|
|
|
?STATVAL(str:format("~.3f", [element(1, CPUTime) / 1000]),
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"seconds">>)
|
2003-01-18 20:42:48 +01:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
get_node_stat(Node, Name)
|
|
|
|
when Name == <<"time/cputime">> ->
|
2015-10-07 00:06:58 +02:00
|
|
|
case catch ejabberd_cluster:call(Node, erlang, statistics, [runtime])
|
2013-03-14 10:33:02 +01:00
|
|
|
of
|
|
|
|
{badrpc, _Reason} ->
|
2016-07-31 13:51:16 +02:00
|
|
|
?STATERR(500, <<"Internal Server Error">>);
|
2013-03-14 10:33:02 +01:00
|
|
|
RunTime ->
|
2016-11-24 13:06:06 +01:00
|
|
|
?STATVAL(str:format("~.3f", [element(1, RunTime) / 1000]),
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"seconds">>)
|
2003-01-18 20:42:48 +01:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
get_node_stat(Node, Name)
|
|
|
|
when Name == <<"users/online">> ->
|
2015-10-07 00:06:58 +02:00
|
|
|
case catch ejabberd_cluster:call(Node, ejabberd_sm,
|
2013-03-14 10:33:02 +01:00
|
|
|
dirty_get_my_sessions_list, [])
|
|
|
|
of
|
|
|
|
{badrpc, _Reason} ->
|
2016-07-31 13:51:16 +02:00
|
|
|
?STATERR(500, <<"Internal Server Error">>);
|
2013-03-14 10:33:02 +01:00
|
|
|
Users ->
|
2016-11-24 13:06:06 +01:00
|
|
|
?STATVAL((integer_to_binary(length(Users))),
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"users">>)
|
2003-01-18 20:42:48 +01:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
get_node_stat(Node, Name)
|
|
|
|
when Name == <<"transactions/committed">> ->
|
2015-10-07 00:06:58 +02:00
|
|
|
case catch ejabberd_cluster:call(Node, mnesia, system_info,
|
2013-03-14 10:33:02 +01:00
|
|
|
[transaction_commits])
|
|
|
|
of
|
|
|
|
{badrpc, _Reason} ->
|
2016-07-31 13:51:16 +02:00
|
|
|
?STATERR(500, <<"Internal Server Error">>);
|
2013-03-14 10:33:02 +01:00
|
|
|
Transactions ->
|
2016-11-24 13:06:06 +01:00
|
|
|
?STATVAL((integer_to_binary(Transactions)),
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"transactions">>)
|
2003-01-29 18:12:23 +01:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
get_node_stat(Node, Name)
|
|
|
|
when Name == <<"transactions/aborted">> ->
|
2015-10-07 00:06:58 +02:00
|
|
|
case catch ejabberd_cluster:call(Node, mnesia, system_info,
|
2013-03-14 10:33:02 +01:00
|
|
|
[transaction_failures])
|
|
|
|
of
|
|
|
|
{badrpc, _Reason} ->
|
2016-07-31 13:51:16 +02:00
|
|
|
?STATERR(500, <<"Internal Server Error">>);
|
2013-03-14 10:33:02 +01:00
|
|
|
Transactions ->
|
2016-11-24 13:06:06 +01:00
|
|
|
?STATVAL((integer_to_binary(Transactions)),
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"transactions">>)
|
2003-01-29 18:12:23 +01:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
get_node_stat(Node, Name)
|
|
|
|
when Name == <<"transactions/restarted">> ->
|
2015-10-07 00:06:58 +02:00
|
|
|
case catch ejabberd_cluster:call(Node, mnesia, system_info,
|
2013-03-14 10:33:02 +01:00
|
|
|
[transaction_restarts])
|
|
|
|
of
|
|
|
|
{badrpc, _Reason} ->
|
2016-07-31 13:51:16 +02:00
|
|
|
?STATERR(500, <<"Internal Server Error">>);
|
2013-03-14 10:33:02 +01:00
|
|
|
Transactions ->
|
2016-11-24 13:06:06 +01:00
|
|
|
?STATVAL((integer_to_binary(Transactions)),
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"transactions">>)
|
2003-01-29 18:12:23 +01:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
get_node_stat(Node, Name)
|
|
|
|
when Name == <<"transactions/logged">> ->
|
2015-10-07 00:06:58 +02:00
|
|
|
case catch ejabberd_cluster:call(Node, mnesia, system_info,
|
2013-03-14 10:33:02 +01:00
|
|
|
[transaction_log_writes])
|
|
|
|
of
|
|
|
|
{badrpc, _Reason} ->
|
2016-07-31 13:51:16 +02:00
|
|
|
?STATERR(500, <<"Internal Server Error">>);
|
2013-03-14 10:33:02 +01:00
|
|
|
Transactions ->
|
2016-11-24 13:06:06 +01:00
|
|
|
?STATVAL((integer_to_binary(Transactions)),
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"transactions">>)
|
2003-01-29 18:12:23 +01:00
|
|
|
end;
|
2003-01-18 20:42:48 +01:00
|
|
|
get_node_stat(_, Name) ->
|
2016-07-31 13:51:16 +02:00
|
|
|
?STATERR(404, <<"Not Found">>).
|
2003-01-18 20:42:48 +01:00
|
|
|
|
|
|
|
search_running_node(SNode) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
search_running_node(SNode,
|
|
|
|
mnesia:system_info(running_db_nodes)).
|
2003-01-18 20:42:48 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
search_running_node(_, []) -> false;
|
2003-01-18 20:42:48 +01:00
|
|
|
search_running_node(SNode, [Node | Nodes]) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case iolist_to_binary(atom_to_list(Node)) of
|
|
|
|
SNode -> Node;
|
|
|
|
_ -> search_running_node(SNode, Nodes)
|
2003-01-18 20:42:48 +01:00
|
|
|
end.
|
2015-06-01 14:38:27 +02:00
|
|
|
|
2018-01-23 08:54:52 +01:00
|
|
|
mod_opt_type(iqdisc) -> fun gen_iq_handler:check_type/1.
|
|
|
|
|
|
|
|
mod_options(Host) ->
|
|
|
|
[{iqdisc, gen_iq_handler:iqdisc(Host)}].
|