snmpa_supervisor.erl
来自「OTP是开放电信平台的简称」· ERL 代码 · 共 556 行 · 第 1/2 页
ERL
556 行
%% ``The contents of this file are subject to the Erlang Public License,%% Version 1.1, (the "License"); you may not use this file except in%% compliance with the License. You should have received a copy of the%% Erlang Public License along with this software. If not, it can be%% retrieved via the world wide web at http://www.erlang.org/.%% %% Software distributed under the License is distributed on an "AS IS"%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See%% the License for the specific language governing rights and limitations%% under the License.%% %% The Initial Developer of the Original Code is Ericsson Utvecklings AB.%% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings%% AB. All Rights Reserved.''%% %% $Id$%%-module(snmpa_supervisor).-behaviour(supervisor).%% External exports-export([start_link/2]).-export([start_sub_sup/1, start_master_sup/1]).-export([start_sub_agent/3, stop_sub_agent/1]).%% Internal exports-export([init/1, config/2]).-define(SERVER, ?MODULE).-include("snmpa_internal.hrl").-include("snmp_verbosity.hrl").-include("snmp_debug.hrl").%%-----------------------------------------------------------------%% Process structure%% =================%%%% ___________________ supervisor __________________%% / | | \ \ %% ___misc_sup___ target_cache symbolic_store local_db agent_sup%% / | \ | | %% mib net_if note_store MA - SA%%%% The supervisor (one at each node) starts:%% snmpa_symbolic_store (one at each node)%% snmpa_local_db (one at each node)%% snmpa_target_cache (one at each node) %% MA - which starts%% own mib (hangs it under misc_sup)%% net_if (hangs it under misc_sup) %% note_store (hangs it under misc_sup)%% SAs - which starts%% own mib (hangs it under misc_sup)%%%% This structure is intended to make the agent fault tolerant. The%% agent processes (by far most code is in these processes) can be%% restarted in case of a failure, as all data needed by these procs%% are stored in the other procs. Any other process crash leads to%% that the entire snmpa_supervisor crashes. If it is restarted, all%% dynamically loaded mibs must be reloaded.%%%% It is up to the supervisor to configure the internal and%% external mibs. Note that the %% agent group (internal MIB) and sysObjectID *must* be configured%% in some way.%%-----------------------------------------------------------------start_link(Type, Opts) -> ?d("start_link -> entry with" "~n Type. ~p" "~n Opts. ~p", [Type, Opts]), start_link(get_agent_type(Opts), Opts, Type).start_link(sub, Opts, _Type) -> start_sub_sup(Opts);start_link(master, Opts, normal) -> start_master_sup(Opts);start_link(master, Opts, {takeover, Node}) when node(Node) -> case start_master_sup(Opts) of {ok, Pid} -> OwnMibNames = get_own_loaded_mibs(), try_load_other_loaded_mibs(Node, OwnMibNames), {ok, Pid}; Else -> Else end.get_own_loaded_mibs() -> AgentInfo = snmpa:info(snmp_master_agent), [ Name || {Name, _, _} <- loaded_mibs(AgentInfo) ].try_load_other_loaded_mibs(Node, OwnMibs) -> case rpc:call(Node, snmpa, info, [snmp_master_agent]) of {badrpc, R} -> error_msg("could not takeover loaded mibs: ~p", [R]); AgentInfo -> LoadedMibs = loaded_mibs(AgentInfo), MibsToLoad = mibs_to_load(LoadedMibs, OwnMibs), lists:foreach(fun(M) -> takeover_mib(M) end, MibsToLoad) end.loaded_mibs(AgentInfo) -> {value, {_, MibInfo}} = key1search(mib_server, AgentInfo), {value, {_, LoadedMibs}} = key1search(loaded_mibs, MibInfo), LoadedMibs.mibs_to_load(OtherMibs, OwnMibs) -> [{N, S, F} || {N, S, F} <- OtherMibs, not lists:member(N, OwnMibs)].takeover_mib({'STANDARD-MIB', _Symbolic, _FileName}) -> ok;takeover_mib({'SNMPv2-MIB', _Symbolic, _FileName}) -> ok;takeover_mib({_MibName, _Symbolic, FileName}) -> case snmpa:load_mibs(snmp_master_agent, [FileName]) of ok -> ok; {error, R} -> error_msg("could not reload mib ~p: ~p", [FileName, R]) end.%% ----------------------------------------------------------------start_sub_sup(Opts) -> ?d("start_sub_sup -> entry with" "~n Opts: ~p", [Opts]), (catch do_start_sub_sup(Opts)).do_start_sub_sup(Opts) -> verify_mandatory([db_dir], Opts), ?d("do_start_sub_sup -> start (sub) supervisor",[]), supervisor:start_link({local, ?SERVER}, ?MODULE, [sub, Opts]). start_master_sup(Opts) -> (catch do_start_master_sup(Opts)).do_start_master_sup(Opts) -> verify_mandatory([db_dir], Opts), supervisor:start_link({local, ?SERVER}, ?MODULE, [master, Opts]). verify_mandatory([], _) -> ok;verify_mandatory([Key|Keys], Opts) -> case lists:keymember(Key, 1, Opts) of true -> verify_mandatory(Keys, Opts); false -> throw({error, {missing_mandatory_option, Key}}) end.%% ----------------------------------------------------------------start_sub_agent(ParentAgent, Subtree, Mibs) when pid(ParentAgent), list(Mibs) -> ?d("start_sub_agent -> entry with" "~n ParentAgent: ~p" "~n Subtree: ~p" "~n Mibs: ~p", [ParentAgent, Subtree, Mibs]), snmpa_agent_sup:start_subagent(ParentAgent, Subtree, Mibs).stop_sub_agent(SubAgentPid) -> snmpa_agent_sup:stop_subagent(SubAgentPid).%% ----------------------------------------------------------------init([AgentType, Opts]) -> ?d("init -> entry with" "~n AgentType: ~p" "~n Opts: ~p", [AgentType, Opts]), put(sname, asup), put(verbosity,get_verbosity(Opts)), ?vlog("starting",[]), ?vdebug("create agent table",[]), ets:new(snmp_agent_table, [set, public, named_table]), ?vdebug("create community cache",[]), ets:new(snmp_community_cache, [bag, public, named_table]), %% Get restart type for the agent Restart = get_opt(restart_type, Opts, permanent), ?vdebug("agent restart type: ~w", [Restart]), %% -- Agent type -- ets:insert(snmp_agent_table, {agent_type, AgentType}), %% -- Prio -- Prio = get_opt(priority, Opts, normal), ?vdebug("[agent table] store priority: ~p",[Prio]), ets:insert(snmp_agent_table, {priority, Prio}), %% -- Versions -- Vsns = get_opt(versions, Opts, [v1,v2,v3]), ?vdebug("[agent table] store versions: ~p",[Vsns]), ets:insert(snmp_agent_table, {versions, Vsns}), %% -- DB-directory -- DbDir = get_opt(db_dir, Opts), ?vdebug("[agent table] store db_dir: ~n ~p",[DbDir]), ets:insert(snmp_agent_table, {db_dir, filename:join([DbDir])}), DbInitError = get_opt(db_init_error, Opts, terminate), ?vdebug("[agent table] store db_init_error: ~n ~p",[DbInitError]), ets:insert(snmp_agent_table, {db_init_error, DbInitError}), %% -- Error report module -- ErrorReportMod = get_opt(error_report_mod, Opts, snmpa_error_logger), ?vdebug("[agent table] store error report module: ~w",[ErrorReportMod]), ets:insert(snmp_agent_table, {error_report_mod, ErrorReportMod}), %% -- mib storage -- MibStorage = case get_opt(mib_storage, Opts, ets) of dets -> {dets, DbDir}; {dets, default} -> {dets, DbDir}; {dets, default, Act} -> {dets, DbDir, Act}; {ets, default} -> {ets, DbDir}; mnesia -> {mnesia, erlang:nodes()}; {mnesia, visible} -> {mnesia, erlang:nodes(visible)}; {mnesia, connected} -> {mnesia, erlang:nodes(connected)}; Other -> Other end, ?vdebug("[agent table] store mib storage: ~w",[MibStorage]), ets:insert(snmp_agent_table, {mib_storage, MibStorage}), %% -- Agent mib storage -- AgentMibStorage = get_opt(agent_mib_storage, Opts, persistent), %% ?vdebug("[agent table] store agent mib storage: ~w",[AgentMibStorage]), ets:insert(snmp_agent_table, {agent_mib_storage, AgentMibStorage}), %% -- System start time -- ?vdebug("[agent table] store system start time",[]), ets:insert(snmp_agent_table, {system_start_time, snmp_misc:now(cs)}), %% -- Symbolic store options -- SsOpts = get_opt(symbolic_store, Opts, []), ?vdebug("[agent table] store symbolic store options: ~w",[SsOpts]), ets:insert(snmp_agent_table, {symbolic_store, SsOpts}), %% -- Local DB options -- LdbOpts = get_opt(local_db, Opts, []), ?vdebug("[agent table] store local db options: ~w",[LdbOpts]), ets:insert(snmp_agent_table, {local_db, LdbOpts}), %% -- Target cache options -- TargetCacheOpts = get_opt(target_cache, Opts, []), ?vdebug("[agent table] store target cache options: ~w",[TargetCacheOpts]), ets:insert(snmp_agent_table, {target_cache, TargetCacheOpts}), %% -- Specs -- SupFlags = {one_for_all, 0, 3600}, MiscSupSpec = sup_spec(snmpa_misc_sup, [], Restart, infinity), SymStoreOpts = [{mib_storage, MibStorage} | SsOpts], SymStoreArgs = [Prio, SymStoreOpts], SymStoreSpec = worker_spec(snmpa_symbolic_store, SymStoreArgs, Restart, 2000), LdbArgs = [Prio, DbDir, LdbOpts],
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?