Skip to content

CLI tools: introduce commands that activate and disactivate free disk space monitoring #8743

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jul 3, 2023
16 changes: 13 additions & 3 deletions deps/rabbit/src/rabbit_disk_monitor.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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}.

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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