Merge pull request #7768 from HJianBo/fix-bad-alarm-msg

fix(alarm): use gen_event:noifty/2 to generate alarm event
This commit is contained in:
JianBo He 2022-04-26 21:18:57 +08:00 committed by GitHub
commit 44689f0f63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 12 deletions

View File

@ -61,7 +61,10 @@ handle_event({set_alarm, {system_memory_high_watermark, []}}, State) ->
Message = to_bin("System memory usage is higher than ~p%", [HighWatermark]), Message = to_bin("System memory usage is higher than ~p%", [HighWatermark]),
emqx_alarm:activate( emqx_alarm:activate(
high_system_memory_usage, high_system_memory_usage,
#{high_watermark => HighWatermark}, #{
high_watermark => HighWatermark,
percent => emqx_os_mon:current_sysmem_percent()
},
Message Message
), ),
{ok, State}; {ok, State};

View File

@ -45,9 +45,9 @@ start(_Type, _Args) ->
ok = maybe_start_quicer(), ok = maybe_start_quicer(),
ok = emqx_bpapi:start(), ok = emqx_bpapi:start(),
wait_boot_shards(), wait_boot_shards(),
ok = emqx_alarm_handler:load(),
{ok, Sup} = emqx_sup:start_link(), {ok, Sup} = emqx_sup:start_link(),
ok = maybe_start_listeners(), ok = maybe_start_listeners(),
ok = emqx_alarm_handler:load(),
emqx_config:add_handlers(), emqx_config:add_handlers(),
register(emqx, self()), register(emqx, self()),
{ok, Sup}. {ok, Sup}.

View File

@ -31,6 +31,10 @@
set_procmem_high_watermark/1 set_procmem_high_watermark/1
]). ]).
-export([
current_sysmem_percent/0
]).
%% gen_server callbacks %% gen_server callbacks
-export([ -export([
init/1, init/1,
@ -72,6 +76,20 @@ get_procmem_high_watermark() ->
set_procmem_high_watermark(Float) -> set_procmem_high_watermark(Float) ->
memsup:set_procmem_high_watermark(Float). memsup:set_procmem_high_watermark(Float).
current_sysmem_percent() ->
case erlang:whereis(memsup) of
undefined ->
undefined;
_Pid ->
{Total, Allocated, _Worst} = memsup:get_memory_data(),
case Total =/= 0 of
true ->
erlang:floor((Allocated / Total) * 10000) / 100;
false ->
undefined
end
end.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% gen_server callbacks %% gen_server callbacks
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
@ -163,14 +181,12 @@ start_check_timer() ->
%% and there is no exported function to remove the alerted flag, %% and there is no exported function to remove the alerted flag,
%% so it can only be checked again at startup. %% so it can only be checked again at startup.
ensure_system_memory_alarm(HW) -> ensure_system_memory_alarm(HW) when HW =< 1.0 andalso HW >= 0 ->
case erlang:whereis(memsup) of case current_sysmem_percent() of
undefined -> Usage when Usage > (HW * 100) ->
ok; gen_event:notify(
_Pid -> alarm_handler, {set_alarm, {system_memory_high_watermark, []}}
{Total, Allocated, _Worst} = memsup:get_memory_data(), );
case Total =/= 0 andalso Allocated / Total > HW of _ ->
true -> emqx_alarm:activate(high_system_memory_usage, #{high_watermark => HW}); ok
false -> ok
end
end. end.