snmpm_server.erl

来自「OTP是开放电信平台的简称」· ERL 代码 · 共 2,152 行 · 第 1/5 页

ERL
2,152
字号
    MiniMIB = snmpm_config:make_mini_mib(),    State = #state{mini_mib       = MiniMIB,		   gct            = GCT,		   note_store     = NoteStore,		   note_store_ref = NoteStoreRef,		   net_if         = NetIf,		   net_if_mod     = NetIfModule,		   net_if_ref     = NetIfRef},    ?vlog("started", []),    {ok, State}.do_init_note_store(Prio) ->    ?vdebug("try start note store", []),    {ok, Verbosity} = snmpm_config:system_info(note_store_verbosity),    {ok, Timeout}   = snmpm_config:system_info(note_store_timeout),    Opts = [{sname,     mns}, 	    {verbosity, Verbosity}, 	    {timeout,   Timeout}],    case snmpm_misc_sup:start_note_store(Prio, Opts) of	{ok, Pid} ->	    ?vtrace("do_init_note_store -> Pid: ~p", [Pid]),	    Ref = erlang:monitor(process, Pid),	    {Pid, Ref};	{error, Reason} ->	    ?vlog("failed starting note-store - Reason: "		  "~n", [Reason]),	    throw({error, {failed_starting_note_store, Reason}})    end.do_init_net_if(NoteStore) ->    ?vdebug("try start net if", []),    {ok, NetIfModule} = snmpm_config:system_info(net_if_module),    case snmpm_misc_sup:start_net_if(NetIfModule, NoteStore) of	{ok, Pid} ->	    ?vtrace("do_init_net_if -> Pid: ~p", [Pid]),	    Ref = erlang:monitor(process, Pid),	    {Pid, NetIfModule, Ref};	{error, Reason} ->	    ?vlog("failed starting net-if - Reason: "		  "~n", [Reason]),	    throw({error, {failed_starting_net_if, Reason}})    end.%% ---------------------------------------------------------------------%% ---------------------------------------------------------------------handle_call({monitor_user, Id, Pid}, _From, State) when pid(Pid) ->    ?vlog("received monitor_user request for ~w [~w]", [Id, Pid]),    Reply = 	case ets:lookup(snmpm_monitor_table, Id) of	    [#monitor{proc = Pid}] ->		?vdebug("already monitored", []),		ok;	    [#monitor{proc = OtherPid}] ->		?vinfo("already registered to ~w", [OtherPid]),		{error, {already_monitored, OtherPid}};	    [] ->		Ref = erlang:monitor(process, Pid),		?vtrace("monitor ref: ~w", [Ref]),		Mon = #monitor{id = Id, mon = Ref, proc = Pid},		ets:insert(snmpm_monitor_table, Mon),		ok	end,    {reply, Reply, State};handle_call({unregister_user, UserId}, _From, State) ->    ?vlog("received request to unregister user ~p", [UserId]),    %% 1) If this user is monitored, then demonitor    case ets:lookup(snmpm_monitor_table, UserId) of	[] ->	    ok;	[#monitor{mon = M}] ->	    maybe_demonitor(M), % This is really overkill (meybe_), but...	    ok    end,    %% 2) Delete all outstanding requests from this user    Pat = #request{user_id = UserId, 		   id = '$1', ref = '$2', mon = '$3', _ = '_'},    Match = ets:match(snmpm_request_table, Pat),    F1 = fun([ReqId, Ref, MonRef]) -> 		 ets:delete(snmpm_request_table, ReqId),		 cancel_timer(Ref),		 maybe_demonitor(MonRef),		 ok	end,    lists:foreach(F1, Match),        %% 3) Unregister all agents registered by this user    Agents = snmpm_config:which_agents(UserId),    F2 = fun({Addr, Port}) ->		 snmpm_config:unregister_agent(UserId, Addr, Port)	 end,    lists:foreach(F2, Agents),    %% 4) Unregister the user    Reply = snmpm_config:unregister_user(UserId),    {reply, Reply, State};%% We will reply to this request later, when the reply comes in from the%% agent, or when the timeout hits (unless we get an error now).handle_call({sync_get, Pid, UserId, Addr, Port, CtxName, Oids, Timeout, ExtraInfo}, From, State) ->    ?vlog("received sync_get [~p] request", [CtxName]),    case (catch handle_sync_get(Pid, 				UserId, Addr, Port, CtxName, Oids, 				Timeout, ExtraInfo, From, State)) of	ok ->	    {noreply, State};	Error ->	    {reply, Error, State}    end;handle_call({sync_get_next, Pid, UserId, Addr, Port, CtxName, Oids, Timeout, ExtraInfo}, From, State) ->    ?vlog("received sync_get_next [~p] request", [CtxName]),    case (catch handle_sync_get_next(Pid, 				     UserId, Addr, Port, CtxName, Oids, 				     Timeout, ExtraInfo, From, State)) of	ok ->	    {noreply, State};	Error ->	    {reply, Error, State}    end;%% Check agent version? This op not in v1handle_call({sync_get_bulk, Pid, UserId, Addr, Port, 	     NonRep, MaxRep, CtxName, Oids, Timeout, ExtraInfo}, 	    From, State) ->    ?vlog("received sync_get_bulk [~p] request", [CtxName]),    case (catch handle_sync_get_bulk(Pid, 				     UserId, Addr, Port, CtxName, 				     NonRep, MaxRep, Oids, 				     Timeout, ExtraInfo, From, State)) of	ok ->	    {noreply, State};	Error ->	    {reply, Error, State}    end;handle_call({sync_set, Pid, UserId, Addr, Port, 	     CtxName, VarsAndVals, Timeout, ExtraInfo}, 	    From, State) ->    ?vlog("received sync_set [~p] request", [CtxName]),    case (catch handle_sync_set(Pid, 				UserId, Addr, Port, CtxName, VarsAndVals, 				Timeout, ExtraInfo, From, State)) of	ok ->	    {noreply, State};	Error ->	    {reply, Error, State}    end;handle_call({async_get, Pid, UserId, Addr, Port, 	     CtxName, Oids, Expire, ExtraInfo}, 	    _From, State) ->    ?vlog("received async_get [~p] request", [CtxName]),    Reply = (catch handle_async_get(Pid, UserId, Addr, Port, CtxName, Oids, 				    Expire, ExtraInfo, State)),    {reply, Reply, State};handle_call({async_get_next, Pid, UserId, Addr, Port, 	     CtxName, Oids, Expire, ExtraInfo}, 	    _From, State) ->    ?vlog("received async_get_next [~p] request", [CtxName]),    Reply = (catch handle_async_get_next(Pid, UserId, Addr, Port, CtxName, 					 Oids, Expire, ExtraInfo, State)),    {reply, Reply, State};%% Check agent version? This op not in v1handle_call({async_get_bulk, Pid, UserId, Addr, Port, 	     NonRep, MaxRep, CtxName, Oids, Expire, ExtraInfo}, 	    _From, State) ->    ?vlog("received async_get_bulk [~p] request", [CtxName]),    Reply = (catch handle_async_get_bulk(Pid, 					 UserId, Addr, Port, CtxName, 					 NonRep, MaxRep, Oids, 					 Expire, ExtraInfo, State)),    {reply, Reply, State};handle_call({async_set, Pid, UserId, Addr, Port, 	     CtxName, VarsAndVals, Expire, ExtraInfo}, 	    _From, State) ->    ?vlog("received async_set [~p] request", [CtxName]),    Reply = (catch handle_async_set(Pid, UserId, Addr, Port, CtxName, 				    VarsAndVals, Expire, ExtraInfo, State)),    {reply, Reply, State};handle_call({cancel_async_request, UserId, ReqId}, _From, State) ->    ?vlog("received cancel_async_request request", []),    Reply = (catch handle_cancel_async_request(UserId, ReqId, State)),    {reply, Reply, State};handle_call({discovery, Pid, UserId, BAddr, Port, Config, Expire, ExtraInfo}, 	    _From, State) ->    ?vlog("received discovery request", []),    Reply = (catch handle_discovery(Pid, UserId, BAddr, Port, Config, 				    Expire, ExtraInfo, State)),    {reply, Reply, State};handle_call({load_mib, Mib}, _From, State) ->    ?vlog("received load_mib request", []),    case snmpm_config:load_mib(Mib) of	ok ->	    MiniMIB = snmpm_config:make_mini_mib(),	    {reply, ok, State#state{mini_mib = MiniMIB}};	Error ->	    {reply, Error, State}    end;handle_call({unload_mib, Mib}, _From, State) ->    ?vlog("received unload_mib request", []),    case snmpm_config:unload_mib(Mib) of	ok ->	    MiniMIB = snmpm_config:make_mini_mib(),	    {reply, ok, State#state{mini_mib = MiniMIB}};	Error ->	    {reply, Error, State}    end;handle_call({verbosity, Verbosity}, _From, State) ->    ?vlog("received verbosity request", []),    put(verbosity, Verbosity),    {reply, ok, State};handle_call({verbosity, net_if, Verbosity}, _From, 	    #state{net_if = Pid, net_if_mod = Mod} = State) ->    ?vlog("received net_if verbosity request", []),    Mod:verbosity(Pid, Verbosity),    {reply, ok, State};handle_call({verbosity, note_store, Verbosity}, _From, 	    #state{note_store = Pid} = State) ->    ?vlog("received note_store verbosity request", []),    snmp_note_store:verbosity(Pid, Verbosity),    {reply, ok, State};handle_call(reconfigure, _From, State) ->    ?vlog("received reconfigure request", []),    Reply = {error, not_implemented},    {reply, Reply, State};handle_call(info, _From, State) ->    ?vlog("received info request", []),    Reply = get_info(State),     {reply, Reply, State};handle_call(is_started, _From, State) ->    ?vlog("received is_started request", []),    IsStarted = is_started(State),     {reply, IsStarted, State};handle_call(stop, _From, State) ->    ?vlog("received stop request", []),    {stop, normal, ok, State};handle_call(Req, _From, State) ->    warning_msg("received unknown request: ~n~p", [Req]),    {reply, {error, unknown_request}, State}.handle_cast(Msg, State) ->    warning_msg("received unknown message: ~n~p", [Msg]),    {noreply, State}.handle_info({sync_timeout, ReqId, From}, State) ->    ?vlog("received sync_timeout [~w] message", [ReqId]),    handle_sync_timeout(ReqId, From, State),    {noreply, State};handle_info({snmp_error, Pdu, Reason}, State) ->    ?vlog("received snmp_error message", []),    handle_snmp_error(Pdu, Reason, State),    {noreply, State};handle_info({snmp_error, Reason, Addr, Port}, State) ->    ?vlog("received snmp_error message", []),    handle_snmp_error(Addr, Port, -1, Reason, State),    {noreply, State};handle_info({snmp_error, ReqId, Reason, Addr, Port}, State) ->    ?vlog("received snmp_error message", []),    handle_snmp_error(Addr, Port, ReqId, Reason, State),    {noreply, State};%% handle_info({snmp_error, ReqId, Pdu, Reason, Addr, Port}, State) ->%%     ?vlog("received snmp_error message", []),%%     handle_snmp_error(Pdu, ReqId, Reason, Addr, Port, State),%%     {noreply, State};handle_info({snmp_pdu, Pdu, Addr, Port}, State) ->    ?vlog("received snmp_pdu message", []),    handle_snmp_pdu(Pdu, Addr, Port, State),    {noreply, State};handle_info({snmp_trap, Trap, Addr, Port}, State) ->    ?vlog("received snmp_trap message", []),    handle_snmp_trap(Trap, Addr, Port, State),    {noreply, State};handle_info({snmp_inform, Ref, Pdu, Addr, Port}, State) ->    ?vlog("received snmp_inform message", []),    handle_snmp_inform(Ref, Pdu, Addr, Port, State),    {noreply, State};handle_info({snmp_report, {ok, Pdu}, Addr, Port}, State) ->    handle_snmp_report(Pdu, Addr, Port, State),    {noreply, State};handle_info({snmp_report, {error, ReqId, Info, Pdu}, Addr, Port}, State) ->    handle_snmp_report(ReqId, Pdu, Info, Addr, Port, State),    {noreply, State};handle_info(gc_timeout, #state{gct = GCT} = State) ->    ?vlog("received gc_timeout message", []),    handle_gc(GCT),    {noreply, State};handle_info({'DOWN', _MonRef, process, Pid, _Reason}, 	    #state{note_store = NoteStore, 		   net_if     = Pid} = State) ->    ?vlog("received 'DOWN' message regarding net_if", []),    {NetIf, _, Ref} = do_init_net_if(NoteStore),    {noreply, State#state{net_if = NetIf, net_if_ref = Ref}};handle_info({'DOWN', _MonRef, process, Pid, _Reason}, 	    #state{note_store = Pid, 		   net_if     = NetIf,		   net_if_mod = Mod} = State) ->    ?vlog("received 'DOWN' message regarding note_store", []),    {ok, Prio} = snmpm_config:system_info(prio),    {NoteStore, Ref} = do_init_note_store(Prio),    Mod:note_store(NetIf, NoteStore),    {noreply, State#state{note_store = NoteStore, note_store_ref = Ref}};handle_info({'DOWN', MonRef, process, Pid, Reason}, State) ->    ?vlog("received 'DOWN' message (~w) from ~w "	  "~n   Reason: ~p", [MonRef, Pid, Reason]),    handle_down(MonRef),    {noreply, State};handle_info({'EXIT', Pid, Reason}, #state{gct = Pid} = State) ->    ?vlog("received 'EXIT' message from the GCT (~w) process: "	  "~n   ~p", [Pid, Reason]),    {ok, Timeout} = snmpm_config:system_info(server_timeout),    {ok, GCT} = gct_start(Timeout),    {noreply, State#state{gct = GCT}};handle_info(Info, State) ->    warning_msg("received unknown info: ~n~p", [Info]),    {noreply, State}.%%----------------------------------------------------------%% Code change%%----------------------------------------------------------                                                                              % downgradecode_change({down, _Vsn}, #state{gct = Pid} = State, _Extra) ->    ?d("code_change(down) -> entry", []),    gct_code_change(Pid),    {ok, State}; % upgradecode_change(_Vsn, #state{gct = Pid} = State0, _Extra) ->    ?d("code_change(up) -> entry", []),    gct_code_change(Pid),    MiniMIB = snmpm_config:make_mini_mib(),    State = State0#state{mini_mib = MiniMIB},    {ok, State}. %%----------------------------------------------------------%% Terminate%%----------------------------------------------------------                                                                              terminate(Reason, #state{gct = GCT}) ->    ?vdebug("terminate: ~p",[Reason]),    gct_stop(GCT),    snmpm_misc_sup:stop_note_store(),    snmpm_misc_sup:stop_net_if(),    ok.%%----------------------------------------------------------------------%% %%----------------------------------------------------------------------handle_sync_get(Pid, UserId, Addr, Port, CtxName, Oids, Timeout, ExtraInfo, 		From, State) ->        ?vtrace("handle_sync_get -> entry with"	    "~n   Pid:     ~p"	    "~n   UserId:  ~p"	    "~n   Addr:    ~p"	    "~n   Port:    ~p"	    "~n   CtxName: ~p"	    "~n   Oids:    ~p"	    "~n   Timeout: ~p"	    "~n   From:    ~p", 	    [Pid, UserId, Addr, Port, CtxName, Oids, Timeout, From]),    case agent_data(Addr, Port, CtxName) of	{ok, Vsn, MsgData} ->	    ?vtrace("handle_sync_get -> send a ~p message", [Vsn]),

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?