Created
October 17, 2018 02:46
-
-
Save rymdolle/96bcb2a6d3b84959df378c6fea8d3895 to your computer and use it in GitHub Desktop.
Simple mumble server ping util
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
#!/usr/bin/env escript | |
%%% Copyright 2018 Olle Mattsson <rymdolle@gmail.com> | |
%%% | |
%%% Ping a mumble server for basic statistics | |
%%% | |
main([]) -> | |
io:format("You must specify an address.\n\n"), | |
usage(); | |
main([Host]) -> | |
init(Host, 64738); | |
main([Host, PortString]) -> | |
case catch list_to_integer(PortString) of | |
Port when is_integer(Port) -> | |
init(Host, Port); | |
_ -> | |
io:format("Invalid port: ~s\n\n", [PortString]), | |
usage() | |
end. | |
usage() -> | |
Script = filename:basename(escript:script_name()), | |
io:format("~s ADDRESS [PORT]\n", [Script]), | |
halt(1). | |
init(Host, Port) -> | |
case gen_udp:open(0, [binary, {active, true}]) of | |
{ok, Socket} -> | |
case inet:getaddr(Host, inet) of | |
{ok, Address = {A,B,C,D}} -> | |
io:format("PING ~s (~b.~b.~b.~b)\n\n", [Host, A,B,C,D]), | |
ping(Socket, Address, Port); | |
{error, Reason} -> | |
io:format("Could not resolv ~s (~p)\n", [Host, Reason]) | |
end; | |
{error, Reason} -> | |
io:format("Could not open socket. (~p)\n", [Reason]) | |
end. | |
ping(Socket, Address, Port) -> | |
Id = rand:uniform(16#ffffffffffffffff), | |
Start = erlang:monotonic_time(), | |
gen_udp:send(Socket, Address, Port, <<0:32, Id:64>>), | |
receive | |
{udp, Socket, Address, Port, | |
<<Maj:16,Min,Rel, Id:64, | |
Users:32/unsigned, | |
MaxUsers:32/unsigned, | |
MaxBandwidth:32/unsigned>>} -> | |
End = erlang:monotonic_time(), | |
Resolution = erlang:convert_time_unit(1, millisecond, native), | |
io:format("version=~b.~b.~b users=~b/~b bandwidth=~b kbit/s time=~f ms\n", | |
[Maj,Min,Rel, Users, MaxUsers, | |
MaxBandwidth div 1000, (End - Start)/Resolution]), | |
if End - Start < 0 -> | |
ok; | |
true -> | |
Sleep = erlang:convert_time_unit(End - Start, native, millisecond), | |
timer:sleep(1000-Sleep) | |
end, | |
ping(Socket, Address, Port) | |
after | |
5000 -> | |
io:format("Connection timed out.\n") | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment