-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathleo_watchdog_cpu.erl
273 lines (247 loc) · 10.6 KB
/
leo_watchdog_cpu.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
%%======================================================================
%%
%% Leo Watchdog
%%
%% Copyright (c) 2012-2017 Rakuten, Inc.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License. You may obtain
%% a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied. See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%%======================================================================
-module(leo_watchdog_cpu).
-author('Yosuke Hara').
-behaviour(leo_watchdog_behaviour).
-include("leo_watchdog.hrl").
-include_lib("eunit/include/eunit.hrl").
%% API
-export([start_link/3,
start_link/4,
stop/0,
state/0
]).
%% Callback
-export([init/1,
update_property/3,
handle_call/2,
handle_fail/2
]).
-record(state, {
threshold_load_avg = 0.0 :: float(),
threshold_cpu_util = 0.0 :: float(),
raised_error_times = 3 :: non_neg_integer(),
cur_error_times = 0 :: non_neg_integer()
}).
%%--------------------------------------------------------------------
%% API
%%--------------------------------------------------------------------
%% @doc Start the server
-spec(start_link(ThresholdLoadAvg, ThresholdCPUUtil, Interval) ->
{ok,Pid} |
ignore |
{error,Error} when ThresholdLoadAvg::float(),
ThresholdCPUUtil::float(),
Interval::pos_integer(),
Pid::pid(),
Error::{already_started,Pid} | term()).
start_link(ThresholdLoadAvg, ThresholdCPUUtil, Interval) ->
start_link(ThresholdLoadAvg, ThresholdCPUUtil,
?DEF_RAISED_ERROR_TIMES, Interval).
%% @doc Start the server
-spec(start_link(ThresholdLoadAvg, ThresholdCPUUtil, RaisedErrorTimes, Interval) ->
{ok,Pid} |
ignore |
{error,Error} when ThresholdLoadAvg::float(),
ThresholdCPUUtil::float(),
RaisedErrorTimes::non_neg_integer(),
Interval::pos_integer(),
Pid::pid(),
Error::{already_started,Pid} | term()).
start_link(ThresholdLoadAvg, ThresholdCPUUtil, RaisedErrorTimes, Interval) ->
State = #state{threshold_load_avg = ThresholdLoadAvg,
threshold_cpu_util = ThresholdCPUUtil,
raised_error_times = RaisedErrorTimes},
leo_watchdog:start_link(?MODULE, ?MODULE, State, Interval).
%% @doc Stop the server
-spec(stop() ->
ok).
stop() ->
leo_watchdog:stop(?MODULE).
%% @doc Retrieves state of the watchdog
-spec(state() ->
{ok, State} when State::[{atom(), any()}]).
state() ->
case catch leo_watchdog:state(leo_watchdog_cpu) of
{ok, State} ->
Props = leo_misc:get_value('properties', State),
Props_1 = lists:zip(record_info(fields, state),tl(tuple_to_list(Props))),
{ok, State ++ Props_1};
_ ->
not_found
end.
%%--------------------------------------------------------------------
%% Callback
%%--------------------------------------------------------------------
%% @doc Initialize this process
-spec(init(State) ->
ok | {error, Cause} when State::any(),
Cause::any()).
init(_State) ->
ok.
%% @doc Update the item's value
-spec(update_property(Item, Value, State) ->
#state{} when Item::atom(),
Value::any(),
State::#state{}).
update_property(raised_error_times, Value, State) ->
State#state{raised_error_times = Value};
update_property(threshold_load_avg, Value, State) ->
State#state{threshold_load_avg = Value};
update_property(threshold_cpu_util, Value, State) ->
State#state{threshold_cpu_util = Value};
update_property(_,_, State) ->
State.
%% @doc Call execution of the watchdog
-spec(handle_call(Id, State) ->
{ok, State} | {{error,Error}, State} when Id::atom(),
State::#state{},
Error::any()).
handle_call(Id, #state{threshold_load_avg = ThresholdLoadAvg,
threshold_cpu_util = ThresholdCpuUtil,
raised_error_times = RaisedErrorTimes,
cur_error_times = CurErrorTimes} = State) ->
try
AVG_1 = case catch cpu_sup:avg1() of
{'EXIT',_} ->
0.0;
OrgAvg1 ->
erlang:round(OrgAvg1 / 256 * 100)
end,
CPU_Util = case os:type() of
{unix, linux} ->
case catch cpu_sup:util([per_cpu]) of
{'EXIT',_} ->
0;
[] ->
0;
Ret_CPUUtil ->
SumCPUUtil =
lists:foldl(fun(I, Sum) ->
Sum + I
end, 0, [Busy || {_,Busy,_,_} <- Ret_CPUUtil]),
erlang:round(SumCPUUtil / length(Ret_CPUUtil))
end;
_OtherOS ->
0
end,
ErrorLevel = case (CurErrorTimes + 1 >= RaisedErrorTimes) of
true ->
?WD_LEVEL_ERROR;
false ->
?WD_LEVEL_WARN
end,
%% Load avg
Props_1 = [{?WD_ITEM_LOAD_AVG_1M, erlang:round(AVG_1) / 100}],
Level_1 = case (ThresholdLoadAvg * 100 < AVG_1) of
true ->
ErrorLevel;
false when (ThresholdLoadAvg * 80 < AVG_1) ->
?WD_LEVEL_WARN;
false ->
catch elarm:clear(Id, ?WD_ITEM_LOAD_AVG),
?WD_LEVEL_SAFE
end,
%% CPU util
Props_2 = [{?WD_ITEM_CPU_UTIL, CPU_Util}],
Level_2 = case (CPU_Util > ThresholdCpuUtil) of
true ->
ErrorLevel;
false ->
catch elarm:clear(Id, ?WD_ITEM_CPU_UTIL),
?WD_LEVEL_SAFE
end,
%% Judge error level by load-avg and cpu-util
case (Level_1 < ?WD_LEVEL_WARN orelse
Level_2 < ?WD_LEVEL_WARN) of
true ->
catch elarm:clear(Id, ?WD_ITEM_CPU);
false ->
void
end,
case (Level_1 >= ?WD_LEVEL_WARN andalso
Level_2 >= ?WD_LEVEL_WARN) of
true ->
error_logger:warning_msg("~p,~p,~p,~p~n",
[{module, ?MODULE_STRING},
{function, "handle_call/2"},{line, ?LINE},
{body, [{triggered_watchdog, cpu_load}] ++ Props_1}]),
error_logger:warning_msg("~p,~p,~p,~p~n",
[{module, ?MODULE_STRING},
{function, "handle_call/2"},{line, ?LINE},
{body, [{triggered_watchdog, cpu_util}] ++ Props_2}]),
catch elarm:raise(Id, ?WD_ITEM_CPU,
#watchdog_state{id = Id,
level = ErrorLevel,
src = ?WD_ITEM_CPU,
props = Props_1 ++ Props_2});
false when Level_1 >= ?WD_LEVEL_WARN ->
error_logger:warning_msg("~p,~p,~p,~p~n",
[{module, ?MODULE_STRING},
{function, "handle_call/2"},{line, ?LINE},
{body, [{triggered_watchdog, cpu_load}] ++ Props_1}]),
catch elarm:raise(Id, ?WD_ITEM_LOAD_AVG,
#watchdog_state{id = Id,
level = ?WD_LEVEL_WARN,
src = ?WD_ITEM_LOAD_AVG,
props = Props_1});
false when Level_2 >= ?WD_LEVEL_WARN ->
error_logger:warning_msg("~p,~p,~p,~p~n",
[{module, ?MODULE_STRING},
{function, "handle_call/2"},{line, ?LINE},
{body, [{triggered_watchdog, cpu_util}] ++ Props_2}]),
catch elarm:raise(Id, ?WD_ITEM_CPU_UTIL,
#watchdog_state{id = Id,
level = ?WD_LEVEL_WARN,
src = ?WD_ITEM_CPU_UTIL,
props = Props_2});
false ->
void
end,
%% Check the result
CurErrorTimes_1 =
case (Level_1 >= ?WD_LEVEL_WARN andalso
Level_2 >= ?WD_LEVEL_WARN) of
true when CurErrorTimes >= RaisedErrorTimes ->
0;
true ->
CurErrorTimes + 1;
false ->
0
end,
{ok, State#state{cur_error_times = CurErrorTimes_1}}
catch
_:Cause ->
error_logger:error_msg(
"~p,~p,~p,~p~n",
[{module, ?MODULE_STRING},
{function, "handle_call/2"},
{line, ?LINE}, {body, Cause}]),
{ok, State}
end.
%% @doc Call execution failed
-spec(handle_fail(Id, Cause) ->
ok | {error,Error} when Id::atom(),
Cause::any(),
Error::any()).
handle_fail(_Id,_Cause) ->
ok.