Mã nguồn minh họa

Một phần của tài liệu Tích hợp tác tử SNMP với các hệ thống quản trị mạng dựa trên XML (Trang 91)

Mã nguồn minh họa được trình bày như trong Phụ lục của luận văn với những nội dung sau:

SNMPtoTrap.java: Mô đun chuyển đổi SNMP Trap sang XML.  SNMPtoID.java: Mô đun thiết lập khuôn dạng tệp tin XML

SNMPtoNotification.java: Mô đun thiết lập khuôn dạng tệp tin XML  SNMPtoObject.java: Mô đun chuyển đổi các đối tượng SNMP sang XML  IDParser.java: Mô đun xử lý các nút trong tệp tin XML

4.6. Kết luận

Chương này trình bày 2 kiểu cổng chuyển đổi tiêu biểu dành cho tích hợp SNMP agent với các hệ quản trị mạng dựa trên XML gồm cổng chuyển đổi SNMP-to-XML và chuyển đổi theo cổng XML/SNMP. Phần chuyển đổi đặc tả của cổng chuyển đổi cũng giới thiệu thủ tục ánh xạ tự động của các mẫu dữ liệu SMI MIB sang các định nghĩa XML Schema trong đó bao hàm ánh xạ dữ liệu quản trị SNMP sang các tài liệu XML. Sự khác biệt cần thiết của phương pháp được giới thiệu so với các phương pháp khác được định hướng bởi mục tiêu gắn kết tiềm năng đầy đủ của XML và XML Schema để bản chuyển đổi được tạo ra phù hợp với hệ thống thuần túy dựa trên XML mà không cần kế thừa gánh nặng từ SNMP. Ngoài ra, chương này cũng trình bày mô hình thực nghiệm chuyển đổi SNMP MIB thành các XML thông qua thuật toán chuyển đổi, định nghĩa lược đồ XML của kiểu dữ liệu SNMP MIB và mã nguồn minh họa (Phụ lục).

Tóm lại, công nghệ XML được xem như là một phương pháp tiếp cận mang tính cải cách để giải quyết các vấn đề tồn tại trong các chuẩn hiện hành và thực hành cho quản trị mạng và các hệ thống. Cần có nghiên cứu sâu hơn để chứng tỏ điều này bằng việc phát triển không chỉ các chuẩn mà còn áp dụng để quản trị các mạng và các hệ thống thực.

KẾT LUẬN

Quản trị mạng dựa trên XML là một lĩnh vực mới hiện đang được nghiên cứu và phát triển, song nó đã được ứng dụng rất nhiều trong thực tế, như Abilene - Visible Backbone, Passive Monitoring, Netopeer(CESNET), ConfD… Luận văn Tích hợp tác tử SNMP với các hệ thống quản trị mạng dựa trên XML bao gồm những nghiên cứu tổng quan về các hệ thống quản trị mạng, kiến trúc các hệ thống quản trị mạng dựa trên SNMP, XML thông qua tham khảo, tìm hiểu và tổng kết từ một số nghiên cứu khoa học.

Các kết quả chính của luận văn bao gồm:

- Trình bày tổng quan về các kiến trúc quản trị mạng và quản trị mạng SNMP.

- Nghiên cứu về XML, kiến trúc quản trị mạng dựa trên XML, phương pháp quản trị mạng tích hợp dựa trên XML.

- Nghiên cứu cấu trúc và quy trình chuyển đổi cổng XML/SNMP cho quản trị mạng tích hợp dựa trên XML. Tìm hiểu về các phương pháp trao đổi của cổng XML/SNMP và xây dựng thuật toán chuyển đổi từ SNMP MIB thành tài liệu XML, thực thi các mô đun chương trình bằng ngôn ngữ Java để minh họa cho những nghiên cứu về lý thuyết.

Hiện tại việc sử dụng thuật toán để thực hiện chuyển đổi tự động SNMP MIB thành tài liệu XML cho cổng XML/SNMP mới đang ở bước triển khai trong phạm vi hẹp. Hướng phát triển trong tương lai sẽ là xây dựng sự thực hiện của cổng XML/SNMP với những cải tiến mang tính hiệu quả, đánh giá được hiệu suất và các khả năng của cổng.

Trong quá trình thực hiện luận văn, tôi đã cố gắng tìm hiểu và tham khảo nhiều tài liệu liên quan. Nhưng do thời gian nghiên cứu và trình độ có hạn, nên không tránh khỏi có những thiếu sót. Tôi rất mong nhận được sự quan tâm, góp ý của các thầy cô giáo, bạn bè, đồng nghiệp và những ai quan tâm đến đề tài này để luận văn được hoàn thiện hơn. Xin trân thành cảm ơn!

