/src/ssim_agentmon.erl

https://github.com/fygrave/ssim · Erlang · 186 lines · 71 code · 25 blank · 90 comment · 0 complexity · cf7202fe23702e22c19e7c83ae67f762 MD5 · raw file

  1. %%%-------------------------------------------------------------------
  2. %%% @author fygrave <fygrave@o0o.nu>
  3. %%% @copyright (C) 2010,2011, fygrave
  4. %%% @doc
  5. %%%
  6. %%% @end
  7. %%% Created : 7 Aug 2010 by fygrave <fygrave@o0o.nu>
  8. %%%-------------------------------------------------------------------
  9. -module(ssim_agentmon).
  10. -behaviour(gen_server).
  11. %% API
  12. -export([start_link/0,
  13. check_status/0,
  14. statistics/0
  15. ]).
  16. %% gen_server callbacks
  17. -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
  18. terminate/2, code_change/3]).
  19. -define(SERVER, ?MODULE).
  20. -define(AGENT_KEY, <<"agent">>).
  21. -record(state, {riaksock,
  22. bucket
  23. }).
  24. -include("deps/amqp_client/include/amqp_client.hrl").
  25. %%%===================================================================
  26. %%% API
  27. %%%===================================================================
  28. %%--------------------------------------------------------------------
  29. %% @doc
  30. %% Starts the server
  31. %%
  32. %% @spec start_link() -> {ok, Pid} | ignore | {error, Error}
  33. %% @end
  34. %%--------------------------------------------------------------------
  35. start_link() ->
  36. gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
  37. check_status() ->
  38. gen_server:call(?MODULE, {check_status}).
  39. statistics() ->
  40. gen_server:call(?MODULE, {statistics}).
  41. %%%===================================================================
  42. %%% gen_server callbacks
  43. %%%===================================================================
  44. %%--------------------------------------------------------------------
  45. %% @private
  46. %% @doc
  47. %% Initializes the server
  48. %%
  49. %% @spec init(Args) -> {ok, State} |
  50. %% {ok, State, Timeout} |
  51. %% ignore |
  52. %% {stop, Reason}
  53. %% @end
  54. %%--------------------------------------------------------------------
  55. init([]) ->
  56. io:format("Starting SSIM AgentMon~n"),
  57. {ok, Params} = ssim_util:get_env(riakc_params),
  58. {ok, SPid} = riakc_pb_socket:start_link(proplists:get_value(db_hostname, Params),
  59. proplists:get_value(db_port, Params)),
  60. State = #state{riaksock = SPid, bucket = proplists:get_value(status_bucket, Params)},
  61. Rez = ssim_mq:subscribe_queue(?AGENT_KEY),
  62. {ok, State}.
  63. %%--------------------------------------------------------------------
  64. %% @private
  65. %% @doc
  66. %% Handling call messages
  67. %%
  68. %% @spec handle_call(Request, From, State) ->
  69. %% {reply, Reply, State} |
  70. %% {reply, Reply, State, Timeout} |
  71. %% {noreply, State} |
  72. %% {noreply, State, Timeout} |
  73. %% {stop, Reason, Reply, State} |
  74. %% {stop, Reason, State}
  75. %% @end
  76. %%--------------------------------------------------------------------
  77. handle_call({statistics}, _From, State) ->
  78. {ok, State};
  79. handle_call({check_status}, _From, State) ->
  80. {ok, State};
  81. handle_call(_Request, _From, State) ->
  82. Reply = ok,
  83. {reply, Reply, State}.
  84. %%--------------------------------------------------------------------
  85. %% @private
  86. %% @doc
  87. %% Handling cast messages
  88. %%
  89. %% @spec handle_cast(Msg, State) -> {noreply, State} |
  90. %% {noreply, State, Timeout} |
  91. %% {stop, Reason, State}
  92. %% @end
  93. %%--------------------------------------------------------------------
  94. handle_cast({up, _Node, _Services}, State) ->
  95. io:format("Cast node up~n"),
  96. {noreply, State};
  97. handle_cast({#'basic.consume_ok'{}, Tag}, State) ->
  98. io:format("Got basic consume~n"),
  99. {noreply, State};
  100. handle_cast({down, _Node}, State)->
  101. io:format("Cast node down~n"),
  102. {noreply, State};
  103. handle_cast(_Msg, State) ->
  104. {noreply, State}.
  105. %%--------------------------------------------------------------------
  106. %% @private
  107. %% @doc
  108. %% Handling all non call/cast messages
  109. %%
  110. %% @spec handle_info(Info, State) -> {noreply, State} |
  111. %% {noreply, State, Timeout} |
  112. %% {stop, Reason, State}
  113. %% @end
  114. %%--------------------------------------------------------------------
  115. handle_info ({nodeup, _Node}, State) ->
  116. io:format("info: Node up~n"),
  117. {noreply, State};
  118. handle_info({#'basic.deliver'{}, #amqp_msg{payload = Body}},State) ->
  119. store_message(binary_to_term(Body), State#state.riaksock, State#state.bucket),
  120. {noreply, State};
  121. handle_info ({nodedown, _Node}, State) ->
  122. io:format("info: Node down~n"),
  123. {noreply, State};
  124. handle_info({#'basic.consume_ok'{}, Tag}, State)->
  125. io:format("Basic consume ok ~p~n", [ Tag]),
  126. {noreply, State};
  127. handle_info(_Info, State) ->
  128. io:format("Info ~p~n",[_Info]),
  129. {noreply, State}.
  130. %%--------------------------------------------------------------------
  131. %% @private
  132. %% @doc
  133. %% This function is called by a gen_server when it is about to
  134. %% terminate. It should be the opposite of Module:init/1 and do any
  135. %% necessary cleaning up. When it returns, the gen_server terminates
  136. %% with Reason. The return value is ignored.
  137. %%
  138. %% @spec terminate(Reason, State) -> void()
  139. %% @end
  140. %%--------------------------------------------------------------------
  141. terminate(_Reason, _State) ->
  142. ok.
  143. %%--------------------------------------------------------------------
  144. %% @private
  145. %% @doc
  146. %% Convert process state when code is changed
  147. %%
  148. %% @spec code_change(OldVsn, State, Extra) -> {ok, NewState}
  149. %% @end
  150. %%--------------------------------------------------------------------
  151. code_change(_OldVsn, State, _Extra) ->
  152. {ok, State}.
  153. %%%===================================================================
  154. %%% Internal functions
  155. %%%===================================================================
  156. store_message(Message, RiakPid, Bucket) ->
  157. io:format("Agent message ~p~n", [ Message ]),
  158. {struct, S} = Message,
  159. Key = ssim_util:always_binary(proplists:get_value(source, S)),
  160. Object = riakc_obj:new(Bucket, Key, iolist_to_binary(mochijson2:encode(Message)), <<"application/json">>),
  161. riakc_pb_socket:put(RiakPid, Object).