From ca19a4e9e8fcfe1cbfbcbbe52bda6039034d781e Mon Sep 17 00:00:00 2001 From: Ery Lee Date: Sun, 8 Mar 2015 15:34:27 +0800 Subject: [PATCH] fix systops --- apps/emqtt/include/emqtt_systop.hrl | 12 ++++++------ apps/emqtt/src/emqtt_broker.erl | 28 ++++++++++++++++------------ 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/apps/emqtt/include/emqtt_systop.hrl b/apps/emqtt/include/emqtt_systop.hrl index f2f4c01be..38d6710bb 100644 --- a/apps/emqtt/include/emqtt_systop.hrl +++ b/apps/emqtt/include/emqtt_systop.hrl @@ -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', ]). %%------------------------------------------------------------------------------ diff --git a/apps/emqtt/src/emqtt_broker.erl b/apps/emqtt/src/emqtt_broker.erl index 2c809b0c6..f29b6020a 100644 --- a/apps/emqtt/src/emqtt_broker.erl +++ b/apps/emqtt/src/emqtt_broker.erl @@ -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)). -