From: Sasa M. <sa...@us...> - 2004-07-16 07:40:58
|
Update of /cvsroot/jrobin/src/org/jrobin/cmd In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20380/org/jrobin/cmd Modified Files: RrdCommander.java RrdDumpCmd.java RrdFetchCmd.java RrdLastCmd.java RrdRestoreCmd.java RrdToolCmd.java RrdXportCmd.java Added Files: RrdUpdateCmd.java Removed Files: RrdUpdateCommand.java Log Message: Completely redesigned and simplified RRDTool commander... --- NEW FILE: RrdUpdateCmd.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.*; import java.io.IOException; class RrdUpdateCmd extends RrdToolCmd { private String[] dsNames; String getCmdType() { return "update"; } Object execute() throws RrdException, IOException { String template = getOptionValue("t", "template"); if (template != null) { dsNames = template.split(":"); } String[] words = getRemainingWords(); if (words.length < 3) { throw new RrdException("Insufficent number of parameters for rrdupdate"); } String path = words[1]; RrdDb rrdDb = getRrdDbReference(path); try { if (dsNames != null) { // template specified, check datasource names for (int i = 0; i < dsNames.length; i++) { rrdDb.getDsIndex(dsNames[i]); // will throw exception if not found } } // parse update strings long timestamp = -1; for (int i = 2; i < words.length; i++) { String[] tokens = words[i].split(":"); if (dsNames != null && dsNames.length + 1 != tokens.length) { throw new RrdException("Template required " + dsNames.length + " values, " + (tokens.length - 1) + " value(s) found in: " + words[i]); } int dsCount = rrdDb.getHeader().getDsCount(); if (dsNames == null && dsCount + 1 != tokens.length) { throw new RrdException("Expected " + dsCount + " values, " + (tokens.length - 1) + " value(s) found in: " + words[i]); } TimeSpec spec = new TimeParser(tokens[0]).parse(); timestamp = spec.getTimestamp(); Sample sample = rrdDb.createSample(timestamp); for (int j = 1; j < tokens.length; j++) { if (dsNames == null) { sample.setValue(j - 1, parseDouble(tokens[j])); } else { sample.setValue(dsNames[j - 1], parseDouble(tokens[j])); } } sample.update(); } return new Long(timestamp); } finally { releaseRrdDbReference(rrdDb); } } } Index: RrdLastCmd.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/cmd/RrdLastCmd.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** RrdLastCmd.java 12 Jul 2004 13:35:17 -0000 1.1 --- RrdLastCmd.java 16 Jul 2004 07:40:48 -0000 1.2 *************** *** 32,38 **** class RrdLastCmd extends RrdToolCmd { - RrdLastCmd(RrdCmdScanner cmdScanner) { - super(cmdScanner); - } String getCmdType() { --- 32,35 ---- *************** *** 41,45 **** Object execute() throws RrdException, IOException { ! String[] words = cmdScanner.getRemainingWords(); if(words.length != 2) { throw new RrdException("Invalid rrdlast syntax"); --- 38,42 ---- Object execute() throws RrdException, IOException { ! String[] words = getRemainingWords(); if(words.length != 2) { throw new RrdException("Invalid rrdlast syntax"); Index: RrdCommander.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/cmd/RrdCommander.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** RrdCommander.java 15 Jul 2004 14:23:53 -0000 1.5 --- RrdCommander.java 16 Jul 2004 07:40:48 -0000 1.6 *************** *** 31,34 **** --- 31,35 ---- import java.io.BufferedReader; import java.io.InputStreamReader; + import java.io.File; /** *************** *** 39,43 **** private static final RrdToolCmd[] rrdCommands = { new RrdCreateCmd(), ! new RrdUpdateCommand() }; --- 40,49 ---- private static final RrdToolCmd[] rrdCommands = { new RrdCreateCmd(), ! new RrdUpdateCmd(), ! new RrdLastCmd(), ! new RrdFetchCmd(), ! new RrdDumpCmd(), ! new RrdRestoreCmd() ! }; *************** *** 121,129 **** } ! public static void main(String[] args) { System.out.println("== JRobin's RRDTool commander =="); System.out.println("Type a RRDTool command after the dollar sign and press Enter."); System.out.println("Start your RRDTool command with 'create', 'update', 'fetch' etc."); System.out.println("Use any word starting with a dot '.' to bail out"); System.out.println("================================"); RrdToolCmd.setRrdDbPoolUsed(false); --- 127,136 ---- } ! public static void main(String[] args) throws IOException { System.out.println("== JRobin's RRDTool commander =="); System.out.println("Type a RRDTool command after the dollar sign and press Enter."); System.out.println("Start your RRDTool command with 'create', 'update', 'fetch' etc."); System.out.println("Use any word starting with a dot '.' to bail out"); + System.out.println("Current directory is: " + new File(".").getCanonicalPath()); System.out.println("================================"); RrdToolCmd.setRrdDbPoolUsed(false); *************** *** 134,138 **** String s = r.readLine(); if(s.startsWith(".")) { ! System.exit(0); } execute(s); --- 141,145 ---- String s = r.readLine(); if(s.startsWith(".")) { ! return; } execute(s); Index: RrdToolCmd.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/cmd/RrdToolCmd.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** RrdToolCmd.java 15 Jul 2004 14:23:53 -0000 1.3 --- RrdToolCmd.java 16 Jul 2004 07:40:48 -0000 1.4 *************** *** 40,44 **** abstract Object execute() throws RrdException, IOException; ! public void setCommand(String command) throws RrdException { cmdScanner = new RrdCmdScanner(command); } --- 40,44 ---- abstract Object execute() throws RrdException, IOException; ! void setCommand(String command) throws RrdException { cmdScanner = new RrdCmdScanner(command); } Index: RrdFetchCmd.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/cmd/RrdFetchCmd.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** RrdFetchCmd.java 12 Jul 2004 13:37:50 -0000 1.2 --- RrdFetchCmd.java 16 Jul 2004 07:40:48 -0000 1.3 *************** *** 37,44 **** static final String DEFAULT_END = "now"; - public RrdFetchCmd(RrdCmdScanner cmdScanner) { - super(cmdScanner); - } - String getCmdType() { return "fetch"; --- 37,40 ---- *************** *** 47,69 **** 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); // --resolution ! String resolutionStr = cmdScanner.getOptionValue("r", "resolution"); ! long resolution = 1; ! if(resolutionStr != null) { ! resolution = parseLong(resolutionStr); ! } // other words ! String[] tokens = cmdScanner.getRemainingWords(); ! if(tokens.length != 3) { throw new RrdException("Invalid rrdfetch syntax"); } ! String path = tokens[1]; ! String consolFun = tokens[2]; RrdDb rrdDb = getRrdDbReference(path); try { --- 43,62 ---- Object execute() throws RrdException, IOException { // --start ! String startStr = getOptionValue("s", "start", DEFAULT_START); TimeSpec spec1 = new TimeParser(startStr).parse(); // --end ! String endStr = getOptionValue("e", "end", DEFAULT_END); TimeSpec spec2 = new TimeParser(endStr).parse(); long[] timestamps = TimeSpec.getTimestamps(spec1, spec2); // --resolution ! String resolutionStr = getOptionValue("r", "resolution", "1"); ! long resolution = parseLong(resolutionStr); // other words ! String[] words = getRemainingWords(); ! if(words.length != 3) { throw new RrdException("Invalid rrdfetch syntax"); } ! String path = words[1]; ! String consolFun = words[2]; RrdDb rrdDb = getRrdDbReference(path); try { Index: RrdRestoreCmd.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/cmd/RrdRestoreCmd.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** RrdRestoreCmd.java 13 Jul 2004 11:45:39 -0000 1.1 --- RrdRestoreCmd.java 16 Jul 2004 07:40:48 -0000 1.2 *************** *** 8,14 **** class RrdRestoreCmd extends RrdToolCmd { - public RrdRestoreCmd(RrdCmdScanner cmdScanner) { - super(cmdScanner); - } String getCmdType() { --- 8,11 ---- *************** *** 17,22 **** Object execute() throws RrdException, IOException { ! boolean check = cmdScanner.getBooleanOption("r", "range-check"); ! String[] words = cmdScanner.getRemainingWords(); if(words.length != 3) { throw new RrdException("Invalid rrdrestore syntax"); --- 14,19 ---- Object execute() throws RrdException, IOException { ! boolean check = getBooleanOption("r", "range-check"); ! String[] words = getRemainingWords(); if(words.length != 3) { throw new RrdException("Invalid rrdrestore syntax"); Index: RrdDumpCmd.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/cmd/RrdDumpCmd.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** RrdDumpCmd.java 12 Jul 2004 13:35:17 -0000 1.1 --- RrdDumpCmd.java 16 Jul 2004 07:40:48 -0000 1.2 *************** *** 32,39 **** class RrdDumpCmd extends RrdToolCmd { - RrdDumpCmd(RrdCmdScanner cmdScanner) { - super(cmdScanner); - } - String getCmdType() { return "dump"; --- 32,35 ---- *************** *** 41,45 **** Object execute() throws RrdException, IOException { ! String[] words = cmdScanner.getRemainingWords(); if(words.length != 2) { throw new RrdException("Invalid rrddump syntax"); --- 37,41 ---- Object execute() throws RrdException, IOException { ! String[] words = getRemainingWords(); if(words.length != 2) { throw new RrdException("Invalid rrddump syntax"); --- RrdUpdateCommand.java DELETED --- Index: RrdXportCmd.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/cmd/RrdXportCmd.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** RrdXportCmd.java 13 Jul 2004 20:57:47 -0000 1.1 --- RrdXportCmd.java 16 Jul 2004 07:40:48 -0000 1.2 *************** *** 32,53 **** 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() { --- 32,38 ---- import java.io.IOException; ! class RrdXportCmd extends RrdToolCmd { ! static final String DEFAULT_START = "end-1day"; ! static final String DEFAULT_END = "now"; String getCmdType() { *************** *** 55,98 **** } ! 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; --- 40,84 ---- } ! Object execute() throws RrdException, IOException { // --start ! String startStr = getOptionValue("s", "start", DEFAULT_START); ! TimeSpec spec1 = new TimeParser(startStr).parse(); // --end ! String endStr = getOptionValue("e", "end", DEFAULT_END); ! TimeSpec spec2 = new TimeParser(endStr).parse(); ! long[] timestamps = TimeSpec.getTimestamps(spec1, spec2); // --step ! String resolutionStr = getOptionValue("step", null, "1"); // Smallest step possible is default ! long resolution = parseLong(resolutionStr); // --maxrows ! String maxrowsStr = 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 = 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); ! else { ! throw new RrdException("Invalid xport syntax: " + words[i]); ! } } // Now create the export data ! RrdExport export = new RrdExport(exportDef); ! ExportData data = export.fetch(maxRows); ! System.out.println(data.exportXml()); return data; *************** *** 102,118 **** * 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] ); } --- 88,103 ---- * 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]); } *************** *** 120,136 **** * 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] ); } --- 105,120 ---- * 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]); } *************** *** 138,162 **** * 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 ); } } --- 122,145 ---- * 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); } + */ } |