From 9fbc247e29395c157f4685721eb660c51b713fa4 Mon Sep 17 00:00:00 2001 From: k32 <10274441+k32@users.noreply.github.com> Date: Mon, 6 Dec 2021 16:47:15 +0100 Subject: [PATCH] feat(system_monitor): Add system_monitor application --- apps/emqx_conf/src/emqx_conf_schema.erl | 62 +++++++++++++++++++++++++ apps/emqx_machine/src/emqx_machine.erl | 13 ++++++ rebar.config | 1 + rebar.config.erl | 1 + 4 files changed, 77 insertions(+) diff --git a/apps/emqx_conf/src/emqx_conf_schema.erl b/apps/emqx_conf/src/emqx_conf_schema.erl index 617703cd2..0a4841e64 100644 --- a/apps/emqx_conf/src/emqx_conf_schema.erl +++ b/apps/emqx_conf/src/emqx_conf_schema.erl @@ -105,6 +105,10 @@ natively in the EMQ X node;
sc(ref("db"), #{ desc => "Settings of the embedded database." })} + , {"system_monitor", + sc(ref("system_monitor"), + #{ desc => "Erlang process and application monitoring." + })} ] ++ emqx_schema:roots(medium) ++ emqx_schema:roots(low) ++ @@ -327,6 +331,64 @@ a crash dump )} ]; +fields("system_monitor") -> + [ {"top_num_items", + sc(non_neg_integer(), + #{ mapping => "system_monitor.top_num_items" + , default => 10 + , desc => "The number of top processes per monitoring group" + }) + } + , {"top_sample_interval", + sc(emqx_schema:duration(), + #{ mapping => "system_monitor.top_sample_interval" + , default => "2s" + , desc => "Specifies how often process top should be collected" + }) + } + , {"top_max_procs", + sc(non_neg_integer(), + #{ mapping => "system_monitor.top_max_procs" + , default => 200000 + , desc => "Stop collecting data when the number of processes exceeds this value" + }) + } + , {"db_hostname", + sc(string(), + #{ mapping => "system_monitor.db_hostname" + , desc => "Hostname of the postgres database that collects the data points" + }) + } + , {"db_port", + sc(integer(), + #{ mapping => "system_monitor.db_port" + , default => 5432 + , desc => "Port of the postgres database that collects the data points" + }) + } + , {"db_username", + sc(string(), + #{ mapping => "system_monitor.db_username" + , default => "system_monitor" + , desc => "EMQX user name in the postgres database" + }) + } + , {"db_password", + sc(binary(), + #{ mapping => "system_monitor.db_password" + , default => "system_monitor_password" + , desc => "EMQX user password in the postgres database" + }) + } + , {"db_name", + sc(string(), + #{ mapping => "system_monitor.db_name" + , default => "postgres" + , desc => "Postgres database name" + }) + } + ]; + fields("db") -> [ {"backend", sc(hoconsc:enum([mnesia, rlog]), diff --git a/apps/emqx_machine/src/emqx_machine.erl b/apps/emqx_machine/src/emqx_machine.erl index fae21eece..178cf8fc7 100644 --- a/apps/emqx_machine/src/emqx_machine.erl +++ b/apps/emqx_machine/src/emqx_machine.erl @@ -32,6 +32,7 @@ start() -> os:set_signal(sigterm, handle) %% default is handle end, ok = set_backtrace_depth(), + start_sysmon(), ekka:start(), ok = print_otp_version_warning(). @@ -54,3 +55,15 @@ print_otp_version_warning() -> ?ULOG("WARNING: Running on Erlang/OTP version ~p. Recommended: 23~n", [?OTP_RELEASE]). -endif. % OTP_RELEASE > 22 + +start_sysmon() -> + case application:get_env(system_monitor, db_hostname) of + undefined -> + %% If there is no sink for the events, there is no reason + %% to run system_monitor_top, ignore it: + ok; + _ -> + application:set_env(system_monitor, callback_mod, system_monitor_pg), + _ = application:ensure_all_started(system_monitor, temporary), + ok + end. diff --git a/rebar.config b/rebar.config index 4fbf1b136..c7d6d70bd 100644 --- a/rebar.config +++ b/rebar.config @@ -62,6 +62,7 @@ , {emqtt, {git, "https://github.com/emqx/emqtt", {tag, "1.4.3"}}} , {rulesql, {git, "https://github.com/emqx/rulesql", {tag, "0.1.4"}}} , {observer_cli, "1.7.1"} % NOTE: depends on recon 2.5.x + , {system_monitor, {git, "https://github.com/klarna-incubator/system_monitor", {tag, "2.2.0"}}} , {getopt, "1.0.2"} , {snabbkaffe, {git, "https://github.com/kafka4beam/snabbkaffe.git", {tag, "0.15.0"}}} , {hocon, {git, "https://github.com/emqx/hocon.git", {tag, "0.20.6"}}} diff --git a/rebar.config.erl b/rebar.config.erl index 22949f233..5bdcd2eb5 100644 --- a/rebar.config.erl +++ b/rebar.config.erl @@ -281,6 +281,7 @@ relx_apps(ReleaseType, Edition) -> , {emqx_plugin_libs, load} , {esasl, load} , observer_cli + , system_monitor , emqx_http_lib , emqx_resource , emqx_connector