1 /*****************************************************************************
2 * RRDtool 1.3.4 Copyright by Tobi Oetiker, 1997-2008
3 *****************************************************************************
4 * change header parameters of an rrd
5 *****************************************************************************
6 * $Id$
7 * $Log$
8 * Revision 1.6 2004/05/26 22:11:12 oetiker
9 * reduce compiler warnings. Many small fixes. -- Mike Slifcak <slif@bellsouth.net>
10 *
11 * Revision 1.5 2002/02/01 20:34:49 oetiker
12 * fixed version number and date/time
13 *
14 * Revision 1.4 2001/08/22 22:29:07 jake
15 * Contents of this patch:
16 * (1) Adds/revises documentation for rrd tune in rrd_tool.c and pod files.
17 * (2) Moves some initialization code from rrd_create.c to rrd_hw.c.
18 * (3) Adds another pass to smoothing for SEASONAL and DEVSEASONAL RRAs.
19 * This pass computes the coefficients as deviations from an average; the
20 * average is added the baseline coefficient of HWPREDICT. Statistical texts
21 * suggest this to preserve algorithm stability. It will not invalidate
22 * RRD files created and smoothed with the old code.
23 * (4) Adds the aberrant-reset flag to rrd tune. This operation, which is
24 * specified for a single data source, causes the holt-winters algorithm to
25 * forget everthing it has learned and start over.
26 * (5) Fixes a few out-of-date code comments.
27 *
28 * Revision 1.3 2001/03/07 21:21:54 oetiker
29 * complete rewrite of rrdgraph documentation. This also includs info
30 * on upcomming/planned changes to the rrdgraph interface and functionality
31 * -- Alex van den Bogaerdt <alex@slot.hollandcasino.nl>
32 *
33 * Revision 1.2 2001/03/04 13:01:55 oetiker
34 * Aberrant Behavior Detection support. A brief overview added to rrdtool.pod.
35 * Major updates to rrd_update.c, rrd_create.c. Minor update to other core files.
36 * This is backwards compatible! But new files using the Aberrant stuff are not readable
37 * by old rrdtool versions. See http://cricket.sourceforge.net/aberrant/rrd_hw.htm
38 * -- Jake Brutlag <jakeb@corp.webtv.net>
39 *
40 *****************************************************************************/
42 #include "rrd_tool.h"
43 #include "rrd_rpncalc.h"
44 #include "rrd_hw.h"
45 #include <locale.h>
47 #ifdef WIN32
48 #include <stdlib.h>
49 #endif
51 int set_hwarg(
52 rrd_t *rrd,
53 enum cf_en cf,
54 enum rra_par_en rra_par,
55 char *arg);
56 int set_deltaarg(
57 rrd_t *rrd,
58 enum rra_par_en rra_par,
59 char *arg);
60 int set_windowarg(
61 rrd_t *rrd,
62 enum rra_par_en,
63 char *arg);
65 int rrd_tune(
66 int argc,
67 char **argv)
68 {
69 rrd_t rrd;
70 int matches;
71 int optcnt = 0;
72 long ds;
73 char ds_nam[DS_NAM_SIZE];
74 char ds_new[DS_NAM_SIZE];
75 long heartbeat;
76 double min;
77 double max;
78 char dst[DST_SIZE];
79 rrd_file_t *rrd_file;
80 struct option long_options[] = {
81 {"heartbeat", required_argument, 0, 'h'},
82 {"minimum", required_argument, 0, 'i'},
83 {"maximum", required_argument, 0, 'a'},
84 {"data-source-type", required_argument, 0, 'd'},
85 {"data-source-rename", required_argument, 0, 'r'},
86 /* added parameter tuning options for aberrant behavior detection */
87 {"deltapos", required_argument, 0, 'p'},
88 {"deltaneg", required_argument, 0, 'n'},
89 {"window-length", required_argument, 0, 'w'},
90 {"failure-threshold", required_argument, 0, 'f'},
91 {"alpha", required_argument, 0, 'x'},
92 {"beta", required_argument, 0, 'y'},
93 {"gamma", required_argument, 0, 'z'},
94 {"gamma-deviation", required_argument, 0, 'v'},
95 {"smoothing-window", required_argument, 0, 's'},
96 {"smoothing-window-deviation", required_argument, 0, 'S'},
97 {"aberrant-reset", required_argument, 0, 'b'},
98 {0, 0, 0, 0}
99 };
101 optind = 0;
102 opterr = 0; /* initialize getopt */
105 rrd_file = rrd_open(argv[1], &rrd, RRD_READWRITE);
106 if (rrd_file == NULL) {
107 rrd_free(&rrd);
108 return -1;
109 }
111 while (1) {
112 int option_index = 0;
113 int opt;
114 char *old_locale = "";
116 opt = getopt_long(argc, argv, "h:i:a:d:r:p:n:w:f:x:y:z:v:b:",
117 long_options, &option_index);
118 if (opt == EOF)
119 break;
121 optcnt++;
122 switch (opt) {
123 case 'h':
124 old_locale = setlocale(LC_NUMERIC, "C");
125 if ((matches =
126 sscanf(optarg, DS_NAM_FMT ":%ld", ds_nam,
127 &heartbeat)) != 2) {
128 rrd_set_error("invalid arguments for heartbeat");
129 rrd_free(&rrd);
130 rrd_close(rrd_file);
131 setlocale(LC_NUMERIC, old_locale);
132 return -1;
133 }
134 setlocale(LC_NUMERIC, old_locale);
135 if ((ds = ds_match(&rrd, ds_nam)) == -1) {
136 rrd_free(&rrd);
137 rrd_close(rrd_file);
138 return -1;
139 }
140 rrd.ds_def[ds].par[DS_mrhb_cnt].u_cnt = heartbeat;
141 break;
143 case 'i':
144 old_locale = setlocale(LC_NUMERIC, "C");
145 if ((matches =
146 sscanf(optarg, DS_NAM_FMT ":%lf", ds_nam, &min)) < 1) {
147 rrd_set_error("invalid arguments for minimum ds value");
148 rrd_free(&rrd);
149 rrd_close(rrd_file);
150 setlocale(LC_NUMERIC, old_locale);
151 return -1;
152 }
153 setlocale(LC_NUMERIC, old_locale);
154 if ((ds = ds_match(&rrd, ds_nam)) == -1) {
155 rrd_free(&rrd);
156 rrd_close(rrd_file);
157 return -1;
158 }
160 if (matches == 1)
161 min = DNAN;
162 rrd.ds_def[ds].par[DS_min_val].u_val = min;
163 break;
165 case 'a':
166 old_locale = setlocale(LC_NUMERIC, "C");
167 if ((matches =
168 sscanf(optarg, DS_NAM_FMT ":%lf", ds_nam, &max)) < 1) {
169 rrd_set_error("invalid arguments for maximum ds value");
170 rrd_free(&rrd);
171 rrd_close(rrd_file);
172 setlocale(LC_NUMERIC, old_locale);
173 return -1;
174 }
175 setlocale(LC_NUMERIC, old_locale);
176 if ((ds = ds_match(&rrd, ds_nam)) == -1) {
177 rrd_free(&rrd);
178 rrd_close(rrd_file);
179 return -1;
180 }
181 if (matches == 1)
182 max = DNAN;
183 rrd.ds_def[ds].par[DS_max_val].u_val = max;
184 break;
186 case 'd':
187 if ((matches =
188 sscanf(optarg, DS_NAM_FMT ":" DST_FMT, ds_nam, dst)) != 2) {
189 rrd_set_error("invalid arguments for data source type");
190 rrd_free(&rrd);
191 rrd_close(rrd_file);
192 return -1;
193 }
194 if ((ds = ds_match(&rrd, ds_nam)) == -1) {
195 rrd_free(&rrd);
196 rrd_close(rrd_file);
197 return -1;
198 }
199 if ((int) dst_conv(dst) == -1) {
200 rrd_free(&rrd);
201 rrd_close(rrd_file);
202 return -1;
203 }
204 /* only reset when something is changed */
205 if (strncmp(rrd.ds_def[ds].dst, dst, DST_SIZE - 1) != 0) {
206 strncpy(rrd.ds_def[ds].dst, dst, DST_SIZE - 1);
207 rrd.ds_def[ds].dst[DST_SIZE - 1] = '\0';
209 rrd.pdp_prep[ds].last_ds[0] = 'U';
210 rrd.pdp_prep[ds].last_ds[1] = 'N';
211 rrd.pdp_prep[ds].last_ds[2] = 'K';
212 rrd.pdp_prep[ds].last_ds[3] = 'N';
213 rrd.pdp_prep[ds].last_ds[4] = '\0';
214 }
215 break;
216 case 'r':
217 if ((matches =
218 sscanf(optarg, DS_NAM_FMT ":" DS_NAM_FMT, ds_nam,
219 ds_new)) != 2) {
220 rrd_set_error("invalid arguments for data source type");
221 rrd_free(&rrd);
222 rrd_close(rrd_file);
223 return -1;
224 }
225 if ((ds = ds_match(&rrd, ds_nam)) == -1) {
226 rrd_free(&rrd);
227 rrd_close(rrd_file);
228 return -1;
229 }
230 strncpy(rrd.ds_def[ds].ds_nam, ds_new, DS_NAM_SIZE - 1);
231 rrd.ds_def[ds].ds_nam[DS_NAM_SIZE - 1] = '\0';
232 break;
233 case 'p':
234 if (set_deltaarg(&rrd, RRA_delta_pos, optarg)) {
235 rrd_free(&rrd);
236 return -1;
237 }
238 break;
239 case 'n':
240 if (set_deltaarg(&rrd, RRA_delta_neg, optarg)) {
241 rrd_free(&rrd);
242 return -1;
243 }
244 break;
245 case 'f':
246 if (set_windowarg(&rrd, RRA_failure_threshold, optarg)) {
247 rrd_free(&rrd);
248 return -1;
249 }
250 break;
251 case 'w':
252 if (set_windowarg(&rrd, RRA_window_len, optarg)) {
253 rrd_free(&rrd);
254 return -1;
255 }
256 break;
257 case 'x':
258 if (set_hwarg(&rrd, CF_HWPREDICT, RRA_hw_alpha, optarg)) {
259 if (set_hwarg(&rrd, CF_MHWPREDICT, RRA_hw_alpha, optarg)) {
260 rrd_free(&rrd);
261 return -1;
262 }
263 rrd_clear_error();
264 }
265 break;
266 case 'y':
267 if (set_hwarg(&rrd, CF_HWPREDICT, RRA_hw_beta, optarg)) {
268 if (set_hwarg(&rrd, CF_MHWPREDICT, RRA_hw_beta, optarg)) {
269 rrd_free(&rrd);
270 return -1;
271 }
272 rrd_clear_error();
273 }
274 break;
275 case 'z':
276 if (set_hwarg(&rrd, CF_SEASONAL, RRA_seasonal_gamma, optarg)) {
277 rrd_free(&rrd);
278 return -1;
279 }
280 break;
281 case 'v':
282 if (set_hwarg(&rrd, CF_DEVSEASONAL, RRA_seasonal_gamma, optarg)) {
283 rrd_free(&rrd);
284 return -1;
285 }
286 break;
287 case 'b':
288 if (sscanf(optarg, DS_NAM_FMT, ds_nam) != 1) {
289 rrd_set_error("invalid argument for aberrant-reset");
290 rrd_free(&rrd);
291 rrd_close(rrd_file);
292 return -1;
293 }
294 if ((ds = ds_match(&rrd, ds_nam)) == -1) {
295 /* ds_match handles it own errors */
296 rrd_free(&rrd);
297 rrd_close(rrd_file);
298 return -1;
299 }
300 reset_aberrant_coefficients(&rrd, rrd_file, (unsigned long) ds);
301 if (rrd_test_error()) {
302 rrd_free(&rrd);
303 rrd_close(rrd_file);
304 return -1;
305 }
306 break;
307 case 's':
308 strcpy(rrd.stat_head->version, RRD_VERSION); /* smoothing_window causes Version 4 */
309 if (set_hwarg
310 (&rrd, CF_SEASONAL, RRA_seasonal_smoothing_window, optarg)) {
311 rrd_free(&rrd);
312 return -1;
313 }
314 break;
315 case 'S':
316 strcpy(rrd.stat_head->version, RRD_VERSION); /* smoothing_window causes Version 4 */
317 if (set_hwarg
318 (&rrd, CF_DEVSEASONAL, RRA_seasonal_smoothing_window,
319 optarg)) {
320 rrd_free(&rrd);
321 return -1;
322 }
323 break;
324 case '?':
325 if (optopt != 0)
326 rrd_set_error("unknown option '%c'", optopt);
327 else
328 rrd_set_error("unknown option '%s'", argv[optind - 1]);
329 rrd_free(&rrd);
330 rrd_close(rrd_file);
331 return -1;
332 }
333 }
334 if (optcnt > 0) {
335 rrd_seek(rrd_file, 0, SEEK_SET);
336 rrd_write(rrd_file, rrd.stat_head, sizeof(stat_head_t) * 1);
337 rrd_write(rrd_file, rrd.ds_def,
338 sizeof(ds_def_t) * rrd.stat_head->ds_cnt);
339 /* need to write rra_defs for RRA parameter changes */
340 rrd_write(rrd_file, rrd.rra_def,
341 sizeof(rra_def_t) * rrd.stat_head->rra_cnt);
342 } else {
343 int i;
345 for (i = 0; i < (int) rrd.stat_head->ds_cnt; i++)
346 if (dst_conv(rrd.ds_def[i].dst) != DST_CDEF) {
347 printf("DS[%s] typ: %s\thbt: %ld\tmin: %1.4f\tmax: %1.4f\n",
348 rrd.ds_def[i].ds_nam,
349 rrd.ds_def[i].dst,
350 rrd.ds_def[i].par[DS_mrhb_cnt].u_cnt,
351 rrd.ds_def[i].par[DS_min_val].u_val,
352 rrd.ds_def[i].par[DS_max_val].u_val);
353 } else {
354 char *buffer = NULL;
356 rpn_compact2str((rpn_cdefds_t *) &
357 (rrd.ds_def[i].par[DS_cdef]), rrd.ds_def,
358 &buffer);
359 printf("DS[%s] typ: %s\tcdef: %s\n", rrd.ds_def[i].ds_nam,
360 rrd.ds_def[i].dst, buffer);
361 free(buffer);
362 }
363 }
364 rrd_close(rrd_file);
365 rrd_free(&rrd);
366 return 0;
367 }
369 int set_hwarg(
370 rrd_t *rrd,
371 enum cf_en cf,
372 enum rra_par_en rra_par,
373 char *arg)
374 {
375 double param;
376 unsigned long i;
377 signed short rra_idx = -1;
379 /* read the value */
380 param = atof(arg);
381 if (param <= 0.0 || param >= 1.0) {
382 rrd_set_error("Holt-Winters parameter must be between 0 and 1");
383 return -1;
384 }
385 /* does the appropriate RRA exist? */
386 for (i = 0; i < rrd->stat_head->rra_cnt; ++i) {
387 if (cf_conv(rrd->rra_def[i].cf_nam) == cf) {
388 rra_idx = i;
389 break;
390 }
391 }
392 if (rra_idx == -1) {
393 rrd_set_error("Holt-Winters RRA does not exist in this RRD");
394 return -1;
395 }
397 /* set the value */
398 rrd->rra_def[rra_idx].par[rra_par].u_val = param;
399 return 0;
400 }
402 int set_deltaarg(
403 rrd_t *rrd,
404 enum rra_par_en rra_par,
405 char *arg)
406 {
407 rrd_value_t param;
408 unsigned long i;
409 signed short rra_idx = -1;
411 param = atof(arg);
412 if (param < 0.1) {
413 rrd_set_error("Parameter specified is too small");
414 return -1;
415 }
416 /* does the appropriate RRA exist? */
417 for (i = 0; i < rrd->stat_head->rra_cnt; ++i) {
418 if (cf_conv(rrd->rra_def[i].cf_nam) == CF_FAILURES) {
419 rra_idx = i;
420 break;
421 }
422 }
423 if (rra_idx == -1) {
424 rrd_set_error("Failures RRA does not exist in this RRD");
425 return -1;
426 }
428 /* set the value */
429 rrd->rra_def[rra_idx].par[rra_par].u_val = param;
430 return 0;
431 }
433 int set_windowarg(
434 rrd_t *rrd,
435 enum rra_par_en rra_par,
436 char *arg)
437 {
438 unsigned long param;
439 unsigned long i, cdp_idx;
440 signed short rra_idx = -1;
442 /* read the value */
443 param = atoi(arg);
444 if (param < 1 || param > MAX_FAILURES_WINDOW_LEN) {
445 rrd_set_error("Parameter must be between %d and %d",
446 1, MAX_FAILURES_WINDOW_LEN);
447 return -1;
448 }
449 /* does the appropriate RRA exist? */
450 for (i = 0; i < rrd->stat_head->rra_cnt; ++i) {
451 if (cf_conv(rrd->rra_def[i].cf_nam) == CF_FAILURES) {
452 rra_idx = i;
453 break;
454 }
455 }
456 if (rra_idx == -1) {
457 rrd_set_error("Failures RRA does not exist in this RRD");
458 return -1;
459 }
461 /* set the value */
462 rrd->rra_def[rra_idx].par[rra_par].u_cnt = param;
464 /* erase existing violations */
465 for (i = 0; i < rrd->stat_head->ds_cnt; i++) {
466 cdp_idx = rra_idx * (rrd->stat_head->ds_cnt) + i;
467 erase_violations(rrd, cdp_idx, rra_idx);
468 }
469 return 0;
470 }