1 25 package org.jrobin.graph; 26 27 import java.util.ArrayList ; 28 import java.io.IOException ; 29 30 import org.jrobin.core.*; 31 32 41 class FetchSource 42 { 43 protected static final int AVG = 0; 47 protected static final int MAX = 1; 48 protected static final int MIN = 2; 49 protected static final int LAST = 3; 50 protected static final int MAX_CF = 4; 51 52 protected static final String [] cfNames = new String [] { "AVERAGE", "MAX", "MIN", "LAST" }; 53 54 private RrdDb rrd; 55 private RrdDef rrdDef; 56 57 private String rrdFile; private String backendName; 59 60 private int numSources = 0; 61 private ArrayList [] datasources = new ArrayList [MAX_CF]; 62 63 private FetchSourceList listReference = null; 64 65 66 75 protected FetchSource( String rrdFile, FetchSourceList listRef ) 76 { 77 this.rrdFile = rrdFile; 78 listReference = listRef; 79 80 for (int i = 0; i < datasources.length; i++) 82 datasources[i] = new ArrayList ( 10 ); 83 } 84 85 96 protected FetchSource( String rrdFile, String consolFunc, String dsName, String name, FetchSourceList listRef ) throws RrdException 97 { 98 this( rrdFile, listRef ); 99 addSource( consolFunc, dsName, name ); 100 } 101 102 114 protected FetchSource( String rrdFile, String consolFunc, String dsName, String name, String backendName, FetchSourceList listRef ) throws RrdException 115 { 116 this( rrdFile, consolFunc, dsName, name, listRef ); 117 setBackendFactory( backendName ); 118 } 119 120 131 protected void addSource( String consolFunc, String dsName, String name ) throws RrdException 132 { 133 if ( consolFunc.equalsIgnoreCase("AVERAGE") || consolFunc.equalsIgnoreCase("AVG") ) 134 datasources[AVG].add( new String [] { dsName, name } ); 135 else if ( consolFunc.equalsIgnoreCase("MAX") || consolFunc.equalsIgnoreCase("MAXIMUM") ) 136 datasources[MAX].add( new String [] { dsName, name } ); 137 else if ( consolFunc.equalsIgnoreCase("MIN") || consolFunc.equalsIgnoreCase("MINIMUM") ) 138 datasources[MIN].add( new String [] { dsName, name } ); 139 else if ( consolFunc.equalsIgnoreCase("LAST") ) 140 datasources[LAST].add( new String [] { dsName, name } ); 141 else 142 throw new RrdException( "Invalid consolidation function specified." ); 143 144 numSources++; 145 } 146 147 153 protected void setBackendFactory( String backendName ) { 154 this.backendName = backendName; 155 } 156 157 167 protected ValueExtractor fetch ( long startTime, long endTime, long resolution, int reduceFactor ) throws IOException , RrdException 168 { 169 if ( rrd == null ) 170 openRrd(); 171 172 int dsSize = 0; 173 String [] dsNames, vNames; 174 175 long rrdStep = rrdDef.getStep(); 176 FetchData[] result = new FetchData[datasources.length]; 177 178 String [] names = new String [numSources]; 179 int tblPos = 0; 180 181 for (int i = 0; i < datasources.length; i++) 182 { 183 dsSize = datasources[i].size(); 184 185 if ( dsSize > 0 ) 186 { 187 dsNames = new String [ dsSize ]; 189 vNames = new String [ dsSize ]; 190 191 for (int j = 0; j < dsSize; j++ ) { 192 String [] spair = (String []) datasources[i].get(j); 193 dsNames[j] = spair[0]; 194 vNames[j] = spair[1]; 195 } 196 197 FetchRequest request = rrd.createFetchRequest( cfNames[i], startTime, endTime, resolution ); 199 request.setFilter( dsNames ); 200 201 FetchData data = request.fetchData(); 202 203 for (int j = 0; j < dsSize; j++) 204 names[ data.getDsIndex(dsNames[j]) + tblPos ] = vNames[j]; 205 tblPos += dsSize; 206 207 result[i] = data; 208 } 209 } 210 211 return new ValueExtractor( names, result, reduceFactor ); 212 } 213 214 223 protected void openRrd() throws RrdException, IOException 224 { 225 if ( rrd == null ) 226 { 227 org.jrobin.core.RrdOpener opener = listReference.getRrdOpener(); 228 229 if ( opener == null ) 230 throw new RrdException( "No RrdOpener specified for RRD management." ); 231 232 if ( rrd == null ) 234 rrd = opener.getRrd( rrdFile, getRrdBackendFactory() ); 235 236 rrdDef = rrd.getRrdDef(); 237 } 238 } 239 240 250 protected RrdDb getRrd() throws RrdException, IOException 251 { 252 if ( rrd == null ) 253 openRrd(); 254 255 return rrd; 256 } 257 258 265 protected void release() throws RrdException, IOException 266 { 267 if ( rrd != null ) 268 { 269 org.jrobin.core.RrdOpener opener = listReference.getRrdOpener(); 270 271 if ( opener == null ) 272 throw new RrdException( "No RrdOpener specified for RRD management." ); 273 274 opener.releaseRrd( rrd ); 275 rrd = null; 276 } 277 } 278 279 293 protected long getLastSampleTime( long startTime, long endTime, long resolution ) throws RrdException, IOException 294 { 295 if ( rrd == null ) 296 openRrd(); 297 298 long minSampleTime = Long.MAX_VALUE, sampleTime = 0; 299 300 for ( int i = 0; i < datasources.length; i++ ) 301 { 302 if ( datasources[i].size() > 0 ) 303 { 304 sampleTime = rrd.findStartMatchArchive( cfNames[i], startTime, resolution ).getEndTime(); 305 306 if ( sampleTime < minSampleTime ) 307 minSampleTime = sampleTime; 308 } 309 } 310 311 return minSampleTime; 312 } 313 314 324 protected long[] getFetchStep( long startTime, long endTime, long resolution ) throws RrdException, IOException 325 { 326 if ( rrd == null ) 327 openRrd(); 328 329 long maxStep = Long.MIN_VALUE, minStep = Long.MAX_VALUE, step = 0; 330 331 for ( int i = 0; i < datasources.length; i++ ) 332 { 333 if ( datasources[i].size() > 0 ) 334 { 335 FetchRequest request = rrd.createFetchRequest( cfNames[i], startTime, endTime, resolution ); 336 step = rrd.findMatchingArchive( request ).getArcStep(); 337 338 if ( step < minStep ) 339 minStep = step; 340 if ( step > maxStep ) 341 maxStep = step; 342 } 343 } 344 345 return new long[] { minStep, maxStep }; 346 } 347 348 protected String getRrdFile() { 349 return rrdFile; 350 } 351 352 protected RrdBackendFactory getRrdBackendFactory() throws RrdException 353 { 354 if ( backendName != null ) 355 return RrdBackendFactory.getFactory( backendName ); 356 357 return RrdBackendFactory.getDefaultFactory(); 358 } 359 360 public void exportXml(XmlWriter xml) { 361 for ( int i = 0; i < datasources.length; i++ ) { 362 for ( int j = 0; j < datasources[i].size(); j++ ) { 363 String [] pair = (String []) datasources[i].get(j); 364 xml.startTag("def"); 365 xml.writeTag("name", pair[1]); 366 xml.writeTag("rrd", rrdFile); 367 xml.writeTag("source", pair[0]); 368 xml.writeTag("cf", cfNames[i]); 369 xml.closeTag(); } 371 } 372 } 373 } 374 | Popular Tags |