fix: incorrect behavior in audit logs during stop and start of EMQX

This commit is contained in:
zhongwencool 2023-12-12 14:29:39 +08:00
parent 4fc14fb4ba
commit e2e7f96515
2 changed files with 33 additions and 21 deletions

View File

@ -336,18 +336,25 @@ audit_log(Level, From, Log) ->
{error, _} -> {error, _} ->
ignore; ignore;
{ok, {Mod, Fun}} -> {ok, {Mod, Fun}} ->
case prune_unnecessary_log(Log) of
false -> ok;
{ok, Log1} -> apply_audit_command(Log1, Mod, Fun, Level, From)
end
end.
apply_audit_command(Log, Mod, Fun, Level, From) ->
try try
apply(Mod, Fun, [Level, From, normalize_audit_log_args(Log)]) apply(Mod, Fun, [Level, From, Log])
catch catch
_:{aborted, {no_exists, emqx_audit}} -> _:{aborted, {no_exists, emqx_audit}} ->
case Log of case Log of
#{cmd := cluster, args := ["leave"]} -> #{cmd := cluster, args := [<<"leave">>]} ->
ok; ok;
_ -> _ ->
?LOG_ERROR(#{ ?LOG_ERROR(#{
msg => "ctl_command_crashed", msg => "ctl_command_crashed",
reason => "emqx_audit table not found", reason => "emqx_audit table not found",
log => normalize_audit_log_args(Log), log => Log,
from => From from => From
}) })
end; end;
@ -356,10 +363,15 @@ audit_log(Level, From, Log) ->
msg => "ctl_command_crashed", msg => "ctl_command_crashed",
stacktrace => Stacktrace, stacktrace => Stacktrace,
reason => Reason, reason => Reason,
log => normalize_audit_log_args(Log), log => Log,
from => From from => From
}) })
end end.
prune_unnecessary_log(Log) ->
case normalize_audit_log_args(Log) of
#{args := [<<"emqx:is_running()">>]} -> false;
Log1 -> {ok, Log1}
end. end.
audit_level(ok, _Duration) -> info; audit_level(ok, _Duration) -> info;

View File

@ -69,7 +69,7 @@ graceful() ->
graceful_wait() -> graceful_wait() ->
?AUDIT(alert, #{ ?AUDIT(alert, #{
cmd => emqx, cmd => emqx,
args => ["stop"], args => [<<"stop">>],
version => emqx_release:version(), version => emqx_release:version(),
from => cli, from => cli,
duration_ms => element(1, erlang:statistics(wall_clock)) duration_ms => element(1, erlang:statistics(wall_clock))