Skip to content

Commit e1a1984

Browse files
CLI tools: introduce commands that enable and disable free disk space monitoring (#8743)
* Closes #8741 References #8740 --------- Co-authored-by: Michael Davis <[email protected]>
1 parent 75a953c commit e1a1984

File tree

5 files changed

+214
-3
lines changed

5 files changed

+214
-3
lines changed

deps/rabbit/src/rabbit_disk_monitor.erl

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,26 @@ handle_call({set_min_check_interval, MinInterval}, _From, State) ->
161161
handle_call({set_max_check_interval, MaxInterval}, _From, State) ->
162162
{reply, ok, set_max_check_interval(MaxInterval, State)};
163163

164-
handle_call({set_enabled, _Enabled = true}, _From, State) ->
164+
handle_call({set_enabled, _Enabled = true}, _From, State = #state{enabled = true}) ->
165165
_ = start_timer(set_disk_limits(State, State#state.limit)),
166-
rabbit_log:info("Free disk space monitor was enabled"),
166+
rabbit_log:info("Free disk space monitor was already enabled"),
167167
{reply, ok, State#state{enabled = true}};
168168

169-
handle_call({set_enabled, _Enabled = false}, _From, State) ->
169+
handle_call({set_enabled, _Enabled = true}, _From, State = #state{enabled = false}) ->
170+
_ = start_timer(set_disk_limits(State, State#state.limit)),
171+
rabbit_log:info("Free disk space monitor was manually enabled"),
172+
{reply, ok, State#state{enabled = true}};
173+
174+
handle_call({set_enabled, _Enabled = false}, _From, State = #state{enabled = true}) ->
170175
_ = erlang:cancel_timer(State#state.timer),
171176
rabbit_log:info("Free disk space monitor was manually disabled"),
172177
{reply, ok, State#state{enabled = false}};
173178

179+
handle_call({set_enabled, _Enabled = false}, _From, State = #state{enabled = false}) ->
180+
_ = erlang:cancel_timer(State#state.timer),
181+
rabbit_log:info("Free disk space monitor was already disabled"),
182+
{reply, ok, State#state{enabled = false}};
183+
174184
handle_call(_Request, _From, State) ->
175185
{noreply, State}.
176186

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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-2023 VMware, Inc. or its affiliates. All rights reserved.
6+
7+
defmodule RabbitMQ.CLI.Ctl.Commands.DisableFreeDiskSpaceMonitoringCommand do
8+
alias RabbitMQ.CLI.Core.DocGuide
9+
10+
@behaviour RabbitMQ.CLI.CommandBehaviour
11+
12+
@default_timeout 60_000
13+
14+
def scopes(), do: [:ctl, :diagnostics]
15+
16+
def switches(), do: [unit: :string, timeout: :integer]
17+
def aliases(), do: [t: :timeout]
18+
19+
def merge_defaults(args, opts) do
20+
timeout =
21+
case opts[:timeout] do
22+
nil -> @default_timeout
23+
:infinity -> @default_timeout
24+
other -> other
25+
end
26+
27+
{args, Map.merge(%{timeout: timeout}, opts)}
28+
end
29+
30+
def validate(args, _) when length(args) > 0 do
31+
{:validation_failure, :too_many_args}
32+
end
33+
34+
def validate(_, _), do: :ok
35+
36+
use RabbitMQ.CLI.Core.RequiresRabbitAppRunning
37+
38+
def run([], %{node: node_name, timeout: timeout}) do
39+
:rabbit_misc.rpc_call(node_name, :rabbit_disk_monitor, :set_enabled, [false], timeout)
40+
end
41+
42+
use RabbitMQ.CLI.DefaultOutput
43+
44+
def formatter(), do: RabbitMQ.CLI.Formatters.String
45+
46+
def usage, do: "disable_free_disk_space_monitoring"
47+
48+
def usage_doc_guides() do
49+
[
50+
DocGuide.alarms(),
51+
DocGuide.monitoring()
52+
]
53+
end
54+
55+
def help_section(), do: :observability_and_health_checks
56+
57+
def description(), do: "Disables free disk space monitoring on a node"
58+
59+
def banner(_, %{node: node_name}),
60+
do: "Disabling free disk space monitoring on node #{node_name}..."
61+
end
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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-2023 VMware, Inc. or its affiliates. All rights reserved.
6+
7+
defmodule RabbitMQ.CLI.Ctl.Commands.EnableFreeDiskSpaceMonitoringCommand do
8+
alias RabbitMQ.CLI.Core.DocGuide
9+
10+
@behaviour RabbitMQ.CLI.CommandBehaviour
11+
12+
@default_timeout 60_000
13+
14+
def scopes(), do: [:ctl, :diagnostics]
15+
16+
def switches(), do: [unit: :string, timeout: :integer]
17+
def aliases(), do: [t: :timeout]
18+
19+
def merge_defaults(args, opts) do
20+
timeout =
21+
case opts[:timeout] do
22+
nil -> @default_timeout
23+
:infinity -> @default_timeout
24+
other -> other
25+
end
26+
27+
{args, Map.merge(%{timeout: timeout}, opts)}
28+
end
29+
30+
def validate(args, _) when length(args) > 0 do
31+
{:validation_failure, :too_many_args}
32+
end
33+
34+
def validate(_, _), do: :ok
35+
36+
use RabbitMQ.CLI.Core.RequiresRabbitAppRunning
37+
38+
def run([], %{node: node_name, timeout: timeout}) do
39+
:rabbit_misc.rpc_call(node_name, :rabbit_disk_monitor, :set_enabled, [true], timeout)
40+
end
41+
42+
use RabbitMQ.CLI.DefaultOutput
43+
44+
def formatter(), do: RabbitMQ.CLI.Formatters.String
45+
46+
def usage, do: "enable_free_disk_space_monitoring"
47+
48+
def usage_doc_guides() do
49+
[
50+
DocGuide.alarms(),
51+
DocGuide.monitoring()
52+
]
53+
end
54+
55+
def help_section(), do: :observability_and_health_checks
56+
57+
def description(), do: "[Re-]enables free disk space monitoring on a node"
58+
59+
def banner(_, %{node: node_name}),
60+
do: "[Re-]enabling free disk space monitoring on node #{node_name}..."
61+
end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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-2023 VMware, Inc. or its affiliates. All rights reserved.
6+
7+
defmodule DisableDiskFreeSpaceMonitoringCommandTest do
8+
use ExUnit.Case, async: false
9+
import TestHelper
10+
11+
@command RabbitMQ.CLI.Ctl.Commands.DisableFreeDiskSpaceMonitoringCommand
12+
13+
setup_all do
14+
RabbitMQ.CLI.Core.Distribution.start()
15+
16+
:ok
17+
end
18+
19+
setup do
20+
{:ok, opts: %{node: get_rabbit_hostname(), timeout: 60_000}}
21+
end
22+
23+
test "validate: with extra arguments returns an arg count error", context do
24+
assert @command.validate(["extra"], context[:opts]) == {:validation_failure, :too_many_args}
25+
end
26+
27+
test "run: request to a named, active node succeeds", context do
28+
assert @command.run([], context[:opts])
29+
end
30+
31+
test "run: request to a non-existent node returns a badrpc" do
32+
opts = %{node: :jake@thedog, timeout: 200}
33+
assert match?({:badrpc, _}, @command.run([], opts))
34+
end
35+
36+
test "banner", context do
37+
assert @command.banner([], context[:opts]) =~ ~r/Disabling free disk space monitoring on node/
38+
end
39+
end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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-2023 VMware, Inc. or its affiliates. All rights reserved.
6+
7+
defmodule EnableDiskFreeSpaceMonitoringCommandTest do
8+
use ExUnit.Case, async: false
9+
import TestHelper
10+
11+
@command RabbitMQ.CLI.Ctl.Commands.EnableFreeDiskSpaceMonitoringCommand
12+
13+
setup_all do
14+
RabbitMQ.CLI.Core.Distribution.start()
15+
16+
:ok
17+
end
18+
19+
setup do
20+
{:ok, opts: %{node: get_rabbit_hostname(), timeout: 60_000}}
21+
end
22+
23+
test "validate: with extra arguments returns an arg count error", context do
24+
assert @command.validate(["extra"], context[:opts]) == {:validation_failure, :too_many_args}
25+
end
26+
27+
test "run: request to a named, active node succeeds", context do
28+
assert @command.run([], context[:opts])
29+
end
30+
31+
test "run: request to a non-existent node returns a badrpc" do
32+
opts = %{node: :jake@thedog, timeout: 200}
33+
assert match?({:badrpc, _}, @command.run([], opts))
34+
end
35+
36+
test "banner", context do
37+
assert @command.banner([], context[:opts]) =~
38+
~r/\[Re\-\]enabling free disk space monitoring on node/
39+
end
40+
end

0 commit comments

Comments
 (0)