From d9f14dacaf8a77f27f9733054dd2d724cc684ec7 Mon Sep 17 00:00:00 2001 From: turtled Date: Mon, 25 Sep 2017 14:59:58 +0800 Subject: [PATCH 1/7] Fix dashboard not showing data bug --- src/emqttd_http.erl | 14 +++++++------- src/emqttd_rest_api.erl | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/emqttd_http.erl b/src/emqttd_http.erl index bc5ba764d..f62329ada 100644 --- a/src/emqttd_http.erl +++ b/src/emqttd_http.erl @@ -26,7 +26,7 @@ -import(proplists, [get_value/2, get_value/3]). --export([http_handler/0, handle_request/2, http_api/0]). +-export([http_handler/0, handle_request/2, http_api/0, inner_handle_request/2]). -include("emqttd_internal.hrl"). @@ -54,14 +54,14 @@ handle_request(Req, State) -> "/api/v2/auth" -> handle_request(Path, Req, State); _ -> - Host = Req:get_header_value("Host"), - [_, Port] = string:tokens(Host, ":"), - case Port of - "18083" -> handle_request(Path, Req, State); - _ -> if_authorized(Req, fun() -> handle_request(Path, Req, State) end) - end + if_authorized(Req, fun() -> handle_request(Path, Req, State) end) end. +inner_handle_request(Req, State) -> + Path = Req:get(path), + handle_request(Path, Req, State). + + handle_request("/api/v2/" ++ Url, Req, #state{dispatch = Dispatch}) -> Dispatch(Req, Url); diff --git a/src/emqttd_rest_api.erl b/src/emqttd_rest_api.erl index d0ab6a855..baa056b81 100644 --- a/src/emqttd_rest_api.erl +++ b/src/emqttd_rest_api.erl @@ -25,7 +25,7 @@ -http_api({"^nodes/(.+?)/clients/(.+?)/?$", 'GET',client_list, []}). -http_api({"^clients/(.+?)/?$", 'GET', client, []}). -http_api({"^clients/(.+?)/?$", 'DELETE', kick_client, []}). --http_api({"^clients/(.+?)/clean_acl_cache?$", 'DELETE', clean_acl_cache, [{<<"topic">>, binary}]}). +-http_api({"^clients/(.+?)/clean_acl_cache?$", 'PUT', clean_acl_cache, [{<<"topic">>, binary}]}). -http_api({"^routes?$", 'GET', route_list, []}). -http_api({"^routes/(.+?)/?$", 'GET', route, []}). From 88c77cf4c2e11e6cb1be27316c353060921b4018 Mon Sep 17 00:00:00 2001 From: turtled Date: Mon, 9 Oct 2017 18:07:09 +0800 Subject: [PATCH 2/7] Auth failure not publish the will message --- src/emqttd_protocol.erl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/emqttd_protocol.erl b/src/emqttd_protocol.erl index 0129faedd..31354dd84 100644 --- a/src/emqttd_protocol.erl +++ b/src/emqttd_protocol.erl @@ -387,7 +387,11 @@ shutdown(conflict, #proto_state{client_id = _ClientId}) -> shutdown(Error, State = #proto_state{will_msg = WillMsg}) -> ?LOG(debug, "Shutdown for ~p", [Error], State), Client = client(State), - send_willmsg(Client, WillMsg), + %% Auth failure not publish the will message + case Error =:= auth_failure of + true -> ok; + false -> send_willmsg(Client, WillMsg) + end, emqttd_hooks:run('client.disconnected', [Error], Client), %% let it down %% emqttd_cm:unreg(ClientId). From 54534967bdfefd2e179b320975b6b026f9118e99 Mon Sep 17 00:00:00 2001 From: turtled Date: Mon, 9 Oct 2017 18:08:46 +0800 Subject: [PATCH 3/7] Fix Dashboard not showing data --- src/emqttd_http.erl | 6 ++++-- src/emqttd_rest_api.erl | 7 ++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/emqttd_http.erl b/src/emqttd_http.erl index f62329ada..a41025294 100644 --- a/src/emqttd_http.erl +++ b/src/emqttd_http.erl @@ -59,8 +59,10 @@ handle_request(Req, State) -> inner_handle_request(Req, State) -> Path = Req:get(path), - handle_request(Path, Req, State). - + case Path of + "/api/v2/auth" -> handle_request(Path, Req, State); + _ -> if_authorized(Req, fun() -> handle_request(Path, Req, State) end) + end. handle_request("/api/v2/" ++ Url, Req, #state{dispatch = Dispatch}) -> Dispatch(Req, Url); diff --git a/src/emqttd_rest_api.erl b/src/emqttd_rest_api.erl index baa056b81..0c4bae3bf 100644 --- a/src/emqttd_rest_api.erl +++ b/src/emqttd_rest_api.erl @@ -212,9 +212,10 @@ session_list('GET', Params, Node, ClientId) -> {ok, [{objects, [session_row(Row) || Row <- Data]}]}. session_row({ClientId, _Pid, _Persistent, Session}) -> - InfoKeys = [clean_sess, max_inflight, inflight_queue, message_queue, - message_dropped, awaiting_rel, awaiting_ack, awaiting_comp, created_at], - [{client_id, ClientId} | [{Key, format(Key, get_value(Key, Session))} || Key <- InfoKeys]]. + Data = lists:append(Session, emqttd_stats:get_session_stats(ClientId)), + InfoKeys = [clean_sess, subscriptions, max_inflight, inflight_len, mqueue_len, + mqueue_dropped, awaiting_rel_len, deliver_msg,enqueue_msg, created_at], + [{client_id, ClientId} | [{Key, format(Key, get_value(Key, Data))} || Key <- InfoKeys]]. %%-------------------------------------------------------------------------- %% subscription From 989d2fd9e7137303b5deb3b7f382c1a84703dfdb Mon Sep 17 00:00:00 2001 From: turtled Date: Tue, 10 Oct 2017 13:10:34 +0800 Subject: [PATCH 4/7] Add more lager configuration --- etc/emq.conf | 12 ++++++++++++ priv/emq.schema | 28 ++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/etc/emq.conf b/etc/emq.conf index 1c0859921..e5fc93f75 100644 --- a/etc/emq.conf +++ b/etc/emq.conf @@ -137,12 +137,24 @@ log.syslog.level = error ## Console log file ## log.console.file = {{ platform_log_dir }}/console.log +## Console log file size +## log.console.size = 10485760 + +## Console log count size +## log.console.count = 5 + ## Info log file ## log.info.file = {{ platform_log_dir }}/info.log ## Error log file log.error.file = {{ platform_log_dir }}/error.log +## Error log file size +## log.error.size = 10485760 + +## Error log file count +## log.error.count = 5 + ## Enable the crash log. Enum: on, off log.crash = on diff --git a/priv/emq.schema b/priv/emq.schema index ce4baf36b..b1fdf138f 100644 --- a/priv/emq.schema +++ b/priv/emq.schema @@ -326,11 +326,31 @@ end}. {datatype, file} ]}. +{mapping, "log.console.size", "lager.handlers", [ + {default, 10485760}, + {datatype, integer} +]}. + +{mapping, "log.console.count", "lager.handlers", [ + {default, 5}, + {datatype, integer} +]}. + {mapping, "log.error.file", "lager.handlers", [ {default, "log/error.log"}, {datatype, file} ]}. +{mapping, "log.error.size", "lager.handlers", [ + {default, 10485760}, + {datatype, integer} +]}. + +{mapping, "log.error.count", "lager.handlers", [ + {default, 5}, + {datatype, integer} +]}. + {mapping, "log.syslog", "lager.handlers", [ {default, off}, {datatype, flag} @@ -370,9 +390,9 @@ end}. undefined -> []; ErrorFilename -> [{lager_file_backend, [{file, ErrorFilename}, {level, error}, - {size, 10485760}, + {size, cuttlefish:conf_get("log.error.size", Conf)}, {date, "$D0"}, - {count, 5}]}] + {count, cuttlefish:conf_get("log.error.count", Conf)}]}] end, ConsoleLogLevel = cuttlefish:conf_get("log.console.level", Conf), @@ -381,9 +401,9 @@ end}. ConsoleHandler = {lager_console_backend, ConsoleLogLevel}, ConsoleFileHandler = {lager_file_backend, [{file, ConsoleLogFile}, {level, ConsoleLogLevel}, - {size, 10485760}, + {size, cuttlefish:conf_get("log.console.size", Conf)}, {date, "$D0"}, - {count, 5}]}, + {count, cuttlefish:conf_get("log.console.count", Conf)}]}, ConsoleHandlers = case cuttlefish:conf_get("log.console", Conf) of off -> []; From 5d30ceccd194f7270ff7912dada44c91faa1befb Mon Sep 17 00:00:00 2001 From: turtled Date: Tue, 10 Oct 2017 14:15:53 +0800 Subject: [PATCH 5/7] Fix passwd_hash return type error --- src/emqttd_auth_mod.erl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/emqttd_auth_mod.erl b/src/emqttd_auth_mod.erl index c94578c46..d413446ff 100644 --- a/src/emqttd_auth_mod.erl +++ b/src/emqttd_auth_mod.erl @@ -63,12 +63,12 @@ passwd_hash(sha256, Password) -> passwd_hash(pbkdf2, {Salt, Password, Macfun, Iterations, Dklen}) -> case pbkdf2:pbkdf2(Macfun, Password, Salt, Iterations, Dklen) of {ok, Hexstring} -> pbkdf2:to_hex(Hexstring); - {error, Error} -> lager:error("PasswdHash with pbkdf2 error:~p", [Error]), error + {error, Error} -> lager:error("PasswdHash with pbkdf2 error:~p", [Error]), <<>> end; passwd_hash(bcrypt, {Salt, Password}) -> case bcrypt:hashpw(Password, Salt) of {ok, HashPassword} -> list_to_binary(HashPassword); - {error, Error}-> lager:error("PasswdHash with bcrypt error:~p", [Error]), error + {error, Error}-> lager:error("PasswdHash with bcrypt error:~p", [Error]), <<>> end. hexstring(<>) -> From 97cf04d7528b51a68ac4b1e5b3413b05bbe7fe51 Mon Sep 17 00:00:00 2001 From: HuangDan Date: Wed, 11 Oct 2017 15:59:25 +0800 Subject: [PATCH 6/7] Add more lager configuration --- etc/emq.conf | 22 ++++++++++++++-------- priv/emq.schema | 25 ++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/etc/emq.conf b/etc/emq.conf index e5fc93f75..227a22457 100644 --- a/etc/emq.conf +++ b/etc/emq.conf @@ -128,12 +128,6 @@ log.console = console ## Console log level. Enum: debug, info, notice, warning, error, critical, alert, emergency log.console.level = error -## Syslog. Enum: on, off -log.syslog = on - -## syslog level. Enum: debug, info, notice, warning, error, critical, alert, emergency -log.syslog.level = error - ## Console log file ## log.console.file = {{ platform_log_dir }}/console.log @@ -146,20 +140,32 @@ log.syslog.level = error ## Info log file ## log.info.file = {{ platform_log_dir }}/info.log +## Info log file size +## log.info.size = 10485760 + +## Info log file count +## log.info.count = 5 + ## Error log file log.error.file = {{ platform_log_dir }}/error.log ## Error log file size -## log.error.size = 10485760 +log.error.size = 10485760 ## Error log file count -## log.error.count = 5 +log.error.count = 5 ## Enable the crash log. Enum: on, off log.crash = on log.crash.file = {{ platform_log_dir }}/crash.log +## Syslog. Enum: on, off +log.syslog = on + +## syslog level. Enum: debug, info, notice, warning, error, critical, alert, emergency +log.syslog.level = error + ##-------------------------------------------------------------------- ## Allow Anonymous and Default ACL ##-------------------------------------------------------------------- diff --git a/priv/emq.schema b/priv/emq.schema index b1fdf138f..6b3f0001f 100644 --- a/priv/emq.schema +++ b/priv/emq.schema @@ -336,6 +336,20 @@ end}. {datatype, integer} ]}. +{mapping, "log.info.file", "lager.handlers", [ + {datatype, file} +]}. + +{mapping, "log.info.size", "lager.handlers", [ + {default, 10485760}, + {datatype, integer} +]}. + +{mapping, "log.info.count", "lager.handlers", [ + {default, 5}, + {datatype, integer} +]}. + {mapping, "log.error.file", "lager.handlers", [ {default, "log/error.log"}, {datatype, file} @@ -395,6 +409,15 @@ end}. {count, cuttlefish:conf_get("log.error.count", Conf)}]}] end, + InfoHandler = case cuttlefish:conf_get("log.info.file", Conf, undefined) of + undefined -> []; + InfoFilename -> [{lager_file_backend, [{file, InfoFilename}, + {level, info}, + {size, cuttlefish:conf_get("log.info.size", Conf)}, + {date, "$D0"}, + {count, cuttlefish:conf_get("log.info.count", Conf)}]}] + end, + ConsoleLogLevel = cuttlefish:conf_get("log.console.level", Conf), ConsoleLogFile = cuttlefish:conf_get("log.console.file", Conf), @@ -420,7 +443,7 @@ end}. cuttlefish:conf_get("log.syslog.level", Conf)]}] end, - ConsoleHandlers ++ ErrorHandler ++ SyslogHandler + ConsoleHandlers ++ ErrorHandler ++ InfoHandler ++ SyslogHandler end }. From c43cae4348c2878241cc24c0ee800bc710167568 Mon Sep 17 00:00:00 2001 From: HuangDan Date: Wed, 11 Oct 2017 20:08:01 +0800 Subject: [PATCH 7/7] Bug fixed for users api --- src/emqttd_rest_api.erl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/emqttd_rest_api.erl b/src/emqttd_rest_api.erl index 0c4bae3bf..85981ea45 100644 --- a/src/emqttd_rest_api.erl +++ b/src/emqttd_rest_api.erl @@ -66,9 +66,9 @@ -http_api({"^users/?$", 'GET', users, []}). -http_api({"^users/?$", 'POST', users, [{<<"username">>, binary}, {<<"password">>, binary}, - {<<"tag">>, binary}]}). + {<<"tags">>, binary}]}). -http_api({"^users/(.+?)/?$", 'GET', users, []}). --http_api({"^users/(.+?)/?$", 'PUT', users, []}). +-http_api({"^users/(.+?)/?$", 'PUT', users, [{<<"tags">>, binary}]}). -http_api({"^users/(.+?)/?$", 'DELETE', users, []}). -http_api({"^auth/?$", 'POST', auth, [{<<"username">>, binary}, {<<"password">>, binary}]}).