From da7b6bdd01703f3a559be50e5e7a267decd415f8 Mon Sep 17 00:00:00 2001 From: Drew Gulino <1379399+dgulinobw@users.noreply.github.com> Date: Thu, 29 Aug 2024 12:41:25 -0400 Subject: [PATCH] using a db query to populate host active state metrics --- src/dog_host.erl | 25 +++++++++++++++++++------ src/dog_keepalive_agent.erl | 8 ++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/dog_host.erl b/src/dog_host.erl index cce176c..e6b2390 100644 --- a/src/dog_host.erl +++ b/src/dog_host.erl @@ -9,19 +9,21 @@ -export([ create/1, delete/1, + get_all/0, + get_all_active/0, get_by_hostkey/1, get_by_id/1, get_by_name/1, - get_all/0, - get_all_active/0, + get_id_by_hostkey/1, get_schema/0, update/2, - update_by_hostkey/2, - get_id_by_hostkey/1 + update_by_hostkey/2 ]). -export([ get_all_active_interfaces/0, + get_all_joined_with_group/0, + get_grouped_active_states/0, get_hostkeys_by_ips/0, get_names_by_ips/0, hash_check/1, @@ -30,8 +32,7 @@ keepalive_age_check/0, keepalive_age_check/1, keepalive_check/0, keepalive_check/1, retirement_check/0, retirement_check/1, - state_event/3, - get_all_joined_with_group/0 + state_event/3 ]). -spec get_all_joined_with_group() -> HostsGroups :: map(). @@ -685,6 +686,18 @@ get_all_active() -> end, {ok, Hosts}. +-spec get_grouped_active_states() -> {ok, {grouped, list() }}. +get_grouped_active_states() -> + {ok, Result} = dog_rethink:run( + fun(X) -> + reql:db(X, dog), + reql:table(X, ?TYPE_TABLE), + reql:group(X, <<"active">>), + reql:count(X) + end + ), + {ok, Result}. + get_names_by_ips() -> get_by_ips(<<"name">>). diff --git a/src/dog_keepalive_agent.erl b/src/dog_keepalive_agent.erl index 90ac9a2..eefaf2b 100644 --- a/src/dog_keepalive_agent.erl +++ b/src/dog_keepalive_agent.erl @@ -119,10 +119,18 @@ code_change(_OldVsn, State, _Extra) -> %% Internal Function Definitions %% ------------------------------------------------------------------ +-spec do_host_active_state_metrics() -> null. +do_host_active_state_metrics() -> + {ok, {grouped, States}} = dog_host:get_grouped_active_states(), + lists:foreach(fun([State,Count]) -> + imetrics:set_gauge_m(<<"host_active_state">>, State, Count) + end, + States). %% @doc Makes a call to the nif to add a resource to %% watch. Logs on error -spec do_watch_keepalives(_) -> ok | pass. do_watch_keepalives(_State) -> + do_host_active_state_metrics(), case dog_agent_checker:check() of true -> {ok, HostsRetirementCheck} = dog_host:retirement_check(),