1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 1996-2019. All Rights Reserved.
5%%
6%% Licensed under the Apache License, Version 2.0 (the "License");
7%% you may not use this file except in compliance with the License.
8%% You may obtain a copy of the License at
9%%
10%%     http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing, software
13%% distributed under the License is distributed on an "AS IS" BASIS,
14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15%% See the License for the specific language governing permissions and
16%% limitations under the License.
17%%
18%% %CopyrightEnd%
19%%
20%%-----------------------------------------------------------------
21%% This module implements the config conversion for the old SNMP
22%% application start method
23%% The purpose is to extract all agent app-env and convert to the
24%% new format
25%%
26%% ---
27%%
28%% What about the restart times for the children
29%% note_store (snmpa_supervisor) and
30%% net_if and mib_server (snmpa_misc_sup)?
31%%
32%%-----------------------------------------------------------------
33
34-module(snmpa_app).
35
36-include("snmp_debug.hrl").
37
38-export([convert_config/0, convert_config/1]).
39
40%% Internal (test)
41-export([start/1]).
42
43
44convert_config() ->
45    ?d("convert_config -> entry", []),
46    convert_config( application:get_all_env(snmp) ).
47
48convert_config(Opts) ->
49    ?d("convert_config -> Opts: ~p", [Opts]),
50    Prio           = get_priority(Opts),
51    DbDir          = get_db_dir(Opts),
52    DbInitError    = terminate,
53    LdbOpts        = get_local_db_opts(Opts),
54    MibStorage     = get_mib_storage(Opts),
55    MibsOpts       = get_mib_server_opts(Opts),
56    SymOpts        = get_symbolic_store_opts(Opts),
57    SetModule      = get_set_mechanism(Opts),
58    AuthModule     = get_authentication_service(Opts),
59    MultiT         = get_multi_threaded(Opts),
60    Vsns           = get_versions(Opts),
61    SupOpts        = get_supervisor_opts(Opts),
62    ErrorReportMod = get_error_report_mod(Opts),
63    AgentType      = get_agent_type(Opts),
64    case AgentType of
65	sub ->
66	    ?d("convert_config -> agent type: sub",[]),
67	    SaVerb = get_sub_agent_verbosity(Opts),
68	    [{agent_type,             AgentType},
69	     {agent_verbosity,        SaVerb},
70	     {get_mechanism,          snmpa_get},
71	     {set_mechanism,          SetModule},
72	     {authentication_service, AuthModule},
73	     {priority,               Prio},
74	     {versions,               Vsns},
75	     {db_dir,                 DbDir},
76	     {db_init_error,          DbInitError},
77	     {multi_threaded,         MultiT},
78	     {error_report_mod,       ErrorReportMod},
79	     {mib_storage,            MibStorage},
80	     {mib_server,             MibsOpts},
81	     {local_db,               LdbOpts},
82	     {supervisor,             SupOpts},
83	     {symbolic_store,         SymOpts}];
84
85	master ->
86	    ?d("convert_config -> agent type: master",[]),
87	    MaVerb    = get_master_agent_verbosity(Opts),
88	    NoteOpts  = get_note_store_opts(Opts),
89	    NiOptions = get_net_if_options(Opts),
90	    NiOpts    = [{options, NiOptions}|get_net_if_opts(Opts)],
91	    AtlOpts   = get_audit_trail_log_opts(Opts),
92	    Mibs      = get_master_agent_mibs(Opts),
93	    ForceLoad = get_force_config_load(Opts),
94	    ConfVerb  = get_opt(verbosity, SupOpts, silence),
95	    ConfDir   = get_config_dir(Opts),
96	    ConfOpts  = [{dir,        ConfDir},
97			 {force_load, ForceLoad},
98			 {verbosity,  ConfVerb}],
99	    [{agent_type,             AgentType},
100	     {agent_verbosity,        MaVerb},
101	     {get_mechanism,          snmpa_get},
102	     {set_mechanism,          SetModule},
103	     {authentication_service, AuthModule},
104	     {db_dir,                 DbDir},
105	     {db_init_error,          DbInitError},
106	     {config,                 ConfOpts},
107	     {priority,               Prio},
108	     {versions,               Vsns},
109	     {multi_threaded,         MultiT},
110	     {error_report_mod,       ErrorReportMod},
111	     {supervisor,             SupOpts},
112	     {mibs,                   Mibs},
113	     {mib_storage,            MibStorage},
114	     {symbolic_store,         SymOpts},
115	     {note_store,             NoteOpts},
116	     {net_if,                 NiOpts},
117	     {mib_server,             MibsOpts},
118	     {local_db,               LdbOpts}] ++ AtlOpts
119    end.
120
121
122start(Type) ->
123    snmp_app_sup:start_agent(Type, convert_config()).
124
125
126%% ------------------------------------------------------------------
127
128get_db_dir(Opts) ->
129    case get_opt(snmp_db_dir, Opts) of
130	{value, Dir} when is_list(Dir) ->
131	    Dir;
132	{value, Bad} ->
133	    exit({bad_config, {db_dir, Bad}});
134	false ->
135	    exit({undefined_config, db_dir})
136    end.
137
138
139%% --
140
141get_priority(Opts) ->
142    case get_opt(snmp_priority, Opts) of
143	{value, Prio} when is_atom(Prio) ->
144	    Prio;
145	_ ->
146	    normal
147    end.
148
149
150%% --
151
152get_mib_storage(Opts) ->
153    get_opt(snmp_mib_storage, Opts, ets).
154
155get_mib_server_opts(Opts) ->
156    Options = [{snmp_mibserver_verbosity, verbosity, silence},
157	       {mibentry_override, mibentry_override, false},
158	       {trapentry_override, trapentry_override, false}],
159    get_opts(Options, Opts, []).
160
161
162%% --
163
164get_audit_trail_log_opts(Opts) ->
165    case get_audit_trail_log(Opts) of
166	false ->
167	    [];
168	Type ->
169	    Dir  = get_audit_trail_log_dir(Opts),
170	    Size = get_audit_trail_log_size(Opts),
171	    AtlOpts0 = [{type,   Type},
172			{dir,    Dir},
173			{size,   Size},
174			{repair, true}],
175	    [{audit_trail_log, AtlOpts0}]
176    end.
177
178
179get_audit_trail_log(Opts) ->
180    case get_opt(audit_trail_log, Opts) of
181	{value, write_log}      -> write;
182	{value, read_log}       -> read;
183	{value, read_write_log} -> read_write;
184	_                       -> false
185    end.
186
187get_audit_trail_log_dir(Opts) ->
188    case get_opt(audit_trail_log_dir, Opts) of
189	{value, Dir} when is_list(Dir) ->
190	    Dir;
191	{value, Bad} ->
192	    exit({bad_config, {audit_trail_log_dir, Bad}});
193	_ ->
194	    exit({undefined_config, audit_trail_log_dir})
195    end.
196
197get_audit_trail_log_size(Opts) ->
198    case get_opt(audit_trail_log_size, Opts) of
199	{value, {MB, MF} = Sz} when is_integer(MB) andalso is_integer(MF) ->
200	    Sz;
201	{value, Bad} ->
202	    exit({bad_config, {audit_trail_log_size, Bad}});
203	_ ->
204	    exit({undefined_config, audit_trail_log_size})
205    end.
206
207
208%% --
209
210get_master_agent_verbosity(Opts) ->
211    get_opt(snmp_master_agent_verbosity, Opts, silence).
212
213
214%% --
215
216get_sub_agent_verbosity(Opts) ->
217    get_opt(snmp_subagent_verbosity, Opts, silence).
218
219
220%% --
221
222get_supervisor_opts(Opts) ->
223    Options = [{snmp_supervisor_verbosity, verbosity, silence}],
224    get_opts(Options, Opts, []).
225
226
227%% --
228
229get_symbolic_store_opts(Opts) ->
230    Options = [{snmp_symbolic_store_verbosity, verbosity, silence}],
231    get_opts(Options, Opts, []).
232
233
234%% --
235
236get_note_store_opts(Opts) ->
237    Options = [{snmp_note_store_verbosity, verbosity, silence}],
238    get_opts(Options, Opts, []).
239
240
241%% --
242
243get_local_db_opts(Opts) ->
244    Options = [{snmp_local_db_auto_repair, repair,    true},
245	       {snmp_local_db_verbosity,   verbosity, silence}],
246    get_opts(Options, Opts, []).
247
248
249%% --
250
251get_multi_threaded(Opts) ->
252    case get_opt(snmp_multi_threaded, Opts) of
253	{value, true} ->
254	    true;
255	{value, false} ->
256	    false;
257	_ ->
258	    false
259    end.
260
261get_versions(Opts) ->
262    F = fun(Ver) ->
263		case get_opt(Ver, Opts) of
264		    {value, true} ->
265			[Ver];
266		    {value, false} ->
267			[];
268		    _ ->
269			[Ver] % Default is true
270		end
271	end,
272    V1 = F(v1),
273    V2 = F(v2),
274    V3 = F(v3),
275    V1 ++ V2 ++ V3.
276
277get_set_mechanism(Opts) ->
278    get_opt(set_mechanism, Opts, snmpa_set).
279
280get_authentication_service(Opts) ->
281    get_opt(authentication_service, Opts, snmpa_acm).
282
283get_error_report_mod(Opts) ->
284    get_opt(snmp_error_report_mod, Opts, snmpa_error_logger).
285
286
287%% --
288
289get_net_if_opts(Opts) ->
290    Options = [{snmp_net_if_module,    module,    snmpa_net_if},
291	       {snmp_net_if_verbosity, verbosity, silence}],
292    get_opts(Options, Opts, []).
293
294get_net_if_options(Opts) ->
295    Options = [recbuf,
296	       {req_limit,          req_limit, infinity},
297	       {bind_to_ip_address, bind_to,   false},
298	       {no_reuse_address,   no_reuse,  false}],
299    get_opts(Options, Opts, []).
300
301
302%% --
303
304get_agent_type(Opts) ->
305    get_opt(snmp_agent_type, Opts, master).
306
307
308%% --
309
310get_config_dir(Opts) ->
311    case get_opt(snmp_config_dir, Opts) of
312	{value, Dir} when is_list(Dir) -> Dir;
313	{value, Bad} ->
314	    exit({bad_config, {config_dir, Bad}});
315	_ ->
316	    exit({undefined_config, config_dir})
317    end.
318
319get_master_agent_mibs(Opts) ->
320    get_opt(snmp_master_agent_mibs, Opts, []).
321
322get_force_config_load(Opts) ->
323    case get_opt(force_config_load, Opts) of
324	{value, true}  -> true;
325	{value, false} -> false;
326	_              -> false
327    end.
328
329
330%% --
331
332get_opts([], _Options, Opts) ->
333    Opts;
334get_opts([{Key1, Key2, Def}|KeyVals], Options, Opts) ->
335    %% If not found among Options, then use default value
336    case lists:keysearch(Key1, 1, Options) of
337	{value, {Key1, Val}} ->
338	    get_opts(KeyVals, Options, [{Key2, Val}|Opts]);
339	false ->
340	    get_opts(KeyVals, Options, [{Key2, Def}|Opts])
341    end;
342get_opts([Key|KeyVals], Options, Opts) ->
343    %% If not found among Options, then ignore
344    case lists:keysearch(Key, 1, Options) of
345	{value, KeyVal} ->
346	    get_opts(KeyVals, Options, [KeyVal|Opts]);
347	false ->
348	    get_opts(KeyVals, Options, Opts)
349    end.
350
351
352%% --
353
354
355get_opt(Key, Opts, Def) ->
356    case get_opt(Key, Opts) of
357	{value, Val} ->
358	    Val;
359	false ->
360	    Def
361    end.
362
363get_opt(Key, Opts) ->
364    case lists:keysearch(Key, 1, Opts) of
365	{value, {_, Val}} ->
366	    {value, Val};
367	false ->
368	    false
369    end.
370
371% i(F) ->
372%     i(F, []).
373
374% i(F, A) ->
375%     io:format("~p: " ++ F ++ "~n", [?MODULE|A]).
376