25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-04 15:36:57 +01:00
xmpp.chapril.org-ejabberd/src/mod_stats.erl

237 lines
7.4 KiB
Erlang
Raw Normal View History

%%%----------------------------------------------------------------------
%%% 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>
%%%
%%%
2019-01-08 22:53:27 +01:00
%%% ejabberd, Copyright (C) 2002-2019 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.
%%%
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.
%%%
%%%----------------------------------------------------------------------
-module(mod_stats).
-author('alexey@process-one.net').
2015-05-21 17:02:36 +02:00
-protocol({xep, 39, '0.6.0'}).
-behaviour(gen_mod).
-export([start/2, stop/1, reload/3, process_iq/1,
mod_options/1, depends/2]).
-include("logger.hrl").
2016-07-31 13:51:16 +02:00
-include("xmpp.hrl").
start(Host, _Opts) ->
2016-07-31 13:51:16 +02:00
gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_STATS,
?MODULE, process_iq).
stop(Host) ->
2016-07-31 13:51:16 +02:00
gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_STATS).
reload(Host, NewOpts, _OldOpts) ->
start(Host, NewOpts).
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)
end.
2016-07-31 13:51:16 +02:00
-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 = <<"No running node found">>,
2016-07-31 13:51:16 +02:00
{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 = <<"No statistics found for this item">>,
2016-07-31 13:51:16 +02:00
{error, xmpp:err_feature_not_implemented(Txt, Lang)}.
2016-07-31 13:51:16 +02:00
-define(STATVAL(Val, Unit), #stat{name = Name, units = Unit, value = Val}).
-define(STATERR(Code, Desc),
2016-07-31 13:51:16 +02:00
#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} ->
2016-07-31 13:51:16 +02:00
?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
Use cache for authentication backends The commit introduces the following API incompatibilities: In ejabberd_auth.erl: * dirty_get_registered_users/0 is renamed to get_users/0 * get_vh_registered_users/1 is renamed to get_users/1 * get_vh_registered_users/2 is renamed to get_users/2 * get_vh_registered_users_number/1 is renamed to count_users/1 * get_vh_registered_users_number/2 is renamed to count_users/2 In ejabberd_auth callbacks * plain_password_required/0 is replaced by plain_password_required/1 where the argument is a virtual host * store_type/0 is replaced by store_type/1 where the argument is a virtual host * set_password/3 is now an optional callback * remove_user/3 callback is no longer needed * remove_user/2 now should return `ok | {error, atom()}` * is_user_exists/2 now must only be implemented for backends with `external` store type * check_password/6 is no longer needed * check_password/4 now must only be implemented for backends with `external` store type * try_register/3 is now an optional callback and should return `ok | {error, atom()}` * dirty_get_registered_users/0 is no longer needed * get_vh_registered_users/1 is no longer needed * get_vh_registered_users/2 is renamed to get_users/2 * get_vh_registered_users_number/1 is no longer needed * get_vh_registered_users_number/2 is renamed to count_users/2 * get_password_s/2 is no longer needed * get_password/2 now must only be implemented for backends with `plain` or `scram` store type Additionally, the commit introduces two new callbacks: * use_cache/1 where the argument is a virtual host * cache_nodes/1 where the argument is a virtual host New options are also introduced: `auth_use_cache`, `auth_cache_missed`, `auth_cache_life_time` and `auth_cache_size`.
2017-05-11 13:37:21 +02:00
ejabberd_auth:count_users(Server)
of
{'EXIT', _Reason} ->
2016-07-31 13:51:16 +02:00
?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) ->
Use cache for authentication backends The commit introduces the following API incompatibilities: In ejabberd_auth.erl: * dirty_get_registered_users/0 is renamed to get_users/0 * get_vh_registered_users/1 is renamed to get_users/1 * get_vh_registered_users/2 is renamed to get_users/2 * get_vh_registered_users_number/1 is renamed to count_users/1 * get_vh_registered_users_number/2 is renamed to count_users/2 In ejabberd_auth callbacks * plain_password_required/0 is replaced by plain_password_required/1 where the argument is a virtual host * store_type/0 is replaced by store_type/1 where the argument is a virtual host * set_password/3 is now an optional callback * remove_user/3 callback is no longer needed * remove_user/2 now should return `ok | {error, atom()}` * is_user_exists/2 now must only be implemented for backends with `external` store type * check_password/6 is no longer needed * check_password/4 now must only be implemented for backends with `external` store type * try_register/3 is now an optional callback and should return `ok | {error, atom()}` * dirty_get_registered_users/0 is no longer needed * get_vh_registered_users/1 is no longer needed * get_vh_registered_users/2 is renamed to get_users/2 * get_vh_registered_users_number/1 is no longer needed * get_vh_registered_users_number/2 is renamed to count_users/2 * get_password_s/2 is no longer needed * get_password/2 now must only be implemented for backends with `plain` or `scram` store type Additionally, the commit introduces two new callbacks: * use_cache/1 where the argument is a virtual host * cache_nodes/1 where the argument is a virtual host New options are also introduced: `auth_use_cache`, `auth_cache_missed`, `auth_cache_life_time` and `auth_cache_size`.
2017-05-11 13:37:21 +02:00
ejabberd_auth:count_users(Host)
+ Total
end,
0, ejabberd_config:get_myhosts()),
?STATVAL((integer_to_binary(NumUsers)),
<<"users">>);
get_local_stat(_Server, _, Name) ->
2016-07-31 13:51:16 +02:00
?STATERR(404, <<"Not Found">>).
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,
[wall_clock])
of
{badrpc, _Reason} ->
2016-07-31 13:51:16 +02:00
?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">> ->
2015-10-07 00:06:58 +02:00
case catch ejabberd_cluster:call(Node, erlang, statistics, [runtime])
of
{badrpc, _Reason} ->
2016-07-31 13:51:16 +02:00
?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">> ->
2015-10-07 00:06:58 +02:00
case catch ejabberd_cluster:call(Node, ejabberd_sm,
dirty_get_my_sessions_list, [])
of
{badrpc, _Reason} ->
2016-07-31 13:51:16 +02:00
?STATERR(500, <<"Internal Server Error">>);
Users ->
?STATVAL((integer_to_binary(length(Users))),
<<"users">>)
end;
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,
[transaction_commits])
of
{badrpc, _Reason} ->
2016-07-31 13:51:16 +02:00
?STATERR(500, <<"Internal Server Error">>);
Transactions ->
?STATVAL((integer_to_binary(Transactions)),
<<"transactions">>)
end;
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,
[transaction_failures])
of
{badrpc, _Reason} ->
2016-07-31 13:51:16 +02:00
?STATERR(500, <<"Internal Server Error">>);
Transactions ->
?STATVAL((integer_to_binary(Transactions)),
<<"transactions">>)
end;
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,
[transaction_restarts])
of
{badrpc, _Reason} ->
2016-07-31 13:51:16 +02:00
?STATERR(500, <<"Internal Server Error">>);
Transactions ->
?STATVAL((integer_to_binary(Transactions)),
<<"transactions">>)
end;
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,
[transaction_log_writes])
of
{badrpc, _Reason} ->
2016-07-31 13:51:16 +02:00
?STATERR(500, <<"Internal Server Error">>);
Transactions ->
?STATVAL((integer_to_binary(Transactions)),
<<"transactions">>)
end;
get_node_stat(_, Name) ->
2016-07-31 13:51:16 +02:00
?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.
2015-06-01 14:38:27 +02:00
mod_options(_Host) ->
[].