From: Arne V. <cob...@us...> - 2004-07-13 20:57:57
|
Update of /cvsroot/jrobin/src/org/jrobin/cmd In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv10289/org/jrobin/cmd Modified Files: RrdCmdScanner.java RrdCommander.java RrdToolCmd.java Added Files: RrdXportCmd.java Log Message: JRobin 1.4.1 - Changed to experimental rrdtool cmd parsing - Added RrdXportCmd Index: RrdCmdScanner.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/cmd/RrdCmdScanner.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** RrdCmdScanner.java 12 Jul 2004 13:35:16 -0000 1.1 --- RrdCmdScanner.java 13 Jul 2004 20:57:44 -0000 1.2 *************** *** 33,39 **** class RrdCmdScanner { ! private static final Pattern PATTERN = Pattern.compile("([^\"\\s]*\"[^\"]*\")|([^\"\\s]+)"); ! private LinkedList words = new LinkedList(); private String cmdType; RrdCmdScanner(String[] cmdWords) { --- 33,43 ---- class RrdCmdScanner { ! //private static final Pattern PATTERN = Pattern.compile("([^\"\\s]*\"[^\"]*\")|([^\"\\s]+)"); ! private String cmdType; + private String command; + + private LinkedList options = new LinkedList(); + private LinkedList words = new LinkedList(); RrdCmdScanner(String[] cmdWords) { *************** *** 46,54 **** } ! RrdCmdScanner(String command) { ! parseWords(command); } ! private void parseWords(String command) { Matcher m = PATTERN.matcher(command); while(m.find()) { --- 50,122 ---- } ! RrdCmdScanner( String command ) ! { ! // Set the command type only ! String cmd = command.trim(); ! int typePos = command.indexOf( ' ' ); ! cmdType = cmd.substring( 0, typePos ); ! ! this.command = cmd.substring( typePos ).trim(); ! ! //parseWords(command); } ! protected void parse( String[] keywords ) ! { ! StringBuffer sbuf = new StringBuffer( "( -)"); ! ! for ( int i = 0; i < keywords.length; i++ ) ! sbuf.append( "|( " + keywords[i] + ":)" ); ! ! Pattern pattern = Pattern.compile( sbuf.toString() ); ! ! parseWords( command, pattern ); ! } ! ! private void parseWords( String command, Pattern pattern ) ! { ! int start = 0, stop = 0; ! ! Matcher m = pattern.matcher(command); ! ! while ( m.find() ) ! { ! if ( start == 0 ) ! start = m.start(); ! else ! { ! stop = m.start(); ! ! // Put this 'word' away ! storeWord( command.substring( start, stop ).trim() ); ! ! // This is a new start ! start = stop; ! stop = 0; ! } ! } ! ! // Ok, see if we have to put the last word away ! if ( start > 0 && stop == 0 ) ! storeWord( command.substring( start ).trim() ); ! } ! ! private void storeWord( String word ) ! { ! if ( word.charAt(0) == '-' ) // This is an option ! options.add( word ); ! else // This is a general 'word' ! { ! // TODO: Remove \ characters or other 'in between' characters that are used in scripting ! // TODO: Remove leading single and double quotes in text, make sure all special characters are detected and treated okay ! // TODO: Best way to try this probably to put the code from: http://www.jrobin.org/phpBB2/viewtopic.php?t=39 in a file ! // TODO: read that file in, and then see if the resulting string parses correctly ! words.add( word ); ! } ! } ! ! /* ! private void parseWords2(String command) { ! // Make this a bit more complex, read until ' -', or keyword Matcher m = PATTERN.matcher(command); while(m.find()) { *************** *** 62,65 **** --- 130,134 ---- } } + */ String getCmdType() { *************** *** 67,70 **** --- 136,171 ---- } + String getOptionValue( String shortFormWord, String longFormWord ) throws RrdException + { + String shortForm = "-" + shortFormWord; + String longForm = "--" + longFormWord; + + for ( int i = 0; i < options.size(); i++ ) + { + String value = null; + String option = (String) options.get( i ); + + if ( shortForm != null && option.startsWith( shortForm ) ) + value = option.substring( shortForm.length() ).trim(); + else if ( longForm != null && option.startsWith( longForm ) ) + { + // Next character might be = + value = option.substring( longForm.length() ).trim(); + if ( value.length() > 1 && value.charAt(0) == '=' ) + value = value.substring( 1 ); + } + + if ( value != null ) + { + options.remove( i ); + + return value; + } + } + + // Option not found + return null; + } + /* String getOptionValue(String shortForm, String longForm) throws RrdException { for(int i = 0; i < words.size(); i++) { *************** *** 87,90 **** --- 188,192 ---- return null; } + */ String getOptionValue(String shortForm, String longForm, String defaultValue) throws RrdException { *************** *** 94,97 **** --- 196,204 ---- boolean getBooleanOption(String shortForm, String longForm) throws RrdException { + return (getOptionValue( shortForm, longForm ) != null); + } + + /* + boolean getBooleanOption2(String shortForm, String longForm) throws RrdException { for(int i = 0; i < words.size(); i++) { String word = (String) words.get(i); *************** *** 105,108 **** --- 212,216 ---- return false; } + */ String[] getRemainingWords() { Index: RrdToolCmd.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/cmd/RrdToolCmd.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** RrdToolCmd.java 12 Jul 2004 13:35:17 -0000 1.1 --- RrdToolCmd.java 13 Jul 2004 20:57:47 -0000 1.2 *************** *** 34,37 **** --- 34,41 ---- abstract class RrdToolCmd { + + // Holds the list of keywords + static String[] keywords = new String[0]; + static boolean rrdDbPoolUsed = true; static boolean standardOutUsed = true; *************** *** 53,56 **** --- 57,61 ---- } + RrdCmdScanner cmdScanner; *************** *** 61,68 **** abstract String getCmdType(); ! Object go() throws IOException, RrdException { if(!getCmdType().equals(cmdScanner.getCmdType())) { return null; } return execute(); } --- 66,78 ---- abstract String getCmdType(); ! Object go() throws IOException, RrdException ! { if(!getCmdType().equals(cmdScanner.getCmdType())) { return null; } + + // Parse the command based on the keywords + cmdScanner.parse( keywords ); + return execute(); } Index: RrdCommander.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/cmd/RrdCommander.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** RrdCommander.java 13 Jul 2004 11:45:39 -0000 1.3 --- RrdCommander.java 13 Jul 2004 20:57:47 -0000 1.4 *************** *** 103,107 **** * unknown RRDTool syntax/command/option, internal RRD errors...) */ ! public static synchronized Object execute(String command) throws IOException, RrdException { RrdCmdScanner cmdScanner = new RrdCmdScanner(command); RrdToolCmd[] commanders = { --- 103,108 ---- * unknown RRDTool syntax/command/option, internal RRD errors...) */ ! public static synchronized Object execute(String command) throws IOException, RrdException ! { RrdCmdScanner cmdScanner = new RrdCmdScanner(command); RrdToolCmd[] commanders = { *************** *** 121,124 **** --- 122,143 ---- throw new RrdException("Unknown RRDTool command: " + command); } + /*public static synchronized Object execute(String command) throws IOException, RrdException { + RrdCmdScanner cmdScanner = new RrdCmdScanner(command); + RrdToolCmd[] commanders = { + new RrdCreateCmd(cmdScanner), + new RrdLastCmd(cmdScanner), + new RrdUpdateCommand(cmdScanner), + new RrdDumpCmd(cmdScanner), + new RrdFetchCmd(cmdScanner), + new RrdXportCmd(cmdScanner) + }; + for(int i = 0; i < commanders.length; i++) { + Object result = commanders[i].go(); + if(result != null) { + return result; + } + } + throw new RrdException("Unknown RRDTool command: " + command); + }*/ public static void main(String[] args) { --- NEW FILE: RrdXportCmd.java --- /* ============================================================ * JRobin : Pure java implementation of RRDTool's functionality * ============================================================ * * Project Info: http://www.jrobin.org * Project Lead: Sasa Markovic (sa...@jr...); * * (C) Copyright 2003, by Sasa Markovic. * * Developers: Sasa Markovic (sa...@jr...) * Arne Vandamme (cob...@jr...) * * This library is free software; you can redistribute it and/or modify it under the terms * of the GNU Lesser General Public License as published by the Free Software Foundation; * either version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License along with this * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307, USA. */ package org.jrobin.cmd; import org.jrobin.core.RrdException; import org.jrobin.graph.RrdExportDef; import org.jrobin.graph.RrdExport; import org.jrobin.graph.ExportData; import java.io.IOException; /** * <p>Description</p> * * @author Arne Vandamme (cob...@jr...) */ public class RrdXportCmd extends RrdToolCmd { static final String DEFAULT_START = "end-1day"; static final String DEFAULT_END = "now"; static { keywords = new String[] { "DEF", "CDEF", "XPORT" }; } public RrdXportCmd( RrdCmdScanner cmdScanner ) { super(cmdScanner); } String getCmdType() { return "xport"; } Object execute() throws RrdException, IOException { // --start String startStr = cmdScanner.getOptionValue( "s", "start", DEFAULT_START ); TimeSpec spec1 = new TimeParser(startStr).parse(); // --end String endStr = cmdScanner.getOptionValue( "e", "end", DEFAULT_END ); TimeSpec spec2 = new TimeParser(endStr).parse(); long[] timestamps = TimeSpec.getTimestamps(spec1, spec2); // --step String resolutionStr = cmdScanner.getOptionValue( "step", null, "1" ); // Smallest step possible is default long resolution = parseLong( resolutionStr ); // --maxrows String maxrowsStr = cmdScanner.getOptionValue( "m", "maxrows", "400" ); int maxRows = parseInt( maxrowsStr ); RrdExportDef exportDef = new RrdExportDef( timestamps[0], timestamps[1] ); exportDef.setResolution( resolution ); exportDef.setStrictExport( true ); // Always use strict export in case of RrdTool command String[] words = cmdScanner.getRemainingWords(); for ( int i = 0; i < words.length; i++ ) { if ( words[i].startsWith("DEF:") ) parseDef( words[i], exportDef ); else if ( words[i].startsWith("CDEF:") ) parseCdef( words[i], exportDef ); else if ( words[i].startsWith("XPORT:") ) parseXport( words[i], exportDef ); } // Now create the export data RrdExport export = new RrdExport( exportDef ); ExportData data = export.fetch( maxRows ); System.out.println( data.exportXml() ); return data; } /** * DEF:vname=rrd:ds-name:CF */ private void parseDef( String word, RrdExportDef def ) throws RrdException { String[] tokens = word.split(":"); if ( tokens.length != 4 ) throw new RrdException( "Invalid DEF command: " + word ); String[] token1 = tokens[1].split("="); if ( token1.length != 2 ) throw new RrdException( "Invalid DEF command: " + word ); def.datasource( token1[0], token1[1], tokens[2], tokens[3] ); } /** * CDEF:vname=rpn-expression */ private void parseCdef( String word, RrdExportDef def ) throws RrdException { String[] tokens = word.split(":"); if ( tokens.length != 2 ) throw new RrdException( "Invalid CDEF command: " + word ); String[] token1 = tokens[1].split("="); if ( token1.length != 2 ) throw new RrdException( "Invalid CDEF command: " + word ); def.datasource( token1[0], token1[1] ); } /** * XPORT:vname:legend */ private void parseXport( String word, RrdExportDef def ) throws RrdException { String[] tokens = word.split(":"); if ( tokens.length < 2 || tokens.length > 3 ) throw new RrdException( "Invalid XPORT command: " + word ); if ( tokens.length == 2 ) def.export( tokens[1] ); else def.export( tokens[1], tokens[2] ); } public static void main( String[] args ) throws Exception { String cmd = "xport --start now-1h --end now DEF:xx=host-inout.lo.rrd:output:AVERAGE DEF:yy=host-inout.lo.rrd:input:AVERAGE CDEF:aa=xx,yy,+,8,* " + "XPORT:xx:\"out bytes\" XPORT:aa:\"in and out bits\""; cmd = "xport --start now-1h --end now -m 10 DEF:xx=/code/idea-projects/jrobin/res/demo/eth0.rrd:ifOutOctets:AVERAGE XPORT:xx:outgoing traffic"; RrdCommander.execute( cmd ); } } |