TÀI LIỆU THAM KHẢO Tiếng Việt

1. Diệp Thanh Nguyên (2010), SNMP toàn tập, http://sites.google.com/ site/ snmptoantap/home.

2. Nguyễn Phương Lan (2003), XML nền tảng & ứng dụng, NXB Lao động Xã hội.

Tiếng Anh

1. Brett McLaughlin (2002), Java and XML Data Binding, O'Reilly. 2. Elliotte (2002), Processing XML with Java, Harold.

3. Frank Strauss (2001), A Library to Access SMI MIB Information, http://www.ibr.cs.tu-bs.de/projects/libsmi/.

4. Internetworking Technology Overview (1999), Simple Network Management Protocol (SNMP), http://www.pulsewan.com

5. J. Won-Ki Hong (1995), Overview of Network Management, Dept. of Computer Science and Engineering POSTECH.

6. Mi-Jung Choi, Hong-Taek Ju, James W. Hong (2003), An Architectural Framework for XML-based Network Management, DP&NM Lab. POSTECH.

7. Mi-Jung Choi, Hong-Taek Ju, James W. Hong (2002), Towards XML and SNMP Integrated Network Management, APNOMS.

8. Mi-Jung Choi, James W. Hong, Hong-Taek Ju (2003), "XML-Based Network Management for IP Networks", ETRI Journal, Vol. 25, No. 6, pp. 445-463.

9. Modder I.W. (1998), Structure and magnetism of metallic systems, Ph.D. Thesis, University of Amsterdam, Amsterdam.

10.Moruzzi V.L., Janak J.F., Williams A.R. (1978), Calculated Electronic Properties of Metals, Pergamon, New York.

11.Phil Shafer (2001), XML-based Network Management, Juniper Networks.

12.Torsten Klie and Frank Strauß (2004), Integrating SNMP Agents with XML-based Management Systems, Technical University of Braunschweig.

13.W3C (2000), Document Object Model (DOM) Level 2 Traversal and Ranges Specification, W3C Recommendation.

14.W3C (2000), Extensible Markup Language (XML), W3C Recommendation, http://www.w3.org/TR/REC-xml . (adsbygoogle = window.adsbygoogle || []).push({});

15.W3C (2002), XML Path Language (XPath) Version 2.0, W3C Working Draft.

16.W3C (2001), XML Schema, W3C Recommendation, Z.

17. W3C (2002), XQuery 1.0: An XML Query Language, W3C Working Draft. 18.W3C (1999), XSL Transformations (XSLT) Version 1.0, W3C

Recommendation.

19.Yoon-Jung Oh, Hong-Taek Ju, James W. Hong (2002), Interaction Translation Methods for XML/SNMP Gateway Using XML Technologies, DP&NM Lab. POSTECH, Korea

PHỤ LỤC 1. SNMPtoTrap.java package snmptoxml; import java.io.*; import org.xml.sax.*; import org.xml.sax.helpers.*; import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.stream.*; import javax.xml.transform.sax.*; public class SNMPtoTrap { BufferedReader in; StreamResult out; TransformerHandler th; AttributesImpl atts; Attributes d;

public static void main(String args[]) { new SNMPtoTrap().doit();

}

public void doit() { try {

in = new BufferedReader(new FileReader("datatrap.txt")); out = new StreamResult("datatrap.xml");

initXML(); String str;

while ((str = in.readLine()) != null) { process(str); } in.close(); closeXML(); } catch (Exception e) { e.printStackTrace(); } }

public void initXML() throws ParserConfigurationException, TransformerConfigurationException, SAXException { SAXTransformerFactory tf =

(SAXTransformerFactory) SAXTransformerFactory.newInstance(); th = tf.newTransformerHandler();

Transformer serializer = th.getTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); th.setResult(out); th.startDocument();

atts = new AttributesImpl();

th.startElement("", "", "xsd:element name ='NodeName'", atts); th.startElement("", "", "xsd:complexType", atts);

th.startElement("", "", "xsd:simpleContent", atts);

th.startElement("", "", "xsd:restriction base = 'xsd:string'", atts); System.out.println("?xml version='1.0' encoding='ISO-8859-1'?"); System.out.println("<xsd:element name ='NodeName'>");

System.out.println("<xsd:complexType>"); System.out.println("<xsd:simpleContent>");

System.out.println("<xsd:restriction base = 'xsd:string'>"); }

