/bindings/erl/src/sigar.erl

https://github.com/Kami/sigar · Erlang · 108 lines · 66 code · 21 blank · 21 comment · 0 complexity · 759eeadbb88d6b8bd9bf1319a7f66e7f MD5 · raw file

  1. %
  2. % Copyright (c) 2009 SpringSource, Inc.
  3. % Copyright (c) 2010 VMware, Inc.
  4. %
  5. % Licensed under the Apache License, Version 2.0 (the "License");
  6. % you may not use this file except in compliance with the License.
  7. % You may obtain a copy of the License at
  8. %
  9. % http://www.apache.org/licenses/LICENSE-2.0
  10. %
  11. % Unless required by applicable law or agreed to in writing, software
  12. % distributed under the License is distributed on an "AS IS" BASIS,
  13. % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. % See the License for the specific language governing permissions and
  15. % limitations under the License.
  16. %
  17. -module(sigar).
  18. % generated by SigarWrapper.pm
  19. -include("../priv/gen/sigar.hrl").
  20. -export([start/0, stop/1, get_value/2]).
  21. % handrolled wrappers
  22. -export([loadavg/1,
  23. net_connection_list/2,
  24. net_interface_list/1,
  25. net_route_list/1,
  26. file_system_list/1,
  27. cpu_info_list/1,
  28. arp_list/1,
  29. who_list/1]).
  30. -define(NETCONN_CLIENT, 0x01).
  31. -define(NETCONN_SERVER, 0x02).
  32. -define(SIGAR_NETCONN_TCP, 0x10).
  33. -define(SIGAR_NETCONN_UDP, 0x20).
  34. -define(SIGAR_NETCONN_RAW, 0x40).
  35. -define(SIGAR_NETCONN_UNIX, 0x80).
  36. start() ->
  37. case load_driver() of
  38. ok ->
  39. S = open_port({spawn, 'sigar_drv'}, [binary]),
  40. {ok, {sigar, S}};
  41. {error, Err} ->
  42. Msg = erl_ddll:format_error(Err),
  43. {error, Msg}
  44. end.
  45. % handrolled wrappers
  46. loadavg({sigar, S}) ->
  47. do_command(S, ?LOADAVG).
  48. net_connection_list({sigar, S}, F) ->
  49. do_command(S, ?NET_CONNECTION_LIST, F).
  50. net_interface_list({sigar, S}) ->
  51. do_command(S, ?NET_INTERFACE_LIST).
  52. net_route_list({sigar, S}) ->
  53. do_command(S, ?NET_ROUTE_LIST).
  54. file_system_list({sigar, S}) ->
  55. do_command(S, ?FILE_SYSTEM_LIST).
  56. cpu_info_list({sigar, S}) ->
  57. do_command(S, ?CPU_INFO_LIST).
  58. arp_list({sigar, S}) ->
  59. do_command(S, ?ARP_LIST).
  60. who_list({sigar, S}) ->
  61. do_command(S, ?WHO_LIST).
  62. % generated by SigarWrapper.pm
  63. -include("../priv/gen/sigar_gen.hrl").
  64. % XXX must be a better way
  65. get_value(Key, List) ->
  66. case lists:keysearch(Key,1,List) of
  67. false ->
  68. 1;
  69. { value, {Key, N} } ->
  70. N
  71. end.
  72. stop({sigar, S}) ->
  73. unlink(S),
  74. port_close(S).
  75. load_driver() ->
  76. Dir = filename:join([filename:dirname(code:which(sigar)), "..", "priv"]),
  77. erl_ddll:load(Dir, "sigar_drv").
  78. do_command(S, C) ->
  79. port_command(S, [C]),
  80. receive
  81. {S, {data, Bin}} -> binary_to_term(Bin)
  82. end.
  83. do_command(S, C, A) ->
  84. port_command(S, [C, A]),
  85. receive
  86. {S, {data, Bin}} -> binary_to_term(Bin)
  87. end.