異世界


2013年4月28日 星期日

使用 ANTLR 開發領域語言

ANTRL Home

ANTLR 参考手册
How to Use ANTLR on .NET

 

資料來源: http://www.ibm.com/developerworks/cn/java/j-lo-antlr/index.html?ca=drs-

內容

 

 

ANTLR簡介

  1. ANTLR語言識別的一個工具(另一種工具語言識別)是一種語言工具,它提供了一個框架,可以通過包含Java的,Ç+ +,或C#動作(動作)的語法描述來,結構語言識別性器的,編譯器的和解釋器。40年的發展已經相當成熟,使用ANTLR
  2. 語言識別的工具有很多種,比如大名鼎鼎的萊克斯和YACC的Linux中有他們的開源版本,分別是FLEX和野牛在Java的社區裡,除了ANTLR外,語言識別工具還有JavaCC的和SableCC等。
  3. 和大多數語言識別工具一樣,ANTLR的使用上下文無關文法描述語言。最新的ANTLR是一個基於LL(*)的語言識別器。在ANTLR中通過解析用戶自定義的上下文無關文法,自動生成詞法分析器(詞法分析器),語法分析器(分析器)和樹分析器(分析器樹)。

- MORE -

2013年4月27日 星期六

SharpSnmp vs. SnmpSharpNet

以個人的觀點而言,SnmpSharpNet 這是一組單純且易用的函式庫。

但,他缺乏了MIB Browser 及 Compiler。我無法直接連結MIB 模組,使用上非常陽春。除非,自己動手做。

-糯米-

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

其他資料及作者 : SharpSnmp vs. SnmpSharpNet  /  Lex Li

2013年4月21日 星期日

OpenNMS

資料來源 : http://www.opennms.org/wiki/Installation:Windows

The OpenNMS Project

 

這是 OpenNMS教程的一部分

這是 OpenNMS 教程。它將為您提供您需要有一個充分運作的 OpenNMS 安裝的基本步驟。

在每個頁面的頂部,您將看到顯示這裡您在本教程中的內容表。

教程中使用的約定

釋放

當我們使用釋放全部大寫的一詞時,指的,OpenNMS,您選擇要在本教程中的選擇釋放部分中安裝的版本。

$OPENNMS_HOME

當我們使用詞$OPENNMS_HOME,它指的您的 OpenNMS 安裝的根目錄中。這將是"/opt/opennms",基於 RPM 安裝,"/usr/share/opennms",對 Debian,或在其他平臺或源安裝上的另一條路徑。


安裝 OpenNMS =>
Windows   在 Windows 上,使用純 java"罐"安裝程式。

C# Based Open Source SNMP for .NET and Mono

資料來源 :

 CodePlexProject Hosting for Open Source Software

lextm / sharpsnmplib : https://github.com/lextm/sharpsnmplib

 

 

C# Based Open Source SNMP for .NET and Mono

