snmp_config.erl

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

ERL
2,125
字号
%% ``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(snmp_config).-include_lib("kernel/include/file.hrl").-include("snmp_types.hrl").-export([config/0]).-export([write_config_file/4, append_config_file/4, read_config_file/3]).-export([write_agent_snmp_files/7, write_agent_snmp_files/12,	 write_agent_snmp_conf/5, 	 write_agent_snmp_context_conf/1, 	 write_agent_snmp_community_conf/1, 	 write_agent_snmp_standard_conf/2, 	 write_agent_snmp_target_addr_conf/4, 	 write_agent_snmp_target_addr_conf/6, 	 write_agent_snmp_target_params_conf/2, 	 write_agent_snmp_notify_conf/2, 	 write_agent_snmp_usm_conf/5, 	 write_agent_snmp_vacm_conf/3, 	 write_manager_snmp_files/8,	 write_manager_snmp_conf/5, 	 write_manager_snmp_users_conf/2, 	 write_manager_snmp_agents_conf/2, 	 write_manager_snmp_usm_conf/2 	 	]).-export([write_agent_config/3, 	 update_agent_config/2, 	 	 write_agent_context_config/3, 	 update_agent_context_config/2, 	 	 write_agent_community_config/3, 	 update_agent_community_config/2, 	 write_agent_standard_config/3, 	 update_agent_standard_config/2, 	 write_agent_target_addr_config/3, 	 update_agent_target_addr_config/2, 	 write_agent_target_params_config/3, 	 update_agent_target_params_config/2, 	 write_agent_notify_config/3, 	 update_agent_notify_config/2, 	 write_agent_vacm_config/3, 	 update_agent_vacm_config/2, 	 write_agent_usm_config/3, 	 update_agent_usm_config/2, 	 write_manager_config/3, 	 update_manager_config/2,	 write_manager_users_config/3, 	 update_manager_users_config/2,	 write_manager_agents_config/3, 	 update_manager_agents_config/2,	 write_manager_usm_config/3, 	 update_manager_usm_config/2	]).%%----------------------------------------------------------------------%% Handy SNMP configuration%%----------------------------------------------------------------------config() ->    case (catch config2()) of	ok ->	    ok;	{error, Reason} ->	    {error, Reason};	E ->	    {error, {failed, E}}    end.config2() ->    intro(),    SysAgentConfig = 	case config_agent() of	    [] ->		[];	    SAC ->		[{agent, SAC}]	end,    SysMgrConfig   = 	case config_manager() of	    [] ->		[];	    SMC ->		[{manager, SMC}]	end,    config_sys(SysAgentConfig ++ SysMgrConfig),    ok.intro() ->    i("~nSimple SNMP configuration tool (version ~s)", [?version]),    i("------------------------------------------------"),    i("Note: Non-trivial configurations still has to be"),    i("      done manually. IP addresses may be entered "),    i("      as dront.ericsson.se (UNIX only) or"),    i("      123.12.13.23"),    i("------------------------------------------------"),    ok.config_agent() ->    case (catch snmp_agent2()) of	ok ->	    [];	{ok, SysConf} ->	    SysConf;	{error, Reason} ->	    error(Reason);	{'EXIT', Reason} ->	    error(Reason);	E ->	    error({agent_config_failed, E})    end.snmp_agent2() ->    case ask("~nConfigure an agent (y/n)?", "y", fun verify_yes_or_no/1) of	yes ->	    {Vsns, ConfigDir, SysConf} = config_agent_sys(),	    config_agent_snmp(ConfigDir, Vsns),	    {ok, SysConf};	no ->	    ok    end.config_manager() ->    case (catch config_manager2()) of	ok ->	    [];	{ok, SysConf} ->	    SysConf;	{error, Reason} ->	    error(Reason);	{'EXIT', Reason} ->	    error(Reason);	E ->	    error({manager_config_failed, E})    end.config_manager2() ->    case ask("~nConfigure a manager (y/n)?", "y", fun verify_yes_or_no/1) of	yes ->	    {Vsns, ConfigDir, SysConf} = config_manager_sys(), 	    config_manager_snmp(ConfigDir, Vsns),	    {ok, SysConf};	no ->	    ok    end.config_sys(SysConfig) ->    i("~n--------------------"),    {ok, DefDir} = file:get_cwd(),    ConfigDir = ask("Configuration directory for system file (absolute path)?",		    DefDir, fun verify_dir/1),        write_sys_config_file(ConfigDir, SysConfig).%% -------------------config_agent_sys() ->    i("~nAgent system config: "      "~n--------------------"),    Prio = ask("1. Agent process priority (low/normal/high)", 	       "normal", fun verify_prio/1),    Vsns = ask("2. What SNMP version(s) should be used "	       "(1,2,3,1&2,1&2&3,2&3)?", "3", fun verify_versions/1),    %% d("Vsns: ~p", [Vsns]),    {ok, DefDir} = file:get_cwd(),    {DefConfDir, Warning} = default_agent_dir(DefDir),    ConfQ = 	if	    Warning == "" ->		"3. Configuration directory (absolute path)?";	    true ->		lists:flatten(		  io_lib:format("3. Configuration directory (absolute path)?" 				"~n   ~s", [Warning]))	end,    ConfigDir = ask(ConfQ, DefConfDir, fun verify_dir/1),    ConfigVerb = ask("4. Config verbosity "		     "(silence/info/log/debug/trace)?", 		     "silence",		     fun verify_verbosity/1),    DbDir     = ask("5. Database directory (absolute path)?", DefDir, 		    fun verify_dir/1),    MibStorageType = ask("6. Mib storage type (ets/dets/mnesia)?", "ets",			 fun verify_mib_storage_type/1),    MibStorage = 	case MibStorageType of	    ets ->		ets;	    dets ->		DetsDir = ask("6b. Mib storage directory (absolute path)?",			      DbDir, fun verify_dir/1),		DetsAction = ask("6c. Mib storage [dets] database start "				 "action "				 "(default/clear/keep)?", 				 "default", fun verify_mib_storage_action/1),		case DetsAction of		    default ->			{dets, DetsDir};		    _ ->			{dets, DetsDir, DetsAction}		end;	    mnesia ->% 		Nodes = ask("Mib storage nodes?", "none", % 			    fun verify_mib_storage_nodes/1),		Nodes = [],		MnesiaAction = ask("6b. Mib storage [mnesia] database start "				   "action "				   "(default/clear/keep)?", 				   "default", fun verify_mib_storage_action/1),		case MnesiaAction of		    default ->			{mnesia, Nodes};		    _ ->			{mnesia, Nodes, MnesiaAction}		end	end,    TargetCacheVerb = ask("7. Target cache verbosity "			  "(silence/info/log/debug/trace)?", "silence",			  fun verify_verbosity/1),    SymStoreVerb = ask("8. Symbolic store verbosity "		       "(silence/info/log/debug/trace)?", "silence",		       fun verify_verbosity/1),    LocalDbVerb = ask("9. Local DB verbosity "		       "(silence/info/log/debug/trace)?", "silence",		       fun verify_verbosity/1),    LocalDbRepair = ask("10. Local DB repair (true/false/force)?", "true",			fun verify_dets_repair/1),    LocalDbAutoSave = ask("11. Local DB auto save (infinity/milli seconds)?", 			  "5000", fun verify_dets_auto_save/1),    ErrorMod = ask("12. Error report module?", "snmpa_error_logger", fun verify_module/1),    Type = ask("13. Agent type (master/sub)?", "master", 	       fun verify_agent_type/1),    AgentConfig = 	case Type of 	    master ->		MasterAgentVerb = ask("14. Master-agent verbosity "				      "(silence/info/log/debug/trace)?", 				      "silence",				      fun verify_verbosity/1),		ForceLoad = ask("15. Shall the agent re-read the "				"configuration files during startup ~n"				"    (and ignore the configuration "				"database) (true/false)?", "true", 				fun verify_bool/1),		MultiThreaded = ask("16. Multi threaded agent (true/false)?", 				    "false",				    fun verify_bool/1),		MeOverride = ask("17. Check for duplicate mib entries when "				 "installing a mib (true/false)?", "false",				 fun verify_bool/1),		TrapOverride = ask("18. Check for duplicate trap names when "				   "installing a mib (true/false)?", "false",				   fun verify_bool/1),		MibServerVerb = ask("19. Mib server verbosity "				    "(silence/info/log/debug/trace)?", 				    "silence",				    fun verify_verbosity/1),		NoteStoreVerb = ask("20. Note store verbosity "				    "(silence/info/log/debug/trace)?", 				    "silence",				    fun verify_verbosity/1),		NoteStoreTimeout = ask("21. Note store GC timeout?", "30000",				       fun verify_timeout/1),		ATL = 		    case ask("22. Shall the agent use an audit trail log "			     "(y/n)?",			     "n", fun verify_yes_or_no/1) of			yes ->			    ATLType = ask("22b. Audit trail log type "					  "(write/read_write)?",					  "read_write", fun verify_atl_type/1),			    ATLDir = ask("22c. Where to store the "					 "audit trail log?",					 DefDir, fun verify_dir/1),			    ATLMaxFiles = ask("22d. Max number of files?", 					      "10", 					      fun verify_pos_integer/1),			    ATLMaxBytes = ask("22e. Max size (in bytes) "					      "of each file?", 					      "10240", 					      fun verify_pos_integer/1),			    ATLSize = {ATLMaxBytes, ATLMaxFiles},			    ATLRepair = ask("22f. Audit trail log repair "					    "(true/false/truncate/snmp_repair)?", "true",					    fun verify_atl_repair/1),			    [{audit_trail_log, [{type,   ATLType},						{dir,    ATLDir},						{size,   ATLSize},						{repair, ATLRepair}]}];			no ->			    []		    end,		NetIfMod = ask("23. Which network interface module shall be used?",			       "snmpa_net_if", fun verify_module/1),		NetIfVerb = ask("24. Network interface verbosity "				"(silence/info/log/debug/trace)?", "silence",				fun verify_verbosity/1),		NetIfBindTo = ask("25. Bind the agent IP address "				  "(true/false)?",				  "false", fun verify_bool/1),		NetIfNoReuse = ask("26. Shall the agents IP address and port "				   "be not reusable (true/false)?",				   "false", fun verify_bool/1),		NetIfReqLimit = ask("27. Agent request limit "				    "(used for flow control) "				    "(infinity/pos integer)?", "infinity",				    fun verify_netif_req_limit/1),		NetIfRecbuf = 		    case ask("28. Receive buffer size of the agent (in bytes) "			     "(default/pos integer)?", "default", 			     fun verify_netif_recbuf/1) of			default ->			    [];			RecBufSz ->			    [{recbuf, RecBufSz}]		    end,		NetIfSndbuf = 		    case ask("29. Send buffer size of the agent (in bytes) "			     "(default/pos integer)?", "default", 			     fun verify_netif_sndbuf/1) of			default ->			    [];			SndBufSz ->			    [{sndbuf, SndBufSz}]		    end,		NetIfOpts = 		    [{bind_to,   NetIfBindTo},		     {no_reuse,  NetIfNoReuse},		     {req_limit, NetIfReqLimit}] ++ NetIfRecbuf ++ NetIfSndbuf,		NetIf = [{module,    NetIfMod},			 {verbosity, NetIfVerb},			 {options,   NetIfOpts}],		[{agent_type,      master},		 {agent_verbosity, MasterAgentVerb},		 {config,          [{dir,        ConfigDir}, 				    {force_load, ForceLoad},				    {verbosity,  ConfigVerb}]},		 {multi_threaded,  MultiThreaded},		 {mib_server,      [{mibentry_override,  MeOverride},				    {trapentry_override, TrapOverride},				    {verbosity,          MibServerVerb}]},		 {note_store,      [{timeout,   NoteStoreTimeout},				    {verbosity, NoteStoreVerb}]},		 {net_if, NetIf}] ++ ATL;	    sub ->		SubAgentVerb = ask("14. Sub-agent verbosity "				   "(silence/info/log/debug/trace)?", 				   "silence",				   fun verify_verbosity/1),		[{agent_type,      sub},		 {agent_verbosity, SubAgentVerb},		 {config,          [{dir, ConfigDir}]}]	end,    SysConfig = 	[{priority,    Prio},	 {versions,    Vsns},	 {db_dir,      DbDir},	 {mib_storage, MibStorage},	 {target_cache, [{verbosity, TargetCacheVerb}]},	 {symbolic_store, [{verbosity, SymStoreVerb}]},	 {local_db, [{repair,    LocalDbRepair},		     {auto_save, LocalDbAutoSave},		     {verbosity, LocalDbVerb}]},	 {error_report_module, ErrorMod}] ++ AgentConfig,    {Vsns, ConfigDir, SysConfig}.config_agent_snmp(Dir, Vsns) ->    i("~nAgent snmp config: "      "~n------------------"),    AgentName  = guess_agent_name(),    EngineName = guess_engine_name(),    SysName    = ask("1. System name (sysName standard variable)", 		     AgentName, fun verify_system_name/1),    EngineID   = ask("2. Engine ID (snmpEngineID standard variable)", 		      EngineName, fun verify_engine_id/1),    MMS        = ask("3. Max message size?", "484", 		     fun verify_max_message_size/1),    AgentUDP   = ask("4. The UDP port the agent listens to. "		     "(standard 161)",		     "4000", fun verify_port_number/1),    Host       = host(),    AgentIP    = ask("5. IP address for the agent (only used as id ~n"		     "   when sending traps)", Host, fun verify_address/1),    ManagerIP  = ask("6. IP address for the manager (only this manager ~n"		     "   will have access to the agent, traps are sent ~n"		     "   to this one)", Host, fun verify_address/1),

⌨️ 快捷键说明

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