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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2011-02-14 13:47:22 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2011 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-19 15:47:33 +01:00
|
|
|
%%%
|
2007-12-24 13:58:05 +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., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
2003-01-11 21:25:11 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_stats).
|
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).
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
-export([start/2,
|
|
|
|
stop/1,
|
2003-01-11 21:25:11 +01:00
|
|
|
process_local_iq/3]).
|
|
|
|
|
2008-10-07 11:55:38 +02:00
|
|
|
-include_lib("exmpp/include/exmpp.hrl").
|
2003-01-11 21:25:11 +01:00
|
|
|
|
2011-07-15 02:50:04 +02:00
|
|
|
start(Host, Opts) when is_list(Host) ->
|
|
|
|
start(list_to_binary(Host), Opts);
|
|
|
|
start(HostB, Opts) ->
|
2003-01-24 21:18:33 +01:00
|
|
|
IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),
|
2011-07-15 02:50:04 +02:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, HostB, ?NS_STATS_s,
|
2003-01-24 21:18:33 +01:00
|
|
|
?MODULE, process_local_iq, IQDisc).
|
2003-01-11 21:25:11 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
stop(Host) ->
|
2009-03-01 21:48:43 +01:00
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, list_to_binary(Host), ?NS_STATS_s).
|
2003-01-29 18:12:23 +01:00
|
|
|
|
2003-01-11 21:25:11 +01:00
|
|
|
|
2008-10-07 11:55:38 +02:00
|
|
|
process_local_iq(_From, To, #iq{type = get,
|
|
|
|
ns = XMLNS, payload = SubEl} = IQ_Rec) ->
|
2010-11-29 20:44:31 +01:00
|
|
|
Node = string:tokens(exmpp_xml:get_attribute_as_list(SubEl, <<"node">>, ""), "/"),
|
2008-10-07 11:55:38 +02:00
|
|
|
Names = get_names(exmpp_xml:get_child_elements(SubEl), []),
|
|
|
|
|
2009-03-01 21:48:43 +01:00
|
|
|
case get_local_stats(exmpp_jid:domain(To), Node, Names) of
|
2008-10-07 11:55:38 +02:00
|
|
|
{result, Res} ->
|
|
|
|
Result = #xmlel{ns = XMLNS, name = 'query', children = Res},
|
|
|
|
exmpp_iq:result(IQ_Rec, Result);
|
|
|
|
{error, Error} ->
|
|
|
|
exmpp_iq:error(IQ_Rec, Error)
|
|
|
|
end;
|
|
|
|
process_local_iq(_From, _To, #iq{type = set} = IQ_Rec) ->
|
|
|
|
exmpp_iq:error(IQ_Rec, 'not-allowed').
|
2003-01-11 21:25:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
get_names([], Res) ->
|
|
|
|
Res;
|
2009-03-01 21:48:43 +01:00
|
|
|
get_names([#xmlel{name = "stat", attrs = Attrs} | Els], Res) ->
|
2010-11-29 20:44:31 +01:00
|
|
|
Name = exmpp_xml:get_attribute_from_list_as_binary(Attrs, <<"name">>, <<>>),
|
2003-01-11 21:25:11 +01:00
|
|
|
case Name of
|
2009-01-08 15:54:00 +01:00
|
|
|
<<>> ->
|
2003-01-11 21:25:11 +01:00
|
|
|
get_names(Els, Res);
|
|
|
|
_ ->
|
|
|
|
get_names(Els, [Name | Res])
|
|
|
|
end;
|
|
|
|
get_names([_ | Els], Res) ->
|
|
|
|
get_names(Els, Res).
|
|
|
|
|
|
|
|
|
2010-11-29 20:44:31 +01:00
|
|
|
-define(STAT(Name), #xmlel{ns = ?NS_STATS_s, name = 'stat', attrs = [?XMLATTR(<<"name">>, Name)]}).
|
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,
|
2009-01-08 15:54:00 +01:00
|
|
|
[?STAT(<<"users/online">>),
|
|
|
|
?STAT(<<"users/total">>),
|
|
|
|
?STAT(<<"users/all-hosts/online">>),
|
|
|
|
?STAT(<<"users/all-hosts/total">>)
|
2003-01-18 20:42:48 +01:00
|
|
|
]};
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
get_local_stats(Server, [], Names) ->
|
|
|
|
{result, lists:map(fun(Name) ->
|
|
|
|
get_local_stat(Server, [], Name)
|
|
|
|
end, Names)};
|
2003-01-18 20:42:48 +01:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
get_local_stats(_Server, ["running nodes", _], []) ->
|
2003-01-11 21:25:11 +01:00
|
|
|
{result,
|
2009-01-08 15:54:00 +01:00
|
|
|
[?STAT(<<"time/uptime">>),
|
|
|
|
?STAT(<<"time/cputime">>),
|
|
|
|
?STAT(<<"users/online">>),
|
2009-01-19 16:27:07 +01:00
|
|
|
?STAT(<<"transactions/committed">>),
|
2009-01-08 15:54:00 +01:00
|
|
|
?STAT(<<"transactions/aborted">>),
|
|
|
|
?STAT(<<"transactions/restarted">>),
|
|
|
|
?STAT(<<"transactions/logged">>)
|
2003-01-18 20:42:48 +01:00
|
|
|
]};
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
get_local_stats(_Server, ["running nodes", ENode], Names) ->
|
2003-01-18 20:42:48 +01:00
|
|
|
case search_running_node(ENode) of
|
|
|
|
false ->
|
2008-10-07 11:55:38 +02:00
|
|
|
{error, 'item-not-found'};
|
2003-01-18 20:42:48 +01:00
|
|
|
Node ->
|
|
|
|
{result,
|
|
|
|
lists:map(fun(Name) -> get_node_stat(Node, Name) end, Names)}
|
|
|
|
end;
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
get_local_stats(_Server, _, _) ->
|
2008-10-07 11:55:38 +02:00
|
|
|
{error, 'feature-not-implemented'}.
|
2003-01-18 20:42:48 +01:00
|
|
|
|
2003-01-11 21:25:11 +01:00
|
|
|
|
|
|
|
|
2003-01-18 20:42:48 +01:00
|
|
|
-define(STATVAL(Val, Unit),
|
2009-03-01 21:48:43 +01:00
|
|
|
#xmlel{ns = ?NS_STATS_s, name = 'stat', attrs =
|
2010-11-29 20:44:31 +01:00
|
|
|
[?XMLATTR(<<"name">>, Name),
|
|
|
|
?XMLATTR(<<"units">>, Unit),
|
|
|
|
?XMLATTR(<<"value">>, Val)
|
2008-10-07 11:55:38 +02:00
|
|
|
]}).
|
2003-01-11 21:25:11 +01:00
|
|
|
|
|
|
|
-define(STATERR(Code, Desc),
|
2009-03-01 21:48:43 +01:00
|
|
|
#xmlel{ns = ?NS_STATS_s, name = 'stat', attrs=
|
2010-11-29 20:44:31 +01:00
|
|
|
[?XMLATTR(<<"name">>, Name)], children =
|
2009-03-01 21:48:43 +01:00
|
|
|
[#xmlel{ns = ?NS_STATS_s, name = 'error', attrs =
|
2010-11-29 20:44:31 +01:00
|
|
|
[?XMLATTR(<<"code">>, Code)], children =
|
2009-01-08 15:54:00 +01:00
|
|
|
[#xmlcdata{cdata = Desc}]}]}).
|
2003-01-11 21:25:11 +01:00
|
|
|
|
|
|
|
|
2009-01-08 15:54:00 +01:00
|
|
|
get_local_stat(Server, [], Name) when Name == <<"users/online">> ->
|
2009-03-01 21:48:43 +01:00
|
|
|
case catch ejabberd_sm:get_vh_session_list(Server) of
|
2007-12-07 01:09:48 +01:00
|
|
|
{'EXIT', _Reason} ->
|
2009-01-08 15:54:00 +01:00
|
|
|
?STATERR(<<"500">>, <<"Internal Server Error">>);
|
2005-04-17 20:08:34 +02:00
|
|
|
Users ->
|
2009-01-08 15:54:00 +01:00
|
|
|
?STATVAL(list_to_binary(integer_to_list(length(Users))), <<"users">>)
|
2005-04-17 20:08:34 +02:00
|
|
|
end;
|
|
|
|
|
2009-01-08 15:54:00 +01:00
|
|
|
get_local_stat(Server, [], Name) when Name == <<"users/total">> ->
|
2007-12-07 01:09:48 +01:00
|
|
|
%%LServer = jlib:nameprep(Server),
|
2009-03-01 21:48:43 +01:00
|
|
|
case catch ejabberd_auth:get_vh_registered_users_number(binary_to_list(Server)) of
|
2007-12-07 01:09:48 +01:00
|
|
|
{'EXIT', _Reason} ->
|
2009-01-08 15:54:00 +01:00
|
|
|
?STATERR(<<"500">>, <<"Internal Server Error">>);
|
2007-05-12 20:14:21 +02:00
|
|
|
NUsers ->
|
2009-01-08 15:54:00 +01:00
|
|
|
?STATVAL(list_to_binary(integer_to_list(NUsers)), <<"users">>)
|
2005-04-17 20:08:34 +02:00
|
|
|
end;
|
|
|
|
|
2009-01-08 15:54:00 +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
|
2007-12-07 01:09:48 +01:00
|
|
|
{'EXIT', _Reason} ->
|
2009-01-08 15:54:00 +01:00
|
|
|
?STATERR(<<"500">>, <<"Internal Server Error">>);
|
2003-01-11 21:25:11 +01:00
|
|
|
Users ->
|
2009-01-08 15:54:00 +01:00
|
|
|
?STATVAL(list_to_binary(integer_to_list(Users)), <<"users">>)
|
2003-01-11 21:25:11 +01:00
|
|
|
end;
|
2005-04-17 20:08:34 +02:00
|
|
|
|
2009-01-08 15:54:00 +01:00
|
|
|
get_local_stat(_Server, [], Name) when Name == <<"users/all-hosts/total">> ->
|
2004-12-12 23:32:15 +01:00
|
|
|
case catch mnesia:table_info(passwd, size) of
|
2007-12-07 01:09:48 +01:00
|
|
|
{'EXIT', _Reason} ->
|
2009-01-08 15:54:00 +01:00
|
|
|
?STATERR(<<"500">>, <<"Internal Server Error">>);
|
2003-01-11 21:25:11 +01:00
|
|
|
Users ->
|
2009-01-08 15:54:00 +01:00
|
|
|
?STATVAL(list_to_binary(integer_to_list(Users)), <<"users">>)
|
2003-01-11 21:25:11 +01:00
|
|
|
end;
|
2005-04-17 20:08:34 +02:00
|
|
|
|
|
|
|
get_local_stat(_Server, _, Name) ->
|
2009-01-08 15:54:00 +01:00
|
|
|
?STATERR(<<"404">>, <<"Not Found">>).
|
2003-01-18 20:42:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-01-08 15:54:00 +01:00
|
|
|
get_node_stat(Node, Name) when Name == <<"time/uptime">> ->
|
2003-01-18 20:42:48 +01:00
|
|
|
case catch rpc:call(Node, erlang, statistics, [wall_clock]) of
|
2007-12-07 01:09:48 +01:00
|
|
|
{badrpc, _Reason} ->
|
2009-01-08 15:54:00 +01:00
|
|
|
?STATERR(<<"500">>, <<"Internal Server Error">>);
|
2003-01-18 20:42:48 +01:00
|
|
|
CPUTime ->
|
2009-01-08 15:54:00 +01:00
|
|
|
?STATVAL(list_to_binary(
|
|
|
|
io_lib:format("~.3f", [element(1, CPUTime)/1000])), "seconds")
|
2003-01-18 20:42:48 +01:00
|
|
|
end;
|
|
|
|
|
2009-01-08 15:54:00 +01:00
|
|
|
get_node_stat(Node, Name) when Name == <<"time/cputime">> ->
|
2003-01-18 20:42:48 +01:00
|
|
|
case catch rpc:call(Node, erlang, statistics, [runtime]) of
|
2007-12-07 01:09:48 +01:00
|
|
|
{badrpc, _Reason} ->
|
2009-01-08 15:54:00 +01:00
|
|
|
?STATERR(<<"500">>, <<"Internal Server Error">>);
|
2003-01-18 20:42:48 +01:00
|
|
|
RunTime ->
|
2009-01-08 15:54:00 +01:00
|
|
|
?STATVAL(list_to_binary(
|
|
|
|
io_lib:format("~.3f", [element(1, RunTime)/1000])), "seconds")
|
2003-01-18 20:42:48 +01:00
|
|
|
end;
|
|
|
|
|
2009-01-08 15:54:00 +01:00
|
|
|
get_node_stat(Node, Name) when Name == <<"users/online">> ->
|
2003-01-18 20:42:48 +01:00
|
|
|
case catch rpc:call(Node, ejabberd_sm, dirty_get_my_sessions_list, []) of
|
2007-12-07 01:09:48 +01:00
|
|
|
{badrpc, _Reason} ->
|
2009-01-08 15:54:00 +01:00
|
|
|
?STATERR(<<"500">>, <<"Internal Server Error">>);
|
2003-01-18 20:42:48 +01:00
|
|
|
Users ->
|
2009-01-08 15:54:00 +01:00
|
|
|
?STATVAL(list_to_binary(integer_to_list(length(Users))), <<"users">>)
|
2003-01-18 20:42:48 +01:00
|
|
|
end;
|
2003-01-29 18:12:23 +01:00
|
|
|
|
2009-01-19 16:27:07 +01:00
|
|
|
get_node_stat(Node, Name) when Name == <<"transactions/committed">> ->
|
2003-01-29 18:12:23 +01:00
|
|
|
case catch rpc:call(Node, mnesia, system_info, [transaction_commits]) of
|
2007-12-07 01:09:48 +01:00
|
|
|
{badrpc, _Reason} ->
|
2009-01-08 15:54:00 +01:00
|
|
|
?STATERR(<<"500">>, <<"Internal Server Error">>);
|
2003-01-29 18:12:23 +01:00
|
|
|
Transactions ->
|
2009-01-08 15:54:00 +01:00
|
|
|
?STATVAL(list_to_binary(integer_to_list(Transactions)), <<"transactions">>)
|
2003-01-29 18:12:23 +01:00
|
|
|
end;
|
|
|
|
|
2009-01-08 15:54:00 +01:00
|
|
|
get_node_stat(Node, Name) when Name == <<"transactions/aborted">> ->
|
2003-01-29 18:12:23 +01:00
|
|
|
case catch rpc:call(Node, mnesia, system_info, [transaction_failures]) of
|
2007-12-07 01:09:48 +01:00
|
|
|
{badrpc, _Reason} ->
|
2009-01-08 15:54:00 +01:00
|
|
|
?STATERR(<<"500">>, <<"Internal Server Error">>);
|
2003-01-29 18:12:23 +01:00
|
|
|
Transactions ->
|
2009-01-08 15:54:00 +01:00
|
|
|
?STATVAL(list_to_binary(integer_to_list(Transactions)), <<"transactions">>)
|
2003-01-29 18:12:23 +01:00
|
|
|
end;
|
|
|
|
|
2009-01-08 15:54:00 +01:00
|
|
|
get_node_stat(Node, Name) when Name == <<"transactions/restarted">> ->
|
2003-01-29 18:12:23 +01:00
|
|
|
case catch rpc:call(Node, mnesia, system_info, [transaction_restarts]) of
|
2007-12-07 01:09:48 +01:00
|
|
|
{badrpc, _Reason} ->
|
2009-01-08 15:54:00 +01:00
|
|
|
?STATERR(<<"500">>, <<"Internal Server Error">>);
|
2003-01-29 18:12:23 +01:00
|
|
|
Transactions ->
|
2009-01-08 15:54:00 +01:00
|
|
|
?STATVAL(list_to_binary(integer_to_list(Transactions)), <<"transactions">>)
|
2003-01-29 18:12:23 +01:00
|
|
|
end;
|
|
|
|
|
2009-01-08 15:54:00 +01:00
|
|
|
get_node_stat(Node, Name) when Name == <<"transactions/logged">> ->
|
2003-01-29 18:12:23 +01:00
|
|
|
case catch rpc:call(Node, mnesia, system_info, [transaction_log_writes]) of
|
2007-12-07 01:09:48 +01:00
|
|
|
{badrpc, _Reason} ->
|
2009-01-08 15:54:00 +01:00
|
|
|
?STATERR(<<"500">>, <<"Internal Server Error">>);
|
2003-01-29 18:12:23 +01:00
|
|
|
Transactions ->
|
2009-01-08 15:54:00 +01:00
|
|
|
?STATVAL(list_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) ->
|
2009-01-08 15:54:00 +01:00
|
|
|
?STATERR(<<"404">>, <<"Not Found">>).
|
2003-01-18 20:42:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
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 atom_to_list(Node) of
|
|
|
|
SNode ->
|
|
|
|
Node;
|
|
|
|
_ ->
|
|
|
|
search_running_node(SNode, Nodes)
|
|
|
|
end.
|
|
|
|
|