This is an automated email from the ASF dual-hosted git repository.

jaydoane pushed a commit to branch pluggable-custodian-monitor
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 43e8882e7a7a023b73789a1d7ec90e0ff7477857
Author: Jay Doane <jaydo...@apache.org>
AuthorDate: Mon Dec 14 18:54:02 2020 -0800

    Support pluggable custodian monitor
    
    Enable build time configurable monitor for custodian and remove custom
    sensu events.
---
 src/custodian/rebar.config.script                  | 35 ++++++++++++++++
 ...{custodian.app.src => custodian.app.src.script} | 37 ++++++++++++-----
 src/custodian/src/custodian_db_checker.erl         | 14 ++-----
 .../{custodian.app.src => custodian_monitor.erl}   | 30 ++++++--------
 ...ustodian.app.src => custodian_noop_monitor.erl} | 40 ++++++++++--------
 src/custodian/src/custodian_server.erl             | 47 +---------------------
 6 files changed, 103 insertions(+), 100 deletions(-)

diff --git a/src/custodian/rebar.config.script 
b/src/custodian/rebar.config.script
new file mode 100644
index 0000000..f32db97
--- /dev/null
+++ b/src/custodian/rebar.config.script
@@ -0,0 +1,35 @@
+% Licensed under the Apache License, Version 2.0 (the "License"); you may not
+% use this file except in compliance with the License. You may obtain a copy of
+% the License at
+%
+%   http://www.apache.org/licenses/LICENSE-2.0
+%
+% Unless required by applicable law or agreed to in writing, software
+% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+% License for the specific language governing permissions and limitations under
+% the License.
+
+
+CouchConfig = case filelib:is_file(os:getenv("COUCHDB_CONFIG")) of
+    true ->
+        {ok, Result} = file:consult(os:getenv("COUCHDB_CONFIG")),
+        Result;
+    false ->
+        []
+end,
+
+CustodianMonitor = case lists:keyfind(custodian_monitor, 1, CouchConfig) of
+    {custodian_monitor, Module} when Module /= "" ->
+        list_to_atom(Module);
+    _ ->
+        custodian_noop_monitor
+end,
+
+CurrentOpts = case lists:keyfind(erl_opts, 1, CONFIG) of
+    {erl_opts, Opts} -> Opts;
+    false -> []
+end,
+
+CustodianOpts = {d, 'CUSTODIAN_MONITOR', CustodianMonitor},
+lists:keystore(erl_opts, 1, CONFIG, {erl_opts, [CustodianOpts | CurrentOpts]}).
diff --git a/src/custodian/src/custodian.app.src 
b/src/custodian/src/custodian.app.src.script
similarity index 54%
copy from src/custodian/src/custodian.app.src
copy to src/custodian/src/custodian.app.src.script
index b93b21e..551b9c2 100644
--- a/src/custodian/src/custodian.app.src
+++ b/src/custodian/src/custodian.app.src.script
@@ -10,20 +10,39 @@
 % License for the specific language governing permissions and limitations under
 % the License.
 
+CouchConfig = case filelib:is_file(os:getenv("COUCHDB_CONFIG")) of
+    true ->
+        {ok, Result} = file:consult(os:getenv("COUCHDB_CONFIG")),
+        Result;
+    false ->
+        []
+end.
+
+CustodianMonitorApp = case lists:keyfind(custodian_monitor_app, 1, 
CouchConfig) of
+    {custodian_monitor_app, AppName} when AppName /= "" ->
+        [list_to_atom(AppName)];
+    _ ->
+        []
+end.
+
+BaseApplications = [
+    kernel,
+    stdlib,
+    couch_log,
+    config,
+    couch_event,
+    couch,
+    mem3
+].
+
+Applications = CustodianMonitorApp ++ BaseApplications.
+
 {application, custodian,
  [
   {description, "in your cluster, looking after your stuff"},
   {vsn, git},
   {registered, []},
-  {applications, [
-                  kernel,
-                  stdlib,
-                  couch_log,
-                  config,
-                  couch_event,
-                  couch,
-                  mem3
-                 ]},
+  {applications, Applications},
   {mod, { custodian_app, []}},
   {env, []}
  ]}.
diff --git a/src/custodian/src/custodian_db_checker.erl 
b/src/custodian/src/custodian_db_checker.erl
index 8308c8e..10502dd 100644
--- a/src/custodian/src/custodian_db_checker.erl
+++ b/src/custodian/src/custodian_db_checker.erl
@@ -149,17 +149,9 @@ get_stats_db() ->
 
 send_missing_db_alert(DbName) ->
     couch_log:notice("Missing system database ~s", [DbName]),
-    Command = [
-        "send-sensu-event --standalone --critical",
-        " --output=\"Missing system database ",
-        binary_to_list(DbName),
-        "\" --handler=default custodian-missing-db-check"],
-    os:cmd(lists:concat(Command)).
+    ?CUSTODIAN_MONITOR:send_missing_db_alert(DbName).
+
 
 clear_missing_dbs_alert() ->
     couch_log:notice("All system databases exist.", []),
