diff --git a/lib/towerops/workers/check_executor_worker.ex b/lib/towerops/workers/check_executor_worker.ex index 139e085f..01145ad5 100644 --- a/lib/towerops/workers/check_executor_worker.ex +++ b/lib/towerops/workers/check_executor_worker.ex @@ -39,9 +39,6 @@ defmodule Towerops.Workers.CheckExecutorWorker do require Logger - @snmp_check_types ~w(snmp_sensor snmp_interface snmp_processor snmp_storage) - @service_check_types ~w(http tcp dns ssl) - @impl Oban.Worker def perform(%Oban.Job{args: %{"check_id" => check_id}}) do case Monitoring.get_check(check_id) do @@ -50,53 +47,41 @@ defmodule Towerops.Workers.CheckExecutorWorker do :ok check -> - cond do - not check.enabled -> - Logger.debug("Check #{check_id} disabled, skipping execution") - - should_skip_snmp_check?(check) -> - Logger.debug("Skipping SNMP check #{check_id} - Phoenix SNMP disabled or device assigned to agent") - schedule_next_check(check) - - should_skip_service_check?(check) -> - Logger.debug("Skipping service check #{check_id} - device assigned to agent") - schedule_next_check(check) - - should_skip_ping_check?(check) -> - Logger.debug("Skipping ping check #{check_id} - device assigned to agent") - schedule_next_check(check) - - true -> - execute_and_record(check) - schedule_next_check(check) - end - + execute_or_skip(check) :ok end end - defp should_skip_snmp_check?(%{check_type: check_type} = check) when check_type in @snmp_check_types do - Client.phoenix_snmp_disabled() or - (check.device_id != nil and Agents.device_has_effective_agent?(check.device_id)) + defp execute_or_skip(%{enabled: false} = check) do + Logger.debug("Check #{check.id} disabled, skipping execution") end - defp should_skip_snmp_check?(_check), do: false + defp execute_or_skip(check) do + if should_skip_check?(check) do + Logger.info("Skipping check #{check.id} (#{check.check_type}) - device has agent or Phoenix SNMP disabled") + else + execute_and_record(check) + end - defp should_skip_service_check?(%{check_type: check_type} = check) when check_type in @service_check_types do - # Skip service checks (HTTP/TCP/DNS/SSL) if device has any agent (cloud or local) - # The agent handles all check execution - no fallback to Phoenix + schedule_next_check(check) + end + + # Unified skip logic for ALL check types. + # Skip when the device has any effective agent assigned at any cascade level, + # or when Phoenix SNMP is disabled for SNMP-type checks. + defp should_skip_check?(check) do + snmp_disabled_for_snmp_check?(check) or device_has_agent?(check) + end + + defp snmp_disabled_for_snmp_check?(check) do + check.check_type in ~w(snmp_sensor snmp_interface snmp_processor snmp_storage) and + Client.phoenix_snmp_disabled() + end + + defp device_has_agent?(check) do check.device_id != nil and Agents.device_has_effective_agent?(check.device_id) end - defp should_skip_service_check?(_check), do: false - - defp should_skip_ping_check?(%{check_type: "ping"} = check) do - # Skip ping if device has any agent (cloud or local) - check.device_id != nil and Agents.device_has_effective_agent?(check.device_id) - end - - defp should_skip_ping_check?(_check), do: false - defp execute_and_record(check) do Logger.debug("Executing check #{check.id} (#{check.check_type})") diff --git a/lib/towerops/workers/check_worker.ex b/lib/towerops/workers/check_worker.ex index e45c273a..89cbaec1 100644 --- a/lib/towerops/workers/check_worker.ex +++ b/lib/towerops/workers/check_worker.ex @@ -57,19 +57,22 @@ defmodule Towerops.Workers.CheckWorker do end defp should_phoenix_execute?(check) do - case check.agent_token_id do - nil -> - # No agent assigned - Phoenix executes - true + # Never execute if the device has any effective agent at any cascade level + if check.device_id != nil and Agents.device_has_effective_agent?(check.device_id) do + false + else + case check.agent_token_id do + nil -> + true - agent_token_id -> - # Agent assigned - only Phoenix executes if it's cloud poller - try do - agent_token = Agents.get_agent_token!(agent_token_id) - agent_token.is_cloud_poller - rescue - Ecto.NoResultsError -> true - end + agent_token_id -> + try do + agent_token = Agents.get_agent_token!(agent_token_id) + agent_token.is_cloud_poller + rescue + Ecto.NoResultsError -> true + end + end end end diff --git a/lib/towerops/workers/device_monitor_worker.ex b/lib/towerops/workers/device_monitor_worker.ex index 2822815f..62342e10 100644 --- a/lib/towerops/workers/device_monitor_worker.ex +++ b/lib/towerops/workers/device_monitor_worker.ex @@ -75,7 +75,7 @@ defmodule Towerops.Workers.DeviceMonitorWorker do :ok Agents.device_has_effective_agent?(device.id) -> - Logger.debug("Skipping Phoenix monitoring for device #{device.name} - device has agent assigned") + Logger.info("Skipping Phoenix monitoring for device #{device.name} - device has agent assigned") :ok true -> diff --git a/lib/towerops/workers/device_poller_worker.ex b/lib/towerops/workers/device_poller_worker.ex index ec23500f..51f4384a 100644 --- a/lib/towerops/workers/device_poller_worker.ex +++ b/lib/towerops/workers/device_poller_worker.ex @@ -92,7 +92,7 @@ defmodule Towerops.Workers.DevicePollerWorker do :ok Agents.device_has_effective_agent?(device.id) -> - Logger.debug("Skipping Phoenix poll for device #{device.name} - device has agent assigned") + Logger.info("Skipping Phoenix poll for device #{device.name} - device has agent assigned") :ok true ->