243
253
when list(Mib) ->
244
254
?vtrace("mib operation on mib ~p", [Mib]),
245
255
case apply(snmpa_mib_data, Operation, [Data0,Mib,MeOverride,TeOverride]) of
246
{error, 'already loaded'} when Operation == load_mib,
256
{error, 'already loaded'} when (Operation =:= load_mib) andalso
248
258
?vlog("ignore mib ~p -> already loaded", [Mib]),
250
{error, 'not loaded'} when Operation == unload_mib,
260
{error, 'not loaded'} when (Operation =:= unload_mib) andalso
252
262
?vlog("ignore mib ~p -> not loaded", [Mib]),
254
264
{error, Reason} ->
265
275
%%-----------------------------------------------------------------
266
276
%% Handle messages
267
277
%%-----------------------------------------------------------------
268
handle_call({lookup, Oid}, _From, #state{data = Data} = State) ->
269
?vlog("lookup ~p",[Oid]),
270
Reply = snmpa_mib_data:lookup(Data, Oid),
279
handle_call(invalidate_cache, _From, #state{cache = Cache} = State) ->
280
?vlog("invalidate_cache", []),
281
NewCache = maybe_invalidate_cache(Cache),
282
{reply, ignore, State#state{cache = NewCache}};
284
handle_call({lookup, Oid}, _From,
285
#state{data = Data, cache = Cache} = State) ->
286
?vlog("lookup ~p", [Oid]),
288
Reply = case maybe_cache_lookup(Cache, Key) of
290
snmpa_mib_data:lookup(Data, Oid);
292
Rep = snmpa_mib_data:lookup(Data, Oid),
293
ets:insert(Cache, {Key, Rep}),
296
?vdebug("lookup -> found in cache", []),
299
?vdebug("lookup -> Reply: ~p",[Reply]),
271
300
{reply, Reply, State};
273
302
handle_call({which_mib, Oid}, _From, #state{data = Data} = State) ->
276
305
?vdebug("which_mib: ~p",[Reply]),
277
306
{reply, Reply, State};
279
handle_call({next, Oid, MibView}, _From, #state{data = Data} = State) ->
280
?vlog("next ~p [~p]",[Oid,MibView]),
281
Reply = snmpa_mib_data:next(Data, Oid, MibView),
282
?vdebug("next: ~p",[Reply]),
308
handle_call({next, Oid, MibView}, _From,
309
#state{data = Data, cache = Cache} = State) ->
310
?vlog("next ~p [~p]",[Oid,MibView]),
311
Key = {next, Oid, MibView},
312
Reply = case maybe_cache_lookup(Cache, Key) of
314
snmpa_mib_data:next(Data, Oid, MibView);
316
Rep = snmpa_mib_data:next(Data, Oid, MibView),
317
ets:insert(Cache, {Key, Rep}),
320
?vdebug("lookup -> found in cache", []),
323
?vdebug("next -> Reply: ~p",[Reply]),
283
324
{reply, Reply, State};
285
326
handle_call({load_mibs, Mibs}, _From,
286
#state{data = Data, teo = TeOverride, meo = MeOverride} = State) ->
330
cache = Cache} = State) ->
287
331
?vlog("load mibs ~p",[Mibs]),
333
NewCache = maybe_invalidate_cache(Cache),
289
335
case (catch mib_operations(load_mib, Mibs, Data,
290
336
MeOverride, TeOverride)) of
297
343
snmpa_mib_data:sync(NData),
298
{reply, Reply, State#state{data = NData}};
344
{reply, Reply, State#state{data = NData, cache = NewCache}};
300
346
handle_call({unload_mibs, Mibs}, _From,
301
#state{data = Data, teo = TeOverride, meo = MeOverride} = State) ->
350
cache = Cache} = State) ->
302
351
?vlog("unload mibs ~p",[Mibs]),
353
NewCache = maybe_invalidate_cache(Cache),
304
356
case (catch mib_operations(unload_mib, Mibs, Data,
305
357
MeOverride, TeOverride)) of
306
358
{'aborted at', Mib, NewData, Reason} ->
307
359
?vlog("aborted at ~p for reason ~p",[Mib,Reason]),
308
{NewData,{error, {'unload aborted at', Mib, Reason}}};
360
{NewData, {error, {'unload aborted at', Mib, Reason}}};
312
364
snmpa_mib_data:sync(NData),
313
{reply, Reply, State#state{data = NData}};
365
{reply, Reply, State#state{data = NData, cache = NewCache}};
315
367
handle_call(which_mibs, _From, #state{data = Data} = State) ->
316
368
?vlog("which mibs",[]),
322
374
Reply = snmpa_mib_data:whereis_mib(Data, Mib),
323
375
{reply, Reply, State};
325
handle_call({register_subagent, Oid, Pid}, _From, State) ->
377
handle_call({register_subagent, Oid, Pid}, _From,
378
#state{data = Data, cache = Cache} = State) ->
326
379
?vlog("register subagent ~p, ~p",[Oid,Pid]),
327
case snmpa_mib_data:register_subagent(State#state.data, Oid, Pid) of
381
NewCache = maybe_invalidate_cache(Cache),
382
case snmpa_mib_data:register_subagent(Data, Oid, Pid) of
328
383
{error, Reason} ->
329
384
?vlog("registration failed: ~p",[Reason]),
330
{reply, {error, Reason}, State};
385
{reply, {error, Reason}, State#state{cache = NewCache}};
332
{reply, ok, State#state{data = NewData}}
387
{reply, ok, State#state{data = NewData, cache = NewCache}}
335
handle_call({unregister_subagent, OidOrPid}, _From, State) ->
390
handle_call({unregister_subagent, OidOrPid}, _From,
391
#state{data = Data, cache = Cache} = State) ->
336
392
?vlog("unregister subagent ~p",[OidOrPid]),
337
case snmpa_mib_data:unregister_subagent(State#state.data, OidOrPid) of
394
NewCache = maybe_invalidate_cache(Cache),
395
case snmpa_mib_data:unregister_subagent(Data, OidOrPid) of
338
396
{ok, NewData, DeletedSubagentPid} ->
339
{reply, {ok, DeletedSubagentPid}, State#state{data=NewData}};
397
{reply, {ok, DeletedSubagentPid}, State#state{data = NewData,
340
399
{error, Reason} ->
341
400
?vlog("unregistration failed: ~p",[Reason]),
342
{reply, {error, Reason}, State};
401
{reply, {error, Reason}, State#state{cache = NewCache}};
344
{reply, ok, State#state{data = NewData}}
403
{reply, ok, State#state{data = NewData, cache = NewCache}}
347
handle_call(info, _From, #state{data = Data} = State) ->
406
handle_call(info, _From, #state{data = Data, cache = Cache} = State) ->
348
407
?vlog("info",[]),
349
{reply, catch snmpa_mib_data:info(Data), State};
409
case (catch snmpa_mib_data:info(Data)) of
410
Info when is_list(Info) ->
411
[{cache, size_cache(Cache)} | Info];
415
{reply, Reply, State};
351
417
handle_call({info, Type}, _From, #state{data = Data} = State) ->
352
418
?vlog("info ~p",[Type]),
353
Reply = case (catch snmpa_mib_data:info(Data, Type)) of
354
Info when list(Info) ->
420
case (catch snmpa_mib_data:info(Data, Type)) of
421
Info when is_list(Info) ->
359
426
{reply, Reply, State};
361
428
handle_call(dump, _From, State) ->
445
code_change({down, _Vsn}, S1, downgrade_to_pre_4_7) ->
446
#state{data = Data, meo = MEO, teo = TEO, backup = B} = S1,
447
stop_backup_server(B),
448
NData = snmpa_mib_data:code_change(down, Data),
449
S2 = {state, NData, MEO, TEO},
512
code_change({down, _Vsn}, S1, downgrade_to_pre_4_12) ->
513
#state{data = Data, meo = MEO, teo = TEO, backup = B, cache = Cache} = S1,
515
S2 = {state, Data, MEO, TEO, B},
454
code_change(_Vsn, S1, upgrade_from_pre_4_7) ->
455
{state, Data, MEO, TEO} = S1,
456
NData = snmpa_mib_data:code_change(up, Data),
457
S2 = #state{data = NData, meo = MEO, teo = TEO},
520
code_change(_Vsn, S1, upgrade_from_pre_4_12) ->
521
{state, Data, MEO, TEO, B} = S1,
523
S2 = #state{data = Data, meo = MEO, teo = TEO, backup = B, cache = Cache},
460
526
code_change(_Vsn, State, _Extra) ->
464
stop_backup_server(undefined) ->
530
%%-----------------------------------------------------------------
531
%% Option access functions
532
%%-----------------------------------------------------------------
534
get_verbosity(Options) ->
535
snmp_misc:get_option(verbosity, Options, ?default_verbosity).
537
get_me_override(Options) ->
538
snmp_misc:get_option(mibentry_override, Options, false).
540
get_te_override(Options) ->
541
snmp_misc:get_option(trapentry_override, Options, false).
543
get_mib_storage(Options) ->
544
snmp_misc:get_option(mib_storage, Options, ets).
547
%% ----------------------------------------------------------------
549
maybe_create_cache(Options) when is_list(Options) ->
550
case snmp_misc:get_option(cache, Options, ?DEFAULT_CACHE_USAGE) of
557
maybe_invalidate_cache(?NO_CACHE) ->
559
maybe_invalidate_cache(Cache) ->
564
ets:new(snmpa_mib_cache, [set, protected, {keypos, 1}]).
566
del_cache(?NO_CACHE) ->
466
stop_backup_server({Pid, _}) when pid(Pid) ->
471
%%-----------------------------------------------------------------
472
%% Option access functions
473
%%-----------------------------------------------------------------
476
snmp_misc:get_option(verbosity,O,?default_verbosity).
478
get_me_override(O) ->
479
snmp_misc:get_option(mibentry_override,O,false).
481
get_te_override(O) ->
482
snmp_misc:get_option(trapentry_override,O,false).
484
get_mib_storage(O) ->
485
snmp_misc:get_option(mib_storage,O,ets).
571
maybe_cache_lookup(?NO_CACHE, _) ->
573
maybe_cache_lookup(Cache, Key) ->
574
ets:lookup(Cache, Key).
576
size_cache(?NO_CACHE) ->
579
case (catch ets:info(Cache, memory)) of
580
Sz when is_integer(Sz) ->
488
587
%% ----------------------------------------------------------------