snmp_config.erl

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

ERL
2,125
字号
verify_usm_name(Name) ->    {ok, Name}.verify_usm_sec_name(Name) ->    {ok, Name}.verify_usm_auth_protocol("no") ->    {ok, usmNoAuthProtocol};verify_usm_auth_protocol("sha") ->    {ok, usmHMACSHAAuthProtocol};verify_usm_auth_protocol("md5") ->    {ok, usmHMACMD5AuthProtocol};verify_usm_auth_protocol(AuthP) ->    {error, "invalid auth protocol: " ++ AuthP}.verify_usm_auth_sha_key(Key) ->    verify_usm_key("auth sha", Key, 20).verify_usm_auth_md5_key(Key) ->    verify_usm_key("auth md5", Key, 16).verify_usm_priv_protocol("no") ->    {ok, usmNoPrivProtocol};verify_usm_priv_protocol("des") ->    {ok, usmDESPrivProtocol};verify_usm_priv_protocol("aes") ->    {ok, usmAesCfb128Protocol};verify_usm_priv_protocol(AuthP) ->    {error, "invalid priv protocol: " ++ AuthP}.verify_usm_priv_des_key(Key) ->    verify_usm_key("priv des", Key, 16).verify_usm_priv_aes_key(Key) ->    verify_usm_key("priv aes", Key, 16).verify_usm_key(_What, "\"\"", _ExpectLength) ->    {ok, ""};verify_usm_key(_What, Key, ExpectLength) when length(Key) == ExpectLength ->    {ok, Key};verify_usm_key(What, [$[|RestKey] = Key0, ExpectLength) ->    case lists:reverse(RestKey) of	[$]|RevRestKey] ->	    Key1 = lists:reverse(RevRestKey),	    verify_usm_key2(What, Key1, ExpectLength);	_ ->	    %% Its not a list ([...]) and its not the correct length, ...	    {error, "invalid " ++ What ++ " key length: " ++ Key0}    end;verify_usm_key(What, Key, ExpectLength) ->    verify_usm_key2(What, Key, ExpectLength).    verify_usm_key2(What, Key0, ExpectLength) ->    case string:tokens(Key0, [$,]) of	Key when length(Key) == ExpectLength ->	    convert_usm_key(Key, []);	_ ->	    {error, "invalid " ++ What ++ " key length: " ++ Key0}    end.    convert_usm_key([], Acc) ->    {ok, lists:reverse(Acc)};convert_usm_key([I|Is], Acc) ->    case (catch list_to_integer(I)) of	Int when integer(Int) ->	    convert_usm_key(Is, [Int|Acc]);	_Err ->	    {error, "invalid key number: " ++ I}    end.	     % ip(Host) ->%     case catch snmp_misc:ip(Host) of% 	{ok, IPtuple} -> tuple_to_list(IPtuple);% 	{error, Reason} -> throw({error, Reason});% 	_Q -> throw({error, {"ip conversion failed", Host}})%     end.% make_ip(Str) ->%     case catch snmp_misc:ip(Str) of% 	{ok, IPtuple} -> tuple_to_list(IPtuple);% 	_Q -> ip(Str)%     end.print_q(Q, mandatory) ->    io:format(Q ++ " ",[]);print_q(Q, Default) when list(Default) ->    io:format(Q ++ " [~s] ",[Default]).%% Defval = string() | mandatoryask(Q, Default, Verify) when list(Q), function(Verify) ->    print_q(Q, Default),    PrelAnsw = io:get_line(''),    Answer = 	case remove_newline(PrelAnsw) of	    "" when Default /= mandatory -> Default;	    "" -> ask(Q, Default, Verify);	    A -> A    end,    case (catch Verify(Answer)) of	{ok, Answer2} ->	    Answer2;	{error, ReasonStr} ->	    i("ERROR: " ++ ReasonStr),	    ask(Q, Default, Verify)    end.host() ->    case (catch inet:gethostname()) of	{ok, Name} ->	    case (catch inet:getaddr(Name, inet)) of		{ok, Addr} when tuple(Addr) ->		    lists:flatten(		      io_lib:format("~w.~w.~w.~w", tuple_to_list(Addr)));		_ ->		    "127.0.0.1"	    end;	_ -> 	    "127.0.0.1"    end.guess_agent_name() ->    case os:type() of	{unix, _} ->	    lists:append(remove_newline(os:cmd("echo $USER")), "'s agent");	{_,_} -> "my agent"    end.guess_engine_name() ->    case os:type() of	{unix, _} ->	    lists:append(remove_newline(os:cmd("echo $USER")), "'s engine");	{_,_} -> "my engine"    end.% guess_user_id() ->%     case os:type() of% 	{unix, _} ->% 	    lists:append(remove_newline(os:cmd("echo $USER")), "'s user");% 	{_,_} -> "user_id"%     end.    remove_newline(Str) ->     lists:delete($\n, Str).%%======================================================================%% File generation%%======================================================================%%----------------------------------------------------------------------%% Dir: string()  (ex: "../conf/")%% ManagerIP, AgentIP: [int(),int(),int(),int()]%% TrapUdp, AgentUDP: integer()%% SysName: string()%%----------------------------------------------------------------------write_agent_snmp_files(Dir, Vsns, ManagerIP, TrapUdp, 		       AgentIP, AgentUDP, SysName)   when list(Dir), list(Vsns), list(ManagerIP), integer(TrapUdp),        list(AgentIP), integer(AgentUDP), list(SysName) ->    write_agent_snmp_files(Dir, Vsns, ManagerIP, TrapUdp, AgentIP, AgentUDP,			   SysName, "trap", none, "", "agentEngine", 484).%% %% ----- Agent config files generator functions -----%% write_agent_snmp_files(Dir, Vsns, ManagerIP, TrapUdp, AgentIP, AgentUDP, 		       SysName, NotifType, SecType, Passwd, EngineID, MMS) ->    write_agent_snmp_conf(Dir, AgentIP, AgentUDP, EngineID, MMS),    write_agent_snmp_context_conf(Dir),    write_agent_snmp_community_conf(Dir),    write_agent_snmp_standard_conf(Dir, SysName),    write_agent_snmp_target_addr_conf(Dir, ManagerIP, TrapUdp, Vsns),    write_agent_snmp_target_params_conf(Dir, Vsns),    write_agent_snmp_notify_conf(Dir, NotifType),    write_agent_snmp_usm_conf(Dir, Vsns, EngineID, SecType, Passwd),    write_agent_snmp_vacm_conf(Dir, Vsns, SecType),    ok.%% %% ------ [agent] agent.conf ------%% write_agent_snmp_conf(Dir, AgentIP, AgentUDP, EngineID, MMS) ->     Comment = "%% This file defines the Agent local configuration info\n""%% The data is inserted into the snmpEngine* variables defined\n""%% in SNMP-FRAMEWORK-MIB, and the intAgent* variables defined\n""%% in OTP-SNMPEA-MIB.\n""%% Each row is a 2-tuple:\n""%% {AgentVariable, Value}.\n""%% For example\n""%% {intAgentUDPPort, 4000}.\n""%% The ip address for the agent is sent as id in traps.\n""%% {intAgentIpAddress, [127,42,17,5]}.\n""%% {snmpEngineID, \"agentEngine\"}.\n""%% {snmpEngineMaxMessageSize, 484}.\n""%%\n\n",    Hdr = header() ++ Comment,     Conf = [{intAgentUDPPort,          AgentUDP}, 	    {intAgentIpAddress,        AgentIP},	    {snmpEngineID,             EngineID},	    {snmpEngineMaxMessageSize, MMS}],    write_agent_config(Dir, Hdr, Conf).write_agent_config(Dir, Hdr, Conf) ->    snmpa_conf:write_agent_config(Dir, Hdr, Conf).    update_agent_config(Dir, Conf) ->    snmpa_conf:append_agent_config(Dir, Conf).    %% %% ------ [agent] context.conf ------%% write_agent_snmp_context_conf(Dir) ->    Comment = "%% This file defines the contexts known to the agent.\n""%% The data is inserted into the vacmContextTable defined\n""%% in SNMP-VIEW-BASED-ACM-MIB.\n""%% Each row is a string:\n""%% ContextName.\n""%%\n""%% The empty string is the default context.\n""%% For example\n""%% \"bridge1\".\n""%% \"bridge2\".\n""%%\n\n",    Hdr = header() ++ Comment,    Conf = [""],    write_agent_context_config(Dir, Hdr, Conf).write_agent_context_config(Dir, Hdr, Conf) ->    snmpa_conf:write_context_config(Dir, Hdr, Conf).update_agent_context_config(Dir, Conf) ->    snmpa_conf:append_context_config(Dir, Conf).    %% %% ------ community.conf ------%% write_agent_snmp_community_conf(Dir) ->    Comment = "%% This file defines the community info which maps to VACM parameters.\n""%% The data is inserted into the snmpCommunityTable defined\n""%% in SNMP-COMMUNITY-MIB.\n""%% Each row is a 5-tuple:\n""%% {CommunityIndex, CommunityName, SecurityName, ContextName, TransportTag}.\n""%% For example\n""%% {\"1\", \"public\", \"initial\", \"\", \"\"}.\n""%% {\"2\", \"secret\", \"secret_name\", \"\", \"tag\"}.\n""%% {\"3\", \"bridge1\", \"initial\", \"bridge1\", \"\"}.\n""%%\n\n",    Hdr = header() ++ Comment,    Conf = [{"public", "public", "initial", "", ""}, 	    {"all-rights", "all-rights", "all-rights", "", ""}, 	    {"standard trap", "standard trap", "initial", "", ""}],     write_agent_community_config(Dir, Hdr, Conf).write_agent_community_config(Dir, Hdr, Conf) ->    snmpa_conf:write_community_config(Dir, Hdr, Conf).update_agent_community_config(Dir, Conf) ->    snmpa_conf:append_community_config(Dir, Conf).    %% %% ------ standard.conf ------%% write_agent_snmp_standard_conf(Dir, SysName) ->    Comment = "%% This file defines the STANDARD-MIB info.\n""%% Each row is a 2-tuple:\n""%% {StandardVariable, Value}.\n""%% For example\n""%% {sysDescr, \"Erlang SNMP agent\"}.\n""%% {sysObjectID, [1,2,3]}.\n""%% {sysContact, \"{mbj,eklas}@erlang.ericsson.se\"}.\n""%% {sysName, \"test\"}.\n""%% {sysLocation, \"erlang\"}.\n""%% {sysServices, 72}.\n""%% {snmpEnableAuthenTraps, enabled}.\n""%%\n\n",    Hdr = header() ++ Comment,    Conf = [{sysDescr,              "Erlang SNMP agent"},	    {sysObjectID,           [1,2,3]},	    {sysContact,            "{mbj,eklas}@erlang.ericsson.se"},	    {sysLocation,           "erlang"}, 	    {sysServices,           72}, 	    {snmpEnableAuthenTraps, enabled},	    {sysName,               SysName}],    write_agent_standard_config(Dir, Hdr, Conf).write_agent_standard_config(Dir, Hdr, Conf) ->    snmpa_conf:write_standard_config(Dir, Hdr, Conf).update_agent_standard_config(Dir, Conf) ->    snmpa_conf:append_standard_config(Dir, Conf).%% %% ------ target_addr.conf ------%% write_agent_snmp_target_addr_conf(Dir, ManagerIp, UDP, Vsns) ->     Timeout    = 1500,     RetryCount = 3,     write_agent_snmp_target_addr_conf(Dir, ManagerIp, UDP, 				      Timeout, RetryCount, 				      Vsns).write_agent_snmp_target_addr_conf(Dir, ManagerIp, UDP, 				  Timeout, RetryCount, 				  Vsns) ->     Comment = "%% This file defines the target address parameters.\n""%% The data is inserted into the snmpTargetAddrTable defined\n""%% in SNMP-TARGET-MIB, and in the snmpTargetAddrExtTable defined\n""%% in SNMP-COMMUNITY-MIB.\n""%% Each row is a 10-tuple:\n""%% {Name, Ip, Udp, Timeout, RetryCount, TagList, ParamsName, EngineId,\n""%%        TMask, MaxMessageSize}.\n""%% The EngineId value is only used if Inform-Requests are sent to this\n""%% target.  If Informs are not sent, this value is ignored, and can be\n""%% e.g. an empty string.  However, if Informs are sent, it is essential\n""%% that the value of EngineId matches the value of the target's\n""%% actual snmpEngineID.\n""%% For example\n""%% {\"1.2.3.4 v1\", [1,2,3,4], 162, \n""%%  1500, 3, \"std_inform\", \"otp_v2\", \"\",\n""%%  [127,0,0,0],  2048}.\n""%%\n\n",    Hdr = header() ++ Comment,    F = fun(v1 = Vsn, Acc) ->		[{mk_ip(ManagerIp, Vsn), 		  ManagerIp, UDP, Timeout, RetryCount, 		  "std_trap", mk_param(Vsn), "", [], 2048}| Acc];	   (v2 = Vsn, Acc) ->		[{mk_ip(ManagerIp, Vsn), 		  ManagerIp, UDP, Timeout, RetryCount, 		  "std_trap", mk_param(Vsn), "", [], 2048},		 {lists:flatten(io_lib:format("~s.2",[mk_ip(ManagerIp, Vsn)])),		  ManagerIp, UDP, Timeout, RetryCount, 		  "std_inform", mk_param(Vsn), "", [], 2048}| Acc];	   (v3 = Vsn, Acc) ->		[{mk_ip(ManagerIp, Vsn), 		  ManagerIp, UDP, Timeout, RetryCount, 		  "std_trap", mk_param(Vsn), "", [], 2048},		 {lists:flatten(io_lib:format("~s.3",[mk_ip(ManagerIp, Vsn)])),		  ManagerIp, UDP, Timeout, RetryCount, 		  "std_inform", mk_param(Vsn), "mgrEngine", [], 2048}| Acc]	end,    Conf = lists:foldl(F, [], Vsns),    write_agent_target_addr_config(Dir, Hdr, Conf).mk_param(Vsn) ->    lists:flatten(io_lib:format("target_~w", [Vsn])).mk_ip([A,B,C,D], Vsn) ->    lists:flatten(io_lib:format("~w.~w.~w.~w ~w", [A,B,C,D,Vsn])).write_agent_target_addr_config(Dir, Hdr, Conf) ->    snmpa_conf:write_target_addr_config(Dir, Hdr, Conf).update_agent_target_addr_config(Dir, Conf) ->    snmpa_conf:append_target_addr_config(Dir, Conf).%% %% ------ target_params.conf ------%% write_agent_snmp_target_params_conf(Dir, Vsns) ->     Comment = "%% This file defines the target parameters.\n""%% The data is inserted into the snmpTargetParamsTable defined\n""%% in SNMP-TARGET-MIB.\n""%% Each row is a 5-tuple:\n""%% {Name, MPModel, SecurityModel, SecurityName, SecurityLevel}.\n""%% For example\n""%% {\"target_v3\", v3, usm, \"\", noAuthNoPriv}.\n""%%\n\n",    Hdr = header() ++ Comment,    Conf = [fun(V) ->		    MP = if V == v1 -> v1;			    V == v2 -> v2c;			    V == v3 -> v3			 end,		    SM = if V == v1 -> v1;			    V == v2 -> v2c;			    V == v3 -> usm			 end,		    Name = lists:flatten(			     io_lib:format("target_~w", [V])),		    {Name, MP, SM, "initial", noAuthNoPriv}	    end(Vsn) || Vsn <- Vsns],    write_agent_target_params_config(Dir, Hdr, Conf).write_agent_target_params_config(Dir, Hdr, Conf) ->    snmpa_conf:write_target_params_config(Dir, Hdr, Conf).update_agent_target_params_config(Dir, Conf) ->    snmpa_conf:append_target_params_config(Dir, Conf).%% %% ------ notify.conf ------%% write_agent_snmp_notify_conf(Dir, NotifyType) ->     Comment = "%% This file defines the notification parameters.\n""%% The data is inserted into the snmpNotifyTable defined\n""%% in SNMP-NOTIFICATION-MIB.\n""%% The Name is used as CommunityString for v1 and v2c.\n"

⌨️ 快捷键说明

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