eb9b4690e0fbb22f1788b95711695a80a8fa3499
1 /*****************************************************************************
2 * RRDtool 1.2.23 Copyright by Tobi Oetiker, 1997-2007
3 *****************************************************************************
4 * rrd_dump Display a RRD
5 *****************************************************************************
6 * $Id$
7 * $Log$
8 * Revision 1.7 2004/05/25 20:53:21 oetiker
9 * prevent small leak when resources are exhausted -- Mike Slifcak
10 *
11 * Revision 1.6 2004/05/25 20:51:49 oetiker
12 * Update displayed copyright messages to be consistent. -- Mike Slifcak
13 *
14 * Revision 1.5 2003/02/13 07:05:27 oetiker
15 * Find attached the patch I promised to send to you. Please note that there
16 * are three new source files (src/rrd_is_thread_safe.h, src/rrd_thread_safe.c
17 * and src/rrd_not_thread_safe.c) and the introduction of librrd_th. This
18 * library is identical to librrd, but it contains support code for per-thread
19 * global variables currently used for error information only. This is similar
20 * to how errno per-thread variables are implemented. librrd_th must be linked
21 * alongside of libpthred
22 *
23 * There is also a new file "THREADS", holding some documentation.
24 *
25 * -- Peter Stamfest <peter@stamfest.at>
26 *
27 * Revision 1.4 2002/02/01 20:34:49 oetiker
28 * fixed version number and date/time
29 *
30 * Revision 1.3 2001/03/10 23:54:39 oetiker
31 * Support for COMPUTE data sources (CDEF data sources). Removes the RPN
32 * parser and calculator from rrd_graph and puts then in a new file,
33 * rrd_rpncalc.c. Changes to core files rrd_create and rrd_update. Some
34 * clean-up of aberrant behavior stuff, including a bug fix.
35 * Documentation update (rrdcreate.pod, rrdupdate.pod). Change xml format.
36 * -- Jake Brutlag <jakeb@corp.webtv.net>
37 *
38 * Revision 1.2 2001/03/04 13:01:55 oetiker
39 *
40 * Revision 1.1.1.1 2001/02/25 22:25:05 oetiker
41 * checkin
42 *
43 *****************************************************************************/
44 #include "rrd_tool.h"
45 #include "rrd_rpncalc.h"
47 #if !(defined(NETWARE) || defined(WIN32))
48 extern char *tzname[2];
49 #endif
51 int rrd_dump(
52 int argc,
53 char **argv)
54 {
55 int rc;
57 if (argc < 2) {
58 rrd_set_error("Not enough arguments");
59 return -1;
60 }
62 if (argc == 3) {
63 rc = rrd_dump_r(argv[1], argv[2]);
64 } else {
65 rc = rrd_dump_r(argv[1], NULL);
66 }
68 return rc;
69 }
71 int rrd_dump_r(
72 const char *filename,
73 char *outname)
74 {
75 unsigned int i, ii, ix, iii = 0;
76 time_t now;
77 char somestring[255];
78 rrd_value_t my_cdp;
79 off_t rra_base, rra_start, rra_next;
80 rrd_file_t *rrd_file;
81 FILE *out_file;
82 rrd_t rrd;
83 rrd_value_t value;
84 struct tm tm;
86 rrd_file = rrd_open(filename, &rrd, RRD_READONLY | RRD_READAHEAD);
87 if (rrd_file == NULL) {
88 rrd_free(&rrd);
89 return (-1);
90 }
92 out_file = NULL;
93 if (outname) {
94 if (!(out_file = fopen(outname, "w"))) {
95 return (-1);
96 }
97 } else {
98 out_file = stdout;
99 }
101 fputs("<!-- Round Robin Database Dump -->", out_file);
102 fputs("<rrd>", out_file);
103 fprintf(out_file, "\t<version> %s </version>\n", RRD_VERSION);
104 fprintf(out_file, "\t<step> %lu </step> <!-- Seconds -->\n",
105 rrd.stat_head->pdp_step);
106 #if HAVE_STRFTIME
107 localtime_r(&rrd.live_head->last_up, &tm);
108 strftime(somestring, 200, "%Y-%m-%d %H:%M:%S %Z", &tm);
109 #else
110 # error "Need strftime"
111 #endif
112 fprintf(out_file, "\t<lastupdate> %ld </lastupdate> <!-- %s -->\n\n",
113 rrd.live_head->last_up, somestring);
114 for (i = 0; i < rrd.stat_head->ds_cnt; i++) {
115 fprintf(out_file, "\t<ds>\n");
116 fprintf(out_file, "\t\t<name> %s </name>\n", rrd.ds_def[i].ds_nam);
117 fprintf(out_file, "\t\t<type> %s </type>\n", rrd.ds_def[i].dst);
118 if (dst_conv(rrd.ds_def[i].dst) != DST_CDEF) {
119 fprintf(out_file,
120 "\t\t<minimal_heartbeat> %lu </minimal_heartbeat>\n",
121 rrd.ds_def[i].par[DS_mrhb_cnt].u_cnt);
122 if (isnan(rrd.ds_def[i].par[DS_min_val].u_val)) {
123 fprintf(out_file, "\t\t<min> NaN </min>\n");
124 } else {
125 fprintf(out_file, "\t\t<min> %0.10e </min>\n",
126 rrd.ds_def[i].par[DS_min_val].u_val);
127 }
128 if (isnan(rrd.ds_def[i].par[DS_max_val].u_val)) {
129 fprintf(out_file, "\t\t<max> NaN </max>\n");
130 } else {
131 fprintf(out_file, "\t\t<max> %0.10e </max>\n",
132 rrd.ds_def[i].par[DS_max_val].u_val);
133 }
134 } else { /* DST_CDEF */
135 char *str = NULL;
137 rpn_compact2str((rpn_cdefds_t *) &(rrd.ds_def[i].par[DS_cdef]),
138 rrd.ds_def, &str);
139 fprintf(out_file, "\t\t<cdef> %s </cdef>\n", str);
140 free(str);
141 }
142 fprintf(out_file, "\n\t\t<!-- PDP Status -->\n");
143 fprintf(out_file, "\t\t<last_ds> %s </last_ds>\n",
144 rrd.pdp_prep[i].last_ds);
145 if (isnan(rrd.pdp_prep[i].scratch[PDP_val].u_val)) {
146 fprintf(out_file, "\t\t<value> NaN </value>\n");
147 } else {
148 fprintf(out_file, "\t\t<value> %0.10e </value>\n",
149 rrd.pdp_prep[i].scratch[PDP_val].u_val);
150 }
151 fprintf(out_file, "\t\t<unknown_sec> %lu </unknown_sec>\n",
152 rrd.pdp_prep[i].scratch[PDP_unkn_sec_cnt].u_cnt);
154 fprintf(out_file, "\t</ds>\n\n");
155 }
157 fputs("<!-- Round Robin Archives -->", out_file);
159 rra_base = rrd_file->header_len;
160 rra_next = rra_base;
162 for (i = 0; i < rrd.stat_head->rra_cnt; i++) {
164 long timer = 0;
166 rra_start = rra_next;
167 rra_next += (rrd.stat_head->ds_cnt
168 * rrd.rra_def[i].row_cnt * sizeof(rrd_value_t));
169 fprintf(out_file, "\t<rra>\n");
170 fprintf(out_file, "\t\t<cf> %s </cf>\n", rrd.rra_def[i].cf_nam);
171 fprintf(out_file,
172 "\t\t<pdp_per_row> %lu </pdp_per_row> <!-- %lu seconds -->\n\n",
173 rrd.rra_def[i].pdp_cnt,
174 rrd.rra_def[i].pdp_cnt * rrd.stat_head->pdp_step);
175 /* support for RRA parameters */
176 fprintf(out_file, "\t\t<params>\n");
177 switch (cf_conv(rrd.rra_def[i].cf_nam)) {
178 case CF_HWPREDICT:
179 case CF_MHWPREDICT:
180 fprintf(out_file, "\t\t<hw_alpha> %0.10e </hw_alpha>\n",
181 rrd.rra_def[i].par[RRA_hw_alpha].u_val);
182 fprintf(out_file, "\t\t<hw_beta> %0.10e </hw_beta>\n",
183 rrd.rra_def[i].par[RRA_hw_beta].u_val);
184 fprintf(out_file,
185 "\t\t<dependent_rra_idx> %lu </dependent_rra_idx>\n",
186 rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
187 break;
188 case CF_SEASONAL:
189 case CF_DEVSEASONAL:
190 fprintf(out_file,
191 "\t\t<seasonal_gamma> %0.10e </seasonal_gamma>\n",
192 rrd.rra_def[i].par[RRA_seasonal_gamma].u_val);
193 fprintf(out_file,
194 "\t\t<seasonal_smooth_idx> %lu </seasonal_smooth_idx>\n",
195 rrd.rra_def[i].par[RRA_seasonal_smooth_idx].u_cnt);
196 fprintf(out_file,
197 "\t\t<dependent_rra_idx> %lu </dependent_rra_idx>\n",
198 rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
199 break;
200 case CF_FAILURES:
201 fprintf(out_file, "\t\t<delta_pos> %0.10e </delta_pos>\n",
202 rrd.rra_def[i].par[RRA_delta_pos].u_val);
203 fprintf(out_file, "\t\t<delta_neg> %0.10e </delta_neg>\n",
204 rrd.rra_def[i].par[RRA_delta_neg].u_val);
205 fprintf(out_file, "\t\t<window_len> %lu </window_len>\n",
206 rrd.rra_def[i].par[RRA_window_len].u_cnt);
207 fprintf(out_file,
208 "\t\t<failure_threshold> %lu </failure_threshold>\n",
209 rrd.rra_def[i].par[RRA_failure_threshold].u_cnt);
210 /* fall thru */
211 case CF_DEVPREDICT:
212 fprintf(out_file,
213 "\t\t<dependent_rra_idx> %lu </dependent_rra_idx>\n",
214 rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
215 break;
216 case CF_AVERAGE:
217 case CF_MAXIMUM:
218 case CF_MINIMUM:
219 case CF_LAST:
220 default:
221 fprintf(out_file, "\t\t<xff> %0.10e </xff>\n",
222 rrd.rra_def[i].par[RRA_cdp_xff_val].u_val);
223 break;
224 }
225 fprintf(out_file, "\t\t</params>\n");
226 fprintf(out_file, "\t\t<cdp_prep>\n");
227 for (ii = 0; ii < rrd.stat_head->ds_cnt; ii++) {
228 unsigned long ivalue;
230 fprintf(out_file, "\t\t\t<ds>\n");
231 /* support for exporting all CDP parameters */
232 /* parameters common to all CFs */
233 /* primary_val and secondary_val do not need to be saved between updates
234 * so strictly speaking they could be omitted.
235 * However, they can be useful for diagnostic purposes, so are included here. */
236 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt
237 + ii].scratch[CDP_primary_val].u_val;
238 if (isnan(value)) {
239 fprintf(out_file,
240 "\t\t\t<primary_value> NaN </primary_value>\n");
241 } else {
242 fprintf(out_file,
243 "\t\t\t<primary_value> %0.10e </primary_value>\n",
244 value);
245 }
246 value =
247 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
248 ii].scratch[CDP_secondary_val].u_val;
249 if (isnan(value)) {
250 fprintf(out_file,
251 "\t\t\t<secondary_value> NaN </secondary_value>\n");
252 } else {
253 fprintf(out_file,
254 "\t\t\t<secondary_value> %0.10e </secondary_value>\n",
255 value);
256 }
257 switch (cf_conv(rrd.rra_def[i].cf_nam)) {
258 case CF_HWPREDICT:
259 case CF_MHWPREDICT:
260 value =
261 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
262 ii].scratch[CDP_hw_intercept].u_val;
263 if (isnan(value)) {
264 fprintf(out_file, "\t\t\t<intercept> NaN </intercept>\n");
265 } else {
266 fprintf(out_file,
267 "\t\t\t<intercept> %0.10e </intercept>\n", value);
268 }
269 value =
270 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
271 ii].scratch[CDP_hw_last_intercept].u_val;
272 if (isnan(value)) {
273 fprintf(out_file,
274 "\t\t\t<last_intercept> NaN </last_intercept>\n");
275 } else {
276 fprintf(out_file,
277 "\t\t\t<last_intercept> %0.10e </last_intercept>\n",
278 value);
279 }
280 value =
281 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
282 ii].scratch[CDP_hw_slope].u_val;
283 if (isnan(value)) {
284 fprintf(out_file, "\t\t\t<slope> NaN </slope>\n");
285 } else {
286 fprintf(out_file, "\t\t\t<slope> %0.10e </slope>\n",
287 value);
288 }
289 value =
290 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
291 ii].scratch[CDP_hw_last_slope].u_val;
292 if (isnan(value)) {
293 fprintf(out_file,
294 "\t\t\t<last_slope> NaN </last_slope>\n");
295 } else {
296 fprintf(out_file,
297 "\t\t\t<last_slope> %0.10e </last_slope>\n",
298 value);
299 }
300 ivalue =
301 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
302 ii].scratch[CDP_null_count].u_cnt;
303 fprintf(out_file, "\t\t\t<nan_count> %lu </nan_count>\n",
304 ivalue);
305 ivalue =
306 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
307 ii].scratch[CDP_last_null_count].u_cnt;
308 fprintf(out_file,
309 "\t\t\t<last_nan_count> %lu </last_nan_count>\n",
310 ivalue);
311 break;
312 case CF_SEASONAL:
313 case CF_DEVSEASONAL:
314 value =
315 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
316 ii].scratch[CDP_hw_seasonal].u_val;
317 if (isnan(value)) {
318 fprintf(out_file, "\t\t\t<seasonal> NaN </seasonal>\n");
319 } else {
320 fprintf(out_file, "\t\t\t<seasonal> %0.10e </seasonal>\n",
321 value);
322 }
323 value =
324 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
325 ii].scratch[CDP_hw_last_seasonal].u_val;
326 if (isnan(value)) {
327 fprintf(out_file,
328 "\t\t\t<last_seasonal> NaN </last_seasonal>\n");
329 } else {
330 fprintf(out_file,
331 "\t\t\t<last_seasonal> %0.10e </last_seasonal>\n",
332 value);
333 }
334 ivalue =
335 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
336 ii].scratch[CDP_init_seasonal].u_cnt;
337 fprintf(out_file, "\t\t\t<init_flag> %lu </init_flag>\n",
338 ivalue);
339 break;
340 case CF_DEVPREDICT:
341 break;
342 case CF_FAILURES:
343 {
344 unsigned short vidx;
345 char *violations_array = (char *) ((void *)
346 rrd.cdp_prep[i *
347 rrd.
348 stat_head->
349 ds_cnt +
350 ii].
351 scratch);
352 fprintf(out_file, "\t\t\t<history> ");
353 for (vidx = 0;
354 vidx < rrd.rra_def[i].par[RRA_window_len].u_cnt;
355 ++vidx) {
356 fprintf(out_file, "%d", violations_array[vidx]);
357 }
358 fprintf(out_file, " </history>\n");
359 }
360 break;
361 case CF_AVERAGE:
362 case CF_MAXIMUM:
363 case CF_MINIMUM:
364 case CF_LAST:
365 default:
366 value =
367 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
368 ii].scratch[CDP_val].u_val;
369 if (isnan(value)) {
370 fprintf(out_file, "\t\t\t<value> NaN </value>\n");
371 } else {
372 fprintf(out_file, "\t\t\t<value> %0.10e </value>\n",
373 value);
374 }
375 fprintf(out_file,
376 "\t\t\t<unknown_datapoints> %lu </unknown_datapoints>\n",
377 rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
378 ii].scratch[CDP_unkn_pdp_cnt].u_cnt);
379 break;
380 }
381 fprintf(out_file, "\t\t\t</ds>\n");
382 }
383 fprintf(out_file, "\t\t</cdp_prep>\n");
385 fprintf(out_file, "\t\t<database>\n");
386 rrd_seek(rrd_file, (rra_start + (rrd.rra_ptr[i].cur_row + 1)
387 * rrd.stat_head->ds_cnt
388 * sizeof(rrd_value_t)), SEEK_SET);
389 timer = -(rrd.rra_def[i].row_cnt - 1);
390 ii = rrd.rra_ptr[i].cur_row;
391 for (ix = 0; ix < rrd.rra_def[i].row_cnt; ix++) {
392 ii++;
393 if (ii >= rrd.rra_def[i].row_cnt) {
394 rrd_seek(rrd_file, rra_start, SEEK_SET);
395 ii = 0; /* wrap if max row cnt is reached */
396 }
397 now = (rrd.live_head->last_up
398 - rrd.live_head->last_up
399 % (rrd.rra_def[i].pdp_cnt * rrd.stat_head->pdp_step))
400 + (timer * rrd.rra_def[i].pdp_cnt * rrd.stat_head->pdp_step);
402 timer++;
403 #if HAVE_STRFTIME
404 localtime_r(&now, &tm);
405 strftime(somestring, 200, "%Y-%m-%d %H:%M:%S %Z", &tm);
406 #else
407 # error "Need strftime"
408 #endif
409 fprintf(out_file, "\t\t\t<!-- %s / %d --> <row>", somestring,
410 (int) now);
411 for (iii = 0; iii < rrd.stat_head->ds_cnt; iii++) {
412 rrd_read(rrd_file, &my_cdp, sizeof(rrd_value_t) * 1);
413 if (isnan(my_cdp)) {
414 fprintf(out_file, "<v> NaN </v>");
415 } else {
416 fprintf(out_file, "<v> %0.10e </v>", my_cdp);
417 };
418 }
419 fprintf(out_file, "</row>\n");
420 }
421 fprintf(out_file, "\t\t</database>\n\t</rra>\n");
423 }
424 fprintf(out_file, "</rrd>\n");
425 rrd_free(&rrd);
426 if (out_file != stdout) {
427 fclose(out_file);
428 }
429 return rrd_close(rrd_file);
430 }