View Javadoc

1   /* ============================================================
2    * JRobin : Pure java implementation of RRDTool's functionality
3    * ============================================================
4    *
5    * Project Info:  http://www.jrobin.org
6    * Project Lead:  Sasa Markovic (saxon@jrobin.org);
7    *
8    * (C) Copyright 2003-2005, by Sasa Markovic.
9    *
10   * Developers:    Sasa Markovic (saxon@jrobin.org)
11   *
12   *
13   * This library is free software; you can redistribute it and/or modify it under the terms
14   * of the GNU Lesser General Public License as published by the Free Software Foundation;
15   * either version 2.1 of the License, or (at your option) any later version.
16   *
17   * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18   * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19   * See the GNU Lesser General Public License for more details.
20   *
21   * You should have received a copy of the GNU Lesser General Public License along with this
22   * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
23   * Boston, MA 02111-1307, USA.
24   */
25  
26  package org.jrobin.cmd;
27  
28  import org.jrobin.core.RrdException;
29  
30  import java.io.BufferedReader;
31  import java.io.File;
32  import java.io.IOException;
33  import java.io.InputStreamReader;
34  
35  /**
36   * Class to be used to execute various RRDTool commands (original syntax of RRDTool 1.0.x must be used).
37   * Currently supported commands are CREATE, UPDATE, LAST, FETCH, DUMP, RESTORE, XPORT, GRAPH, TUNE, INFO
38   */
39  public class RrdCommander {
40  	private static final RrdToolCmd[] rrdCommands = {
41  			new RrdCreateCmd(),
42  			new RrdUpdateCmd(),
43  			new RrdLastCmd(),
44  			new RrdFetchCmd(),
45  			new RrdDumpCmd(),
46  			new RrdRestoreCmd(),
47  			new RrdXportCmd(),
48  			new RrdGraphCmd(),
49  			new RrdTuneCmd(),
50  			new RrdInfoCmd()
51  	};
52  
53  	/**
54  	 * Checks if the output from any RRDTool command will be visible on the standard output device
55  	 * (console). Default setting is <code>true</code>.
56  	 *
57  	 * @return true, if the output will be visible on the standard output device; false, otherwise.
58  	 */
59  	public static synchronized boolean isStandardOutUsed() {
60  		return RrdToolCmd.isStandardOutUsed();
61  	}
62  
63  	/**
64  	 * Method used to control access to stdout (System.out, console) for all RRDTool commands. By default,
65  	 * all RRDTool commands are allowed to print results to stdout, in a form used by RRDTool.
66  	 *
67  	 * @param standardOutUsed <code>true</code> if the output should be visible on the
68  	 *                        standard output device, <code>false</code> otherwise.
69  	 */
70  	public static synchronized void setStandardOutUsed(boolean standardOutUsed) {
71  		RrdToolCmd.setStandardOutUsed(standardOutUsed);
72  	}
73  
74  	/**
75  	 * Checks if the class uses {@link org.jrobin.core.RrdDbPool} internally while executing
76  	 * RRDTool commands.
77  	 *
78  	 * @return true if the pool is used, false otherwise
79  	 */
80  	public static synchronized boolean isRrdDbPoolUsed() {
81  		return RrdToolCmd.isRrdDbPoolUsed();
82  	}
83  
84  	/**
85  	 * Forces or prohibits {@link org.jrobin.core.RrdDbPool} usage internally while executing
86  	 * RRDTool commands
87  	 *
88  	 * @param rrdDbPoolUsed true, to force pool usage, false otherwise.
89  	 */
90  	public static synchronized void setRrdDbPoolUsed(boolean rrdDbPoolUsed) {
91  		RrdToolCmd.setRrdDbPoolUsed(rrdDbPoolUsed);
92  	}
93  
94  	/**
95  	 * Executes single RRDTool command. The command string should start with some
96  	 * well known RRDTool command word (create, update, fetch, graph...)<p>
97  	 *
98  	 * @param command RRDTool command like: <p>
99  	 *                <pre>
100 	 *                create test.rrd --start "noon yesterday" --step 300 DS:x:GAUGE:600:U:U RRA:AVERAGE:0.5:5:1000
101 	 *                update test.rrd N:1000
102 	 *                last test.rrd
103 	 *                ...
104 	 *                </pre>
105 	 * @return Result of specific RRDTool command. It is guaranteed that the result of any
106 	 *         successfully executed command will be always different from null.
107 	 *         Unsuccessfully executed commands will always throw
108 	 *         an exception, so you need not check for null results.<p>
109 	 *         Exact type of the result depends from the
110 	 *         type of executed RRDTool command:<p>
111 	 *         <ul>
112 	 *         <li><b>create</b>: returns java.lang.String containing path to the newly created RRD file.
113 	 *         <li><b>last</b>: returns java.lang.Long representing timestamp of the last update.
114 	 *         <li><b>update</b>: returns java.lang.Long representing timestamp of the last update.
115 	 *         <li><b>dump</b>: returns (very long) java.lang.String representing the content of a RRD file
116 	 *         in XML format.
117 	 *         <li><b>fetch</b>: returns {@link org.jrobin.core.FetchData} object representing fetched data.
118 	 *         <li><b>restore</b>: returns path to the restored RRD file.
119 	 *         <li><b>xport</b>: returns java.lang.String containing exported data
120 	 *         <li><b>graph</b>: returns {@link org.jrobin.graph.RrdGraphInfo} object containing graph info
121 	 *         <li><b>tune</b>: returns path to the tuned RRD file
122 	 *         </ul>
123 	 * @throws IOException  thrown in case of I/O error
124 	 * @throws RrdException thrown for all other errors (parsing errors,
125 	 *                      unknown RRDTool syntax/command/option, internal RRD errors...)
126 	 */
127 	public static synchronized Object execute(String command) throws IOException, RrdException {
128 		String cmd = command.trim(), rrdtool = "rrdtool ";
129 		if (cmd.startsWith(rrdtool)) {
130 			cmd = cmd.substring(rrdtool.length());
131 		}
132 		for (RrdToolCmd rrdCommand : rrdCommands) {
133 			if (cmd.startsWith(rrdCommand.getCmdType() + " ")) {
134 				return rrdCommand.executeCommand(cmd);
135 			}
136 		}
137 		throw new RrdException("Unknown RRDTool command: " + command);
138 	}
139 
140 	/**
141 	 * A small demo which allows you to pass arbitrary RRDTool commands to JRobin
142 	 *
143 	 * @param args Not used
144 	 * @throws IOException
145 	 */
146 	public static void main(String[] args) throws IOException {
147 		System.out.println("== JRobin's RRDTool commander ==");
148 		System.out.println("Type a RRDTool command after the dollar sign and press Enter.");
149 		System.out.println("Start your RRDTool command with 'create', 'update', 'fetch' etc.");
150 		System.out.println("Start line with 'create', 'update', 'fetch' etc.");
151 		System.out.println("Enter dot ('.') to bail out");
152 		System.out.println("Current directory is: " + new File(".").getCanonicalPath());
153 		System.out.println("================================");
154 		RrdToolCmd.setRrdDbPoolUsed(false);
155 		BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
156 		while (true) {
157 			try {
158 				System.out.print("$ ");
159 				String s = r.readLine().trim();
160 				if (s.length() > 0) {
161 					if (!s.startsWith(".")) {
162 						execute(s);
163 					}
164 					else {
165 						break;
166 					}
167 				}
168 			}
169 			catch (Exception e) {
170 				e.printStackTrace(System.err);
171 			}
172 		}
173 	}
174 }