Skip to content
This repository was archived by the owner on Nov 17, 2020. It is now read-only.

Add endpoints for memory: absolute and relative #181

Merged
merged 4 commits into from
Apr 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/rabbit_mgmt_dispatcher.erl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ dispatcher() ->
{["cluster-name"], rabbit_mgmt_wm_cluster_name, []},
{["nodes"], rabbit_mgmt_wm_nodes, []},
{["nodes", node], rabbit_mgmt_wm_node, []},
{["nodes", node, "memory"], rabbit_mgmt_wm_node_memory, [absolute]},
{["nodes", node, "memory", "relative"], rabbit_mgmt_wm_node_memory, [relative]},
{["extensions"], rabbit_mgmt_wm_extensions, []},
{["all-configuration"], rabbit_mgmt_wm_definitions, []}, %% This was the old name, let's not break things gratuitously.
{["definitions"], rabbit_mgmt_wm_definitions, []},
Expand Down
92 changes: 92 additions & 0 deletions src/rabbit_mgmt_wm_node_memory.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
%% The contents of this file are subject to the Mozilla Public License
%% Version 1.1 (the "License"); you may not use this file except in
%% compliance with the License. You may obtain a copy of the License at
%% http://www.mozilla.org/MPL/
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
%% License for the specific language governing rights and limitations
%% under the License.
%%
%% The Original Code is RabbitMQ Management Console.
%%
%% The Initial Developer of the Original Code is GoPivotal, Inc.
%% Copyright (c) 2007-2016 Pivotal Software, Inc. All rights reserved.
%%

-module(rabbit_mgmt_wm_node_memory).

-export([init/1, to_json/2, content_types_provided/2, is_authorized/2]).
-export([finish_request/2, allowed_methods/2]).
-export([encodings_provided/2]).
-export([resource_exists/2]).

-include("rabbit_mgmt.hrl").
-include_lib("webmachine/include/webmachine.hrl").
-include_lib("rabbit_common/include/rabbit.hrl").

%%--------------------------------------------------------------------

init([Mode]) -> {ok, {Mode, #context{}}}.

finish_request(ReqData, {Mode, Context}) ->
{ok, rabbit_mgmt_cors:set_headers(ReqData, Context), {Mode, Context}}.

allowed_methods(ReqData, Context) ->
{['HEAD', 'GET', 'OPTIONS'], ReqData, Context}.

content_types_provided(ReqData, Context) ->
{[{"application/json", to_json}], ReqData, Context}.

encodings_provided(ReqData, Context) ->
{[{"identity", fun(X) -> X end},
{"gzip", fun(X) -> zlib:gzip(X) end}], ReqData, Context}.

resource_exists(ReqData, Context) ->
{node_exists(ReqData, get_node(ReqData)), ReqData, Context}.

to_json(ReqData, {Mode, Context}) ->
rabbit_mgmt_util:reply(augment(Mode, ReqData), ReqData, {Mode, Context}).

is_authorized(ReqData, {Mode, Context}) ->
{Res, RD, C} = rabbit_mgmt_util:is_authorized_monitor(ReqData, Context),
{Res, RD, {Mode, C}}.

%%--------------------------------------------------------------------
get_node(ReqData) ->
list_to_atom(binary_to_list(rabbit_mgmt_util:id(node, ReqData))).

node_exists(ReqData, Node) ->
case [N || N <- rabbit_mgmt_wm_nodes:all_nodes(ReqData),
proplists:get_value(name, N) == Node] of
[] -> false;
[_] -> true
end.

augment(Mode, ReqData) ->
Node = get_node(ReqData),
case node_exists(ReqData, Node) of
false ->
not_found;
true ->
case rpc:call(Node, rabbit_vm, memory, [], infinity) of
{badrpc, _} -> [{memory, not_available}];
Result -> [{memory, format(Mode, Result)}]
end
end.

format(absolute, Result) ->
Result;
format(relative, Result) ->
{[{total, Total}], Rest} = lists:splitwith(fun({Key, _}) ->
Key == total
end, Result),
[{total, 100} | [{K, percentage(V, Total)} || {K, V} <- Rest]].

percentage(Part, Total) ->
case round((Part/Total) * 100) of
0 when Part =/= 0 ->
1;
Int ->
Int
end.
36 changes: 35 additions & 1 deletion test/src/rabbit_mgmt_test_http.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

-export([http_get/1, http_put/3, http_delete/2]).

-import(rabbit_mgmt_test_util, [assert_list/2, assert_item/2, test_item/2]).
-import(rabbit_mgmt_test_util, [assert_list/2, assert_item/2, test_item/2,
assert_keys/2]).
-import(rabbit_misc, [pget/2]).

cors_test() ->
Expand Down Expand Up @@ -104,6 +105,39 @@ nodes_test() ->
http_delete("/users/monitor", ?NO_CONTENT),
ok.

memory_test() ->
[Node] = http_get("/nodes"),
Path = "/nodes/" ++ binary_to_list(pget(name, Node)) ++ "/memory",
Result = http_get(Path, ?OK),
assert_keys([memory], Result),
Keys = [total, connection_readers, connection_writers, connection_channels,
connection_other, queue_procs, queue_slave_procs, plugins,
other_proc, mnesia, mgmt_db, msg_index, other_ets, binary, code,
atom, other_system],
assert_keys(Keys, pget(memory, Result)),
http_get("/nodes/nonode/memory", ?NOT_FOUND),
%% Relative memory as a percentage of the total
Result1 = http_get(Path ++ "/relative", ?OK),
assert_keys([memory], Result1),
Breakdown = pget(memory, Result1),
assert_keys(Keys, Breakdown),
assert_item([{total, 100}], Breakdown),
assert_percentage(Breakdown),
http_get("/nodes/nonode/memory/relative", ?NOT_FOUND),
ok.

assert_percentage(Breakdown) ->
Total = lists:sum([P || {K, P} <- Breakdown, K =/= total]),
Count = length(Breakdown) - 1,
%% Rounding up and down can lose some digits. Never more than the number
%% of items in the breakdown.
case ((Total =< 100 + Count) andalso (Total >= 100 - Count)) of
false ->
throw({bad_percentage, Total, Breakdown});
true ->
ok
end.

auth_test() ->
http_put("/users/user", [{password, <<"user">>},
{tags, <<"">>}], [?CREATED, ?NO_CONTENT]),
Expand Down
12 changes: 11 additions & 1 deletion test/src/rabbit_mgmt_test_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

-module(rabbit_mgmt_test_util).

-export([assert_list/2, assert_item/2, test_item/2]).
-export([assert_list/2, assert_item/2, test_item/2, assert_keys/2]).

assert_list(Exp, Act) ->
case length(Exp) == length(Act) of
Expand All @@ -43,3 +43,13 @@ test_item(Exp, Act) ->
test_item0(Exp, Act) ->
[{did_not_find, ExpI, in, Act} || ExpI <- Exp,
not lists:member(ExpI, Act)].

assert_keys(Exp, Act) ->
case test_key0(Exp, Act) of
[] -> ok;
Or -> throw(Or)
end.

test_key0(Exp, Act) ->
[{did_not_find, ExpI, in, Act} || ExpI <- Exp,
not proplists:is_defined(ExpI, Act)].