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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2014-03-13 12:29:21 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2014 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
|
|
|
|
2003-01-24 21:18:33 +01:00
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([start/2, stop/1, process_local_iq/3]).
|
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").
|
2003-03-09 21:46:47 +01:00
|
|
|
-include("jlib.hrl").
|
2003-01-11 21:25:11 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
start(Host, Opts) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
IQDisc = gen_mod:get_opt(iqdisc, Opts, fun gen_iq_handler:check_type/1,
|
|
|
|
one_queue),
|
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, Host,
|
|
|
|
?NS_STATS, ?MODULE, process_local_iq, IQDisc).
|
2003-01-11 21:25:11 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
stop(Host) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, Host,
|
|
|
|
?NS_STATS).
|
2003-01-11 21:25:11 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
process_local_iq(_From, To,
|
|
|
|
#iq{id = _ID, type = Type, xmlns = XMLNS,
|
|
|
|
sub_el = SubEl} =
|
|
|
|
IQ) ->
|
2003-01-11 21:25:11 +01:00
|
|
|
case Type of
|
2013-03-14 10:33:02 +01:00
|
|
|
set ->
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERR_NOT_ALLOWED]};
|
|
|
|
get ->
|
|
|
|
#xmlel{children = Els} = SubEl,
|
|
|
|
Node = str:tokens(xml:get_tag_attr_s(<<"node">>, SubEl),
|
|
|
|
<<"/">>),
|
|
|
|
Names = get_names(Els, []),
|
|
|
|
case get_local_stats(To#jid.server, Node, Names) of
|
|
|
|
{result, Res} ->
|
|
|
|
IQ#iq{type = result,
|
|
|
|
sub_el =
|
|
|
|
[#xmlel{name = <<"query">>,
|
|
|
|
attrs = [{<<"xmlns">>, XMLNS}],
|
|
|
|
children = Res}]};
|
|
|
|
{error, Error} ->
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, Error]}
|
|
|
|
end
|
2003-01-11 21:25:11 +01:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
get_names([], Res) -> Res;
|
|
|
|
get_names([#xmlel{name = <<"stat">>, attrs = Attrs}
|
|
|
|
| Els],
|
|
|
|
Res) ->
|
|
|
|
Name = xml:get_attr_s(<<"name">>, Attrs),
|
2003-01-11 21:25:11 +01:00
|
|
|
case Name of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"">> -> get_names(Els, Res);
|
|
|
|
_ -> get_names(Els, [Name | Res])
|
2003-01-11 21:25:11 +01:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
get_names([_ | Els], Res) -> get_names(Els, Res).
|
2003-01-11 21:25:11 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-define(STAT(Name),
|
|
|
|
#xmlel{name = <<"stat">>, attrs = [{<<"name">>, Name}],
|
|
|
|
children = []}).
|
2003-01-18 20:42:48 +01:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
get_local_stats(_Server, [], []) ->
|
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">>)]};
|
2005-04-17 20:08:34 +02:00
|
|
|
get_local_stats(Server, [], Names) ->
|
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">>, _],
|
|
|
|
[]) ->
|
|
|
|
{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) ->
|
2003-01-18 20:42:48 +01:00
|
|
|
case search_running_node(ENode) of
|
2013-03-14 10:33:02 +01:00
|
|
|
false -> {error, ?ERR_ITEM_NOT_FOUND};
|
|
|
|
Node ->
|
|
|
|
{result,
|
|
|
|
lists:map(fun (Name) -> get_node_stat(Node, Name) end,
|
|
|
|
Names)}
|
2003-01-18 20:42:48 +01:00
|
|
|
end;
|
2005-04-17 20:08:34 +02:00
|
|
|
get_local_stats(_Server, _, _) ->
|
2003-03-09 21:46:47 +01:00
|
|
|
{error, ?ERR_FEATURE_NOT_IMPLEMENTED}.
|
2003-01-18 20:42:48 +01:00
|
|
|
|
|
|
|
-define(STATVAL(Val, Unit),
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{name = <<"stat">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"name">>, Name}, {<<"units">>, Unit},
|
|
|
|
{<<"value">>, Val}],
|
|
|
|
children = []}).
|
2003-01-11 21:25:11 +01:00
|
|
|
|
|
|
|
-define(STATERR(Code, Desc),
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{name = <<"stat">>, attrs = [{<<"name">>, Name}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"error">>,
|
|
|
|
attrs = [{<<"code">>, Code}],
|
|
|
|
children = [{xmlcdata, Desc}]}]}).
|
|
|
|
|
|
|
|
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} ->
|
|
|
|
?STATERR(<<"500">>, <<"Internal Server Error">>);
|
|
|
|
Users ->
|
|
|
|
?STATVAL((iolist_to_binary(integer_to_list(length(Users)))),
|
|
|
|
<<"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
|
|
|
|
ejabberd_auth:get_vh_registered_users_number(Server)
|
|
|
|
of
|
|
|
|
{'EXIT', _Reason} ->
|
|
|
|
?STATERR(<<"500">>, <<"Internal Server Error">>);
|
|
|
|
NUsers ->
|
|
|
|
?STATVAL((iolist_to_binary(integer_to_list(NUsers))),
|
|
|
|
<<"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">> ->
|
2004-12-12 23:32:15 +01:00
|
|
|
case catch mnesia:table_info(session, size) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{'EXIT', _Reason} ->
|
|
|
|
?STATERR(<<"500">>, <<"Internal Server Error">>);
|
|
|
|
Users ->
|
|
|
|
?STATVAL((iolist_to_binary(integer_to_list(Users))),
|
|
|
|
<<"users">>)
|
2003-01-11 21:25:11 +01:00
|
|
|
end;
|
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) ->
|
|
|
|
ejabberd_auth:get_vh_registered_users_number(Host)
|
|
|
|
+ Total
|
|
|
|
end,
|
|
|
|
0, ?MYHOSTS),
|
|
|
|
?STATVAL((iolist_to_binary(integer_to_list(NumUsers))),
|
|
|
|
<<"users">>);
|
2005-04-17 20:08:34 +02:00
|
|
|
get_local_stat(_Server, _, Name) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
?STATERR(<<"404">>, <<"Not Found">>).
|
|
|
|
|
|
|
|
get_node_stat(Node, Name)
|
|
|
|
when Name == <<"time/uptime">> ->
|
|
|
|
case catch rpc:call(Node, erlang, statistics,
|
|
|
|
[wall_clock])
|
|
|
|
of
|
|
|
|
{badrpc, _Reason} ->
|
|
|
|
?STATERR(<<"500">>, <<"Internal Server Error">>);
|
|
|
|
CPUTime ->
|
|
|
|
?STATVAL(list_to_binary(
|
|
|
|
io_lib:format("~.3f",
|
|
|
|
[element(1, CPUTime) / 1000])),
|
|
|
|
<<"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">> ->
|
|
|
|
case catch rpc:call(Node, erlang, statistics, [runtime])
|
|
|
|
of
|
|
|
|
{badrpc, _Reason} ->
|
|
|
|
?STATERR(<<"500">>, <<"Internal Server Error">>);
|
|
|
|
RunTime ->
|
|
|
|
?STATVAL(list_to_binary(
|
|
|
|
io_lib:format("~.3f",
|
|
|
|
[element(1, RunTime) / 1000])),
|
|
|
|
<<"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">> ->
|
|
|
|
case catch rpc:call(Node, ejabberd_sm,
|
|
|
|
dirty_get_my_sessions_list, [])
|
|
|
|
of
|
|
|
|
{badrpc, _Reason} ->
|
|
|
|
?STATERR(<<"500">>, <<"Internal Server Error">>);
|
|
|
|
Users ->
|
|
|
|
?STATVAL((iolist_to_binary(integer_to_list(length(Users)))),
|
|
|
|
<<"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">> ->
|
|
|
|
case catch rpc:call(Node, mnesia, system_info,
|
|
|
|
[transaction_commits])
|
|
|
|
of
|
|
|
|
{badrpc, _Reason} ->
|
|
|
|
?STATERR(<<"500">>, <<"Internal Server Error">>);
|
|
|
|
Transactions ->
|
|
|
|
?STATVAL((iolist_to_binary(integer_to_list(Transactions))),
|
|
|
|
<<"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">> ->
|
|
|
|
case catch rpc:call(Node, mnesia, system_info,
|
|
|
|
[transaction_failures])
|
|
|
|
of
|
|
|
|
{badrpc, _Reason} ->
|
|
|
|
?STATERR(<<"500">>, <<"Internal Server Error">>);
|
|
|
|
Transactions ->
|
|
|
|
?STATVAL((iolist_to_binary(integer_to_list(Transactions))),
|
|
|
|
<<"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">> ->
|
|
|
|
case catch rpc:call(Node, mnesia, system_info,
|
|
|
|
[transaction_restarts])
|
|
|
|
of
|
|
|
|
{badrpc, _Reason} ->
|
|
|
|
?STATERR(<<"500">>, <<"Internal Server Error">>);
|
|
|
|
Transactions ->
|
|
|
|
?STATVAL((iolist_to_binary(integer_to_list(Transactions))),
|
|
|
|
<<"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">> ->
|
|
|
|
case catch rpc:call(Node, mnesia, system_info,
|
|
|
|
[transaction_log_writes])
|
|
|
|
of
|
|
|
|
{badrpc, _Reason} ->
|
|
|
|
?STATERR(<<"500">>, <<"Internal Server Error">>);
|
|
|
|
Transactions ->
|
|
|
|
?STATVAL((iolist_to_binary(integer_to_list(Transactions))),
|
|
|
|
<<"transactions">>)
|
2003-01-29 18:12:23 +01:00
|
|
|
end;
|
2003-01-18 20:42:48 +01:00
|
|
|
get_node_stat(_, Name) ->
|
2013-03-14 10:33:02 +01: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.
|