1 |
|
%%%------------------------------------------------------------------- |
2 |
|
%%% File : service_admin_extra_stats.erl |
3 |
|
%%% Author : Badlop <badlop@process-one.net>, Piotr Nosek <piotr.nosek@erlang-solutions.com> |
4 |
|
%%% Purpose : Contributed administrative functions and commands |
5 |
|
%%% Created : 10 Aug 2008 by Badlop <badlop@process-one.net> |
6 |
|
%%% |
7 |
|
%%% |
8 |
|
%%% ejabberd, Copyright (C) 2002-2008 ProcessOne |
9 |
|
%%% |
10 |
|
%%% This program is free software; you can redistribute it and/or |
11 |
|
%%% modify it under the terms of the GNU General Public License as |
12 |
|
%%% published by the Free Software Foundation; either version 2 of the |
13 |
|
%%% License, or (at your option) any later version. |
14 |
|
%%% |
15 |
|
%%% This program is distributed in the hope that it will be useful, |
16 |
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 |
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
18 |
|
%%% General Public License for more details. |
19 |
|
%%% |
20 |
|
%%% You should have received a copy of the GNU General Public License |
21 |
|
%%% along with this program; if not, write to the Free Software |
22 |
|
%%% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
23 |
|
%%% |
24 |
|
%%%------------------------------------------------------------------- |
25 |
|
|
26 |
|
-module(service_admin_extra_stats). |
27 |
|
-author('badlop@process-one.net'). |
28 |
|
|
29 |
|
|
30 |
|
-export([ |
31 |
|
commands/0, |
32 |
|
|
33 |
|
stats/1, stats/2 |
34 |
|
]). |
35 |
|
|
36 |
|
-ignore_xref([commands/0, stats/1, stats/2]). |
37 |
|
|
38 |
|
-include("mongoose.hrl"). |
39 |
|
-include("ejabberd_commands.hrl"). |
40 |
|
|
41 |
|
%%% |
42 |
|
%%% Register commands |
43 |
|
%%% |
44 |
|
|
45 |
|
-spec commands() -> [ejabberd_commands:cmd(), ...]. |
46 |
|
commands() -> |
47 |
166 |
[ |
48 |
|
#ejabberd_commands{name = stats, tags = [stats], |
49 |
|
desc = "Get statistical value:" |
50 |
|
" registeredusers onlineusers onlineusersnode uptimeseconds", |
51 |
|
module = ?MODULE, function = stats, |
52 |
|
args = [{name, binary}], |
53 |
|
result = {stat, integer}}, |
54 |
|
#ejabberd_commands{name = stats_host, tags = [stats], |
55 |
|
desc = "Get statistical value for this host:" |
56 |
|
" registeredusers onlineusers", |
57 |
|
module = ?MODULE, function = stats, |
58 |
|
args = [{name, binary}, {host, binary}], |
59 |
|
result = {stat, integer}} |
60 |
|
]. |
61 |
|
|
62 |
|
%%% |
63 |
|
%%% Stats |
64 |
|
%%% |
65 |
|
|
66 |
|
-spec stats(binary()) -> integer() | {error, string()}. |
67 |
|
stats(Name) -> |
68 |
4 |
case Name of |
69 |
|
<<"uptimeseconds">> -> |
70 |
1 |
trunc(element(1, erlang:statistics(wall_clock))/1000); |
71 |
|
<<"registeredusers">> -> |
72 |
1 |
Domains = lists:flatmap(fun mongoose_domain_api:get_domains_by_host_type/1, |
73 |
|
?ALL_HOST_TYPES), |
74 |
1 |
lists:sum([ejabberd_auth:get_vh_registered_users_number(Domain) || Domain <- Domains]); |
75 |
|
<<"onlineusersnode">> -> |
76 |
1 |
ejabberd_sm:get_node_sessions_number(); |
77 |
|
<<"onlineusers">> -> |
78 |
1 |
ejabberd_sm:get_total_sessions_number(); |
79 |
|
_ -> |
80 |
:-( |
{error, "Wrong command name."} |
81 |
|
end. |
82 |
|
|
83 |
|
|
84 |
|
-spec stats(binary(), jid:server()) -> integer() | {error, string()}. |
85 |
|
stats(Name, Host) -> |
86 |
4 |
case Name of |
87 |
|
<<"registeredusers">> -> |
88 |
2 |
ejabberd_auth:get_vh_registered_users_number(Host); |
89 |
|
<<"onlineusers">> -> |
90 |
2 |
ejabberd_sm:get_vh_session_number(Host); |
91 |
|
_ -> |
92 |
:-( |
{error, "Wrong command name."} |
93 |
|
end. |