/
riaknostic_check_sysctl.erl
112 lines (97 loc) · 3.79 KB
/
riaknostic_check_sysctl.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
%% -------------------------------------------------------------------
%%
%% riaknostic - automated diagnostic tools for Riak
%%
%% Copyright (c) 2011 Basho Technologies, Inc. All Rights Reserved.
%%
%% 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.
%%
%% -------------------------------------------------------------------
%% @doc Diagnostic that checks the sysctl settings and offers conservative
%% recommendations
-module(riaknostic_check_sysctl).
-behaviour(riaknostic_check).
-export([description/0,
valid/0,
check/0,
format/1]).
-define(LINUX_PARAMS, [
{"vm.swappiness", 0, gte},
{"net.core.wmem_default", 8388608, lte},
{"net.core.rmem_default", 8388608, lte},
{"net.core.wmem_max", 8388608, lte},
{"net.core.rmem_max", 8388608, lte},
{"net.core.netdev_max_backlog", 10000, lte},
{"net.core.somaxconn", 4000, lte},
{"net.ipv4.tcp_max_syn_backlog", 40000, lte},
{"net.ipv4.tcp_fin_timeout", 15, gte},
{"net.ipv4.tcp_tw_reuse", 1, eq}
]).
-define(OPENBSD_PARAMS, [
{"kern.somaxconn", 4000, lte}
]).
-spec description() -> string().
description() ->
"Check sysctl tuning parameters".
-spec valid() -> boolean().
valid() ->
riaknostic_node:can_connect().
-spec check() -> [{lager:log_level(), term()}].
check() ->
Params = case os:type() of
{unix, linux} -> ?LINUX_PARAMS;
{unix, openbsd} -> ?OPENBSD_PARAMS;
{unix, darwin} -> [];
{unix, freebsd} -> [];
{unix, sunos} -> [];
_ -> []
end,
Recs = check_params(Params),
Recs.
check_params(List) ->
check_params(List, []).
check_params([], Acc) ->
Acc;
check_params([H|T], Acc) ->
{Param, Val, Direction } = H,
Output = riaknostic_util:run_command("sysctl -n " ++ Param),
Actual = list_to_integer(Output -- "\n"),
Good = case Direction of
gte -> Actual =< Val;
lte -> Actual >= Val;
eq -> Actual == Val
end,
Note = case Good of
true -> {info, {good, Param, Actual, Val, Direction}};
false -> {critical, {bad, Param, Actual, Val, Direction}}
end,
check_params(T, Acc ++ [Note]).
-spec format(term()) -> {io:format(), [term()]}.
format({good, Param, Actual, Val, Direction}) ->
{"~s is ~p ~s ~p", [Param, Actual, direction_to_word(Direction), Val]};
format({bad, Param, Actual, Val, Direction}) ->
{"~s is ~p, should be ~s~p", [Param, Actual, direction_to_word2(Direction), Val]}.
direction_to_word(Direction) ->
case Direction of
gte -> "greater than or equal to";
lte -> "lesser than or equal to";
eq -> "equal to"
end.
direction_to_word2(Direction) ->
case Direction of
gte -> "no more than ";
lte -> "at least ";
eq -> ""
end.