CLI tools: introduce commands that enable and disable free disk space monitoring (#8743)

* Closes #8741

References #8740

---------

Co-authored-by: Michael Davis <mcarsondavis@gmail.com>
This commit is contained in:
Michael Klishin 2023-07-04 00:14:01 +04:00 committed by GitHub
parent 75a953ce28
commit e1a1984293
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 214 additions and 3 deletions

View File

@ -161,16 +161,26 @@ handle_call({set_min_check_interval, MinInterval}, _From, State) ->
handle_call({set_max_check_interval, MaxInterval}, _From, State) ->
{reply, ok, set_max_check_interval(MaxInterval, State)};
handle_call({set_enabled, _Enabled = true}, _From, State) ->
handle_call({set_enabled, _Enabled = true}, _From, State = #state{enabled = true}) ->
_ = start_timer(set_disk_limits(State, State#state.limit)),
rabbit_log:info("Free disk space monitor was enabled"),
rabbit_log:info("Free disk space monitor was already enabled"),
{reply, ok, State#state{enabled = true}};
handle_call({set_enabled, _Enabled = false}, _From, State) ->
handle_call({set_enabled, _Enabled = true}, _From, State = #state{enabled = false}) ->
_ = start_timer(set_disk_limits(State, State#state.limit)),
rabbit_log:info("Free disk space monitor was manually enabled"),
{reply, ok, State#state{enabled = true}};
handle_call({set_enabled, _Enabled = false}, _From, State = #state{enabled = true}) ->
_ = erlang:cancel_timer(State#state.timer),
rabbit_log:info("Free disk space monitor was manually disabled"),
{reply, ok, State#state{enabled = false}};
handle_call({set_enabled, _Enabled = false}, _From, State = #state{enabled = false}) ->
_ = erlang:cancel_timer(State#state.timer),
rabbit_log:info("Free disk space monitor was already disabled"),
{reply, ok, State#state{enabled = false}};
handle_call(_Request, _From, State) ->
{noreply, State}.

View File

@ -0,0 +1,61 @@
## 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-2023 VMware, Inc. or its affiliates. All rights reserved.
defmodule RabbitMQ.CLI.Ctl.Commands.DisableFreeDiskSpaceMonitoringCommand do
alias RabbitMQ.CLI.Core.DocGuide
@behaviour RabbitMQ.CLI.CommandBehaviour
@default_timeout 60_000
def scopes(), do: [:ctl, :diagnostics]
def switches(), do: [unit: :string, timeout: :integer]
def aliases(), do: [t: :timeout]
def merge_defaults(args, opts) do
timeout =
case opts[:timeout] do
nil -> @default_timeout
:infinity -> @default_timeout
other -> other
end
{args, Map.merge(%{timeout: timeout}, opts)}
end
def validate(args, _) when length(args) > 0 do
{:validation_failure, :too_many_args}
end
def validate(_, _), do: :ok
use RabbitMQ.CLI.Core.RequiresRabbitAppRunning
def run([], %{node: node_name, timeout: timeout}) do
:rabbit_misc.rpc_call(node_name, :rabbit_disk_monitor, :set_enabled, [false], timeout)
end
use RabbitMQ.CLI.DefaultOutput
def formatter(), do: RabbitMQ.CLI.Formatters.String
def usage, do: "disable_free_disk_space_monitoring"
def usage_doc_guides() do
[
DocGuide.alarms(),
DocGuide.monitoring()
]
end
def help_section(), do: :observability_and_health_checks
def description(), do: "Disables free disk space monitoring on a node"
def banner(_, %{node: node_name}),
do: "Disabling free disk space monitoring on node #{node_name}..."
end

View File

@ -0,0 +1,61 @@
## 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-2023 VMware, Inc. or its affiliates. All rights reserved.
defmodule RabbitMQ.CLI.Ctl.Commands.EnableFreeDiskSpaceMonitoringCommand do
alias RabbitMQ.CLI.Core.DocGuide
@behaviour RabbitMQ.CLI.CommandBehaviour
@default_timeout 60_000
def scopes(), do: [:ctl, :diagnostics]
def switches(), do: [unit: :string, timeout: :integer]
def aliases(), do: [t: :timeout]
def merge_defaults(args, opts) do
timeout =
case opts[:timeout] do
nil -> @default_timeout
:infinity -> @default_timeout
other -> other
end
{args, Map.merge(%{timeout: timeout}, opts)}
end
def validate(args, _) when length(args) > 0 do
{:validation_failure, :too_many_args}
end
def validate(_, _), do: :ok
use RabbitMQ.CLI.Core.RequiresRabbitAppRunning
def run([], %{node: node_name, timeout: timeout}) do
:rabbit_misc.rpc_call(node_name, :rabbit_disk_monitor, :set_enabled, [true], timeout)
end
use RabbitMQ.CLI.DefaultOutput
def formatter(), do: RabbitMQ.CLI.Formatters.String
def usage, do: "enable_free_disk_space_monitoring"
def usage_doc_guides() do
[
DocGuide.alarms(),
DocGuide.monitoring()
]
end
def help_section(), do: :observability_and_health_checks
def description(), do: "[Re-]enables free disk space monitoring on a node"
def banner(_, %{node: node_name}),
do: "[Re-]enabling free disk space monitoring on node #{node_name}..."
end

View File

@ -0,0 +1,39 @@
## 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-2023 VMware, Inc. or its affiliates. All rights reserved.
defmodule DisableDiskFreeSpaceMonitoringCommandTest do
use ExUnit.Case, async: false
import TestHelper
@command RabbitMQ.CLI.Ctl.Commands.DisableFreeDiskSpaceMonitoringCommand
setup_all do
RabbitMQ.CLI.Core.Distribution.start()
:ok
end
setup do
{:ok, opts: %{node: get_rabbit_hostname(), timeout: 60_000}}
end
test "validate: with extra arguments returns an arg count error", context do
assert @command.validate(["extra"], context[:opts]) == {:validation_failure, :too_many_args}
end
test "run: request to a named, active node succeeds", context do
assert @command.run([], context[:opts])
end
test "run: request to a non-existent node returns a badrpc" do
opts = %{node: :jake@thedog, timeout: 200}
assert match?({:badrpc, _}, @command.run([], opts))
end
test "banner", context do
assert @command.banner([], context[:opts]) =~ ~r/Disabling free disk space monitoring on node/
end
end

View File

@ -0,0 +1,40 @@
## 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-2023 VMware, Inc. or its affiliates. All rights reserved.
defmodule EnableDiskFreeSpaceMonitoringCommandTest do
use ExUnit.Case, async: false
import TestHelper
@command RabbitMQ.CLI.Ctl.Commands.EnableFreeDiskSpaceMonitoringCommand
setup_all do
RabbitMQ.CLI.Core.Distribution.start()
:ok
end
setup do
{:ok, opts: %{node: get_rabbit_hostname(), timeout: 60_000}}
end
test "validate: with extra arguments returns an arg count error", context do
assert @command.validate(["extra"], context[:opts]) == {:validation_failure, :too_many_args}
end
test "run: request to a named, active node succeeds", context do
assert @command.run([], context[:opts])
end
test "run: request to a non-existent node returns a badrpc" do
opts = %{node: :jake@thedog, timeout: 200}
assert match?({:badrpc, _}, @command.run([], opts))
end
test "banner", context do
assert @command.banner([], context[:opts]) =~
~r/\[Re\-\]enabling free disk space monitoring on node/
end
end