From 99a5f14301aeb86b2758c3c5527eca5331b79c80 Mon Sep 17 00:00:00 2001 From: JimMoen Date: Tue, 21 Dec 2021 10:44:54 +0800 Subject: [PATCH] fix(telemetry): use required fields, rolling distro use PRETTY_NAME --- apps/emqx_modules/src/emqx_telemetry.erl | 49 +++++++++++++----------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/apps/emqx_modules/src/emqx_telemetry.erl b/apps/emqx_modules/src/emqx_telemetry.erl index 932af8005..198e410f0 100644 --- a/apps/emqx_modules/src/emqx_telemetry.erl +++ b/apps/emqx_modules/src/emqx_telemetry.erl @@ -220,37 +220,24 @@ os_info() -> [{os_name, Name}, {os_version, Version}]; {unix, _} -> - case file:read_file_info("/etc/os-release") of + case file:read_file("/etc/os-release") of {error, _} -> [{os_name, "Unknown"}, {os_version, "Unknown"}]; - {ok, FileInfo} -> - case FileInfo#file_info.access of - Access when Access =:= read orelse Access =:= read_write -> - OSInfo = 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(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 + {ok, FileContent} -> + OSInfo = parse_os_release(FileContent), + [{os_name, get_value("NAME", OSInfo)}, + {os_version, get_value("VERSION", OSInfo, + get_value("VERSION_ID", OSInfo, + get_value("PRETTY_NAME", OSInfo)))}] end; {win32, nt} -> Ver = os:cmd("ver"), case re:run(Ver, "[a-zA-Z ]+ \\[Version ([0-9]+[\.])+[0-9]+\\]", [{capture, none}]) of match -> [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 "), [{os_name, Name}, {os_version, Version}]; @@ -307,7 +294,8 @@ generate_uuid() -> <> = <<16#01:4, TimeHigh:12>>, <> = <<1:1, 0:1, ClockSeq:14>>, <> = <>, - 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}) -> OSInfo = os_info(), @@ -339,7 +327,22 @@ report_telemetry(State = #state{url = URL}) -> httpc_request(Method, URL, Headers, 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) -> list_to_binary(L); +bin(A) when is_atom(A) -> + atom_to_binary(A); bin(B) when is_binary(B) -> B.