1 25 26 package org.jrobin.core; 27 28 import java.io.*; 29 30 42 public class RrdToolkit { 43 private static RrdToolkit ourInstance; 44 45 49 public synchronized static RrdToolkit getInstance() { 50 if (ourInstance == null) { 51 ourInstance = new RrdToolkit(); 52 } 53 return ourInstance; 54 } 55 56 private RrdToolkit() { 57 } 58 59 69 public void addDatasource(String sourcePath, String destPath, DsDef newDatasource) 70 throws IOException, RrdException { 71 if (Util.sameFilePath(sourcePath, destPath)) { 72 throw new RrdException("Source and destination paths are the same"); 73 } 74 RrdDb rrdSource = new RrdDb(sourcePath); 75 RrdDef rrdDef = rrdSource.getRrdDef(); 76 rrdDef.setPath(destPath); 77 rrdDef.addDatasource(newDatasource); 78 RrdDb rrdDest = new RrdDb(rrdDef); 79 rrdSource.copyStateTo(rrdDest); 80 rrdSource.close(); 81 rrdDest.close(); 82 } 83 84 100 public void addDatasource(String sourcePath, DsDef newDatasource, boolean saveBackup) 101 throws IOException, RrdException { 102 String destPath = Util.getTmpFilename(); 103 addDatasource(sourcePath, destPath, newDatasource); 104 copyFile(destPath, sourcePath, saveBackup); 105 } 106 107 117 public void removeDatasource(String sourcePath, String destPath, String dsName) 118 throws IOException, RrdException { 119 if (Util.sameFilePath(sourcePath, destPath)) { 120 throw new RrdException("Source and destination paths are the same"); 121 } 122 RrdDb rrdSource = new RrdDb(sourcePath); 123 RrdDef rrdDef = rrdSource.getRrdDef(); 124 rrdDef.setPath(destPath); 125 rrdDef.removeDatasource(dsName); 126 RrdDb rrdDest = new RrdDb(rrdDef); 127 rrdSource.copyStateTo(rrdDest); 128 rrdSource.close(); 129 rrdDest.close(); 130 } 131 132 148 public void removeDatasource(String sourcePath, String dsName, boolean saveBackup) 149 throws IOException, RrdException { 150 String destPath = Util.getTmpFilename(); 151 removeDatasource(sourcePath, destPath, dsName); 152 copyFile(destPath, sourcePath, saveBackup); 153 } 154 155 165 public void addArchive(String sourcePath, String destPath, ArcDef newArchive) 166 throws IOException, RrdException { 167 if (Util.sameFilePath(sourcePath, destPath)) { 168 throw new RrdException("Source and destination paths are the same"); 169 } 170 RrdDb rrdSource = new RrdDb(sourcePath); 171 RrdDef rrdDef = rrdSource.getRrdDef(); 172 rrdDef.setPath(destPath); 173 rrdDef.addArchive(newArchive); 174 RrdDb rrdDest = new RrdDb(rrdDef); 175 rrdSource.copyStateTo(rrdDest); 176 rrdSource.close(); 177 rrdDest.close(); 178 } 179 180 196 public void addArchive(String sourcePath, ArcDef newArchive, boolean saveBackup) 197 throws IOException, RrdException { 198 String destPath = Util.getTmpFilename(); 199 addArchive(sourcePath, destPath, newArchive); 200 copyFile(destPath, sourcePath, saveBackup); 201 } 202 203 214 public void removeArchive(String sourcePath, String destPath, String consolFun, int steps) 215 throws IOException, RrdException { 216 if (Util.sameFilePath(sourcePath, destPath)) { 217 throw new RrdException("Source and destination paths are the same"); 218 } 219 RrdDb rrdSource = new RrdDb(sourcePath); 220 RrdDef rrdDef = rrdSource.getRrdDef(); 221 rrdDef.setPath(destPath); 222 rrdDef.removeArchive(consolFun, steps); 223 RrdDb rrdDest = new RrdDb(rrdDef); 224 rrdSource.copyStateTo(rrdDest); 225 rrdSource.close(); 226 rrdDest.close(); 227 } 228 229 246 public void removeArchive(String sourcePath, String consolFun, int steps, 247 boolean saveBackup) throws IOException, RrdException { 248 String destPath = Util.getTmpFilename(); 249 removeArchive(sourcePath, destPath, consolFun, steps); 250 copyFile(destPath, sourcePath, saveBackup); 251 } 252 253 private static void copyFile(String sourcePath, String destPath, boolean saveBackup) 254 throws IOException { 255 File source = new File(sourcePath); 256 File dest = new File(destPath); 257 if (saveBackup) { 258 String backupPath = getBackupPath(destPath); 259 File backup = new File(backupPath); 260 deleteFile(backup); 261 if (!dest.renameTo(backup)) { 262 throw new IOException("Could not create backup file " + backupPath); 263 } 264 } 265 deleteFile(dest); 266 if (!source.renameTo(dest)) { 267 throw new IOException("Could not create file " + destPath + " from " + sourcePath); 268 } 269 } 270 271 private static String getBackupPath(String destPath) { 272 String backupPath = destPath; 273 do { 274 backupPath += ".bak"; 275 } while(new File(backupPath).exists()); 276 return backupPath; 277 } 278 279 287 public void setDsHeartbeat(String sourcePath, String datasourceName, 288 long newHeartbeat) throws RrdException, IOException { 289 RrdDb rrd = new RrdDb(sourcePath); 290 Datasource ds = rrd.getDatasource(datasourceName); 291 ds.setHeartbeat(newHeartbeat); 292 rrd.close(); 293 } 294 295 305 public void setDsMinValue(String sourcePath, String datasourceName, 306 double newMinValue, boolean filterArchivedValues) throws RrdException, IOException { 307 RrdDb rrd = new RrdDb(sourcePath); 308 Datasource ds = rrd.getDatasource(datasourceName); 309 ds.setMinValue(newMinValue, filterArchivedValues); 310 rrd.close(); 311 } 312 313 323 public void setDsMaxValue(String sourcePath, String datasourceName, 324 double newMaxValue, boolean filterArchivedValues) throws RrdException, IOException { 325 RrdDb rrd = new RrdDb(sourcePath); 326 Datasource ds = rrd.getDatasource(datasourceName); 327 ds.setMaxValue(newMaxValue, filterArchivedValues); 328 rrd.close(); 329 } 330 331 342 public void setDsMinMaxValue(String sourcePath, String datasourceName, 343 double newMinValue, double newMaxValue, boolean filterArchivedValues) 344 throws RrdException, IOException { 345 RrdDb rrd = new RrdDb(sourcePath); 346 Datasource ds = rrd.getDatasource(datasourceName); 347 ds.setMinMaxValue(newMinValue, newMaxValue, filterArchivedValues); 348 rrd.close(); 349 } 350 351 360 public void setArcXff(String sourcePath, String consolFun, int steps, 361 double newXff) throws RrdException, IOException { 362 RrdDb rrd = new RrdDb(sourcePath); 363 Archive arc = rrd.getArchive(consolFun, steps); 364 arc.setXff(newXff); 365 rrd.close(); 366 } 367 368 380 public void resizeArchive(String sourcePath, String destPath, String consolFun, 381 int numSteps, int newRows) 382 throws IOException, RrdException { 383 if (Util.sameFilePath(sourcePath, destPath)) { 384 throw new RrdException("Source and destination paths are the same"); 385 } 386 if (newRows < 2) { 387 throw new RrdException("New arcihve size must be at least 2"); 388 } 389 RrdDb rrdSource = new RrdDb(sourcePath); 390 RrdDef rrdDef = rrdSource.getRrdDef(); 391 ArcDef arcDef = rrdDef.findArchive(consolFun, numSteps); 392 if (arcDef.getRows() != newRows) { 393 arcDef.setRows(newRows); 394 rrdDef.setPath(destPath); 395 RrdDb rrdDest = new RrdDb(rrdDef); 396 rrdSource.copyStateTo(rrdDest); 397 rrdDest.close(); 398 } 399 rrdSource.close(); 400 } 401 402 414 public void resizeArchive(String sourcePath, String consolFun, 415 int numSteps, int newRows, boolean saveBackup) 416 throws IOException, RrdException { 417 String destPath = Util.getTmpFilename(); 418 resizeArchive(sourcePath, destPath, consolFun, numSteps, newRows); 419 copyFile(destPath, sourcePath, saveBackup); 420 } 421 422 private static void deleteFile(File file) throws IOException { 423 if (file.exists() && !file.delete()) { 424 throw new IOException("Could not delete file: " + file.getCanonicalPath()); 425 } 426 } 427 } 428 429 | Popular Tags |