|
|
|||||||||||||||||||||||||
|
SNMP Tools GPW log2sql SNMP query (C#) Sniffer (C#) PING / Trace (C#) |
|
MIB tree module// Author: Toomas Kaljus
// http://www.digigrupp.com namespace DG { public class MIB { private System.Collections.SortedList MIBTree = new System.Collections.SortedList(); // translate OID to full path of MIB names public string Oid2Name(string S) { string Result = ""; string[] OID = S.Split('.'); string CurrentKey = "root"; bool EndOfPath = false; for (int i=0; i<OID.Length; i++) { if (!EndOfPath) { System.Collections.SortedList SubTree = MIBTree[CurrentKey] as System.Collections.SortedList; if (SubTree != null) { CurrentKey = SubTree[OID[i].ToString()] as string; if (CurrentKey == null) EndOfPath = true; } else { EndOfPath = true; // following numbers are left in numeric form as they are not found in current MIB base } } if (EndOfPath) CurrentKey = OID[i].ToString(); if (Result != "") Result += "."; Result += CurrentKey; } return Result; } // load mib-files public bool Load(string Path) { // initialize MIBTree root (this does not have to come from mib-file) System.Collections.SortedList RootTree = new System.Collections.SortedList(); MIBTree["root"] = RootTree; RootTree["0"] = "ccitt"; RootTree["1"] = "iso"; RootTree["2"] = "joint-iso-ccitt"; bool Result = false; string[] MibFiles = System.IO.Directory.GetFiles(Path, "*.mib"); for (int f=0; f<MibFiles.Length; f++) { string Buffer; string FileCacheDate = MIBTree[System.IO.Path.GetFileName(MibFiles[f])] as string; if (FileCacheDate != System.IO.File.GetLastWriteTime(MibFiles[f]).ToString("yyyy-MM-dd HH:mm:ss")) { using (System.IO.StreamReader StreamReader = new System.IO.StreamReader(MibFiles[f])) Buffer = StreamReader.ReadToEnd(); // remove comments string[] CleanItUp = Buffer.Split('\n'); for (int l=0; l<CleanItUp.Length; l++) { int ll = CleanItUp[l].IndexOf("--"); if (ll >= 0) CleanItUp[l] = CleanItUp[l].Substring(0, ll); } Buffer = string.Join("\n", CleanItUp); System.Console.Write(("Loading " + System.IO.Path.GetFileName(MibFiles[f])).PadRight(79) + "\r"); int ObjectCount = 0; ObjectCount += SearchFor(Buffer, "MODULE-IDENTITY"); ObjectCount += SearchFor(Buffer, "OBJECT IDENTIFIER"); ObjectCount += SearchFor(Buffer, "OBJECT-TYPE"); FileCacheDate = System.IO.File.GetLastWriteTime(MibFiles[f]).ToString("yyyy-MM-dd HH:mm:ss"); MIBTree[System.IO.Path.GetFileName(MibFiles[f])] = FileCacheDate; Result = true; } } return Result; } // Load cached MIB base public void LoadMibCache(string Path) { if (System.IO.File.Exists(Path)) using (System.IO.StreamReader StreamReader = new System.IO.StreamReader(Path)) { string Name = ""; System.Collections.SortedList SubTree = null; string Line; while ((Line = StreamReader.ReadLine()) != null) { if (Line.StartsWith("@")) { string[] Pair = Line.Substring(1).Split('='); MIBTree[Pair[0]] = Pair[1]; } else { if (Line.StartsWith(" ")) { string[] Pair = Line.Substring(1).Split('='); SubTree[Pair[0]] = Pair[1]; } else { Name = Line; SubTree = new System.Collections.SortedList(); MIBTree[Name] = SubTree; } } } } } // Cache MIB base public void SaveMibCache(string Path) { using (System.IO.StreamWriter StreamWriter = new System.IO.StreamWriter(Path)) { string[] Keys = new string[MIBTree.Count]; MIBTree.Keys.CopyTo(Keys, 0); for (int i=0; i<Keys.Length; i++) { if (MIBTree[Keys[i]] is System.Collections.SortedList) { StreamWriter.WriteLine(Keys[i]); System.Collections.SortedList SubTree = MIBTree[Keys[i]] as System.Collections.SortedList; string[] SubKeys = new string[SubTree.Count]; SubTree.Keys.CopyTo(SubKeys, 0); for (int j=0; j<SubKeys.Length; j++) { StreamWriter.WriteLine(" " + SubKeys[j] + "=" + (SubTree[SubKeys[j]] as string)); } } else { StreamWriter.WriteLine("@" + Keys[i] + "=" + MIBTree[Keys[i]]); } } } } // search for identifiers, extract their names and addresses, build MIBTree private int SearchFor(string S, string Identifier) { int Result = 0; string Name = ""; string Adr = ""; int i = S.IndexOf(Identifier); while (i > 0) { i--; for (int x=i; x>=0; x--) if ((x == 0) || (S[x - 1] != ' ') || (S[x - 1] <= '\r') || (S[x - 1] <= '\n')) { for (int y=x; y>=0; y--) if ((y == 0) || ((S[y - 1] <= '@') && (S[y - 1] != '-') && ((S[y - 1] < '0') || (S[y - 1] > '9')))) try { Name = S.Substring(y, x - y).Trim(); int j = S.IndexOf("::=", i + 2); int k = S.IndexOf("{", j + 3); int l = S.IndexOf("}", j + 3); Adr = S.Substring(k + 1, l - k - 1).Trim().Replace("\t", " "); while (Adr.IndexOf(" ") >= 0) Adr = Adr.Replace(" ", " "); if (Name != "") if (Name != "SYNTAX") { string[] A = Adr.Split(' '); if (Adr.StartsWith("1.3.")) { string[] AN = Oid2Name(Adr).Split('.'); string AA = ""; for (int an=0; an<AN.Length-1; an++) if (char.IsNumber(AN[an], 0)) AA += AN[an] + "(" + AN[an] + ") "; AA += AN[AN.Length - 1]; A = AA.Split(' '); } string Parent = A[A.Length - 2].Split('(')[0].Trim(); string Value = A[A.Length - 1].Trim(); for (int a=0; a<A.Length-2; a++) { string RootParent = A[a].Split(new char[] {'(', ')'})[0].Trim(); string[] A0 = A[a + 1].Split(new char[] {'(', ')'}); string RootName = A0[0].Trim(); string RootValue = A0[1].Trim(); System.Collections.SortedList ParentTree = MIBTree[RootParent] as System.Collections.SortedList; if (ParentTree == null) { ParentTree = new System.Collections.SortedList(); MIBTree[RootParent] = ParentTree; } if (RootValue.IndexOf("(") > 0) RootValue = RootValue.Split(new char[] {'(', ')'})[1].Trim(); ParentTree[RootValue] = RootName; } System.Collections.SortedList SubTree = MIBTree[Parent] as System.Collections.SortedList; if (SubTree == null) { SubTree = new System.Collections.SortedList(); MIBTree[Parent] = SubTree; } if (Value.IndexOf("(") > 0) Value = Value.Split(new char[] {'(', ')'})[1].Trim(); SubTree[Value] = Name; Result++; } break; } catch { } break; } i = S.IndexOf(Identifier, i + 2); } return Result; } } } |
|||||||||||||||||||||||
Copyright © 2003-2007 Digi Grupp OÜ
|
|||||||||||||||||||||||||