summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarek Majkowski <majek@lshift.net>2009-10-29 13:06:02 +0000
committerMarek Majkowski <majek@lshift.net>2009-10-29 13:06:02 +0000
commit89f5ec33cac5bcea68f87db108da1213ad6c819d (patch)
tree75904c5236d7d7d742a88f78c9f66fde870eb828
parent7c4dbbf42891f0d685b623ff4142131c3584ac0f (diff)
downloadrabbitmq-server-89f5ec33cac5bcea68f87db108da1213ad6c819d.tar.gz
QA: yes, that's better error handling...
-rw-r--r--src/vm_memory_monitor.erl26
1 files changed, 10 insertions, 16 deletions
diff --git a/src/vm_memory_monitor.erl b/src/vm_memory_monitor.erl
index 4e33c025..9acf1354 100644
--- a/src/vm_memory_monitor.erl
+++ b/src/vm_memory_monitor.erl
@@ -195,17 +195,20 @@ get_mem_limit(MemFraction, TotalMemory) ->
%%----------------------------------------------------------------------------
%% Internal Helpers
%%----------------------------------------------------------------------------
+cmd(Command) ->
+ [Exec| _Rest] = string:tokens(Command, " "),
+ case os:find_executable(Exec) of
+ false -> throw({command_not_found, Exec});
+ _ -> ok
+ end,
+ os:cmd(Command).
%% get_total_memory(OS) -> Total
%% Windows and Freebsd code based on: memsup:get_memory_usage/1
%% Original code was part of OTP and released under "Erlang Public License".
get_total_memory({unix,darwin}) ->
- File = os:cmd("/usr/bin/vm_stat"),
- case string:str(File, " not found\n") of
- 0 -> ok;
- _ -> throw({command_not_found, "/usr/bin/vm_stat"})
- end,
+ File = cmd("/usr/bin/vm_stat"),
Lines = string:tokens(File, "\n"),
Dict = dict:from_list(lists:map(fun parse_line_mach/1, Lines)),
[PageSize, Inactive, Active, Free, Wired] =
@@ -233,11 +236,7 @@ get_total_memory({unix, linux}) ->
dict:fetch('MemTotal', Dict);
get_total_memory({unix, sunos}) ->
- File = os:cmd("/usr/sbin/prtconf"),
- case string:str(File, "No such file or directory\n") of
- 0 -> ok;
- _ -> throw({command_not_found, "/usr/sbin/prtconf"})
- end,
+ File = cmd("/usr/sbin/prtconf"),
Lines = string:tokens(File, "\n"),
Dict = dict:from_list(lists:map(fun parse_line_sunos/1, Lines)),
dict:fetch('Memory size', Dict);
@@ -288,12 +287,7 @@ parse_line_sunos(Line) ->
end.
freebsd_sysctl(Def) ->
- Result = os:cmd("/sbin/sysctl -n " ++ Def),
- case string:str(Result, " not found\n") of
- 0 -> ok;
- _ -> throw({command_not_found, "/sbin/sysctl"})
- end,
- list_to_integer(Result -- "\n").
+ list_to_integer(cmd("/sbin/sysctl -n " ++ Def) -- "\n").
%% file:read_file does not work on files in /proc as it seems to get
%% the size of the file first and then read that many bytes. But files