1## This Source Code Form is subject to the terms of the Mozilla Public 2## License, v. 2.0. If a copy of the MPL was not distributed with this 3## file, You can obtain one at https://mozilla.org/MPL/2.0/. 4## 5## Copyright (c) 2007-2021 VMware, Inc. or its affiliates. All rights reserved. 6 7defmodule RabbitMQ.CLI.Diagnostics.Commands.ListNetworkInterfacesCommand do 8 @moduledoc """ 9 Displays all network interfaces (NICs) reported by the target node. 10 """ 11 import RabbitMQ.CLI.Core.Platform, only: [line_separator: 0] 12 import RabbitMQ.CLI.Core.ANSI 13 14 @behaviour RabbitMQ.CLI.CommandBehaviour 15 16 def switches(), do: [timeout: :integer, offline: :boolean] 17 def aliases(), do: [t: :timeout] 18 19 use RabbitMQ.CLI.Core.MergesNoDefaults 20 use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments 21 22 def run([], %{offline: true}) do 23 :rabbit_net.getifaddrs() 24 end 25 def run([], %{node: node_name, timeout: timeout}) do 26 :rabbit_misc.rpc_call(node_name, :rabbit_net, :getifaddrs, [], timeout) 27 end 28 29 def output(nic_map, %{node: node_name, formatter: "json"}) when map_size(nic_map) == 0 do 30 {:ok, %{"result" => "ok", "node" => node_name, "interfaces" => %{}}} 31 end 32 def output(nic_map, %{node: node_name}) when map_size(nic_map) == 0 do 33 {:ok, "Node #{node_name} reported no network interfaces"} 34 end 35 def output(nic_map0, %{node: node_name, formatter: "json"}) do 36 nic_map = Enum.map(nic_map0, fn ({k, v}) -> {to_string(k), v} end) 37 {:ok, 38 %{ 39 "result" => "ok", 40 "interfaces" => Enum.into(nic_map, %{}), 41 "message" => "Node #{node_name} reported network interfaces" 42 }} 43 end 44 def output(nic_map, _) when is_map(nic_map) do 45 lines = nic_lines(nic_map) 46 47 {:ok, Enum.join(lines, line_separator())} 48 end 49 use RabbitMQ.CLI.DefaultOutput 50 51 def help_section(), do: :observability_and_health_checks 52 53 def description(), do: "Lists network interfaces (NICs) on the target node" 54 55 def usage, do: "list_network_interfaces" 56 57 def banner([], %{node: node_name}) do 58 "Asking node #{node_name} to report its network interfaces ..." 59 end 60 61 # 62 # Implementation 63 # 64 65 defp nic_lines(nic_map) do 66 Enum.reduce(nic_map, [], 67 fn({iface, props}, acc) -> 68 iface_lines = Enum.reduce(props, [], 69 fn({prop, val}, inner_acc) -> 70 ["#{prop}: #{val}" | inner_acc] 71 end) 72 73 header = "#{bright("Interface #{iface}")}\n" 74 acc ++ [header | iface_lines] ++ ["\n"] 75 end) 76 end 77end 78