-    Command = [
-        "send-sensu-event --standalone --ok",
-        " --output=\"All system databases exist\"",
-        " --handler=default custodian-missing-db-check"],
-    os:cmd(lists:concat(Command)).
+    ?CUSTODIAN_MONITOR:clear_missing_dbs_alert().
diff --git a/src/custodian/src/custodian.app.src 
b/src/custodian/src/custodian_monitor.erl
similarity index 57%
copy from src/custodian/src/custodian.app.src
copy to src/custodian/src/custodian_monitor.erl
index b93b21e..700e092 100644
--- a/src/custodian/src/custodian.app.src
+++ b/src/custodian/src/custodian_monitor.erl
@@ -10,20 +10,16 @@
 % License for the specific language governing permissions and limitations under
 % the License.
 
-{application, custodian,
- [
-  {description, "in your cluster, looking after your stuff"},
-  {vsn, git},
-  {registered, []},
-  {applications, [
-                  kernel,
-                  stdlib,
-                  couch_log,
-                  config,
-                  couch_event,
-                  couch,
-                  mem3
-                 ]},
-  {mod, { custodian_app, []}},
-  {env, []}
- ]}.
+-module(custodian_monitor).
+
+
+-callback send_missing_db_alert(DbName :: binary()) ->
+    Output :: string().
+
+
+-callback clear_missing_dbs_alert() ->
+    Output :: string().
+
+
+-callback send_event({Type :: atom(), Count :: non_neg_integer()}) ->
+    Output :: string().
diff --git a/src/custodian/src/custodian.app.src 
b/src/custodian/src/custodian_noop_monitor.erl
similarity index 57%
rename from src/custodian/src/custodian.app.src
rename to src/custodian/src/custodian_noop_monitor.erl
index b93b21e..ed4711b 100644
--- a/src/custodian/src/custodian.app.src
+++ b/src/custodian/src/custodian_noop_monitor.erl
@@ -10,20 +10,26 @@
 % License for the specific language governing permissions and limitations under
 % the License.
 
-{application, custodian,
- [
-  {description, "in your cluster, looking after your stuff"},
-  {vsn, git},
-  {registered, []},
-  {applications, [
-                  kernel,
-                  stdlib,
-                  couch_log,
-                  config,
-                  couch_event,
-                  couch,
-                  mem3
-                 ]},
-  {mod, { custodian_app, []}},
-  {env, []}
- ]}.
+-module(custodian_noop_monitor).
+
+
+-behaviour(custodian_monitor).
+
+
+-export([
+    send_missing_db_alert/1,
+    clear_missing_dbs_alert/0,
+    send_event/1
+]).
+
+
+send_missing_db_alert(_DbName) ->
+    "no-op".
+
+
+clear_missing_dbs_alert() ->
+    "no-op".
+
+
+send_event(_Item) ->
+    "no-op".
diff --git a/src/custodian/src/custodian_server.erl 
b/src/custodian/src/custodian_server.erl
index 322cc32..8ce02b7 100644
--- a/src/custodian/src/custodian_server.erl
+++ b/src/custodian/src/custodian_server.erl
@@ -143,52 +143,7 @@ handle_db_event(_DbName, _Event, _St) ->
     {ok, nil}.
 
 check_shards() ->
-    [send_sensu_event(Item) || Item <- custodian:summary()].
-
-send_sensu_event({_, Count} = Item) ->
-    Level = case Count of
-        0 ->
-            "--ok";
-        1 ->
-            couch_log:critical("~s", [describe(Item)]),
-            "--critical";
-        _ ->
-            couch_log:warning("~s", [describe(Item)]),
-            "--warning"
-    end,
-    Cmd = lists:concat([
-        "send-sensu-event --standalone ",
-        Level,
-        " --output=\"",
-        describe(Item),
-        "\" ",
-        check_name(Item)
-    ]),
-    os:cmd(Cmd).
-
-describe({{safe, N}, Count}) ->
-    lists:concat([Count, " ", shards(Count), " in cluster with only ", N,
-                  " ", copies(N), " on nodes that are currently up"]);
-describe({{live, N}, Count}) ->
-    lists:concat([Count, " ", shards(Count), " in cluster with only ",
-                  N, " ", copies(N), " on nodes not in maintenance mode"]);
-describe({conflicted, Count}) ->
-    lists:concat([Count, " conflicted ", shards(Count), " in cluster"]).
-
-check_name({{Type, N}, _}) ->
-    lists:concat(["custodian-", N, "-", Type, "-shards-check"]);
-check_name({Type, _}) ->
-    lists:concat(["custodian-", Type, "-shards-check"]).
-
-shards(1) ->
-    "shard";
-shards(_) ->
-    "shards".
-
-copies(1) ->
-    "copy";
-copies(_) ->
-    "copies".
+    [?CUSTODIAN_MONITOR:send_event(Item) || Item <- custodian:summary()].
 
 
 -ifdef(TEST).

Reply via email to