[jboss-cvs] JBossAS SVN: r111559 - branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Jun 8 10:19:32 EDT 2011


Author: thauser at redhat.com
Date: 2011-06-08 10:19:32 -0400 (Wed, 08 Jun 2011)
New Revision: 111559

Modified:
   branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/Generator.java
   branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/MIBGenerator.java
   branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/Parser.java
   branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/ParserBindings.java
Log:
commit work before attempting to add new commandline parser

Modified: branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/Generator.java
===================================================================
--- branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/Generator.java	2011-06-08 12:50:05 UTC (rev 111558)
+++ branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/Generator.java	2011-06-08 14:19:32 UTC (rev 111559)
@@ -52,7 +52,7 @@
 * 3) Enable user to define the names of OIDs themselves somehow.
 * 
 * 
-*  @author <a href="mailto:tom.hauser at gmail.com">Tom Hauser</a>
+*  @author <a href="mailto:tom.hauser at gmail.com>Tom Hauser</a>
 **/
 
 public class Generator {
@@ -60,10 +60,11 @@
 	private AttributeMappings mbList;
 	private ArrayList<MIBObject> miboList; //internal list of MIBObjects
 	private ArrayList<MappedAttribute> maList;
-	private HashMap<String, OIDDef> oidDefMap;
+	private HashMap<String, OIDDef> oidDefMap;		
 	
-	// need some way to prevent collisions
-		
+	/** 
+	 * Default constructor. Nulls all around.
+	 */
 	
 	public Generator(){
 		this.outputResName = null;
@@ -72,6 +73,15 @@
 		this.maList = null;
 	}
 	
+	/** 
+	 * Most used constructor. 
+	 * 
+	 * @param outputResName the filename, can be either .mib or .xml. This extension will decide which type of 
+	 * file is produced by this class.
+	 * @param maList the list of MappedAttributes, received from the Parser.
+	 * @param mbList the list of ManagedBeans. This can be null if, for example, we're parsing an MIB.
+	 */
+	
 	public Generator(String outputResName, ArrayList<MappedAttribute> maList, AttributeMappings mbList){
 		this.outputResName = outputResName;
 		this.mbList = mbList;
@@ -105,15 +115,7 @@
 	public void setMbList(AttributeMappings mbList){
 		this.mbList = mbList;
 	}
-		
-/*	public MBeanServer getMBeanServer(){
-		return this.server;
-	}
 	
-	public void setMBeanServer(MBeanServer server){
-		this.server = server;
-	}*/
-	
 	public HashMap<String, OIDDef> getOidDefMap(){
 		return this.oidDefMap;
 	}
@@ -121,11 +123,16 @@
 	public void setOidDefMap(HashMap<String, OIDDef> oidDefMap){
 		this.oidDefMap = oidDefMap;
 	}
-	//we're done gathering attributes. write the MIB.
+
+	/** 
+	 * Entry point method. Called by MIBGenerator class after all of the parsing is completed. 
+	 * Does nothing really itself. Which methods are called depends on the name of the output file.
+	 */
+
 	public void writeFile(){
 		try{
 			PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(outputResName)));
-			if (outputResName.contains(".mib")){
+			if (outputResName.matches("\\.mib$")){
 				createOidDefinitions();
 				createMibObjects();
 				writeMibHeader(out);
@@ -133,7 +140,7 @@
 				writeMibDefinitions(out);
 				writeMibObjects(out);
 			}
-			else if (outputResName.contains(".xml")){
+			else if (outputResName.matches("\\.xml$")){
 				writeXmlHeader(out);
 				writeXmlDefinitions(out);
 			}
@@ -142,6 +149,11 @@
 		catch (Exception e){e.printStackTrace();}
 	}
 	
+	/**
+	 * Simple class that writes the header to the MIB file.  
+	 * TODO: make the name JBOSS-AS-MIB configurable.
+	 * @param out the PrintWriter to output to.
+	 */
 	
 	private void writeMibHeader(PrintWriter out){
 		out.println("-- This MIB Generated by the JBoss MIB Generator");
@@ -151,6 +163,13 @@
 		// maybe add more here, if notrrayList we don't need this to be a seperate method.
 	}
 	
+	/**
+	 * Another simple class that outputs the Imports of the generated MIB.
+	 * The current imports will be recognized by any standard snmp tool (notably net-snmp)
+	 * TODO: Make these imports configurable. 
+	 * @param out
+	 */
+	
 	private void writeMibImports(PrintWriter out){
 		out.println("IMPORTS");
 		out.println("\tOBJECT-TYPE,");
@@ -166,11 +185,12 @@
 		out.println();
 	}
 	
-	/** This method is used to write the definitions of our SYNTAX inside the JBoss MIB
-	 *  For use when we figure out what OIDs we want our metrics to be. 
-	 * @param attrList list of MBeans that we're interested in
+	/** 
+	 *  This method outputs all of the OID definitions we need in order for a manager
+	 *  that loads the generated MIB to be able to know the names of metrics it is getting back.
+	 *  TODO: enable exact configuration of this section
 	 * @param out The PrintWriter that writes to the file we need it to
-	 * @author Tom Hauser
+	 * @author <a href="mailto:tom.hauser at gmail.com>Tom Hauser</a>
 	 */	
 	private void writeMibDefinitions(PrintWriter out){
 		/*org             OBJECT IDENTIFIER ::= { iso 3 }
@@ -189,6 +209,11 @@
 		}
 	}
 	
+	/**
+	 * Outputs all of the objects in miboList to the outputFile.
+	 * @param out
+	 */
+	
 	private void writeMibObjects(PrintWriter out){
 		Iterator<MIBObject> aIt = miboList.iterator();
 		while (aIt.hasNext()){
@@ -197,6 +222,11 @@
 		out.println("END");
 	}
 	
+	/**
+	 * This method generates an appropriate MIBObject from a given MappedAttribute, and adds
+	 * it to this instance's list of mibObjects for writing to the file.
+	 */
+	
 	private void createMibObjects(){
 		Iterator<MappedAttribute> aIt = maList.iterator();
 		while (aIt.hasNext()){
@@ -205,6 +235,12 @@
 		}
 	}		
 	
+	/** 
+	 * Method to create initial OID Definitions, to be at the top of the file in order to allow 
+	 * a manager to know the name of recieved metrics. 
+	 * TODO: make this user configurable, through a file or some such.
+	 */
+	
 	private void createOidDefinitions(){		
 		// expansion: make a way to discover these, rather than hardcoding them. 
 		// suggestion: add a map of oid-prefixes from the attributes.xml and create OIDDefs out of those.
@@ -218,10 +254,27 @@
 		this.oidDefMap.put(system, mib2system);
 	}
 	
+	/** 
+	 * Needs implementation. Writing the xml header out of a given MIB.
+	 * @param out
+	 */
 	private void writeXmlHeader(PrintWriter out){}
+	
+	/**
+	 * Needs implementation. Write the actual bindings out, given an MIB
+	 * @param out
+	 */
 	private void writeXmlDefinitions(PrintWriter out){}
+	
+	
+	
 	/* Internal Classes ----- */
 	
+	
+	/** 
+	 * Internal class used to represent a single MIB Object. Created by reading the required 
+	 * data from a MappedAttribute object, and filling in the rest.
+	 */
 	private class MIBObject{
 		private String name;
 		private String syntax;
@@ -230,71 +283,55 @@
 		private String description;
 		private String objectId; 
 		private String oidDef;
-				
-		//need to modify the information saved in mapped attribute maybe.
-		//or just make a new class to hold this info in, instead of an internal one.
-		//we need to get the information out of the MappedAttribute and format it 
-		//correctly for use in an MIB.
 			
 		MIBObject(MappedAttribute ma){
+			// names must begin with lowercase, or the manager complains / gives warnings
 			this.name = ma.getName().substring(0,1).toLowerCase() + ma.getName().substring(1);
 			
+			//if the ma has an snmpType defined, we use that as the type. Otherwise we cannot know it, use
+			//the general OCTET STRING type.
 			if (ma.getSnmpType()!= null)
 				this.syntax = ma.getSnmpType();
 			else
 				this.syntax = "OCTET STRING (SIZE(0..255))";
 					
-			if (ma.isReadWrite()) // since we only have "rw" and "ro", this needs expansion.
+			//there are more values possible here for an MIB, but not for a JMX attr; we only 
+			//need to worry about these possibilities
+			if (ma.isReadWrite()) 
 				maxAccess = "read-write";
 			else
 				maxAccess = "read-only";
 			
+			/**
+			 * Possible values for STATUS ::= "current"
+			 *                               | deprecated
+			 *                               | obsolete
+			 * Not quite sure how these other values correspond to any meaning for JMX attributes. May 
+			 * be worth further investigation.
+			 */
 			this.status = "current"; 
+			// perhaps have this as an optional attribute in the attributes.xml? Left as blank 
+			// because there is no way to query the server about a given attribute
 			this.description = ""; 
 			String[] temp = ma.getOid().split("\\."); // this will contain the full numerical OID.
+			// We need the last element in the array to be the registered OID, so we can put the correct 
+			// name in the output MIB
 			this.objectId = temp[temp.length-1];
-			
+						
+			// check if there is already an OID definition for this prefix. If there is make this MIBObject's 
+			// oidDef reflect that
 			if (oidDefMap.containsKey(ma.getOidPrefix())){
 				this.oidDef = oidDefMap.get(ma.getOidPrefix()).getName();
 			}
 			// if they aren't part of the Map, then we add them to the map with a filler name "CHANGE-ME"
 			// as there is no way to glean this name from the attributes.xml without 
-			// complexifying it a great deal
+			// complexifying it a great deal, OR making them configurable in another file (a very good possibility)
 			else{
 				OIDDef newOidDef = new OIDDef("CHANGE-ME", ma.getOidPrefix());
 				oidDefMap.put(ma.getOidPrefix(), newOidDef);
 			}
 		}
 		
-/*		public String findType(MappedAttribute ma){
-			Object o = null;
-			try{
-			o = server.getAttribute(new ObjectName(ma.getMbean()), ma.getName());
-			}catch (Exception e){return "OCTET STRING (SIZE(0..255))";}				
-
-			if (o instanceof Long){
-				return "DisplayString";
-			}
-			else if (o instanceof String){
-				return "DisplayString";
-			}
-			else if (o instanceof Integer){
-				return "INTEGER";
-			}
-			else if (o instanceof OID){
-				return "OBJECT IDENTIFIER";
-			}
-			else if (o instanceof TimeTicks){
-				return "TimeTicks";
-			}
-			else if (o instanceof Counter32){
-				return "Counter32";
-			}
-			else{
-				return "OCTET STRING (SIZE(0..255))";
-			}
-		}*/
-
 		@Override
 		public String toString(){
 			StringBuffer buf = new StringBuffer();
@@ -316,10 +353,17 @@
 		}
 	}
 	
+	/**
+	 * Internal class for keeping track of an OID Definition (used at the top of an MIB for naming purposes)
+	 * see http://www.simpleweb.org/ietf/mibs/modules/IETF/txt/SNMPv2-SMI 
+	 * @author thauser
+	 *
+	 */
+	
 		private class OIDDef{
-			private String name;
-			private String definition;
-			private String rawOid;
+			private String name; // the name of the OID, as dictated by the naming standard
+			private String definition; // the OID with '.' replaced by ' ', ready to be output into the MIB
+			private String rawOid; // the full dotted-string oid, untouched.
 			
 			public OIDDef(String name, String oid){
 				this.name = name;
@@ -327,6 +371,7 @@
 				this.definition = parseDefinition(oid);
 			}
 			
+			//mutators
 			public String getName(){
 				return this.name;	
 			}
@@ -343,6 +388,7 @@
 				this.definition = def;
 			}
 			
+			// returns a string with the '.' turned into ' '
 			public String parseDefinition(String oid){
 				String[] tokens = oid.split("\\.");
 				String temp = "{ ";
@@ -362,6 +408,8 @@
 				return buf.toString();
 			}
 			
+			// used to compare OIDdefs. if their rawOid is equal, they are equal. Otherwise, they're not equal, even if 
+			// they have the same name and the same definition.
 			@Override
 			public boolean equals(Object other){
 			    if (other == null) return false;

Modified: branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/MIBGenerator.java
===================================================================
--- branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/MIBGenerator.java	2011-06-08 12:50:05 UTC (rev 111558)
+++ branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/MIBGenerator.java	2011-06-08 14:19:32 UTC (rev 111559)
@@ -34,8 +34,6 @@
 package org.jboss.jmx.adaptor.snmp.generator;
 
 public class MIBGenerator {
-	static String outputFileName; // the name of the output file (*.mib or *.xml)
-	static String inputFileName; // input filename (*.mib or *.xml)
 	static Parser parser; // XMLParser instantiation that parses the xml
 	static Generator generator; // generator that outputs the mib or xml
 	
@@ -50,12 +48,39 @@
 	 * @author Tom Hauser
 	 */
 	public static void main (String [] args){
-		 inputFileName = args[0];
-		 outputFileName = args[1];
-		 parser = new Parser(inputFileName);
+		 sanitizeParams(args);
+		 parser = new Parser(args[0]);
 		 parser.parse();
-		 generator = new Generator(outputFileName, parser.getMaList(), parser.getMbList());
+		 generator = new Generator(args[1], parser.getMaList(), parser.getMbList());
 		 generator.writeFile();		
 	}
 	
+	private static void sanitizeParams(String [] args){
+		if (args[0].equals("-help") || args.length != 2){
+			System.out.println(displayHelpMessage());
+			System.exit(1);
+		}
+		else if ((!args[0].matches("\\.mib$") || !args[0].matches("\\.xml$")) 
+			  && (!args[1].matches("\\.mib$") || !args[1].matches("\\.xml$"))){
+			System.out.println("Invalid filetypes entered. Please follow the guidelines below.");
+			System.out.println(displayHelpMessage());
+			System.exit(2);
+		}
+		else if ((args[0].matches("\\.mib$") && args[1].matches("\\.mib$"))
+			  || (args[0].matches("\\.xml$") && args[1].matches("\\.xml$"))){
+			System.out.println("The input/output filetypes are the same. This is not permitted.");
+			System.out.println(displayHelpMessage());
+			System.exit(3);
+		}
+	}
+	
+	private static String displayHelpMessage(){
+		StringBuffer buf = new StringBuffer();
+		buf.append("Usage:  \t-help : Display this message\n");
+		buf.append("Example:\tjava -jar jboss-as-varia-mib-generator.jar inputFile.{mib,xml} outputFile.{mib,xml}\n");
+		buf.append("Note: The types must be opposites. If you wish to convert from xml to mib, the first\n");
+		buf.append("file should have the .xml extension, and the second should have the .mib extension.");
+		return buf.toString();
+	}
+	
 }
\ No newline at end of file

Modified: branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/Parser.java
===================================================================
--- branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/Parser.java	2011-06-08 12:50:05 UTC (rev 111558)
+++ branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/Parser.java	2011-06-08 14:19:32 UTC (rev 111559)
@@ -40,7 +40,7 @@
 /**
  * This class is used by the MIBGenerator to get the initial list of MappedAttributes out of an input file. 
  * 
- * @author thauser
+ * @author<a href="mailto:tom.hauser at gmail.com"> or <a href="mailto:thauser at redhat.com">Tom Hauser
  *
  */
 
@@ -80,7 +80,7 @@
 	}
 
 	/**
-	 * Workhorse method. Based on the name of the input / output file, we either parse
+	 * Based on the name of the input / output file, we either parse
 	 * an MIB or an XML, and from this construct a nescessary ArrayList of ManagedAttributes to write out	 
 	 * */
 	
@@ -101,6 +101,13 @@
 		}		
 	}
 	
+	/**
+	 * All work is done here, using ObjectModelFactory. ParseBindings is used to define how the given xml is parsed, 
+	 * and creates a AttributeMappings object, which is then used to add all ManagedAttributes along with their associated
+	 * data to the member maList. 
+	 *
+	 * @throws Exception
+	 */
 	private void parseXml() throws Exception{
 		ObjectModelFactory omf = new ParserBindings();
 		InputStream is = null;

Modified: branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/ParserBindings.java
===================================================================
--- branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/ParserBindings.java	2011-06-08 12:50:05 UTC (rev 111558)
+++ branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/ParserBindings.java	2011-06-08 14:19:32 UTC (rev 111559)
@@ -33,26 +33,9 @@
 /**
  * Parse the mapping of JMX mbean attributes to SNMP OIDs
  * 
- * TODO: extend this parsing. 
- * 		-Add "table" element. This is not represented by any MBean attribute but is still recorded by the system,
- *       as a a way to conceptually organize the desired exposed JMX metrics. This element would have the oid-prefix
- *       attribute, which would replace the mbean oid-prefix 
- *      -Remove the ability for there to be an oid-prefix for the MBeans. instead, but this into the "table" element.
- *       the reason we should do this is because SNMP works with "Objects" and "Instances". The way it is currently
- *       parsed makes it seem like each MBean is a table, and we do not want to force this behavior.
- *      
- *      New scheme for parsing: 
- *      <mbean name=...> (MBean we're interested in.)
- *         <attribute name=... oid="1.2.3.4.1.0"/> (This attribute is a scalar, because it has no prefix. it's Object is 1.2.3.4.1 and the instance is 0) 
- *         ..
- *         <table name=... oid-prefix="1.2.3.4.1.8"/>  (This indicates the creation of a conceptual table.)
- *         	 <row name=... oid=".1"/> (This is a row in the table) neither this nor the table are directly accessible in SNMP.
- *            <attribute name=... oid=".1"/> (this is an actual instance of the row (a single column in the row.) 
- *            								 This attribute is accessed by 1.2.3.4.1.8.1.1 <tableOID><rowOID><instanceOID>
- *          ..
  *      </mbean>
  * 
- * @author <a href="mailto:hwr at pilhuhn.de">Heiko W. Rupp</a>
+ * @author <a href="mailto:tom.hauser at gmail.com>Tom Hauser</a>
  * @version $Revision: 111505 $
  */
 public class ParserBindings implements ObjectModelFactory



More information about the jboss-cvs-commits mailing list