2003-01-11 21:25:11 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_stats.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Purpose :
|
|
|
|
%%% Created : 11 Jan 2003 by Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Id : $Id$
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_stats).
|
|
|
|
-author('alexey@sevcom.net').
|
|
|
|
-vsn('$Revision$ ').
|
|
|
|
|
2003-01-24 21:18:33 +01:00
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
2003-01-22 21:40:40 +01:00
|
|
|
-export([start/1,
|
2003-01-29 18:12:23 +01:00
|
|
|
stop/0,
|
2003-01-11 21:25:11 +01:00
|
|
|
process_local_iq/3]).
|
|
|
|
|
2003-03-09 21:46:47 +01:00
|
|
|
-include("jlib.hrl").
|
2003-01-11 21:25:11 +01:00
|
|
|
|
2003-01-24 21:18:33 +01:00
|
|
|
start(Opts) ->
|
|
|
|
IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),
|
2003-01-22 21:40:40 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, ?NS_STATS,
|
2003-01-24 21:18:33 +01:00
|
|
|
?MODULE, process_local_iq, IQDisc).
|
2003-01-11 21:25:11 +01:00
|
|
|
|
2003-01-29 18:12:23 +01:00
|
|
|
stop() ->
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, ?NS_STATS).
|
|
|
|
|
2003-01-11 21:25:11 +01:00
|
|
|
|
|
|
|
process_local_iq(From, To, {iq, ID, Type, XMLNS, SubEl}) ->
|
|
|
|
Lang = xml:get_tag_attr_s("xml:lang", SubEl),
|
|
|
|
case Type of
|
|
|
|
set ->
|
2003-05-09 21:32:59 +02:00
|
|
|
{iq, ID, error, XMLNS, [SubEl, ?ERR_NOT_ALLOWED]};
|
2003-01-11 21:25:11 +01:00
|
|
|
get ->
|
|
|
|
{xmlelement, _, Attrs, Els} = SubEl,
|
|
|
|
Node = string:tokens(xml:get_tag_attr_s("node", SubEl), "/"),
|
|
|
|
Names = get_names(Els, []),
|
|
|
|
|
2003-01-18 20:42:48 +01:00
|
|
|
case get_local_stats(Node, Names) of
|
|
|
|
{result, Res} ->
|
|
|
|
{iq, ID, result, XMLNS,
|
|
|
|
[{xmlelement, "query", [{"xmlns", XMLNS}], Res}]};
|
2003-03-09 21:46:47 +01:00
|
|
|
{error, Error} ->
|
2003-01-18 20:42:48 +01:00
|
|
|
{iq, ID, error, XMLNS,
|
2003-03-09 21:46:47 +01:00
|
|
|
[SubEl, Error]}
|
2003-01-11 21:25:11 +01:00
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
get_names([], Res) ->
|
|
|
|
Res;
|
|
|
|
get_names([{xmlelement, "stat", Attrs, _} | Els], Res) ->
|
|
|
|
Name = xml:get_attr_s("name", Attrs),
|
|
|
|
case Name of
|
|
|
|
"" ->
|
|
|
|
get_names(Els, Res);
|
|
|
|
_ ->
|
|
|
|
get_names(Els, [Name | Res])
|
|
|
|
end;
|
|
|
|
get_names([_ | Els], Res) ->
|
|
|
|
get_names(Els, Res).
|
|
|
|
|
|
|
|
|
2003-01-18 20:42:48 +01:00
|
|
|
-define(STAT(Name), {xmlelement, "stat", [{"name", Name}], []}).
|
|
|
|
|
2003-01-11 21:25:11 +01:00
|
|
|
get_local_stats([], []) ->
|
|
|
|
{result,
|
2003-01-18 20:42:48 +01:00
|
|
|
[?STAT("users/online"),
|
|
|
|
?STAT("users/total")
|
|
|
|
]};
|
|
|
|
|
2003-01-11 21:25:11 +01:00
|
|
|
get_local_stats([], Names) ->
|
2003-01-18 20:42:48 +01:00
|
|
|
{result, lists:map(fun(Name) -> get_local_stat([], Name) end, Names)};
|
|
|
|
|
|
|
|
get_local_stats(["running nodes", _], []) ->
|
2003-01-11 21:25:11 +01:00
|
|
|
{result,
|
2003-01-18 20:42:48 +01:00
|
|
|
[?STAT("time/uptime"),
|
|
|
|
?STAT("time/cputime"),
|
2003-01-29 18:12:23 +01:00
|
|
|
?STAT("users/online"),
|
|
|
|
?STAT("transactions/commited"),
|
|
|
|
?STAT("transactions/aborted"),
|
|
|
|
?STAT("transactions/restarted"),
|
|
|
|
?STAT("transactions/logged")
|
2003-01-18 20:42:48 +01:00
|
|
|
]};
|
|
|
|
|
|
|
|
get_local_stats(["running nodes", ENode], Names) ->
|
|
|
|
case search_running_node(ENode) of
|
|
|
|
false ->
|
|
|
|
{error, "404", "Not Found"};
|
|
|
|
Node ->
|
|
|
|
{result,
|
|
|
|
lists:map(fun(Name) -> get_node_stat(Node, Name) end, Names)}
|
|
|
|
end;
|
|
|
|
|
2003-01-11 21:25:11 +01:00
|
|
|
get_local_stats(_, _) ->
|
2003-03-09 21:46:47 +01:00
|
|
|
{error, ?ERR_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),
|
2003-01-11 21:25:11 +01:00
|
|
|
{xmlelement, "stat",
|
|
|
|
[{"name", Name},
|
|
|
|
{"units", Unit},
|
|
|
|
{"value", Val}
|
|
|
|
], []}).
|
|
|
|
|
|
|
|
-define(STATERR(Code, Desc),
|
|
|
|
{xmlelement, "stat",
|
|
|
|
[{"name", Name}],
|
|
|
|
[{xmlelement, "error",
|
|
|
|
[{"code", Code}],
|
|
|
|
[{xmlcdata, Desc}]}]}).
|
|
|
|
|
|
|
|
|
2003-01-18 20:42:48 +01:00
|
|
|
%get_local_stat([], Name) when Name == "time/uptime" ->
|
|
|
|
% ?STATVAL(io_lib:format("~.3f", [element(1, statistics(wall_clock))/1000]),
|
|
|
|
% "seconds");
|
|
|
|
%get_local_stat([], Name) when Name == "time/cputime" ->
|
|
|
|
% ?STATVAL(io_lib:format("~.3f", [element(1, statistics(runtime))/1000]),
|
|
|
|
% "seconds");
|
2003-01-11 21:25:11 +01:00
|
|
|
get_local_stat([], Name) when Name == "users/online" ->
|
|
|
|
case catch ejabberd_sm:dirty_get_sessions_list() of
|
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?STATERR("500", "Internal Server Error");
|
|
|
|
Users ->
|
2003-01-18 20:42:48 +01:00
|
|
|
?STATVAL(integer_to_list(length(Users)), "users")
|
2003-01-11 21:25:11 +01:00
|
|
|
end;
|
|
|
|
get_local_stat([], Name) when Name == "users/total" ->
|
|
|
|
case catch ejabberd_auth:dirty_get_registered_users() of
|
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?STATERR("500", "Internal Server Error");
|
|
|
|
Users ->
|
2003-01-18 20:42:48 +01:00
|
|
|
?STATVAL(integer_to_list(length(Users)), "users")
|
2003-01-11 21:25:11 +01:00
|
|
|
end;
|
|
|
|
get_local_stat(_, Name) ->
|
|
|
|
?STATERR("404", "Not Found").
|
2003-01-18 20:42:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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(
|
|
|
|
io_lib:format("~.3f", [element(1, CPUTime)/1000]), "seconds")
|
|
|
|
end;
|
|
|
|
|
|
|
|
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(
|
|
|
|
io_lib:format("~.3f", [element(1, RunTime)/1000]), "seconds")
|
|
|
|
end;
|
|
|
|
|
|
|
|
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(integer_to_list(length(Users)), "users")
|
|
|
|
end;
|
2003-01-29 18:12:23 +01:00
|
|
|
|
|
|
|
get_node_stat(Node, Name) when Name == "transactions/commited" ->
|
|
|
|
case catch rpc:call(Node, mnesia, system_info, [transaction_commits]) of
|
|
|
|
{badrpc, Reason} ->
|
|
|
|
?STATERR("500", "Internal Server Error");
|
|
|
|
Transactions ->
|
|
|
|
?STATVAL(integer_to_list(Transactions), "transactions")
|
|
|
|
end;
|
|
|
|
|
|
|
|
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(integer_to_list(Transactions), "transactions")
|
|
|
|
end;
|
|
|
|
|
|
|
|
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(integer_to_list(Transactions), "transactions")
|
|
|
|
end;
|
|
|
|
|
|
|
|
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(integer_to_list(Transactions), "transactions")
|
|
|
|
end;
|
|
|
|
|
2003-01-18 20:42:48 +01:00
|
|
|
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 atom_to_list(Node) of
|
|
|
|
SNode ->
|
|
|
|
Node;
|
|
|
|
_ ->
|
|
|
|
search_running_node(SNode, Nodes)
|
|
|
|
end.
|
|
|
|
|