broker, metrics

This commit is contained in:
Ery Lee 2015-03-07 01:32:21 +08:00
parent a53cc60573
commit 818d4741a6
6 changed files with 22 additions and 12 deletions

View File

@ -25,8 +25,8 @@
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
-record(topic, { -record(topic, {
name :: binary(), name :: binary(),
type :: static | dynamic | bridge type :: static | dynamic | bridge,
node :: node(), node :: node()
}). }).
-type topic() :: #topic{}. -type topic() :: #topic{}.

View File

@ -61,6 +61,7 @@ print_vsn() ->
start_servers(Sup) -> start_servers(Sup) ->
{ok, SessOpts} = application:get_env(session), {ok, SessOpts} = application:get_env(session),
{ok, RetainOpts} = application:get_env(retain), {ok, RetainOpts} = application:get_env(retain),
{ok, BrokerOpts} = application:get_env(broker),
lists:foreach( lists:foreach(
fun({Name, F}) when is_function(F) -> fun({Name, F}) when is_function(F) ->
?PRINT("~s is starting...", [Name]), ?PRINT("~s is starting...", [Name]),
@ -83,6 +84,8 @@ start_servers(Sup) ->
{"emqtt auth", emqtt_auth}, {"emqtt auth", emqtt_auth},
{"emqtt pubsub", emqtt_pubsub}, {"emqtt pubsub", emqtt_pubsub},
{"emqtt router", emqtt_router}, {"emqtt router", emqtt_router},
{"emqtt broker", emqtt_broker, BrokerOpts},
{"emqtt metrics", emqtt_metrics},
{"emqtt monitor", emqtt_monitor} {"emqtt monitor", emqtt_monitor}
]). ]).

View File

@ -58,12 +58,10 @@ start_link(Options) ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [Options], []). gen_server:start_link({local, ?SERVER}, ?MODULE, [Options], []).
version() -> version() ->
{ok, Version} = application:get_key(emqtt, vsn), {ok, Version} = application:get_key(emqtt, vsn), Version.
Version.
description() -> description() ->
{ok, Descr} = application:get_key(emqtt, description), {ok, Descr} = application:get_key(emqtt, description), Descr.
Descr.
uptime() -> uptime() ->
gen_server:call(?SERVER, uptime). gen_server:call(?SERVER, uptime).
@ -78,9 +76,8 @@ init([Options]) ->
ets:new(?MODULE, [set, public, name_table, {write_concurrency, true}]), ets:new(?MODULE, [set, public, name_table, {write_concurrency, true}]),
{ok, #state{started_at = os:timestamp(), sys_interval = SysInterval}}. {ok, #state{started_at = os:timestamp(), sys_interval = SysInterval}}.
handle_call(uptime, _From, State = #state{started_at = Ts}) -> handle_call(uptime, _From, State) ->
Secs = timer:now_diff(os:timestamp(), Ts) div 1000000, {reply, uptime(State), State};
{reply, uptime(seconds, Secs), State};
handle_call(_Request, _From, State) -> handle_call(_Request, _From, State) ->
{reply, ok, State}. {reply, ok, State}.
@ -103,6 +100,10 @@ code_change(_OldVsn, State, _Extra) ->
systop(Topic) -> systop(Topic) ->
<<"$SYS/broker/", Topic/binary>>. <<"$SYS/broker/", Topic/binary>>.
uptime(#state{started_at = Ts}) ->
Secs = timer:now_diff(os:timestamp(), Ts) div 1000000,
uptime(seconds, Secs).
uptime(seconds, Secs) when Secs < 60 -> uptime(seconds, Secs) when Secs < 60 ->
[integer_to_list(Secs), " seconds"]; [integer_to_list(Secs), " seconds"];
uptime(seconds, Secs) -> uptime(seconds, Secs) ->

View File

@ -80,7 +80,7 @@ dec(Metric, Val) ->
init(_Args) -> init(_Args) ->
% Bytes sent and received % Bytes sent and received
[ok = emqtt_pubsub:create(<<"$SYS/broker/", Topic/binary>>) || Topic <- ?SYSTOP_METRICS], [ok = emqtt_pubsub:create(Topic) || Topic <- ?SYSTOP_METRICS],
% $SYS/broker/version % $SYS/broker/version
%## Uptime %## Uptime
% $SYS/broker/uptime % $SYS/broker/uptime
@ -111,5 +111,3 @@ code_change(_OldVsn, State, _Extra) ->
%% Internal Function Definitions %% Internal Function Definitions
%% ------------------------------------------------------------------ %% ------------------------------------------------------------------

View File

@ -39,6 +39,7 @@
-export([start_link/0]). -export([start_link/0]).
-export([topics/0, -export([topics/0,
create/1,
subscribe/2, subscribe/2,
unsubscribe/2, unsubscribe/2,
publish/1, publish/1,
@ -94,6 +95,10 @@ start_link() ->
topics() -> topics() ->
mnesia:dirty_all_keys(topic). mnesia:dirty_all_keys(topic).
%TODO
create(Topic) ->
ok.
%% %%
%% @doc Subscribe Topic or Topics %% @doc Subscribe Topic or Topics
%% %%

View File

@ -49,6 +49,9 @@
{retain, [ {retain, [
{store_limit, 100000} {store_limit, 100000}
]}, ]},
{broker, [
{sys_interval, 60}
]},
{listen, [ {listen, [
{mqtt, 1883, [ {mqtt, 1883, [
{backlog, 512}, {backlog, 512},