summaryrefslogtreecommitdiff
path: root/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/report_command.ex
blob: c06497a7e6f2a7554a7492c0e16be26fb3eacba2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
## This Source Code Form is subject to the terms of the Mozilla Public
## License, v. 2.0. If a copy of the MPL was not distributed with this
## file, You can obtain one at https://mozilla.org/MPL/2.0/.
##
## Copyright (c) 2007-2020 VMware, Inc. or its affiliates.  All rights reserved.

defmodule RabbitMQ.CLI.Ctl.Commands.ReportCommand do
  alias RabbitMQ.CLI.Core.DocGuide
  alias RabbitMQ.CLI.Ctl.Commands.{
    ClusterStatusCommand,
    EnvironmentCommand,
    ListBindingsCommand,
    ListChannelsCommand,
    ListConnectionsCommand,
    ListExchangesCommand,
    ListGlobalParametersCommand,
    ListParametersCommand,
    ListPermissionsCommand,
    ListPoliciesCommand,
    ListQueuesCommand,
    StatusCommand
  }
  alias RabbitMQ.CLI.Diagnostics.Commands.{
    CommandLineArgumentsCommand,
    OsEnvCommand
  }

  @behaviour RabbitMQ.CLI.CommandBehaviour

  def scopes(), do: [:ctl, :diagnostics]

  use RabbitMQ.CLI.Core.MergesNoDefaults

  def validate([_ | _] = args, _) when length(args) != 0,
    do: {:validation_failure, :too_many_args}

  def validate([], %{formatter: formatter}) do
    case formatter do
      "report" -> :ok
      _other -> {:validation_failure, "Only report formatter is supported"}
    end
  end

  def validate([], _), do: :ok

  use RabbitMQ.CLI.Core.RequiresRabbitAppRunning

  def run([], %{node: node_name} = opts) do
    case :rabbit_misc.rpc_call(node_name, :rabbit_vhost, :list_names, []) do
      {:badrpc, _} = err ->
        err

      vhosts ->
        data = [
          run_command(StatusCommand, [], opts),
          run_command(ClusterStatusCommand, [], opts),
          run_command(EnvironmentCommand, [], opts),
          run_command(ListConnectionsCommand, info_keys(ListConnectionsCommand), opts),
          run_command(ListChannelsCommand, info_keys(ListChannelsCommand), opts),
          run_command(CommandLineArgumentsCommand, [], opts),
          run_command(OsEnvCommand, [], opts)
        ]

        vhost_data =
          vhosts
          |> Enum.flat_map(fn v ->
            opts = Map.put(opts, :vhost, v)

            [
              run_command(ListQueuesCommand, info_keys(ListQueuesCommand), opts),
              run_command(ListExchangesCommand, info_keys(ListExchangesCommand), opts),
              run_command(ListBindingsCommand, info_keys(ListBindingsCommand), opts),
              run_command(ListPermissionsCommand, [], opts),
              run_command(ListPoliciesCommand, [], opts),
              run_command(ListGlobalParametersCommand, [], opts),
              run_command(ListParametersCommand, [], opts),

            ]
          end)

        data ++ vhost_data
    end
  end

  use RabbitMQ.CLI.DefaultOutput

  def formatter(), do: RabbitMQ.CLI.Formatters.Report

  def usage, do: "report"

  def usage_doc_guides() do
    [
      DocGuide.monitoring()
    ]
  end

  def help_section(), do: :observability_and_health_checks

  def description(), do: "Generate a server status report containing a concatenation of all server status information for support purposes"

  def banner(_, %{node: node_name}), do: "Reporting server status of node #{node_name} ..."

  #
  # Implementation
  #

  defp run_command(command, args, opts) do
    {args, opts} = command.merge_defaults(args, opts)
    banner = command.banner(args, opts)
    command_result = command.run(args, opts) |> command.output(opts)
    {command, banner, command_result}
  end

  defp info_keys(command) do
    command.info_keys()
    |> Enum.map(&to_string/1)
  end
end