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.Ctl.Commands.StatusCommand do
8  alias RabbitMQ.CLI.Core.DocGuide
9  alias RabbitMQ.CLI.InformationUnit, as: IU
10  import RabbitMQ.CLI.Core.{Alarms, ANSI, DataCoercion, Listeners, Memory, Platform}
11
12  @behaviour RabbitMQ.CLI.CommandBehaviour
13
14  @default_timeout 60_000
15
16  def scopes(), do: [:ctl, :diagnostics]
17
18  def switches(), do: [unit: :string, timeout: :integer]
19  def aliases(), do: [t: :timeout]
20
21  def merge_defaults(args, opts) do
22    timeout =
23      case opts[:timeout] do
24        nil -> @default_timeout
25        :infinity -> @default_timeout
26        other -> other
27      end
28
29    {args, Map.merge(%{unit: "gb", timeout: timeout}, opts)}
30  end
31
32  def validate(args, _) when length(args) > 0 do
33    {:validation_failure, :too_many_args}
34  end
35  def validate(_, %{unit: unit}) do
36    case IU.known_unit?(unit) do
37      true ->
38        :ok
39
40      false ->
41        {:validation_failure, "unit '#{unit}' is not supported. Please use one of: bytes, mb, gb"}
42    end
43  end
44  def validate(_, _), do: :ok
45
46  use RabbitMQ.CLI.Core.RequiresRabbitAppRunning
47
48  def run([], %{node: node_name, timeout: timeout}) do
49    :rabbit_misc.rpc_call(node_name, :rabbit, :status, [], timeout)
50  end
51
52  def output({:error, :timeout}, %{node: node_name}) do
53    {:error, RabbitMQ.CLI.Core.ExitCodes.exit_software(),
54     "Error: timed out while waiting for a response from #{node_name}."}
55  end
56
57  def output(result, %{formatter: "erlang"}) do
58    {:ok, result}
59  end
60
61  def output(result, %{formatter: "json"}) when is_list(result) do
62    m = result_map(result) |> Map.update(:alarms, [], fn xs -> alarm_maps(xs) end)
63
64    {:ok, m}
65  end
66
67  def output(result, %{node: node_name, unit: unit}) when is_list(result) do
68    m = result_map(result)
69
70    product_name_section = case m do
71      %{:product_name => product_name} when product_name != "" ->
72        ["Product name: #{product_name}"]
73      _ ->
74        []
75    end
76    product_version_section = case m do
77      %{:product_version => product_version} when product_version != "" ->
78        ["Product version: #{product_version}"]
79      _ ->
80        []
81    end
82
83    runtime_section = [
84      "#{bright("Runtime")}\n",
85      "OS PID: #{m[:pid]}",
86      "OS: #{m[:os]}",
87      # TODO: format
88      "Uptime (seconds): #{m[:uptime]}",
89      "Is under maintenance?: #{m[:is_under_maintenance]}"
90    ] ++
91    product_name_section ++
92    product_version_section ++
93    [
94      "RabbitMQ version: #{m[:rabbitmq_version]}",
95      "Node name: #{node_name}",
96      "Erlang configuration: #{m[:erlang_version]}",
97      "Erlang processes: #{m[:processes][:used]} used, #{m[:processes][:limit]} limit",
98      "Scheduler run queue: #{m[:run_queue]}",
99      "Cluster heartbeat timeout (net_ticktime): #{m[:net_ticktime]}"
100    ]
101
102    plugin_section = [
103      "\n#{bright("Plugins")}\n",
104      "Enabled plugin file: #{m[:enabled_plugin_file]}",
105      "Enabled plugins:\n"
106    ] ++ Enum.map(m[:active_plugins], fn pl -> " * #{pl}" end)
107
108    data_directory_section = [
109      "\n#{bright("Data directory")}\n",
110      "Node data directory: #{m[:data_directory]}",
111      "Raft data directory: #{m[:raft_data_directory]}"
112    ]
113
114    config_section = [
115      "\n#{bright("Config files")}\n"
116    ] ++ Enum.map(m[:config_files], fn path -> " * #{path}" end)
117
118    log_section = [
119      "\n#{bright("Log file(s)")}\n"
120    ] ++ Enum.map(m[:log_files], fn path -> " * #{path}" end)
121
122    alarms_section = [
123      "\n#{bright("Alarms")}\n",
124    ] ++ case m[:alarms] do
125           [] -> ["(none)"]
126           xs -> alarm_lines(xs, node_name)
127         end
128
129    breakdown = compute_relative_values(m[:memory])
130    memory_calculation_strategy = to_atom(m[:vm_memory_calculation_strategy])
131    total_memory = get_in(m[:memory], [:total, memory_calculation_strategy])
132
133    readable_watermark_setting = case m[:vm_memory_high_watermark_setting] do
134      %{:relative => val} -> "#{val} of available memory"
135      # absolute value
136      %{:absolute => val} -> "#{IU.convert(val, unit)} #{unit}"
137    end
138    memory_section = [
139      "\n#{bright("Memory")}\n",
140      "Total memory used: #{IU.convert(total_memory, unit)} #{unit}",
141      "Calculation strategy: #{memory_calculation_strategy}",
142      "Memory high watermark setting: #{readable_watermark_setting}, computed to: #{IU.convert(m[:vm_memory_high_watermark_limit], unit)} #{unit}\n"
143    ] ++ Enum.map(breakdown, fn({category, val}) -> "#{category}: #{IU.convert(val[:bytes], unit)} #{unit} (#{val[:percentage]} %)" end)
144
145    file_descriptors = [
146      "\n#{bright("File Descriptors")}\n",
147      "Total: #{m[:file_descriptors][:total_used]}, limit: #{m[:file_descriptors][:total_limit]}",
148      "Sockets: #{m[:file_descriptors][:sockets_used]}, limit: #{m[:file_descriptors][:sockets_limit]}"
149    ]
150
151    disk_space_section = [
152      "\n#{bright("Free Disk Space")}\n",
153      "Low free disk space watermark: #{IU.convert(m[:disk_free_limit], unit)} #{unit}",
154      "Free disk space: #{IU.convert(m[:disk_free], unit)} #{unit}"
155    ]
156
157    totals_section = [
158      "\n#{bright("Totals")}\n",
159      "Connection count: #{m[:totals][:connection_count]}",
160      "Queue count: #{m[:totals][:queue_count]}",
161      "Virtual host count: #{m[:totals][:virtual_host_count]}"
162    ]
163
164    listeners_section = [
165      "\n#{bright("Listeners")}\n",
166    ] ++ case m[:listeners] do
167           [] -> ["(none)"]
168           xs -> listener_lines(xs)
169         end
170    lines = runtime_section ++ plugin_section ++ data_directory_section ++
171            config_section ++ log_section ++ alarms_section ++ memory_section ++
172            file_descriptors ++ disk_space_section ++ totals_section ++ listeners_section
173
174    {:ok, Enum.join(lines, line_separator())}
175  end
176
177  use RabbitMQ.CLI.DefaultOutput
178
179  def formatter(), do: RabbitMQ.CLI.Formatters.String
180
181  def usage, do: "status [--unit <unit>]"
182
183  def usage_additional() do
184    [
185      ["--unit <bytes | mb | gb>", "byte multiple (bytes, megabytes, gigabytes) to use"],
186      ["--formatter <json | erlang>", "alternative formatter (JSON, Erlang terms)"]
187    ]
188  end
189
190  def usage_doc_guides() do
191    [
192      DocGuide.monitoring()
193    ]
194  end
195
196  def help_section(), do: :observability_and_health_checks
197
198  def description(), do: "Displays status of a node"
199
200  def banner(_, %{node: node_name}), do: "Status of node #{node_name} ..."
201
202  #
203  # Implementation
204  #
205
206  defp result_map(result) do
207    %{
208      os: os_name(Keyword.get(result, :os)),
209      pid: Keyword.get(result, :pid),
210      product_name: Keyword.get(result, :product_name) |> to_string,
211      product_version: Keyword.get(result, :product_version) |> to_string,
212      rabbitmq_version: Keyword.get(result, :rabbitmq_version) |> to_string,
213      erlang_version: Keyword.get(result, :erlang_version) |> to_string |> String.trim_trailing,
214      uptime: Keyword.get(result, :uptime),
215      is_under_maintenance: Keyword.get(result, :is_under_maintenance, false),
216      processes: Enum.into(Keyword.get(result, :processes), %{}),
217      run_queue: Keyword.get(result, :run_queue),
218      net_ticktime: net_ticktime(result),
219
220      vm_memory_calculation_strategy: Keyword.get(result, :vm_memory_calculation_strategy),
221      vm_memory_high_watermark_setting: Keyword.get(result, :vm_memory_high_watermark) |> formatted_watermark,
222      vm_memory_high_watermark_limit: Keyword.get(result, :vm_memory_limit),
223
224      disk_free_limit: Keyword.get(result, :disk_free_limit),
225      disk_free: Keyword.get(result, :disk_free),
226
227      file_descriptors: Enum.into(Keyword.get(result, :file_descriptors), %{}),
228
229      alarms: Keyword.get(result, :alarms),
230      listeners: listener_maps(Keyword.get(result, :listeners, [])),
231      memory: Keyword.get(result, :memory) |> Enum.into(%{}),
232
233      data_directory: Keyword.get(result, :data_directory) |> to_string,
234      raft_data_directory: Keyword.get(result, :raft_data_directory) |> to_string,
235
236      config_files: Keyword.get(result, :config_files) |> Enum.map(&to_string/1),
237      log_files: Keyword.get(result, :log_files) |> Enum.map(&to_string/1),
238
239      active_plugins: Keyword.get(result, :active_plugins) |> Enum.map(&to_string/1),
240      enabled_plugin_file: Keyword.get(result, :enabled_plugin_file) |> to_string,
241
242      totals: Keyword.get(result, :totals)
243    }
244  end
245
246  defp net_ticktime(result) do
247    case Keyword.get(result, :kernel) do
248      {:net_ticktime, n}   -> n
249      n when is_integer(n) -> n
250      _                    -> :undefined
251    end
252  end
253end
254