restart_module/2,
% Sessions
- num_active_users/2, num_resources/2, resource_num/3,
+ num_resources/2, resource_num/3,
kick_session/4, status_num/2, status_num/1,
status_list/2, status_list/1, connected_users_info/0,
connected_users_vhost/1, set_presence/7,
" - 0: code reloaded, module restarted\n"
" - 1: error: module not loaded\n"
" - 2: code not reloaded, but module restarted"},
- #ejabberd_commands{name = num_active_users, tags = [accounts, stats],
- desc = "Get number of users active in the last days (only Mnesia)",
- policy = admin,
- module = ?MODULE, function = num_active_users,
- args = [{host, binary}, {days, integer}],
- args_example = [<<"myserver.com">>, 3],
- args_desc = ["Name of host to check", "Number of days to calculate sum"],
- result = {users, integer},
- result_example = 123,
- result_desc = "Number of users active on given server in last n days"},
#ejabberd_commands{name = delete_old_users, tags = [accounts, purge],
desc = "Delete users that didn't log in last days, or that never logged",
longdesc = "To protect admin accounts, configure this for example:\n"
|| X <- binary_to_list(
crypto:hash(binary_to_atom(Method, latin1), AccountPass))]).
-num_active_users(Host, Days) ->
- DB_Type = gen_mod:get_module_opt(Host, mod_last, db_type),
- list_last_activity(Host, true, Days, DB_Type).
-
-%% Code based on ejabberd/src/web/ejabberd_web_admin.erl
-list_last_activity(Host, Integral, Days, mnesia) ->
- TimeStamp = p1_time_compat:system_time(seconds),
- TS = TimeStamp - Days * 86400,
- case catch mnesia:dirty_select(
- last_activity, [{{last_activity, {'_', Host}, '$1', '_'},
- [{'>', '$1', TS}],
- [{'trunc', {'/',
- {'-', TimeStamp, '$1'},
- 86400}}]}]) of
- {'EXIT', _Reason} ->
- [];
- Vals ->
- Hist = histogram(Vals, Integral),
- if
- Hist == [] ->
- 0;
- true ->
- Left = Days - length(Hist),
- Tail = if
- Integral ->
- lists:duplicate(Left, lists:last(Hist));
- true ->
- lists:duplicate(Left, 0)
- end,
- lists:nth(Days, Hist ++ Tail)
- end
- end;
-list_last_activity(_Host, _Integral, _Days, DB_Type) ->
- throw({error, iolist_to_binary(io_lib:format("Unsupported backend: ~p",
- [DB_Type]))}).
-
-histogram(Values, Integral) ->
- histogram(lists:sort(Values), Integral, 0, 0, []).
-histogram([H | T], Integral, Current, Count, Hist) when Current == H ->
- histogram(T, Integral, Current, Count + 1, Hist);
-histogram([H | _] = Values, Integral, Current, Count, Hist) when Current < H ->
- if
- Integral ->
- histogram(Values, Integral, Current + 1, Count, [Count | Hist]);
- true ->
- histogram(Values, Integral, Current + 1, 0, [Count | Hist])
- end;
-histogram([], _Integral, _Current, Count, Hist) ->
- if
- Count > 0 ->
- lists:reverse([Count | Hist]);
- true ->
- lists:reverse(Hist)
- end.
-
-
delete_old_users(Days) ->
%% Get the list of registered users
Users = ejabberd_auth:get_users(),