snmpget  範例:

   1: // typical usage
   2: // snmpget -c=public -v=1 localhost 1.3.6.1.2.1.1.1.0
   3: // snmpget -v=3 -l=authPriv -a=MD5 -A=authentication -x=DES -X=privacy -u=user localhost 1.3.6.1.2.1.1.1.0
   4: using System;
   5: using System.Collections.Generic;
   6: using System.Linq;
   7: using System.Net;
   8: using System.Net.Sockets;
   9: using Lextm.SharpSnmpLib;
  10: using Lextm.SharpSnmpLib.Security;
  11: using Lextm.SharpSnmpLib.Messaging;
  12: using Mono.Options;
  13:  
  14: namespace SnmpGet
  15: {
  16:     internal static class Program
  17:     {
  18:         public static void Main(string[] args)
  19:         {
  20:             string community = "public";
  21:             bool showHelp   = false;
  22:             bool showVersion = false;
  23:             VersionCode version = VersionCode.V1;
  24:             int timeout = 1000;
  25:             int retry = 0;
  26:             Levels level = Levels.Reportable;
  27:             string user = string.Empty;
  28:             string authentication = string.Empty;
  29:             string authPhrase = string.Empty;
  30:             string privacy = string.Empty;
  31:             string privPhrase = string.Empty;
  32:             bool dump = false;
  33:  
  34:             OptionSet p = new OptionSet()
  35:                 .Add("c:", "Community name, (default is public)", delegate (string v) { if (v != null) community = v; })
  36:                 .Add("l:", "Security level, (default is noAuthNoPriv)", delegate (string v)
  37:                                                                                    {
  38:                                                                                        if (v.ToUpperInvariant() == "NOAUTHNOPRIV")
  39:                                                                                        {
  40:                                                                                            level = Levels.Reportable;
  41:                                                                                        }
  42:                                                                                        else if (v.ToUpperInvariant() == "AUTHNOPRIV")
  43:                                                                                        {
  44:                                                                                            level = Levels.Authentication | Levels.Reportable;
  45:                                                                                        }
  46:                                                                                        else if (v.ToUpperInvariant() == "AUTHPRIV")
  47:                                                                                        {
  48:                                                                                            level = Levels.Authentication | Levels.Privacy | Levels.Reportable;
  49:                                                                                        }
  50:                                                                                        else
  51:                                                                                        {
  52:                                                                                            throw new ArgumentException("no such security mode: " + v);
  53:                                                                                        }
  54:                                                                                    })
  55:                 .Add("a:", "Authentication method (MD5 or SHA)", delegate (string v) { authentication = v; })
  56:                 .Add("A:", "Authentication passphrase", delegate(string v) { authPhrase = v; })
  57:                 .Add("x:", "Privacy method", delegate (string v) { privacy = v; })
  58:                 .Add("X:", "Privacy passphrase", delegate (string v) { privPhrase = v; })
  59:                 .Add("u:", "Security name", delegate(string v) { user = v; })
  60:                 .Add("h|?|help", "Print this help information.", delegate (string v) { showHelp = v != null; })
  61:                 .Add("V", "Display version number of this application.", delegate (string v) { showVersion = v != null; })
  62:                 .Add("d", "Display message dump", delegate(string v) { dump = true; })
  63:                 .Add("t:", "Timeout value (unit is second).", delegate (string v) { timeout = int.Parse(v) * 1000; })
  64:                 .Add("r:", "Retry count (default is 0)", delegate (string v) { retry = int.Parse(v); })
  65:                 .Add("v|version:", "SNMP version (1, 2, and 3 are currently supported)", delegate (string v)
  66:                                                                                                {
  67:                                                                                                    switch (int.Parse(v))
  68:                                                                                                    {
  69:                                                                                                        case 1:
  70:                                                                                                            version = VersionCode.V1;
  71:                                                                                                            break;
  72:                                                                                                        case 2:
  73:                                                                                                            version = VersionCode.V2;
  74:                                                                                                            break;
  75:                                                                                                        case 3:
  76:                                                                                                            version = VersionCode.V3;
  77:                                                                                                            break;
  78:                                                                                                        default:
  79:                                                                                                            throw new ArgumentException("no such version: " + v);
  80:                                                                                                    }
  81:                                                                                                });
  82:  
  83:             if (args.Length == 0)
  84:             {
  85:                 ShowHelp(p);
  86:                 return;
  87:             }
  88:  
  89:             List<string> extra;
  90:             try
  91:             {
  92:                 extra = p.Parse(args);
  93:             }
  94:             catch (OptionException ex)
  95:             {
  96:                 Console.WriteLine(ex.Message);
  97:                 return;
  98:             }
  99:  
 100:             if (showHelp)
 101:             {
 102:                 ShowHelp(p);
 103:                 return;
 104:             }
 105:  
 106:             if (extra.Count < 2)
 107:             {
 108:                 Console.WriteLine("invalid variable number: " + extra.Count);
 109:                 return;
 110:             }  
 111:             
 112:             if (showVersion)
 113:             {
 114:                 Console.WriteLine(System.Reflection.Assembly.GetExecutingAssembly().GetName().Version);
 115:                 return;
 116:             }
 117:             
 118:             IPAddress ip;
 119:             bool parsed = IPAddress.TryParse(extra[0], out ip);
 120:             if (!parsed)
 121:             {
 122:                 foreach (IPAddress address in
 123:                     Dns.GetHostAddresses(extra[0]).Where(address => address.AddressFamily == AddressFamily.InterNetwork))
 124:                 {
 125:                     ip = address;
 126:                     break;
 127:                 }
 128:  
 129:                 if (ip == null)
 130:                 {
 131:                     Console.WriteLine("invalid host or wrong IP address found: " + extra[0]);
 132:                     return;
 133:                 }
 134:             }
 135:  
 136:             try
 137:             {
 138:                 List<Variable> vList = new List<Variable>();
 139:                 for (int i = 1; i < extra.Count; i++)
 140:                 {
 141:                     Variable test = new Variable(new ObjectIdentifier(extra[i]));
 142:                     vList.Add(test);
 143:                 }
 144:  
 145:                 IPEndPoint receiver = new IPEndPoint(ip, 161);
 146:                 if (version != VersionCode.V3)
 147:                 {
 148:                     foreach (
 149:                         Variable variable in
 150:                             Messenger.Get(version, receiver, new OctetString(community), vList, timeout))
 151:                     {
 152:                         Console.WriteLine(variable);
 153:                     }
 154:  
 155:                     return;
 156:                 }
 157:                 
 158:                 if (string.IsNullOrEmpty(user))
 159:                 {
 160:                     Console.WriteLine("User name need to be specified for v3.");
 161:                     return;
 162:                 }
 163:  
 164:                 IAuthenticationProvider auth = (level & Levels.Authentication) == Levels.Authentication
 165:                                                    ? GetAuthenticationProviderByName(authentication, authPhrase)
 166:                                                    : DefaultAuthenticationProvider.Instance;
 167:  
 168:                 IPrivacyProvider priv;
 169:                 if ((level & Levels.Privacy) == Levels.Privacy)
 170:                 {
 171:                     priv = new DESPrivacyProvider(new OctetString(privPhrase), auth);
 172:                 }
 173:                 else
 174:                 {
 175:                     priv = new DefaultPrivacyProvider(auth);
 176:                 }
 177:  
 178:                 Discovery discovery = Messenger.NextDiscovery;
 179:                 ReportMessage report = discovery.GetResponse(timeout, receiver);
 180:  
 181:                 GetRequestMessage request = new GetRequestMessage(VersionCode.V3, Messenger.NextMessageId, Messenger.NextRequestId, new OctetString(user), vList, priv, Messenger.MaxMessageSize, report);
 182:  
 183:                 ISnmpMessage response = request.GetResponse(timeout, receiver);
 184:                 if (dump)
 185:                 {
 186:                     Console.WriteLine(ByteTool.Convert(request.ToBytes()));
 187:                 }
 188:  
 189:                 if (response.Pdu().ErrorStatus.ToInt32() != 0) // != ErrorCode.NoError
 190:                 {
 191:                     throw ErrorException.Create(
 192:                         "error in response",
 193:                         receiver.Address,
 194:                         response);
 195:                 }
 196:  
 197:                 foreach (Variable v in response.Pdu().Variables)
 198:                 {
 199:                     Console.WriteLine(v);
 200:                 }
 201:             }
 202:             catch (SnmpException ex)
 203:             {
 204:                 Console.WriteLine(ex);
 205:             }
 206:             catch (SocketException ex)
 207:             {
 208:                 Console.WriteLine(ex);
 209:             }
 210:         }
 211:  
 212:         private static void ShowHelp(OptionSet optionSet)
 213:         {
 214:             Console.WriteLine("#SNMP is available at http://sharpsnmplib.codeplex.com");
 215:             Console.WriteLine("snmpget [Options] IP-address|host-name OID [OID] ...");
 216:             Console.WriteLine("Options:");
 217:             optionSet.WriteOptionDescriptions(Console.Out);
 218:         }
 219:  
 220:         private static IAuthenticationProvider GetAuthenticationProviderByName(string authentication, string phrase)
 221:         {
 222:             if (authentication.ToUpperInvariant() == "MD5")
 223:             {
 224:                 return new MD5AuthenticationProvider(new OctetString(phrase));
 225:             }
 226:             
 227:             if (authentication.ToUpperInvariant() == "SHA")
 228:             {
 229:                 return new SHA1AuthenticationProvider(new OctetString(phrase));
 230:             }
 231:  
 232:             throw new ArgumentException("unknown name", "authentication");
 233:         }
 234:     }
 235: }