增加主机报警
This commit is contained in:
parent
c28ee8af88
commit
f320d7defa
@ -337,11 +337,13 @@ handle_event({call, From}, {attach_channel, _}, _, State = #state{uuid = UUID, c
|
||||
{keep_state, State, [{reply, From, {error, <<"channel existed">>}}]};
|
||||
|
||||
%% 授权通过后,才能将主机的状态设置为在线状态
|
||||
handle_event({call, From}, {create_session, PubKey}, ?STATE_ACTIVATED, State = #state{uuid = UUID, aes = Aes}) ->
|
||||
handle_event({call, From}, {create_session, PubKey}, ?STATE_ACTIVATED, State = #state{uuid = UUID, aes = Aes, name = Name}) ->
|
||||
Reply = #{<<"a">> => true, <<"aes">> => Aes},
|
||||
EncReply = iot_cipher_rsa:encode(Reply, PubKey),
|
||||
{ok, AffectedRow} = host_bo:change_status(UUID, ?HOST_ONLINE),
|
||||
warn_status(Name, <<"在线"/utf8>>),
|
||||
report_event(UUID, ?HOST_ONLINE),
|
||||
|
||||
lager:debug("[iot_host] host_id(session) uuid: ~p, create_session, will change status, affected_row: ~p", [UUID, AffectedRow]),
|
||||
{keep_state, State#state{has_session = true}, [{reply, From, {ok, <<10:8, EncReply/binary>>}}]};
|
||||
|
||||
@ -514,7 +516,7 @@ handle_event(cast, heartbeat, _, State = #state{heartbeat_counter = HeartbeatCou
|
||||
{keep_state, State#state{heartbeat_counter = HeartbeatCounter + 1}};
|
||||
|
||||
%% 没有收到心跳包,主机下线, 设备状态不变
|
||||
handle_event(info, {timeout, _, heartbeat_ticker}, _, State = #state{uuid = UUID, heartbeat_counter = 0, channel_pid = ChannelPid}) ->
|
||||
handle_event(info, {timeout, _, heartbeat_ticker}, _, State = #state{uuid = UUID, heartbeat_counter = 0, name = Name, channel_pid = ChannelPid}) ->
|
||||
lager:warning("[iot_host] uuid: ~p, heartbeat lost, devices will unknown", [UUID]),
|
||||
{ok, #{<<"status">> := Status}} = host_bo:get_host_by_uuid(UUID),
|
||||
case Status of
|
||||
@ -524,6 +526,7 @@ handle_event(info, {timeout, _, heartbeat_ticker}, _, State = #state{uuid = UUID
|
||||
lager:debug("[iot_host] host: ~p, host_maybe_offline, host now is offline, do nothing", [UUID]);
|
||||
?HOST_ONLINE ->
|
||||
{ok, _} = host_bo:change_status(UUID, ?HOST_OFFLINE),
|
||||
warn_status(Name, <<"离线"/utf8>>),
|
||||
report_event(UUID, ?HOST_OFFLINE)
|
||||
end,
|
||||
|
||||
@ -631,6 +634,13 @@ report_event(UUID, NewStatus) when is_binary(UUID), is_integer(NewStatus) ->
|
||||
iot_router:route_uuid(UUID, FieldsList, Timestamp),
|
||||
lager:debug("[iot_host] host_uuid: ~p, route fields: ~p", [UUID, FieldsList]).
|
||||
|
||||
%% 报警主机的状态
|
||||
warn_status(Name, Status) when is_binary(Name), is_binary(Status) ->
|
||||
Warn = iolist_to_binary([<<"主机: "/utf8>>, Name, <<" || ">>, Status]),
|
||||
iot_watchdog:warn(Warn);
|
||||
warn_status(_, _) ->
|
||||
ok.
|
||||
|
||||
%% 将当前的state转换成map
|
||||
state_map(#state{host_id = HostId, uuid = UUID, aes = Aes, has_session = HasSession, heartbeat_counter = HeartbeatCounter, channel_pid = ChannelPid, metrics = Metrics}) ->
|
||||
#{
|
||||
|
||||
@ -13,9 +13,7 @@
|
||||
|
||||
%% API
|
||||
-export([start_link/0]).
|
||||
-export([detection/3]).
|
||||
|
||||
-export([test/0]).
|
||||
-export([detection/3, warn/1]).
|
||||
|
||||
%% gen_server callbacks
|
||||
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
|
||||
@ -43,21 +41,6 @@
|
||||
limiters = #{}
|
||||
}).
|
||||
|
||||
test() ->
|
||||
Metric = #{
|
||||
<<"cpu_temperature">> => 90.5,
|
||||
<<"disk">> => #{
|
||||
<<"total">> => 7129,
|
||||
<<"used">> => 345
|
||||
},
|
||||
<<"memory">> => #{
|
||||
<<"total">> => 990,
|
||||
<<"used">> => 900
|
||||
}
|
||||
},
|
||||
detection(<<"1234">>, <<"test1234">>, Metric).
|
||||
|
||||
|
||||
%%%===================================================================
|
||||
%%% API
|
||||
%%%===================================================================
|
||||
@ -68,6 +51,10 @@ detection(HostUUID, Name, Metric) when is_binary(HostUUID), is_binary(Name), is_
|
||||
detection(HostUUID, Name, _) when is_binary(HostUUID), is_binary(Name) ->
|
||||
ok.
|
||||
|
||||
-spec warn(Warn :: binary()) -> no_return().
|
||||
warn(Warn) when is_binary(Warn) ->
|
||||
gen_server:cast(?SERVER, {warn, Warn}).
|
||||
|
||||
%% @doc Spawns the server and registers the local name (unique)
|
||||
-spec(start_link() ->
|
||||
{ok, Pid :: pid()} | ignore | {error, Reason :: term()}).
|
||||
@ -137,7 +124,6 @@ handle_cast({detection, HostUUID, Name, Metric},
|
||||
Body = format_warn(Subject, Users, PriKey),
|
||||
case catch do_post(Url, Body) of
|
||||
{ok, Resp} ->
|
||||
lager:debug("[iot_watchdog] url: ~p, body: ~ts, resp: ~ts", [Url, Body, Resp]),
|
||||
iot_logger:write(LoggerPid, [Body, Resp]);
|
||||
{error, Reason} ->
|
||||
lager:warning("[iot_watchdog] url: ~p, send body: ~ts, get error: ~p", [Url, Body, Reason])
|
||||
@ -146,7 +132,17 @@ handle_cast({detection, HostUUID, Name, Metric},
|
||||
ok
|
||||
end,
|
||||
|
||||
{noreply, State#state{limiters = maps:put(HostUUID, NLimiter, Limiters)}}.
|
||||
{noreply, State#state{limiters = maps:put(HostUUID, NLimiter, Limiters)}};
|
||||
|
||||
handle_cast({warn, Warn}, State = #state{url = Url, users = Users, pri_key = PriKey, logger_pid = LoggerPid}) ->
|
||||
Body = format_warn(Warn, Users, PriKey),
|
||||
case catch do_post(Url, Body) of
|
||||
{ok, Resp} ->
|
||||
iot_logger:write(LoggerPid, [Body, Resp]);
|
||||
{error, Reason} ->
|
||||
lager:warning("[iot_watchdog] url: ~p, send body: ~ts, get error: ~p", [Url, Body, Reason])
|
||||
end,
|
||||
{noreply, State}.
|
||||
|
||||
%% @private
|
||||
%% @doc Handling all non call/cast messages
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user