Last active
January 17, 2018 17:37
-
-
Save lukebakken/ebb3af04ac94145f292350e7e5fd1177 to your computer and use it in GitHub Desktop.
Ping Riak nodes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(pinger). | |
-export([ping/1, | |
ping/3, | |
pinglist/4, | |
pingall/0, | |
pingall/1, | |
pingall/2, | |
ts_to_string/1 | |
]). | |
ts_to_string(TS) -> | |
{{Y, Mon, D}, {H, Min, S}} = calendar:now_to_datetime(TS), | |
io_lib:format("~p-~p-~p ~p:~p:~p", [Y, Mon, D, H, Min, S]). | |
ping(Node) -> | |
Start = now(), | |
Response = net_adm:ping(Node), | |
End = now(), | |
Elapsed = timer:now_diff(End, Start), | |
io:format("ping ~p responded ~p in ~b usec (s: ~s e: ~s)~n", [Node, Response, Elapsed, | |
ts_to_string(Start), | |
ts_to_string(End)]), | |
{Response, Elapsed}. | |
ping(PPid, Node, {Good0, Bad0, Min0, Max0, Average0, Count}) -> | |
{Good, Bad, Min, Max, Average} = case ping(Node) of | |
{pong, Elapsed} -> | |
{Good0 + 1, Bad0, min(Min0, Elapsed), max(Max0, Elapsed), (Average0 * Count + Elapsed)/(Count + 1)}; | |
_ -> | |
{Good0, Bad0 + 1, Min0, Max0, Average0} | |
end, | |
Rslt = {Good, Bad, Min, Max, Average, Count + 1}, | |
PPid ! {self(), Node, Rslt}. | |
node_stats(Node, Stats) -> | |
case dict:is_key(Node, Stats) of | |
false -> | |
{0, 0, none, 0, 0.0, 0}; | |
true -> | |
dict:fetch(Node, Stats) | |
end. | |
receive_results([], Stats) -> | |
Stats; | |
receive_results([Pid|T], Stats0) -> | |
Stats1 = receive | |
{Pid, Node, Rslt} -> dict:store(Node, Rslt, Stats0) | |
end, | |
receive_results(T, Stats1). | |
pinglist(_Nodes, _Delay, Stats, 0) -> Stats; | |
pinglist(Nodes, Delay, Stats, Count) -> | |
Pids = [spawn(pinger, ping, [self(), Node, node_stats(Node, Stats)]) || Node <- Nodes], | |
Stats1 = receive_results(Pids, Stats), | |
timer:sleep(Delay), | |
pinglist(Nodes, Delay, Stats1, Count - 1). | |
pingall() -> | |
pingall(1). | |
pingall(Count) -> | |
pingall(1000, Count). | |
pingall(Delay, Count) when is_integer(Delay), is_integer(Count), Count > 0 -> | |
{ok,Ring} = riak_core_ring_manager:get_my_ring(), | |
Members = riak_core_ring:all_members(Ring), | |
Stats = pinglist(Members, Delay, dict:new(), Count), | |
FinalStats = dict:to_list(Stats), | |
[io:format("~p: ~b pong, ~b fail, ~p/~b/~f min/max/average usec, ~b pings sent~n", [Node, Good, Bad, Min, Max, Ave, Cnt]) || {Node, {Good, Bad, Min, Max, Ave, Cnt}} <- FinalStats]. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment