fix(telemetry): use required fields, rolling distro use PRETTY_NAME

This commit is contained in:
JimMoen 2021-12-21 10:44:54 +08:00
parent 6d2c83af7b
commit 99a5f14301
1 changed files with 26 additions and 23 deletions

View File

@ -220,37 +220,24 @@ os_info() ->
[{os_name, Name}, [{os_name, Name},
{os_version, Version}]; {os_version, Version}];
{unix, _} -> {unix, _} ->
case file:read_file_info("/etc/os-release") of case file:read_file("/etc/os-release") of
{error, _} -> {error, _} ->
[{os_name, "Unknown"}, [{os_name, "Unknown"},
{os_version, "Unknown"}]; {os_version, "Unknown"}];
{ok, FileInfo} -> {ok, FileContent} ->
case FileInfo#file_info.access of OSInfo = parse_os_release(FileContent),
Access when Access =:= read orelse Access =:= read_write -> [{os_name, get_value("NAME", OSInfo)},
OSInfo = lists:foldl(fun(Line, Acc) -> {os_version, get_value("VERSION", OSInfo,
[Var, Value] = string:tokens(Line, "="), get_value("VERSION_ID", OSInfo,
NValue = case Value of get_value("PRETTY_NAME", OSInfo)))}]
_ when is_list(Value) ->
lists:nth(1, string:tokens(Value, "\""));
_ ->
Value
end,
[{Var, NValue} | Acc]
end, [], string:tokens(os:cmd("cat /etc/os-release"), "\n")),
[{os_name, get_value("NAME", OSInfo, "Unknown")},
{os_version, get_value("VERSION", OSInfo,
get_value("VERSION_ID", OSInfo, "Unknown"))}];
_ ->
[{os_name, "Unknown"},
{os_version, "Unknown"}]
end
end; end;
{win32, nt} -> {win32, nt} ->
Ver = os:cmd("ver"), Ver = os:cmd("ver"),
case re:run(Ver, "[a-zA-Z ]+ \\[Version ([0-9]+[\.])+[0-9]+\\]", [{capture, none}]) of case re:run(Ver, "[a-zA-Z ]+ \\[Version ([0-9]+[\.])+[0-9]+\\]", [{capture, none}]) of
match -> match ->
[NVer | _] = string:tokens(Ver, "\r\n"), [NVer | _] = string:tokens(Ver, "\r\n"),
{match, [Version]} = re:run(NVer, "([0-9]+[\.])+[0-9]+", [{capture, first, list}]), {match, [Version]} =
re:run(NVer, "([0-9]+[\.])+[0-9]+", [{capture, first, list}]),
[Name | _] = string:split(NVer, " [Version "), [Name | _] = string:split(NVer, " [Version "),
[{os_name, Name}, [{os_name, Name},
{os_version, Version}]; {os_version, Version}];
@ -307,7 +294,8 @@ generate_uuid() ->
<<NTimeHigh:16>> = <<16#01:4, TimeHigh:12>>, <<NTimeHigh:16>> = <<16#01:4, TimeHigh:12>>,
<<NClockSeq:16>> = <<1:1, 0:1, ClockSeq:14>>, <<NClockSeq:16>> = <<1:1, 0:1, ClockSeq:14>>,
<<Node:48>> = <<First:7, 1:1, Last:40>>, <<Node:48>> = <<First:7, 1:1, Last:40>>,
list_to_binary(io_lib:format("~.16B-~.16B-~.16B-~.16B-~.16B", [TimeLow, TimeMid, NTimeHigh, NClockSeq, Node])). list_to_binary(io_lib:format( "~.16B-~.16B-~.16B-~.16B-~.16B"
, [TimeLow, TimeMid, NTimeHigh, NClockSeq, Node])).
get_telemetry(#state{uuid = UUID}) -> get_telemetry(#state{uuid = UUID}) ->
OSInfo = os_info(), OSInfo = os_info(),
@ -339,7 +327,22 @@ report_telemetry(State = #state{url = URL}) ->
httpc_request(Method, URL, Headers, Body) -> httpc_request(Method, URL, Headers, Body) ->
httpc:request(Method, {URL, Headers, "application/json", Body}, [], []). httpc:request(Method, {URL, Headers, "application/json", Body}, [], []).
parse_os_release(FileContent) ->
lists:foldl(fun(Line, Acc) ->
[Var, Value] = string:tokens(Line, "="),
NValue = case Value of
_ when is_list(Value) ->
lists:nth(1, string:tokens(Value, "\""));
_ ->
Value
end,
[{Var, NValue} | Acc]
end,
[], string:tokens(binary:bin_to_list(FileContent), "\n")).
bin(L) when is_list(L) -> bin(L) when is_list(L) ->
list_to_binary(L); list_to_binary(L);
bin(A) when is_atom(A) ->
atom_to_binary(A);
bin(B) when is_binary(B) -> bin(B) when is_binary(B) ->
B. B.