1 25 26 package org.jrobin.core; 27 28 import java.util.ArrayList ; 29 import java.util.Date ; 30 import java.util.GregorianCalendar ; 31 import java.util.StringTokenizer ; 32 import java.io.*; 33 34 57 public class RrdDef { 58 59 public static final long DEFAULT_STEP = 300L; 60 62 public static final long DEFAULT_INITIAL_SHIFT = -10L; 63 64 private String path; 65 private long startTime = Util.getTime() + DEFAULT_INITIAL_SHIFT; 66 private long step = DEFAULT_STEP; 67 private ArrayList dsDefs = new ArrayList (), arcDefs = new ArrayList (); 68 69 77 public RrdDef(String path) throws RrdException { 78 if(path == null || path.length() == 0) { 79 throw new RrdException("No path specified"); 80 } 81 this.path = path; 82 } 83 84 90 public RrdDef(String path, long step) throws RrdException { 91 this(path); 92 if(step <= 0) { 93 throw new RrdException("Invalid RRD step specified: " + step); 94 } 95 this.step = step; 96 } 97 98 106 public RrdDef(String path, long startTime, long step) throws RrdException { 107 this(path, step); 108 if(startTime < 0) { 109 throw new RrdException("Invalid RRD start time specified: " + startTime); 110 } 111 this.startTime = startTime; 112 } 113 114 118 public String getPath() { 119 return path; 120 } 121 122 126 public long getStartTime() { 127 return startTime; 128 } 129 130 134 public long getStep() { 135 return step; 136 } 137 138 142 public void setPath(String path) { 143 this.path = path; 144 } 145 146 150 public void setStartTime(long startTime) { 151 this.startTime = startTime; 152 } 153 154 158 public void setStartTime(Date date) { 159 this.startTime = Util.getTimestamp(date); 160 } 161 162 166 public void setStartTime(GregorianCalendar gc) { 167 this.startTime = Util.getTimestamp(gc); 168 } 169 170 174 public void setStep(long step) { 175 this.step = step; 176 } 177 178 184 public void addDatasource(DsDef dsDef) throws RrdException { 185 if(dsDefs.contains(dsDef)) { 186 throw new RrdException("Datasource already defined: " + dsDef.dump()); 187 } 188 dsDefs.add(dsDef); 189 } 190 191 206 public void addDatasource(String dsName, String dsType, long heartbeat, 207 double minValue, double maxValue) throws RrdException { 208 addDatasource(new DsDef(dsName, dsType, heartbeat, minValue, maxValue)); 209 } 210 211 227 public void addDatasource(String rrdToolDsDef) throws RrdException { 228 RrdException rrdException = new RrdException( 229 "Wrong rrdtool-like datasource definition: " + rrdToolDsDef); 230 StringTokenizer tokenizer = new StringTokenizer (rrdToolDsDef, ":"); 231 if (tokenizer.countTokens() != 6) { 232 throw rrdException; 233 } 234 String [] tokens = new String [6]; 235 for (int curTok = 0; tokenizer.hasMoreTokens(); curTok++) { 236 tokens[curTok] = tokenizer.nextToken(); 237 } 238 if (!tokens[0].equalsIgnoreCase("DS")) { 239 throw rrdException; 240 } 241 String dsName = tokens[1]; 242 String dsType = tokens[2]; 243 long dsHeartbeat; 244 try { 245 dsHeartbeat = Long.parseLong(tokens[3]); 246 } 247 catch(NumberFormatException nfe) { 248 throw rrdException; 249 } 250 double minValue = Double.NaN; 251 if(!tokens[4].equalsIgnoreCase("U")) { 252 try { 253 minValue = Double.parseDouble(tokens[4]); 254 } 255 catch(NumberFormatException nfe) { 256 throw rrdException; 257 } 258 } 259 double maxValue = Double.NaN; 260 if(!tokens[5].equalsIgnoreCase("U")) { 261 try { 262 maxValue = Double.parseDouble(tokens[5]); 263 } 264 catch(NumberFormatException nfe) { 265 throw rrdException; 266 } 267 } 268 addDatasource(new DsDef(dsName, dsType, dsHeartbeat, minValue, maxValue)); 269 } 270 271 276 public void addDatasource(DsDef[] dsDefs) throws RrdException { 277 for(int i = 0; i < dsDefs.length; i++) { 278 addDatasource(dsDefs[i]); 279 } 280 } 281 282 288 public void addArchive(ArcDef arcDef) throws RrdException { 289 if(arcDefs.contains(arcDef)) { 290 throw new RrdException("Archive already defined: " + arcDef.dump()); 291 } 292 arcDefs.add(arcDef); 293 } 294 295 301 public void addArchive(ArcDef[] arcDefs) throws RrdException { 302 for(int i = 0; i < arcDefs.length; i++) { 303 addArchive(arcDefs[i]); 304 } 305 } 306 307 320 public void addArchive(String consolFun, double xff, int steps, int rows) 321 throws RrdException { 322 addArchive(new ArcDef(consolFun, xff, steps, rows)); 323 } 324 325 341 public void addArchive(String rrdToolArcDef) throws RrdException { 342 RrdException rrdException = new RrdException( 343 "Wrong rrdtool-like archive definition: " + rrdToolArcDef); 344 StringTokenizer tokenizer = new StringTokenizer (rrdToolArcDef, ":"); 345 if (tokenizer.countTokens() != 5) { 346 throw rrdException; 347 } 348 String [] tokens = new String [5]; 349 for (int curTok = 0; tokenizer.hasMoreTokens(); curTok++) { 350 tokens[curTok] = tokenizer.nextToken(); 351 } 352 if (!tokens[0].equalsIgnoreCase("RRA")) { 353 throw rrdException; 354 } 355 String consolFun = tokens[1]; 356 double xff; 357 try { 358 xff = Double.parseDouble(tokens[2]); 359 } 360 catch(NumberFormatException nfe) { 361 throw rrdException; 362 } 363 int steps; 364 try { 365 steps = Integer.parseInt(tokens[3]); 366 } 367 catch(NumberFormatException nfe) { 368 throw rrdException; 369 } 370 int rows; 371 try { 372 rows = Integer.parseInt(tokens[4]); 373 } 374 catch(NumberFormatException nfe) { 375 throw rrdException; 376 } 377 addArchive(new ArcDef(consolFun, xff, steps, rows)); 378 } 379 380 void validate() throws RrdException { 381 if(dsDefs.size() == 0) { 382 throw new RrdException("No RRD datasource specified. At least one is needed."); 383 } 384 if(arcDefs.size() == 0) { 385 throw new RrdException("No RRD archive specified. At least one is needed."); 386 } 387 } 388 389 393 public DsDef[] getDsDefs() { 394 return (DsDef[]) dsDefs.toArray(new DsDef[0]); 395 } 396 397 401 public ArcDef[] getArcDefs() { 402 return (ArcDef[]) arcDefs.toArray(new ArcDef[0]); 403 } 404 405 409 public int getDsCount() { 410 return dsDefs.size(); 411 } 412 413 417 public int getArcCount() { 418 return arcDefs.size(); 419 } 420 421 426 public String dump() { 427 StringBuffer buffer = new StringBuffer ("create \""); 428 buffer.append(path + "\""); 429 buffer.append(" --start " + getStartTime()); 430 buffer.append(" --step " + getStep() + " "); 431 for(int i = 0; i < dsDefs.size(); i++) { 432 DsDef dsDef = (DsDef) dsDefs.get(i); 433 buffer.append(dsDef.dump() + " "); 434 } 435 for(int i = 0; i < arcDefs.size(); i++) { 436 ArcDef arcDef = (ArcDef) arcDefs.get(i); 437 buffer.append(arcDef.dump() + " "); 438 } 439 return buffer.toString().trim(); 440 } 441 442 String getRrdToolCommand() { 443 return dump(); 444 } 445 446 void removeDatasource(String dsName) throws RrdException { 447 for(int i = 0; i < dsDefs.size(); i++) { 448 DsDef dsDef = (DsDef) dsDefs.get(i); 449 if(dsDef.getDsName().equals(dsName)) { 450 dsDefs.remove(i); 451 return; 452 } 453 } 454 throw new RrdException("Could not find datasource named '" + dsName + "'"); 455 } 456 457 void removeArchive(String consolFun, int steps) throws RrdException { 458 ArcDef arcDef = findArchive(consolFun, steps); 459 if(!arcDefs.remove(arcDef)) { 460 throw new RrdException("Could not remove archive " + consolFun + "/" + steps); 461 } 462 } 463 464 ArcDef findArchive(String consolFun, int steps) throws RrdException { 465 for(int i = 0; i < arcDefs.size(); i++) { 466 ArcDef arcDef = (ArcDef) arcDefs.get(i); 467 if(arcDef.getConsolFun().equals(consolFun) && arcDef.getSteps() == steps) { 468 return arcDef; 469 } 470 } 471 throw new RrdException("Could not find archive " + consolFun + "/" + steps); 472 } 473 474 479 public void exportXmlTemplate(OutputStream out) { 480 XmlWriter xml = new XmlWriter(out); 481 xml.startTag("rrd_def"); 482 xml.writeTag("path", getPath()); 483 xml.writeTag("step", getStep()); 484 xml.writeTag("start", getStartTime()); 485 DsDef[] dsDefs = getDsDefs(); 487 for(int i = 0; i < dsDefs.length; i++) { 488 xml.startTag("datasource"); 489 xml.writeTag("name", dsDefs[i].getDsName()); 490 xml.writeTag("type", dsDefs[i].getDsType()); 491 xml.writeTag("heartbeat", dsDefs[i].getHeartbeat()); 492 xml.writeTag("min", dsDefs[i].getMinValue(), "U"); 493 xml.writeTag("max", dsDefs[i].getMaxValue(), "U"); 494 xml.closeTag(); } 496 ArcDef[] arcDefs = getArcDefs(); 497 for(int i = 0; i < arcDefs.length; i++) { 498 xml.startTag("archive"); 499 xml.writeTag("cf", arcDefs[i].getConsolFun()); 500 xml.writeTag("xff", arcDefs[i].getXff()); 501 xml.writeTag("steps", arcDefs[i].getSteps()); 502 xml.writeTag("rows", arcDefs[i].getRows()); 503 xml.closeTag(); } 505 xml.closeTag(); xml.flush(); 507 } 508 509 514 public String exportXmlTemplate() { 515 ByteArrayOutputStream out = new ByteArrayOutputStream(); 516 exportXmlTemplate(out); 517 return out.toString(); 518 } 519 520 525 public void exportXmlTemplate(String filePath) throws IOException { 526 FileOutputStream out = new FileOutputStream(filePath, false); 527 exportXmlTemplate(out); 528 out.close(); 529 } 530 531 536 public long getEstimatedSize() { 537 int dsCount = dsDefs.size(); 538 int arcCount = arcDefs.size(); 539 int rowsCount = 0; 540 for(int i = 0; i < arcDefs.size(); i++) { 541 ArcDef arcDef = (ArcDef) arcDefs.get(i); 542 rowsCount += arcDef.getRows(); 543 } 544 return calculateSize(dsCount, arcCount, rowsCount); 545 } 546 547 static long calculateSize(int dsCount, int arcCount, int rowsCount) { 548 return 64L + 128L * dsCount + 56L * arcCount + 549 20L * dsCount * arcCount + 8L * dsCount * rowsCount; 550 } 551 552 564 public boolean equals(Object obj) { 565 if(obj == null || !(obj instanceof RrdDef)) { 566 return false; 567 } 568 RrdDef rrdDef2 = (RrdDef) obj; 569 if(step != rrdDef2.step) { 571 return false; 572 } 573 DsDef[] dsDefs = getDsDefs(), dsDefs2 = rrdDef2.getDsDefs(); 575 if(dsDefs.length != dsDefs2.length) { 576 return false; 577 } 578 for(int i = 0; i < dsDefs.length; i++) { 579 boolean matched = false; 580 for(int j = 0; j < dsDefs2.length; j++) { 581 if(dsDefs[i].exactlyEqual(dsDefs2[j])) { 582 matched = true; 583 break; 584 } 585 } 586 if(!matched) { 588 return false; 589 } 590 } 591 ArcDef[] arcDefs = getArcDefs(), arcDefs2 = rrdDef2.getArcDefs(); 593 if(arcDefs.length != arcDefs2.length) { 594 return false; 595 } 596 for(int i = 0; i < arcDefs.length; i++) { 597 boolean matched = false; 598 for(int j = 0; j < arcDefs2.length; j++) { 599 if(arcDefs[i].exactlyEqual(arcDefs2[j])) { 600 matched = true; 601 break; 602 } 603 } 604 if(!matched) { 606 return false; 607 } 608 } 609 return true; 611 } 612 } 613 | Popular Tags |