|
|
@@ -21,15 +21,7 @@
|
|
|
-include("logger.hrl").
|
|
|
|
|
|
%% APIs
|
|
|
--export([start_link/1]).
|
|
|
-
|
|
|
--export([ get_check_interval/0
|
|
|
- , set_check_interval/1
|
|
|
- , get_process_high_watermark/0
|
|
|
- , set_process_high_watermark/1
|
|
|
- , get_process_low_watermark/0
|
|
|
- , set_process_low_watermark/1
|
|
|
- ]).
|
|
|
+-export([start_link/0]).
|
|
|
|
|
|
%% gen_server callbacks
|
|
|
-export([ init/1
|
|
|
@@ -42,61 +34,19 @@
|
|
|
|
|
|
-define(VM_MON, ?MODULE).
|
|
|
|
|
|
-start_link(Opts) ->
|
|
|
- gen_server:start_link({local, ?VM_MON}, ?MODULE, [Opts], []).
|
|
|
-
|
|
|
%%--------------------------------------------------------------------
|
|
|
%% API
|
|
|
%%--------------------------------------------------------------------
|
|
|
-
|
|
|
-get_check_interval() ->
|
|
|
- call(get_check_interval).
|
|
|
-
|
|
|
-set_check_interval(Seconds) ->
|
|
|
- call({set_check_interval, Seconds}).
|
|
|
-
|
|
|
-get_process_high_watermark() ->
|
|
|
- call(get_process_high_watermark).
|
|
|
-
|
|
|
-set_process_high_watermark(Float) ->
|
|
|
- call({set_process_high_watermark, Float}).
|
|
|
-
|
|
|
-get_process_low_watermark() ->
|
|
|
- call(get_process_low_watermark).
|
|
|
-
|
|
|
-set_process_low_watermark(Float) ->
|
|
|
- call({set_process_low_watermark, Float}).
|
|
|
-
|
|
|
-call(Req) ->
|
|
|
- gen_server:call(?VM_MON, Req, infinity).
|
|
|
+start_link() ->
|
|
|
+ gen_server:start_link({local, ?VM_MON}, ?MODULE, [], []).
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
%% gen_server callbacks
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
|
|
-init([Opts]) ->
|
|
|
- {ok, ensure_check_timer(#{check_interval => proplists:get_value(check_interval, Opts),
|
|
|
- process_high_watermark => proplists:get_value(process_high_watermark, Opts),
|
|
|
- process_low_watermark => proplists:get_value(process_low_watermark, Opts),
|
|
|
- timer => undefined})}.
|
|
|
-
|
|
|
-handle_call(get_check_interval, _From, State) ->
|
|
|
- {reply, maps:get(check_interval, State, undefined), State};
|
|
|
-
|
|
|
-handle_call({set_check_interval, Seconds}, _From, State) ->
|
|
|
- {reply, ok, State#{check_interval := Seconds}};
|
|
|
-
|
|
|
-handle_call(get_process_high_watermark, _From, State) ->
|
|
|
- {reply, maps:get(process_high_watermark, State, undefined), State};
|
|
|
-
|
|
|
-handle_call({set_process_high_watermark, Float}, _From, State) ->
|
|
|
- {reply, ok, State#{process_high_watermark := Float}};
|
|
|
-
|
|
|
-handle_call(get_process_low_watermark, _From, State) ->
|
|
|
- {reply, maps:get(process_low_watermark, State, undefined), State};
|
|
|
-
|
|
|
-handle_call({set_process_low_watermark, Float}, _From, State) ->
|
|
|
- {reply, ok, State#{process_low_watermark := Float}};
|
|
|
+init([]) ->
|
|
|
+ start_check_timer(),
|
|
|
+ {ok, #{}}.
|
|
|
|
|
|
handle_call(Req, _From, State) ->
|
|
|
?LOG(error, "[VM_MON] Unexpected call: ~p", [Req]),
|
|
|
@@ -106,10 +56,9 @@ handle_cast(Msg, State) ->
|
|
|
?LOG(error, "[VM_MON] Unexpected cast: ~p", [Msg]),
|
|
|
{noreply, State}.
|
|
|
|
|
|
-handle_info({timeout, Timer, check},
|
|
|
- State = #{timer := Timer,
|
|
|
- process_high_watermark := ProcHighWatermark,
|
|
|
- process_low_watermark := ProcLowWatermark}) ->
|
|
|
+handle_info({timeout, _Timer, check}, State) ->
|
|
|
+ ProcHighWatermark = emqx_config:get([sysmon, vm, process_high_watermark]),
|
|
|
+ ProcLowWatermark = emqx_config:get([sysmon, vm, process_low_watermark]),
|
|
|
ProcessCount = erlang:system_info(process_count),
|
|
|
case ProcessCount / erlang:system_info(process_limit) * 100 of
|
|
|
Percent when Percent >= ProcHighWatermark ->
|
|
|
@@ -121,14 +70,15 @@ handle_info({timeout, Timer, check},
|
|
|
_Precent ->
|
|
|
ok
|
|
|
end,
|
|
|
- {noreply, ensure_check_timer(State)};
|
|
|
+ start_check_timer(),
|
|
|
+ {noreply, State};
|
|
|
|
|
|
handle_info(Info, State) ->
|
|
|
?LOG(error, "[VM_MON] Unexpected info: ~p", [Info]),
|
|
|
{noreply, State}.
|
|
|
|
|
|
-terminate(_Reason, #{timer := Timer}) ->
|
|
|
- emqx_misc:cancel_timer(Timer).
|
|
|
+terminate(_Reason, _State) ->
|
|
|
+ ok.
|
|
|
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
{ok, State}.
|
|
|
@@ -137,5 +87,6 @@ code_change(_OldVsn, State, _Extra) ->
|
|
|
%% Internal functions
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
|
|
-ensure_check_timer(State = #{check_interval := Interval}) ->
|
|
|
- State#{timer := emqx_misc:start_timer(timer:seconds(Interval), check)}.
|
|
|
+start_check_timer() ->
|
|
|
+ Interval = emqx_config:get([sysmon, vm, process_check_interval]),
|
|
|
+ emqx_misc:start_timer(timer:seconds(Interval), check).
|