public void process(String s) throws SAXException { String[] elements = s.split("\\ ");

atts.clear();

System.out.println("<xsd:attribute name = " + "'" + elements[0] + "'" + " type = 'xsd:string' use = 'fixed' value =");

System.out.println(elements[1] + "/ >");

th.startElement("", "", "xsd:attribute name =", atts);

th.characters(elements[0].toCharArray(), 0, elements[0].length()); th.startElement("", "", "type = 'xsd:string' use = 'fixed' value =", atts); th.characters(elements[1].toCharArray(), 0, elements[1].length()); th.endElement("", "", "");

}

public void closeXML() throws SAXException { th.endElement("", "", "xsd:restriction"); th.endElement("", "", "xsd:simpleContent"); th.endElement("", "", "xsd:complexType"); th.endElement("", "", "xsd:element"); th.endDocument(); System.out.println("</xsd:restriction >"); System.out.println("</xsd:simpleContent>"); System.out.println("</xsd:complexType>"); System.out.println("</xsd:element>");

} } 2. SNMPtoID.java package snmptoxml; import java.io.*; import org.xml.sax.*; import org.xml.sax.helpers.*; import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.stream.*; import javax.xml.transform.sax.*; public class SNMPtoID { (adsbygoogle = window.adsbygoogle || []).push({});

BufferedReader in; StreamResult out; TransformerHandler th; AttributesImpl atts; Attributes d;

public static void main(String args[]) { new SNMPtoID().doit();

}

public void doit() { try {

in = new BufferedReader(new FileReader("dataidentity.txt")); out = new StreamResult("dataidentity.xml");

initXML(); String str;

while ((str = in.readLine()) != null) { process(str); } in.close(); closeXML(); } catch (Exception e) { e.printStackTrace(); } }

public void initXML() throws ParserConfigurationException, TransformerConfigurationException, SAXException { SAXTransformerFactory tf =

(SAXTransformerFactory) SAXTransformerFactory.newInstance(); th = tf.newTransformerHandler();

Transformer serializer = th.getTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); th.setResult(out); th.startDocument();

atts = new AttributesImpl();

th.startElement("", "", "xsd:element name ='NodeName'", atts); th.startElement("", "", "xsd:complexType", atts);

th.startElement("", "", "xsd:sequence ", atts);

th.startElement("", "", "<xsd:attribute name = 'oid' type ='xsd:string' use = 'fixed' value ='OidValue' / ", atts);

System.out.println("?xml version='1.0' encoding='ISO-8859-1'?"); System.out.println("<xsd:element name ='NodeName'>");

System.out.println("<xsd:complexType>"); System.out.println("<xsd:sequence>");

System.out.println("(children element definition )"); System.out.println("</xsd:sequence>");

System.out.println("<xsd:attribute name = 'oid' type ='xsd:string' use = 'fixed' value ='OidValue' / >");

}

public void process(String s) throws SAXException { String[] elements = s.split("\\ ");

atts.clear();

System.out.println("<xsd:attribute name = " + "'" + elements[0] + "'" + " type = 'xsd:string' use = 'fixed' value =");

System.out.println(elements[1] + "/ >");

th.startElement("", "", "xsd:attribute name =", atts);

th.characters(elements[0].toCharArray(), 0, elements[0].length()); th.startElement("", "", "type = 'xsd:string' use = 'fixed' value =", atts); th.characters(elements[1].toCharArray(), 0, elements[1].length()); th.endElement("", "", "");

}

