Skip to content

Instantly share code, notes, and snippets.

@developerworks
Created October 4, 2014 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save developerworks/553129e3015995b56028 to your computer and use it in GitHub Desktop.
Save developerworks/553129e3015995b56028 to your computer and use it in GitHub Desktop.
mod_system_information.erl
%%%----------------------------------------------------------------------
%%% File : mod_onlineusers.erl
%%% Author : Michael Weibel <michael.weibel@amiadogroup.com>
%%% Purpose : Display number of online users
%%% Created : 2011-08-16
%%%----------------------------------------------------------------------
%%% https://github.com/candy-chat/mod_onlineusers
%%% http://blog.zlxstar.me/blog/2013/07/21/dicorvery-user-muclist
-module(mod_system_information).
-author('hezhiqiang').
-vsn('').
%%-define(ejabberd_debug, true).
-behaviour(gen_mod).
-export([
start/2,
stop/1,
process/2
]).
-include("ejabberd.hrl").
-include("jlib.hrl").
-include("ejabberd_http.hrl").
-include("logger.hrl").
%%%----------------------------------------------------------------------
%%% REQUEST HANDLERS
%%%----------------------------------------------------------------------
%% [
%% {access_module,mnesia},
%% {auto_repair,true},
%% {backup_module,mnesia_backup},
%% {checkpoints,[]},
%% {db_nodes,[ejabberd@localhost]},
%% {debug,none},
%% {directory,"/var/lib/ejabberd"},
%% {dump_log_load_regulation,false},
%% {dump_log_time_threshold,180000},
%% {dump_log_update_in_place,true},
%% {dump_log_write_threshold,1000},
%% {event_module,mnesia_event},
%% {extra_db_nodes,[]},
%% {fallback_activated,false},
%% {held_locks,[]},
%% {ignore_fallback_at_startup,false},
%% {fallback_error_function,{mnesia,lkill}},
%% {is_running,yes},
%% {local_tables,[
%% shaper,
%% mod_register_ip,
%% local_config,
%% caps_features,
%% acl,
%% access,
%% carboncopy,
%% http_bind,
%% reg_users_counter,
%% pubsub_subscription,
%% bytestream,
%% privacy,
%% passwd,
%% irc_custom,
%% roster,
%% last_activity,
%% sr_user,
%% roster_version,
%% pubsub_last_item,
%% offline_msg,
%% route,
%% motd,
%% s2s,
%% vcard,
%% pubsub_index,
%% sr_group,
%% session_counter,
%% vcard_search,
%% motd_users,
%% schema,
%% session,
%% private_storage,
%% pubsub_item,
%% muc_room,
%% pubsub_state,
%% iq_response,
%% temporarily_blocked,
%% muc_registered,
%% muc_online_room,
%% pubsub_node
%% ]},
%% {lock_queue,[]},
%% {log_version,"4.3"},
%% {master_node_tables,[]},
%% {max_wait_for_decision,infinity},
%% {protocol_version,{8,1}},
%% {running_db_nodes,[ejabberd@localhost]},
%% {schema_location,opt_disc},
%% {schema_version,{2,0}},
%% {subscribers,[<0.15778.0>,<0.16018.0>,<0.15961.0>,<0.15954.0>]},
%% {tables,[
%% carboncopy,
%% http_bind,
%% reg_users_counter,
%% pubsub_subscription,
%% bytestream,
%% privacy,
%% local_config,
%% passwd,
%% irc_custom,
%% shaper,
%% roster,
%% last_activity,
%% sr_user,
%% roster_version,
%% pubsub_last_item,
%% offline_msg,
%% route,
%% motd,
%% access,
%% acl,
%% s2s,
%% vcard,
%% pubsub_index,
%% caps_features,
%% sr_group,
%% session_counter,
%% mod_register_ip,
%% vcard_search,
%% motd_users,
%% schema,
%% session,
%% private_storage,
%% pubsub_item,
%% muc_room,
%% pubsub_state,
%% iq_response,
%% temporarily_blocked,
%% muc_registered,
%% muc_online_room,
%% pubsub_node
%% ]},
%% {transaction_commits,56},
%% {transaction_failures,81},
%% {transaction_log_writes,0},
%% {transaction_restarts,0},
%% {transactions,[]},
%% {use_dir,true},
%% {core_dir,false},
%% {no_table_loaders,2},
%% {dc_dump_limit,4},
%% {send_compressed,0},
%% {version,"4.12.3"}
%% ]
process(_LocalPath, _Request) ->
%% Users = mnesia:table_info(session, size),
%% 已连接用户数
ConnectedUsersNumber = ejabberd_sm:connected_users_number(),
%% 用户列表
AllSessionList = ejabberd_sm:dirty_get_sessions_list(),
?DEBUG("All sessions ~p~n", [AllSessionList]),
%% Mnesia 表信息
MnesiaSystemInfo = mnesia:system_info(all),
?DEBUG("Mnesia Information ~p~n", [MnesiaSystemInfo]),
%% [{<<"root">>,<<"xmpp.hezhiqiang.info">>,<<"3439698832141213525690305">>}]
%% 构造一个JSON对象数组
%% 对象: {[]}
%% 数组: []
AllSessions = lists:map(fun({User, Server, Resource}) ->
{[{user, User}, {server, Server}, {resource, Resource}]}
end, AllSessionList),
RemoveElements = [subscribers, fallback_error_function],
MnesiaSystemInfoJiffy = lists:filtermap(fun({Key, Value}) ->
case lists:any(fun(E2) -> E2 =:= Key end, RemoveElements) of
true ->
false;
false ->
case Key of
directory ->
{true, {Key, list_to_bitstring(Value)}};
version ->
{true, {Key, list_to_bitstring(Value)}};
log_version ->
{true, {Key, list_to_bitstring(Value)}};
schema_version ->
{V1, V2} = Value,
{true, {Key, list_to_bitstring([integer_to_list(V1), ".", integer_to_list(V2)])}};
protocol_version ->
{V1, V2} = Value,
{true, {Key, list_to_bitstring([integer_to_list(V1), ".", integer_to_list(V2)])}};
_ ->
{true, {Key, Value}}
end
end
end, MnesiaSystemInfo),
Json = jiffy:encode({[
{connected_users_number, ConnectedUsersNumber},
{sessions, AllSessions},
{mnesia, {MnesiaSystemInfoJiffy}}
]}),
{200, [], Json}.
%%%----------------------------------------------------------------------
%%% BEHAVIOUR CALLBACKS
%%%----------------------------------------------------------------------
start(_Host, _Opts) ->
?INFO_MSG("===Starting module mod_system_information===", []),
ok.
stop(_Host) ->
?INFO_MSG("===Stopping module mod_system_information===", []),
ok.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment