fix systops

This commit is contained in:
Ery Lee 2015-03-08 15:34:27 +08:00
parent dc8b7f032e
commit ca19a4e9e8
2 changed files with 22 additions and 18 deletions

View File

@ -31,20 +31,20 @@
%% $SYS Topics of Broker
%%------------------------------------------------------------------------------
-define(SYSTOP_BROKERS, [
version, % Broker version
uptime, % Broker uptime
timestamp, % Broker timestamp
description % Broker description
version, % Broker version
uptime, % Broker uptime
datetime, % Broker local datetime
description % Broker description
]).
%%------------------------------------------------------------------------------
%% $SYS Topics of Clients
%%------------------------------------------------------------------------------
-define(SYSTOP_CLIENTS, [
%'clients/connected',
%'clients/disconnected',
'clients/total', % total clients connected current
'clients/max' % max clients connected
%'clients/connected',
%'clients/disconnected',
]).
%%------------------------------------------------------------------------------

View File

@ -42,7 +42,7 @@
-export([start_link/1]).
-export([version/0, uptime/0, description/0]).
-export([version/0, uptime/0, datetime/0, description/0]).
%% ------------------------------------------------------------------
%% gen_server Function Exports
@ -69,6 +69,12 @@ description() ->
uptime() ->
gen_server:call(?SERVER, uptime).
datetime() ->
{{Y, M, D}, {H, MM, S}} = calendar:local_time(),
lists:flatten(
io_lib:format(
"~4..0w-~2..0w-~2..0w ~2..0w:~2..0w:~2..0w", [Y, M, D, H, MM, S])).
%% ------------------------------------------------------------------
%% gen_server Function Definitions
%% ------------------------------------------------------------------
@ -82,10 +88,8 @@ init([Options]) ->
[ets:insert(?TABLE, {Name, 0}) || Name <- ?SYSTOP_CLIENTS],
[ets:insert(?TABLE, {Name, 0}) || Name <- ?SYSTOP_PUBSUB],
% retain version, description
retain(systop(version), list_to_binary(version())),
retain(systop(description), list_to_binary(description())),
State = #state{started_at = os:timestamp(), sys_interval = SysInterval},
{ok, tick(State)}.
gen_server:cast(self(), prepare),
{ok, tick(#state{started_at = os:timestamp(), sys_interval = SysInterval})}.
handle_call(uptime, _From, State) ->
{reply, uptime(State), State};
@ -93,14 +97,19 @@ handle_call(uptime, _From, State) ->
handle_call(_Request, _From, State) ->
{reply, ok, State}.
handle_cast(prepare, State) ->
retain(systop(version), list_to_binary(version())),
retain(systop(description), list_to_binary(description())),
{noreply, State};
handle_cast(_Msg, State) ->
{noreply, State}.
handle_info(tick, State) ->
publish(systop(uptime), list_to_binary(uptime(State))),
[publish(systop(Name), i2b(Val)) || {Name, Val} <- ets:tab2list(?TABLE)],
publish(systop(datetime), list_to_binary(datetime())),
%%TODO... call emqtt_cm here?
[publish(systop(client, Stat), i2b(Val)) || {Stat, Val} <- emqtt_cm:stats()],
[publish(systop(Stat), i2b(Val)) || {Stat, Val} <- emqtt_cm:stats()],
%%TODO... call emqtt_pubsub here?
[publish(systop(Stat), i2b(Val)) || {Stat, Val} <- emqtt_cm:stats()],
{noreply, tick(State)};
@ -117,10 +126,6 @@ code_change(_OldVsn, State, _Extra) ->
%% ------------------------------------------------------------------
%% Internal Function Definitions
%% ------------------------------------------------------------------
systop(Prefix, Name) ->
systop(list_to_atom(lists:concat([Prefix, '/', Name]))).
systop(Name) when is_atom(Name) ->
list_to_binary(lists:concat(["$SYS/brokers/", node(), "/", Name])).
@ -161,4 +166,3 @@ tick(State = #state{sys_interval = SysInterval}) ->
i2b(I) when is_integer(I) ->
list_to_binary(integer_to_list(I)).