jrobinrrdstrategy.java
来自「opennms得相关源码 请大家看看」· Java 代码 · 共 602 行 · 第 1/2 页
JAVA
602 行
//// This file is part of the OpenNMS(R) Application.//// OpenNMS(R) is Copyright (C) 2002-2005 The OpenNMS Group, Inc. All rights reserved.// OpenNMS(R) is a derivative work, containing both original code, included code and modified// code that was published under the GNU General Public License. Copyrights for modified // and included code are below.//// OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.//// Modifications://// Jul 8, 2004: Created this file.//// Original code base Copyright (C) 1999-2001 Oculan Corp. All rights reserved.//// This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.//// This program 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 General Public License for more details. //// You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.// // For more information contact: // OpenNMS Licensing <license@opennms.org>// http://www.opennms.org/// http://www.opennms.com///// Tab Size = 8//package org.opennms.netmgt.rrd;import java.awt.Color;import java.awt.Font;import java.io.ByteArrayInputStream;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import org.apache.log4j.Category;import org.jrobin.core.FetchData;import org.jrobin.core.FetchPoint;import org.jrobin.core.RrdDb;import org.jrobin.core.RrdDef;import org.jrobin.core.RrdException;import org.jrobin.core.Sample;import org.jrobin.graph.RrdGraph;import org.jrobin.graph.RrdGraphDef;import org.opennms.core.utils.ThreadCategory;/** * Provides a JRobin based implementation of RrdStrategy. It uses JRobin 1.4 in * FILE mode (NIO is too memory consuming for the large number of files that we * open) */public class JRobinRrdStrategy implements RrdStrategy { private boolean m_initialized = false; /** * Closes the JRobin RrdDb. */ public void closeFile(Object rrdFile) throws Exception { ((RrdDb) rrdFile).close(); } /** * Creates and returns a RrdDef represented by the parameters. */ public Object createDefinition(String creator, String directory, String dsName, int step, String dsType, int dsHeartbeat, String dsMin, String dsMax, List rraList) throws Exception { File f = new File(directory); f.mkdirs(); String fileName = directory + File.separator + dsName + ".rrd"; RrdDef def = new RrdDef(fileName); // def.setStartTime(System.currentTimeMillis()/1000L - 2592000L); def.setStartTime(1000); def.setStep(step); double min = (dsMin == null || "U".equals(dsMin) ? Double.NaN : Double.parseDouble(dsMin)); double max = (dsMax == null || "U".equals(dsMax) ? Double.NaN : Double.parseDouble(dsMax)); def.addDatasource(dsName, dsType, dsHeartbeat, min, max); for (Iterator it = rraList.iterator(); it.hasNext();) { String rra = (String) it.next(); def.addArchive(rra); } return def; } /** * Creates the JRobin RrdDb from the def by opening the file and then * closing. TODO: Change the interface here to create the file and return it * opened. */ public void createFile(Object rrdDef) throws Exception { RrdDb rrd = new RrdDb((RrdDef) rrdDef); rrd.close(); } /** * Opens the JRobin RrdDb by name and returns it. */ public Object openFile(String fileName) throws Exception { RrdDb rrd = new RrdDb(fileName); return rrd; } /** * Creates a sample from the JRobin RrdDb and passes in the data provided. */ public void updateFile(Object rrdFile, String data) throws Exception { Sample sample = ((RrdDb) rrdFile).createSample(); sample.setAndUpdate(data); } /** * Initialized the RrdDb to use the FILE factory because the NIO factory * uses too much memory for our implementation. */ public synchronized void initialize() throws Exception { if (!m_initialized) { RrdDb.setDefaultFactory("FILE"); m_initialized = true; } } /* * (non-Javadoc) * * @see org.opennms.netmgt.rrd.RrdStrategy#graphicsInitialize() */ public void graphicsInitialize() throws Exception { initialize(); } /** * Fetch the last value from the JRobin RrdDb file. */ public Double fetchLastValue(String fileName, int interval) throws NumberFormatException, org.opennms.netmgt.rrd.RrdException { RrdDb rrd = null; try { Category log = ThreadCategory.getInstance(getClass()); rrd = new RrdDb(fileName); long lastUpdateTime = rrd.getLastUpdateTime(); long now = System.currentTimeMillis(); long preciseCollectTime = (now - (now % interval)) / 1000L; long collectTime = (now - interval) / 1000L; log.debug("rrd last update time: " + lastUpdateTime + " collect time " + collectTime + " precise collect time " + preciseCollectTime); FetchData data = rrd.createFetchRequest("AVERAGE", preciseCollectTime, preciseCollectTime).fetchData(); //FetchData data = rrd.createFetchRequest("AVERAGE", lastUpdateTime, lastUpdateTime).fetchData(); for(int i = 0; i < data.getRowCount(); i++) { FetchPoint point = data.getRow(i); log.debug("got the following from row: " + i + " of my RRD: " + point.dump()); } double[] vals = data.getValues(0); if (vals.length > 0) { return new Double(vals[vals.length - 1]); } return null; } catch (IOException e) { throw new org.opennms.netmgt.rrd.RrdException("Exception occurred fetching data from " + fileName, e); } catch (RrdException e) { throw new org.opennms.netmgt.rrd.RrdException("Exception occurred fetching data from " + fileName, e); } finally { if (rrd != null) try { rrd.close(); } catch (IOException e) { Category log = ThreadCategory.getInstance(getClass()); log.error("Failed to close rrd file: " + fileName, e); } } } public Double fetchLastValueInRange(String fileName, int interval, int range) throws NumberFormatException, org.opennms.netmgt.rrd.RrdException { RrdDb rrd = null; try { Category log = ThreadCategory.getInstance(getClass()); rrd = new RrdDb(fileName); long lastUpdateTime = rrd.getLastUpdateTime(); long now = System.currentTimeMillis(); long latestUpdateTime = (now - (now % interval)) / 1000L; long earliestUpdateTime = ((now - (now % interval)) - range) / 1000L; log.debug("fetching data from " + earliestUpdateTime + " to " + latestUpdateTime); FetchData data = rrd.createFetchRequest("AVERAGE", earliestUpdateTime, latestUpdateTime).fetchData(); //FetchData data = rrd.createFetchRequest("AVERAGE", lastUpdateTime, lastUpdateTime).fetchData(); double[] vals = data.getValues(0); long[] times = data.getTimestamps(); // step backwards through the array of values until we get something that's a number for(int i = vals.length - 1; i >= 0; i--) { if ( Double.isNaN(vals[i]) ) { log.debug("Got a NaN value at interval: " + times[i]); } else { log.debug("Got a non NaN value at interval: " + times[i] + " : " + vals[i] ); return new Double(vals[i]); } } return null; } catch (IOException e) { throw new org.opennms.netmgt.rrd.RrdException("Exception occurred fetching data from " + fileName, e); } catch (RrdException e) { throw new org.opennms.netmgt.rrd.RrdException("Exception occurred fetching data from " + fileName, e); } finally { if (rrd != null) try { rrd.close(); } catch (IOException e) { Category log = ThreadCategory.getInstance(getClass()); log.error("Failed to close rrd file: " + fileName, e); } } } private Color getColor(String colorValue) { int colorVal = Integer.parseInt(colorValue, 16); return new Color(colorVal); } private String[] tokenize(String line, String delims, boolean processQuoted) { Category log = ThreadCategory.getInstance(getClass()); List tokenList = new LinkedList(); StringBuffer currToken = new StringBuffer(); boolean quoting = false; boolean escaping = false; boolean debugTokens = Boolean.getBoolean("org.opennms.netmgt.rrd.debugTokens"); if (debugTokens) log.debug("tokenize: line=" + line + " delims=" + delims); for (int i = 0; i < line.length(); i++) { char ch = line.charAt(i); if (debugTokens) log.debug("tokenize: checking char: " + ch); if (escaping) { if (ch == 'n') { currToken.append('\n'); } else if (ch == 'r') { currToken.append('\r'); } else if (ch == 't') { currToken.append('\t'); } else { currToken.append(ch); } escaping = false; if (debugTokens) log.debug("tokenize: escaped. appended to " + currToken); } else if (ch == '\\') { if (debugTokens) log.debug("tokenize: found a backslash... escaping currToken = " + currToken); if (quoting && !processQuoted) currToken.append(ch); else escaping = true; } else if (ch == '\"') { if (!processQuoted) currToken.append(ch); if (quoting) { if (debugTokens) log.debug("tokenize: found a quote ending quotation currToken = " + currToken); quoting = false; } else { if (debugTokens) log.debug("tokenize: found a quote beginning quotation currToke =" + currToken); quoting = true; } } else if (!quoting && delims.indexOf(ch) >= 0) { if (debugTokens) log.debug("tokenize: found a token: " + ch + " ending token [" + currToken + "] and starting a new one"); tokenList.add(currToken.toString()); currToken = new StringBuffer(); } else { if (debugTokens) log.debug("tokenize: appending " + ch + " to token: " + currToken); currToken.append(ch); } } if (escaping || quoting) { if (debugTokens) log.debug("tokenize: ended string but escaping = " + escaping + " and quoting = " + quoting); throw new IllegalArgumentException("unable to tokenize string " + line + " with token chars " + delims);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?