1
%% ``The contents of this file are subject to the Erlang Public License,
2
%% Version 1.1, (the "License"); you may not use this file except in
3
%% compliance with the License. You should have received a copy of the
4
%% Erlang Public License along with this software. If not, it can be
5
%% retrieved via the world wide web at http://www.erlang.org/.
7
%% Software distributed under the License is distributed on an "AS IS"
8
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
9
%% the License for the specific language governing rights and limitations
12
%% The Initial Developer of the Original Code is Ericsson Utvecklings AB.
13
%% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
14
%% AB. All Rights Reserved.''
18
-module(snmp_agent_sup).
20
-behaviour(supervisor).
23
-export([start_link/1, start_link/2, start_subagent/3, stop_subagent/1]).
28
%%%-----------------------------------------------------------------
29
%%% This is a supervisor for the mib processes. Each agent has one
31
%%%-----------------------------------------------------------------
33
supervisor:start_link({local, snmp_agent_sup}, snmp_agent_sup,
35
start_link(Prio, AgentSpec) ->
36
supervisor:start_link({local, snmp_agent_sup}, snmp_agent_sup,
39
start_subagent(ParentAgent, Subtree, Mibs) ->
40
Max = find_max(supervisor:which_children(snmp_agent_sup), 1),
41
[{_, Prio}] = ets:lookup(snmp_agent_table, priority),
43
Options = [{priority, Prio}, {mibs,Mibs}, {misc_sup, snmp_misc_sup}],
44
Agent = {{sub_agent, Max},
45
{snmp_agent, start_link,
46
[ParentAgent, Ref, Options]},
47
permanent, 2000, worker, [snmp_agent]},
48
case supervisor:start_child(snmp_agent_sup, Agent) of
50
snmp_agent:register_subagent(ParentAgent, Subtree, SA),
56
stop_subagent(SubAgentPid) ->
57
case find_name(supervisor:which_children(snmp_agent_sup), SubAgentPid) of
58
undefined -> no_such_child;
60
supervisor:terminate_child(snmp_agent_sup, Name),
61
supervisor:delete_child(snmp_agent_sup, Name),
65
init([Prio, Children]) ->
66
process_flag(priority, Prio),
67
%% 20 restarts in ten minutes. If the agent crashes and restarts,
68
%% it may very well crash again, because the management application
69
%% tries to resend the very same request. This depends on the resend
70
%% strategy used by the management application.
71
SupFlags = {one_for_one, 20, 600},
72
{ok, {SupFlags, Children}}.
75
find_max([{{sub_agent, N}, _, _, _} | T], M) when N >= M -> find_max(T, N+1);
76
find_max([_|T], M) -> find_max(T, M);
79
find_name([{Name, Pid, _, _} | _T], Pid)-> Name;
80
find_name([_|T], Pid) -> find_name(T, Pid);
81
find_name([], _Pid) -> undefined.