diff --git a/lib/towerops_web/live/site_live/show.ex b/lib/towerops_web/live/site_live/show.ex index 6f85120c..9b250565 100644 --- a/lib/towerops_web/live/site_live/show.ex +++ b/lib/towerops_web/live/site_live/show.ex @@ -5,6 +5,8 @@ defmodule ToweropsWeb.SiteLive.Show do alias Towerops.Devices alias Towerops.Monitoring alias Towerops.Sites + alias Towerops.Snmp + alias Towerops.Workers.DiscoveryWorker @impl true def mount(_params, _session, socket) do @@ -26,6 +28,30 @@ defmodule ToweropsWeb.SiteLive.Show do |> assign(:latency_chart_data, latency_chart_data)} end + @impl true + def handle_event("force_rediscover_all", _params, socket) do + devices = socket.assigns.device + snmp_devices = Enum.filter(devices, & &1.snmp_enabled) + + if Enum.empty?(snmp_devices) do + {:noreply, put_flash(socket, :error, "No SNMP-enabled devices at this site")} + else + # Enqueue discovery for all SNMP-enabled devices + Enum.each(snmp_devices, fn device -> + enqueue_discovery(device.id) + end) + + count = length(snmp_devices) + + {:noreply, + put_flash( + socket, + :info, + "Discovery started for #{count} device#{if count == 1, do: "", else: "s"}" + )} + end + end + defp load_site_latency_chart_data(devices) do if Enum.empty?(devices) do nil @@ -61,4 +87,15 @@ defmodule ToweropsWeb.SiteLive.Show do y: check.response_time_ms } end + + # Enqueue discovery job - safe to call in test environment + defp enqueue_discovery(device_id) do + if Application.get_env(:towerops, :env) == :test do + # In test, run synchronously + Task.start(fn -> Snmp.discover_device(Devices.get_device!(device_id)) end) + else + # In dev/prod, enqueue to Exq + {:ok, _job} = Exq.enqueue(Exq, "discovery", DiscoveryWorker, [device_id]) + end + end end diff --git a/lib/towerops_web/live/site_live/show.html.heex b/lib/towerops_web/live/site_live/show.html.heex index 4f826823..c6092344 100644 --- a/lib/towerops_web/live/site_live/show.html.heex +++ b/lib/towerops_web/live/site_live/show.html.heex @@ -9,6 +9,15 @@ {@page_title} <:subtitle>{@site.location} <:actions> + <%= if @device != [] do %> + <.button + type="button" + phx-click="force_rediscover_all" + data-confirm="This will trigger SNMP discovery for all devices at this site. Continue?" + > + <.icon name="hero-magnifying-glass" class="h-4 w-4" /> Force Rediscover All + + <% end %> <.button navigate={~p"/sites/#{@site.id}/edit"}> <.icon name="hero-pencil" class="h-4 w-4" /> Edit