public void closeXML() throws SAXException { th.endElement("", "", "xsd:complexType"); th.endElement("", "", "xsd:element"); th.endDocument();

System.out.println("</xsd:complexType>"); System.out.println("</xsd:element>");

} } 3. SNMPtoNotification.java package snmptoxml; import java.io.*; import org.xml.sax.*; import org.xml.sax.helpers.*; import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.stream.*; import javax.xml.transform.sax.*; public class SNMPtoNotification { BufferedReader in;

StreamResult out; TransformerHandler th; AttributesImpl atts; Attributes d; (adsbygoogle = window.adsbygoogle || []).push({});

public static void main(String args[]) { new SNMPtoNotification().doit(); }

public void doit() { try {

in = new BufferedReader(new FileReader("datanotification.txt")); out = new StreamResult("datanotification.xml");

initXML(); String str;

while ((str = in.readLine()) != null) { process(str); } in.close(); closeXML(); } catch (Exception e) { e.printStackTrace(); } }

public void initXML() throws ParserConfigurationException, TransformerConfigurationException, SAXException { SAXTransformerFactory tf =

(SAXTransformerFactory) SAXTransformerFactory.newInstance(); th = tf.newTransformerHandler();

serializer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); // pretty XML output serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); th.setResult(out); th.startDocument();

atts = new AttributesImpl();

th.startElement("", "", "xsd:element name ='NodeName'", atts); th.startElement("", "", "xsd:complexType", atts);

th.startElement("", "", "<xsd:attribute name = 'oid' type ='xsd:string' use = 'fixed' value ='OidValue' / ", atts);

System.out.println("?xml version='1.0' encoding='ISO-8859-1'?"); System.out.println("<xsd:element name ='NodeName'>");

System.out.println("<xsd:complexType>");

System.out.println("<xsd:attribute name = 'oid' type ='xsd:string' use = 'fixed' value ='OidValue'/ >");

}

public void process(String s) throws SAXException { String[] elements = s.split("\\ ");

atts.clear();

System.out.println("<xsd:attribute name = " + "'" + elements[0] + "'" + " type = 'xsd:string' use = 'fixed' value =");

System.out.println(elements[1] + "/ >");

th.startElement("", "", "xsd:attribute name =", atts);

th.characters(elements[0].toCharArray(), 0, elements[0].length()); th.startElement("", "", "type = 'xsd:string' use = 'fixed' value =", atts); th.characters(elements[1].toCharArray(), 0, elements[1].length()); th.endElement("", "", "");

}

public void closeXML() throws SAXException { th.endElement("", "", "xsd:complexType"); th.endElement("", "", "xsd:element"); th.endDocument(); System.out.println("</xsd:complexType>"); System.out.println("</xsd:element>"); } }

4. SNMPtoObject.java package snmptoxml; import java.io.*; import org.xml.sax.*; import org.xml.sax.helpers.*; import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.stream.*; import javax.xml.transform.sax.*; public class SNMPtoObject { BufferedReader in;

StreamResult out; TransformerHandler th; AttributesImpl atts;

public static void main(String args[]) { new SNMPtoObject().doit();

}

public void doit() { try {

in = new BufferedReader(new FileReader("dataobjectv1.txt")); out = new StreamResult("dataobjectv1.xml");

initXML(); String str; (adsbygoogle = window.adsbygoogle || []).push({});

while ((str = in.readLine()) != null) { process(str); } in.close(); closeXML(); } catch (Exception e) { e.printStackTrace(); } }

public void initXML() throws ParserConfigurationException, TransformerConfigurationException, SAXException { SAXTransformerFactory tf =

(SAXTransformerFactory) SAXTransformerFactory.newInstance(); th = tf.newTransformerHandler();

Transformer serializer = th.getTransformer();

serializer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");

serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); serializer.setOutputProperty(OutputKeys.INDENT, "yes");

th.startDocument();

atts = new AttributesImpl();

th.startElement("", "", "xsd:element name ='NodeName'", atts); th.startElement("", "", "xsd:complexType", atts);

th.startElement("", "", "xsd:simpleContent", atts);

th.startElement("", "", "xsd:restriction base = 'xsd:string'", atts); th.startElement("", "", "xsd:sequence ", atts);

th.endElement("", "", "xsd:sequence");

th.startElement("", "", "<xsd:attribute name = 'oid' type ='xsd:string' use = 'fixed' value ='OidValue' / ", atts);

System.out.println("?xml version='1.0' encoding='ISO-8859-1'?"); System.out.println("<xsd:element name ='NodeName'>");

System.out.println("<xsd:complexType>"); System.out.println("<xsd:simpleContent>");

System.out.println("<xsd:restriction base = 'xsd:string'>"); System.out.println("<xsd:sequence>");

System.out.println("(lower part node definition part)"); System.out.println("</xsd:sequence>");

System.out.println("<xsd:attribute name = 'oid' type ='xsd:string' use = 'fixed' value ='OidValue'/ >");

}

public void process(String s) throws SAXException { String[] elements = s.split("\\ ");

atts.clear();

System.out.println("<xsd:attribute name = " + "'" + elements[0] + "'" + " type = 'xsd:string' use = 'fixed' value =");

System.out.println(elements[1] + "/ >");

th.startElement("", "", "xsd:attribute name =", atts);

th.characters(elements[0].toCharArray(), 0, elements[0].length()); th.startElement("", "", "type = 'xsd:string' use = 'fixed' value =", atts); th.characters(elements[1].toCharArray(), 0, elements[1].length()); th.endElement("", "", "");

}

