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

Commit 4ffdb3f

Browse files
author
Diana Corbacho
committed
New memory endpoint
1 parent b2f8f3c commit 4ffdb3f

File tree

4 files changed

+102
-2
lines changed

4 files changed

+102
-2
lines changed

src/rabbit_mgmt_dispatcher.erl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ dispatcher() ->
4141
{["cluster-name"], rabbit_mgmt_wm_cluster_name, []},
4242
{["nodes"], rabbit_mgmt_wm_nodes, []},
4343
{["nodes", node], rabbit_mgmt_wm_node, []},
44+
{["nodes", node, "memory"], rabbit_mgmt_wm_node_memory, []},
4445
{["extensions"], rabbit_mgmt_wm_extensions, []},
4546
{["all-configuration"], rabbit_mgmt_wm_definitions, []}, %% This was the old name, let's not break things gratuitously.
4647
{["definitions"], rabbit_mgmt_wm_definitions, []},

src/rabbit_mgmt_wm_node_memory.erl

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
%% The contents of this file are subject to the Mozilla Public License
2+
%% Version 1.1 (the "License"); you may not use this file except in
3+
%% compliance with the License. You may obtain a copy of the License at
4+
%% http://www.mozilla.org/MPL/
5+
%%
6+
%% Software distributed under the License is distributed on an "AS IS"
7+
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
8+
%% License for the specific language governing rights and limitations
9+
%% under the License.
10+
%%
11+
%% The Original Code is RabbitMQ Management Console.
12+
%%
13+
%% The Initial Developer of the Original Code is GoPivotal, Inc.
14+
%% Copyright (c) 2007-2016 Pivotal Software, Inc. All rights reserved.
15+
%%
16+
17+
-module(rabbit_mgmt_wm_node_memory).
18+
19+
-export([init/1, to_json/2, content_types_provided/2, is_authorized/2]).
20+
-export([finish_request/2, allowed_methods/2]).
21+
-export([encodings_provided/2]).
22+
-export([resource_exists/2]).
23+
24+
-include("rabbit_mgmt.hrl").
25+
-include_lib("webmachine/include/webmachine.hrl").
26+
-include_lib("rabbit_common/include/rabbit.hrl").
27+
28+
%%--------------------------------------------------------------------
29+
30+
init(_Config) -> {ok, #context{}}.
31+
32+
finish_request(ReqData, Context) ->
33+
{ok, rabbit_mgmt_cors:set_headers(ReqData, Context), Context}.
34+
35+
allowed_methods(ReqData, Context) ->
36+
{['HEAD', 'GET', 'OPTIONS'], ReqData, Context}.
37+
38+
content_types_provided(ReqData, Context) ->
39+
{[{"application/json", to_json}], ReqData, Context}.
40+
41+
encodings_provided(ReqData, Context) ->
42+
{[{"identity", fun(X) -> X end},
43+
{"gzip", fun(X) -> zlib:gzip(X) end}], ReqData, Context}.
44+
45+
resource_exists(ReqData, Context) ->
46+
{node_exists(ReqData, get_node(ReqData)), ReqData, Context}.
47+
48+
to_json(ReqData, Context) ->
49+
rabbit_mgmt_util:reply(augment(ReqData), ReqData, Context).
50+
51+
is_authorized(ReqData, Context) ->
52+
rabbit_mgmt_util:is_authorized_monitor(ReqData, Context).
53+
54+
%%--------------------------------------------------------------------
55+
get_node(ReqData) ->
56+
list_to_atom(binary_to_list(rabbit_mgmt_util:id(node, ReqData))).
57+
58+
node_exists(ReqData, Node) ->
59+
case [N || N <- rabbit_mgmt_wm_nodes:all_nodes(ReqData),
60+
proplists:get_value(name, N) == Node] of
61+
[] -> false;
62+
[_] -> true
63+
end.
64+
65+
augment(ReqData) ->
66+
Node = get_node(ReqData),
67+
case node_exists(ReqData, Node) of
68+
false ->
69+
not_found;
70+
true ->
71+
case rpc:call(Node, rabbit_vm, memory, [], infinity) of
72+
{badrpc, _} -> [{memory, not_available}];
73+
Result -> [{memory, Result}]
74+
end
75+
end.

test/src/rabbit_mgmt_test_http.erl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020

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

23-
-import(rabbit_mgmt_test_util, [assert_list/2, assert_item/2, test_item/2]).
23+
-import(rabbit_mgmt_test_util, [assert_list/2, assert_item/2, test_item/2,
24+
assert_keys/2]).
2425
-import(rabbit_misc, [pget/2]).
2526

2627
cors_test() ->
@@ -104,6 +105,19 @@ nodes_test() ->
104105
http_delete("/users/monitor", ?NO_CONTENT),
105106
ok.
106107

108+
memory_test() ->
109+
[Node] = http_get("/nodes"),
110+
Path = "/nodes/" ++ binary_to_list(pget(name, Node)) ++ "/memory",
111+
Result = http_get(Path, ?OK),
112+
assert_keys([memory], Result),
113+
Keys = [total, connection_readers, connection_writers, connection_channels,
114+
connection_other, queue_procs, queue_slave_procs, plugins,
115+
other_proc, mnesia, mgmt_db, msg_index, other_ets, binary, code,
116+
atom, other_system],
117+
assert_keys(Keys, pget(memory, Result)),
118+
http_get("/nodes/nonode/memory", ?NOT_FOUND),
119+
ok.
120+
107121
auth_test() ->
108122
http_put("/users/user", [{password, <<"user">>},
109123
{tags, <<"">>}], [?CREATED, ?NO_CONTENT]),

test/src/rabbit_mgmt_test_util.erl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
-module(rabbit_mgmt_test_util).
1818

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

2121
assert_list(Exp, Act) ->
2222
case length(Exp) == length(Act) of
@@ -43,3 +43,13 @@ test_item(Exp, Act) ->
4343
test_item0(Exp, Act) ->
4444
[{did_not_find, ExpI, in, Act} || ExpI <- Exp,
4545
not lists:member(ExpI, Act)].
46+
47+
assert_keys(Exp, Act) ->
48+
case test_key0(Exp, Act) of
49+
[] -> ok;
50+
Or -> throw(Or)
51+
end.
52+
53+
test_key0(Exp, Act) ->
54+
[{did_not_find, ExpI, in, Act} || ExpI <- Exp,
55+
not proplists:is_defined(ExpI, Act)].

0 commit comments

Comments
 (0)