diff options
Diffstat (limited to '')
-rw-r--r-- | js/rrdFile.js | 159 |
1 files changed, 97 insertions, 62 deletions
diff --git a/js/rrdFile.js b/js/rrdFile.js index 870c88e..eabebc7 100644 --- a/js/rrdFile.js +++ b/js/rrdFile.js | |||
@@ -5,7 +5,7 @@ | |||
5 | * Igor Sfiligoi, isfiligoi@ucsd.edu | 5 | * Igor Sfiligoi, isfiligoi@ucsd.edu |
6 | * | 6 | * |
7 | * Original repository: http://javascriptrrd.sourceforge.net/ | 7 | * Original repository: http://javascriptrrd.sourceforge.net/ |
8 | * | 8 | * |
9 | * MIT License [http://www.opensource.org/licenses/mit-license.php] | 9 | * MIT License [http://www.opensource.org/licenses/mit-license.php] |
10 | * | 10 | * |
11 | */ | 11 | */ |
@@ -17,10 +17,10 @@ | |||
17 | * | 17 | * |
18 | * This software can be used to read files produced by the RRDTool | 18 | * This software can be used to read files produced by the RRDTool |
19 | * but has been developed independently. | 19 | * but has been developed independently. |
20 | * | 20 | * |
21 | * Limitations: | 21 | * Limitations: |
22 | * | 22 | * |
23 | * This version of the module assumes RRD files created on linux | 23 | * This version of the module assumes RRD files created on linux |
24 | * with intel architecture and supports both 32 and 64 bit CPUs. | 24 | * with intel architecture and supports both 32 and 64 bit CPUs. |
25 | * All integers in RRD files are suppoes to fit in 32bit values. | 25 | * All integers in RRD files are suppoes to fit in 32bit values. |
26 | * | 26 | * |
@@ -33,7 +33,7 @@ | |||
33 | 33 | ||
34 | /* | 34 | /* |
35 | * Dependencies: | 35 | * Dependencies: |
36 | * | 36 | * |
37 | * The data provided to this module require an object of a class | 37 | * The data provided to this module require an object of a class |
38 | * that implements the following methods: | 38 | * that implements the following methods: |
39 | * getByteAt(idx) - Return a 8 bit unsigned integer at offset idx | 39 | * getByteAt(idx) - Return a 8 bit unsigned integer at offset idx |
@@ -89,13 +89,16 @@ RRDDS.prototype.getMax = function() { | |||
89 | // ============================================================ | 89 | // ============================================================ |
90 | // RRD RRA Info class | 90 | // RRD RRA Info class |
91 | function RRDRRAInfo(rrd_data,rra_def_idx, | 91 | function RRDRRAInfo(rrd_data,rra_def_idx, |
92 | rrd_align,row_cnt,pdp_step,my_idx) { | 92 | int_align,row_cnt,pdp_step,my_idx) { |
93 | this.rrd_data=rrd_data; | 93 | this.rrd_data=rrd_data; |
94 | this.rra_def_idx=rra_def_idx; | 94 | this.rra_def_idx=rra_def_idx; |
95 | this.rrd_align=rrd_align; | 95 | this.int_align=int_align; |
96 | this.row_cnt=row_cnt; | 96 | this.row_cnt=row_cnt; |
97 | this.pdp_step=pdp_step; | 97 | this.pdp_step=pdp_step; |
98 | this.my_idx=my_idx; | 98 | this.my_idx=my_idx; |
99 | |||
100 | // char nam[20], uint row_cnt, uint pdp_cnt | ||
101 | this.rra_pdp_cnt_idx=rra_def_idx+Math.ceil(20/int_align)*int_align+int_align; | ||
99 | } | 102 | } |
100 | 103 | ||
101 | RRDRRAInfo.prototype.getIdx = function() { | 104 | RRDRRAInfo.prototype.getIdx = function() { |
@@ -110,10 +113,7 @@ RRDRRAInfo.prototype.getNrRows = function() { | |||
110 | // Get number of slots used for consolidation | 113 | // Get number of slots used for consolidation |
111 | // Mostly for internal use | 114 | // Mostly for internal use |
112 | RRDRRAInfo.prototype.getPdpPerRow = function() { | 115 | RRDRRAInfo.prototype.getPdpPerRow = function() { |
113 | if (this.rrd_align==32) | 116 | return this.rrd_data.getLongAt(this.rra_pdp_cnt_idx); |
114 | return this.rrd_data.getLongAt(this.rra_def_idx+24,20); | ||
115 | else | ||
116 | return this.rrd_data.getLongAt(this.rra_def_idx+32,20); | ||
117 | } | 117 | } |
118 | 118 | ||
119 | // Get RRA step (expressed in seconds) | 119 | // Get RRA step (expressed in seconds) |
@@ -158,7 +158,7 @@ function RRDRRA(rrd_data,rra_ptr_idx, | |||
158 | } | 158 | } |
159 | } else { | 159 | } else { |
160 | throw RangeError("Row idx ("+ row_idx +") out of range [0-" + this.row_cnt +")."); | 160 | throw RangeError("Row idx ("+ row_idx +") out of range [0-" + this.row_cnt +")."); |
161 | } | 161 | } |
162 | } | 162 | } |
163 | } | 163 | } |
164 | 164 | ||
@@ -199,43 +199,89 @@ RRDRRA.prototype.getElFast = function(row_idx,ds_idx) { | |||
199 | function RRDHeader(rrd_data) { | 199 | function RRDHeader(rrd_data) { |
200 | this.rrd_data=rrd_data; | 200 | this.rrd_data=rrd_data; |
201 | this.validate_rrd(); | 201 | this.validate_rrd(); |
202 | this.load_header(); | ||
203 | this.calc_idxs(); | 202 | this.calc_idxs(); |
204 | } | 203 | } |
205 | 204 | ||
206 | // Internal, used for initialization | 205 | // Internal, used for initialization |
207 | RRDHeader.prototype.validate_rrd = function() { | 206 | RRDHeader.prototype.validate_rrd = function() { |
207 | if (this.rrd_data.getLength()<1) throw new InvalidRRD("Empty file."); | ||
208 | if (this.rrd_data.getLength()<16) throw new InvalidRRD("File too short."); | ||
208 | if (this.rrd_data.getCStringAt(0,4)!=="RRD") throw new InvalidRRD("Wrong magic id."); | 209 | if (this.rrd_data.getCStringAt(0,4)!=="RRD") throw new InvalidRRD("Wrong magic id."); |
209 | 210 | ||
210 | this.rrd_version=this.rrd_data.getCStringAt(4,5); | 211 | this.rrd_version=this.rrd_data.getCStringAt(4,5); |
211 | if ((this.rrd_version!=="0003")&&(this.rrd_version!=="0004")) { | 212 | if ((this.rrd_version!=="0003")&&(this.rrd_version!=="0004")&&(this.rrd_version!=="0001")) { |
212 | throw new InvalidRRD("Unsupported RRD version "+this.rrd_version+"."); | 213 | throw new InvalidRRD("Unsupported RRD version "+this.rrd_version+"."); |
213 | } | 214 | } |
214 | 215 | ||
215 | if (this.rrd_data.getDoubleAt(12)==8.642135e+130) { | 216 | this.float_width=8; |
216 | this.rrd_align=32; | 217 | if (this.rrd_data.getLongAt(12)==0) { |
217 | } else if (this.rrd_data.getDoubleAt(16)==8.642135e+130) { | 218 | // not a double here... likely 64 bit |
218 | this.rrd_align=64; | 219 | this.float_align=8; |
220 | if (! (this.rrd_data.getDoubleAt(16)==8.642135e+130)) { | ||
221 | // uhm... wrong endian? | ||
222 | this.rrd_data.switch_endian=true; | ||
223 | } | ||
224 | if (this.rrd_data.getDoubleAt(16)==8.642135e+130) { | ||
225 | // now, is it all 64bit or only float 64 bit? | ||
226 | if (this.rrd_data.getLongAt(28)==0) { | ||
227 | // true 64 bit align | ||
228 | this.int_align=8; | ||
229 | this.int_width=8; | ||
230 | } else { | ||
231 | // integers are 32bit aligned | ||
232 | this.int_align=4; | ||
233 | this.int_width=4; | ||
234 | } | ||
235 | } else { | ||
236 | throw new InvalidRRD("Magic float not found at 16."); | ||
237 | } | ||
219 | } else { | 238 | } else { |
220 | throw new InvalidRRD("Unsupported platform."); | 239 | /// should be 32 bit alignment |
240 | if (! (this.rrd_data.getDoubleAt(12)==8.642135e+130)) { | ||
241 | // uhm... wrong endian? | ||
242 | this.rrd_data.switch_endian=true; | ||
243 | } | ||
244 | if (this.rrd_data.getDoubleAt(12)==8.642135e+130) { | ||
245 | this.float_align=4; | ||
246 | this.int_align=4; | ||
247 | this.int_width=4; | ||
248 | } else { | ||
249 | throw new InvalidRRD("Magic float not found at 12."); | ||
250 | } | ||
221 | } | 251 | } |
222 | } | 252 | this.unival_width=this.float_width; |
253 | this.unival_align=this.float_align; | ||
223 | 254 | ||
224 | // Internal, used for initialization | 255 | // process the header here, since I need it for validation |
225 | RRDHeader.prototype.load_header = function() { | 256 | |
226 | if (this.rrd_align==32) { | 257 | // char magic[4], char version[5], double magic_float |
227 | this.ds_cnt=this.rrd_data.getLongAt(20,false); | 258 | |
228 | this.rra_cnt=this.rrd_data.getLongAt(24,false); | 259 | // long ds_cnt, long rra_cnt, long pdp_step, unival par[10] |
229 | this.pdp_step=this.rrd_data.getLongAt(28,false); | 260 | this.ds_cnt_idx=Math.ceil((4+5)/this.float_align)*this.float_align+this.float_width; |
230 | // 8*10 unused values follow | 261 | this.rra_cnt_idx=this.ds_cnt_idx+this.int_width; |
231 | this.top_header_size=112; | 262 | this.pdp_step_idx=this.rra_cnt_idx+this.int_width; |
232 | } else { | 263 | |
233 | //get only the low 32 bits, the high 32 should always be 0 | 264 | //always get only the low 32 bits, the high 32 on 64 bit archs should always be 0 |
234 | this.ds_cnt=this.rrd_data.getLongAt(24,false); | 265 | this.ds_cnt=this.rrd_data.getLongAt(this.ds_cnt_idx); |
235 | this.rra_cnt=this.rrd_data.getLongAt(32,false); | 266 | if (this.ds_cnt<1) { |
236 | this.pdp_step=this.rrd_data.getLongAt(40,false); | 267 | throw new InvalidRRD("ds count less than 1."); |
237 | // 8*10 unused values follow | 268 | } |
238 | this.top_header_size=128; | 269 | |
270 | this.rra_cnt=this.rrd_data.getLongAt(this.rra_cnt_idx); | ||
271 | if (this.ds_cnt<1) { | ||
272 | throw new InvalidRRD("rra count less than 1."); | ||
273 | } | ||
274 | |||
275 | this.pdp_step=this.rrd_data.getLongAt(this.pdp_step_idx); | ||
276 | if (this.pdp_step<1) { | ||
277 | throw new InvalidRRD("pdp step less than 1."); | ||
278 | } | ||
279 | |||
280 | // best guess, assuming no weird align problems | ||
281 | this.top_header_size=Math.ceil((this.pdp_step_idx+this.int_width)/this.unival_align)*this.unival_align+10*this.unival_width; | ||
282 | var t=this.rrd_data.getLongAt(this.top_header_size); | ||
283 | if (t==0) { | ||
284 | throw new InvalidRRD("Could not find first DS name."); | ||
239 | } | 285 | } |
240 | } | 286 | } |
241 | 287 | ||
@@ -243,43 +289,29 @@ RRDHeader.prototype.load_header = function() { | |||
243 | RRDHeader.prototype.calc_idxs = function() { | 289 | RRDHeader.prototype.calc_idxs = function() { |
244 | this.ds_def_idx=this.top_header_size; | 290 | this.ds_def_idx=this.top_header_size; |
245 | // char ds_nam[20], char dst[20], unival par[10] | 291 | // char ds_nam[20], char dst[20], unival par[10] |
246 | this.ds_el_size=120; | 292 | this.ds_el_size=Math.ceil((20+20)/this.unival_align)*this.unival_align+10*this.unival_width; |
247 | 293 | ||
248 | this.rra_def_idx=this.ds_def_idx+this.ds_el_size*this.ds_cnt; | 294 | this.rra_def_idx=this.ds_def_idx+this.ds_el_size*this.ds_cnt; |
249 | // char cf_nam[20], uint row_cnt, uint pdp_cnt, unival par[10] | 295 | // char cf_nam[20], uint row_cnt, uint pdp_cnt, unival par[10] |
250 | this.row_cnt_idx; | 296 | this.row_cnt_idx=Math.ceil(20/this.int_align)*this.int_align; |
251 | if (this.rrd_align==32) { | 297 | this.rra_def_el_size=Math.ceil((this.row_cnt_idx+2*this.int_width)/this.unival_align)*this.unival_align+10*this.unival_width; |
252 | this.rra_def_el_size=108; | ||
253 | this.row_cnt_idx=20; | ||
254 | } else { | ||
255 | this.rra_def_el_size=120; | ||
256 | this.row_cnt_idx=24; | ||
257 | } | ||
258 | 298 | ||
259 | this.live_head_idx=this.rra_def_idx+this.rra_def_el_size*this.rra_cnt; | 299 | this.live_head_idx=this.rra_def_idx+this.rra_def_el_size*this.rra_cnt; |
260 | // time_t last_up, int last_up_usec | 300 | // time_t last_up, int last_up_usec |
261 | if (this.rrd_align==32) { | 301 | this.live_head_size=2*this.int_width; |
262 | this.live_head_size=8; | ||
263 | } else { | ||
264 | this.live_head_size=16; | ||
265 | } | ||
266 | 302 | ||
267 | this.pdp_prep_idx=this.live_head_idx+this.live_head_size; | 303 | this.pdp_prep_idx=this.live_head_idx+this.live_head_size; |
268 | // char last_ds[30], unival scratch[10] | 304 | // char last_ds[30], unival scratch[10] |
269 | this.pdp_prep_el_size=112; | 305 | this.pdp_prep_el_size=Math.ceil(30/this.unival_align)*this.unival_align+10*this.unival_width; |
270 | 306 | ||
271 | this.cdp_prep_idx=this.pdp_prep_idx+this.pdp_prep_el_size*this.ds_cnt; | 307 | this.cdp_prep_idx=this.pdp_prep_idx+this.pdp_prep_el_size*this.ds_cnt; |
272 | // unival scratch[10] | 308 | // unival scratch[10] |
273 | this.cdp_prep_el_size=80; | 309 | this.cdp_prep_el_size=10*this.unival_width; |
274 | 310 | ||
275 | this.rra_ptr_idx=this.cdp_prep_idx+this.cdp_prep_el_size*this.ds_cnt*this.rra_cnt; | 311 | this.rra_ptr_idx=this.cdp_prep_idx+this.cdp_prep_el_size*this.ds_cnt*this.rra_cnt; |
276 | // uint cur_row | 312 | // uint cur_row |
277 | if (this.rrd_align==32) { | 313 | this.rra_ptr_el_size=1*this.int_width; |
278 | this.rra_ptr_el_size=4; | 314 | |
279 | } else { | ||
280 | this.rra_ptr_el_size=8; | ||
281 | } | ||
282 | |||
283 | this.header_size=this.rra_ptr_idx+this.rra_ptr_el_size*this.rra_cnt; | 315 | this.header_size=this.rra_ptr_idx+this.rra_ptr_el_size*this.rra_cnt; |
284 | } | 316 | } |
285 | 317 | ||
@@ -325,7 +357,7 @@ RRDHeader.prototype.getDSbyIdx = function(idx) { | |||
325 | return new RRDDS(this.rrd_data,this.ds_def_idx+this.ds_el_size*idx,idx); | 357 | return new RRDDS(this.rrd_data,this.ds_def_idx+this.ds_el_size*idx,idx); |
326 | } else { | 358 | } else { |
327 | throw RangeError("DS idx ("+ idx +") out of range [0-" + this.ds_cnt +")."); | 359 | throw RangeError("DS idx ("+ idx +") out of range [0-" + this.ds_cnt +")."); |
328 | } | 360 | } |
329 | } | 361 | } |
330 | RRDHeader.prototype.getDSbyName = function(name) { | 362 | RRDHeader.prototype.getDSbyName = function(name) { |
331 | for (var idx=0; idx<this.ds_cnt; idx++) { | 363 | for (var idx=0; idx<this.ds_cnt; idx++) { |
@@ -344,20 +376,23 @@ RRDHeader.prototype.getRRAInfo = function(idx) { | |||
344 | if ((idx>=0) && (idx<this.rra_cnt)) { | 376 | if ((idx>=0) && (idx<this.rra_cnt)) { |
345 | return new RRDRRAInfo(this.rrd_data, | 377 | return new RRDRRAInfo(this.rrd_data, |
346 | this.rra_def_idx+idx*this.rra_def_el_size, | 378 | this.rra_def_idx+idx*this.rra_def_el_size, |
347 | this.rrd_align,this.rra_def_row_cnts[idx],this.pdp_step, | 379 | this.int_align,this.rra_def_row_cnts[idx],this.pdp_step, |
348 | idx); | 380 | idx); |
349 | } else { | 381 | } else { |
350 | throw RangeError("RRA idx ("+ idx +") out of range [0-" + this.rra_cnt +")."); | 382 | throw RangeError("RRA idx ("+ idx +") out of range [0-" + this.rra_cnt +")."); |
351 | } | 383 | } |
352 | } | 384 | } |
353 | 385 | ||
354 | // ============================================================ | 386 | // ============================================================ |
355 | // RRDFile class | 387 | // RRDFile class |
356 | // Given a BinaryFile, gives access to the RRD archive fields | 388 | // Given a BinaryFile, gives access to the RRD archive fields |
357 | // | 389 | // |
358 | // Arguments: | 390 | // Arguments: |
359 | // bf must be an object compatible with the BinaryFile interface | 391 | // bf must be an object compatible with the BinaryFile interface |
360 | function RRDFile(bf) { | 392 | // file_options - currently no semantics... introduced for future expandability |
393 | function RRDFile(bf,file_options) { | ||
394 | this.file_options=file_options; | ||
395 | |||
361 | var rrd_data=bf | 396 | var rrd_data=bf |
362 | 397 | ||
363 | this.rrd_header=new RRDHeader(rrd_data); | 398 | this.rrd_header=new RRDHeader(rrd_data); |
@@ -396,7 +431,7 @@ function RRDFile(bf) { | |||
396 | } | 431 | } |
397 | 432 | ||
398 | this.getRRA = function(idx) { | 433 | this.getRRA = function(idx) { |
399 | var rra_info=this.rrd_header.getRRAInfo(idx); | 434 | rra_info=this.rrd_header.getRRAInfo(idx); |
400 | return new RRDRRA(rrd_data, | 435 | return new RRDRRA(rrd_data, |
401 | this.rrd_header.rra_ptr_idx+idx*this.rrd_header.rra_ptr_el_size, | 436 | this.rrd_header.rra_ptr_idx+idx*this.rrd_header.rra_ptr_el_size, |
402 | rra_info, | 437 | rra_info, |