|
| 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([Mode]) -> {ok, {Mode, #context{}}}. |
| 31 | + |
| 32 | +finish_request(ReqData, {Mode, Context}) -> |
| 33 | + {ok, rabbit_mgmt_cors:set_headers(ReqData, Context), {Mode, 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, {Mode, Context}) -> |
| 49 | + rabbit_mgmt_util:reply(augment(Mode, ReqData), ReqData, {Mode, Context}). |
| 50 | + |
| 51 | +is_authorized(ReqData, {Mode, Context}) -> |
| 52 | + {Res, RD, C} = rabbit_mgmt_util:is_authorized_monitor(ReqData, Context), |
| 53 | + {Res, RD, {Mode, C}}. |
| 54 | + |
| 55 | +%%-------------------------------------------------------------------- |
| 56 | +get_node(ReqData) -> |
| 57 | + list_to_atom(binary_to_list(rabbit_mgmt_util:id(node, ReqData))). |
| 58 | + |
| 59 | +node_exists(ReqData, Node) -> |
| 60 | + case [N || N <- rabbit_mgmt_wm_nodes:all_nodes(ReqData), |
| 61 | + proplists:get_value(name, N) == Node] of |
| 62 | + [] -> false; |
| 63 | + [_] -> true |
| 64 | + end. |
| 65 | + |
| 66 | +augment(Mode, ReqData) -> |
| 67 | + Node = get_node(ReqData), |
| 68 | + case node_exists(ReqData, Node) of |
| 69 | + false -> |
| 70 | + not_found; |
| 71 | + true -> |
| 72 | + case rpc:call(Node, rabbit_vm, memory, [], infinity) of |
| 73 | + {badrpc, _} -> [{memory, not_available}]; |
| 74 | + Result -> [{memory, format(Mode, Result)}] |
| 75 | + end |
| 76 | + end. |
| 77 | + |
| 78 | +format(absolute, Result) -> |
| 79 | + Result; |
| 80 | +format(relative, Result) -> |
| 81 | + {[{total, Total}], Rest} = lists:splitwith(fun({Key, _}) -> |
| 82 | + Key == total |
| 83 | + end, Result), |
| 84 | + [{total, 100} | [{K, percentage(V, Total)} || {K, V} <- Rest]]. |
| 85 | + |
| 86 | +percentage(Part, Total) -> |
| 87 | + case round((Part/Total) * 100) of |
| 88 | + 0 when Part =/= 0 -> |
| 89 | + 1; |
| 90 | + Int -> |
| 91 | + Int |
| 92 | + end. |
0 commit comments