public void closeXML() throws SAXException { th.endElement("", "", "xsd:restriction"); th.endElement("", "", "xsd:simpleContent"); th.endElement("", "", "xsd:complexType"); th.endElement("", "", "xsd:element"); th.endDocument(); System.out.println("</xsd:restriction >"); System.out.println("</xsd:simpleContent>"); System.out.println("</xsd:complexType>"); System.out.println("</xsd:element>"); } }

5. IDParser.java

package snmptoxml; import org.w3c.dom.*;

import org.apache.xerces.parsers.DOMParser; public class IDParser { (adsbygoogle = window.adsbygoogle || []).push({});

static String displayStrings[] = new String[1000]; static int numberDisplayLines = 0;

public static void displayDocument(String uri) { try {

DOMParser parser = new DOMParser(); parser.parse(uri);

Document document = parser.getDocument(); display(document, "");

} catch (Exception e) {

e.printStackTrace(System.err); }

}

public static void display(Node node, String indent) { if (node == null) {

return; }

int type = node.getNodeType(); switch (type) {

case Node.DOCUMENT_NODE: {

displayStrings[numberDisplayLines] = indent;

displayStrings[numberDisplayLines] += "<?xml version = \"1.0\"encoding =\"" + "UTF-8" + "\"?"; numberDisplayLines++; display(((Document) node).getDocumentElement(), ""); break; } case Node.ELEMENT_NODE: { displayStrings[numberDisplayLines] = indent; displayStrings[numberDisplayLines] += "<"; displayStrings[numberDisplayLines] += node.getNodeName();

int length = (node.getAttributes() != null) ? node.getAttributes().getLength() : 0;

Attr attributes[] = new Attr[length];

for (int loopIndex = 0; loopIndex < length; loopIndex++) { attributes[loopIndex] =

(Attr) node.getAttributes().item(loopIndex); }

Attr attribute = attributes[loopIndex]; displayStrings[numberDisplayLines] += " "; displayStrings[numberDisplayLines] += attribute.getNodeName(); displayStrings[numberDisplayLines] += "=\""; displayStrings[numberDisplayLines] += attribute.getNodeValue(); displayStrings[numberDisplayLines] += "=\""; } displayStrings[numberDisplayLines] += ">"; numberDisplayLines++;

NodeList childNodes = node.getChildNodes(); if (childNodes != null) {

length = childNodes.getLength(); indent += " ";

for (int loopIndex = 0; loopIndex < length; loopIndex++) { display(childNodes.item(loopIndex), indent); } } break; } case Node.CDATA_SECTION_NODE: { displayStrings[numberDisplayLines] = indent; displayStrings[numberDisplayLines] += "<![CDATA["; displayStrings[numberDisplayLines] += node.getNodeValue(); displayStrings[numberDisplayLines] += "]]>"; numberDisplayLines++; break; } case Node.TEXT_NODE: { displayStrings[numberDisplayLines] = indent; String newText = node.getNodeValue().trim();

if (newText.indexOf("\n") < 0 && newText.length() > 0) { displayStrings[numberDisplayLines] += newText; numberDisplayLines++; } break; } case Node.PROCESSING_INSTRUCTION_NODE: { displayStrings[numberDisplayLines] = indent; displayStrings[numberDisplayLines] += "<?"; displayStrings[numberDisplayLines] += node.getNodeName(); String text = node.getNodeValue();

if (text != null && text.length() > 0) {

displayStrings[numberDisplayLines] += text; } displayStrings[numberDisplayLines] += "?>"; numberDisplayLines++; break; }

}

if (type == Node.ELEMENT_NODE) {

displayStrings[numberDisplayLines] = indent.substring(0, indent.length() - 3); displayStrings[numberDisplayLines] += "</"; displayStrings[numberDisplayLines] += node.getNodeName(); displayStrings[numberDisplayLines] += ">"; numberDisplayLines++; indent += " "; } }

public static void main(String args[]) { displayDocument(args[0]);

for (int loopIndex = 0; loopIndex < numberDisplayLines; loopIndex++) { System.out.println(displayStrings[loopIndex]);

} } } (adsbygoogle = window.adsbygoogle || []).push({});

Một phần của tài liệu Tích hợp tác tử SNMP với các hệ thống quản trị mạng dựa trên XML (Trang 91)