89559af7a51f71c17996ee39e377e12386ba59e0
1 /*****************************************************************************
2 * RRDtool 1.3.2 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 int set_hwarg(
48 rrd_t *rrd,
49 enum cf_en cf,
50 enum rra_par_en rra_par,
51 char *arg);
52 int set_deltaarg(
53 rrd_t *rrd,
54 enum rra_par_en rra_par,
55 char *arg);
56 int set_windowarg(
57 rrd_t *rrd,
58 enum rra_par_en,
59 char *arg);
61 int rrd_tune(
62 int argc,
63 char **argv)
64 {
65 rrd_t rrd;
66 int matches;
67 int optcnt = 0;
68 long ds;
69 char ds_nam[DS_NAM_SIZE];
70 char ds_new[DS_NAM_SIZE];
71 long heartbeat;
72 double min;
73 double max;
74 char dst[DST_SIZE];
75 rrd_file_t *rrd_file;
76 struct option long_options[] = {
77 {"heartbeat", required_argument, 0, 'h'},
78 {"minimum", required_argument, 0, 'i'},
79 {"maximum", required_argument, 0, 'a'},
80 {"data-source-type", required_argument, 0, 'd'},
81 {"data-source-rename", required_argument, 0, 'r'},
82 /* added parameter tuning options for aberrant behavior detection */
83 {"deltapos", required_argument, 0, 'p'},
84 {"deltaneg", required_argument, 0, 'n'},
85 {"window-length", required_argument, 0, 'w'},
86 {"failure-threshold", required_argument, 0, 'f'},
87 {"alpha", required_argument, 0, 'x'},
88 {"beta", required_argument, 0, 'y'},
89 {"gamma", required_argument, 0, 'z'},
90 {"gamma-deviation", required_argument, 0, 'v'},
91 {"smoothing-window", required_argument, 0, 's'},
92 {"smoothing-window-deviation", required_argument, 0, 'S'},
93 {"aberrant-reset", required_argument, 0, 'b'},
94 {0, 0, 0, 0}
95 };
97 optind = 0;
98 opterr = 0; /* initialize getopt */
101 rrd_init(&rrd);
102 rrd_file = rrd_open(argv[1], &rrd, RRD_READWRITE);
103 if (rrd_file == NULL) {
104 rrd_free(&rrd);
105 return -1;
106 }
108 while (1) {
109 int option_index = 0;
110 int opt;
111 char *old_locale = "";
113 opt = getopt_long(argc, argv, "h:i:a:d:r:p:n:w:f:x:y:z:v:b:",
114 long_options, &option_index);
115 if (opt == EOF)
116 break;
118 optcnt++;
119 switch (opt) {
120 case 'h':
121 old_locale = setlocale(LC_NUMERIC, "C");
122 if ((matches =
123 sscanf(optarg, DS_NAM_FMT ":%ld", ds_nam,
124 &heartbeat)) != 2) {
125 rrd_set_error("invalid arguments for heartbeat");
126 rrd_free(&rrd);
127 rrd_close(rrd_file);
128 setlocale(LC_NUMERIC, old_locale);
129 return -1;
130 }
131 setlocale(LC_NUMERIC, old_locale);
132 if ((ds = ds_match(&rrd, ds_nam)) == -1) {
133 rrd_free(&rrd);
134 rrd_close(rrd_file);
135 return -1;
136 }
137 rrd.ds_def[ds].par[DS_mrhb_cnt].u_cnt = heartbeat;
138 break;
140 case 'i':
141 old_locale = setlocale(LC_NUMERIC, "C");
142 if ((matches =
143 sscanf(optarg, DS_NAM_FMT ":%lf", ds_nam, &min)) < 1) {
144 rrd_set_error("invalid arguments for minimum ds value");
145 rrd_free(&rrd);
146 rrd_close(rrd_file);
147 setlocale(LC_NUMERIC, old_locale);
148 return -1;
149 }
150 setlocale(LC_NUMERIC, old_locale);
151 if ((ds = ds_match(&rrd, ds_nam)) == -1) {
152 rrd_free(&rrd);
153 rrd_close(rrd_file);
154 return -1;
155 }
157 if (matches == 1)
158 min = DNAN;
159 rrd.ds_def[ds].par[DS_min_val].u_val = min;
160 break;
162 case 'a':
163 old_locale = setlocale(LC_NUMERIC, "C");
164 if ((matches =
165 sscanf(optarg, DS_NAM_FMT ":%lf", ds_nam, &max)) < 1) {
166 rrd_set_error("invalid arguments for maximum ds value");
167 rrd_free(&rrd);
168 rrd_close(rrd_file);
169 setlocale(LC_NUMERIC, old_locale);
170 return -1;
171 }
172 setlocale(LC_NUMERIC, old_locale);
173 if ((ds = ds_match(&rrd, ds_nam)) == -1) {
174 rrd_free(&rrd);
175 rrd_close(rrd_file);
176 return -1;
177 }
178 if (matches == 1)
179 max = DNAN;
180 rrd.ds_def[ds].par[DS_max_val].u_val = max;
181 break;
183 case 'd':
184 if ((matches =
185 sscanf(optarg, DS_NAM_FMT ":" DST_FMT, ds_nam, dst)) != 2) {
186 rrd_set_error("invalid arguments for data source type");
187 rrd_free(&rrd);
188 rrd_close(rrd_file);
189 return -1;
190 }
191 if ((ds = ds_match(&rrd, ds_nam)) == -1) {
192 rrd_free(&rrd);
193 rrd_close(rrd_file);
194 return -1;
195 }
196 if ((int) dst_conv(dst) == -1) {
197 rrd_free(&rrd);
198 rrd_close(rrd_file);
199 return -1;
200 }
201 /* only reset when something is changed */
202 if (strncmp(rrd.ds_def[ds].dst, dst, DST_SIZE - 1) != 0) {
203 strncpy(rrd.ds_def[ds].dst, dst, DST_SIZE - 1);
204 rrd.ds_def[ds].dst[DST_SIZE - 1] = '\0';
206 rrd.pdp_prep[ds].last_ds[0] = 'U';
207 rrd.pdp_prep[ds].last_ds[1] = 'N';
208 rrd.pdp_prep[ds].last_ds[2] = 'K';
209 rrd.pdp_prep[ds].last_ds[3] = 'N';
210 rrd.pdp_prep[ds].last_ds[4] = '\0';
211 }
212 break;
213 case 'r':
214 if ((matches =
215 sscanf(optarg, DS_NAM_FMT ":" DS_NAM_FMT, ds_nam,
216 ds_new)) != 2) {
217 rrd_set_error("invalid arguments for data source type");
218 rrd_free(&rrd);
219 rrd_close(rrd_file);
220 return -1;
221 }
222 if ((ds = ds_match(&rrd, ds_nam)) == -1) {
223 rrd_free(&rrd);
224 rrd_close(rrd_file);
225 return -1;
226 }
227 strncpy(rrd.ds_def[ds].ds_nam, ds_new, DS_NAM_SIZE - 1);
228 rrd.ds_def[ds].ds_nam[DS_NAM_SIZE - 1] = '\0';
229 break;
230 case 'p':
231 if (set_deltaarg(&rrd, RRA_delta_pos, optarg)) {
232 rrd_free(&rrd);
233 return -1;
234 }
235 break;
236 case 'n':
237 if (set_deltaarg(&rrd, RRA_delta_neg, optarg)) {
238 rrd_free(&rrd);
239 return -1;
240 }
241 break;
242 case 'f':
243 if (set_windowarg(&rrd, RRA_failure_threshold, optarg)) {
244 rrd_free(&rrd);
245 return -1;
246 }
247 break;
248 case 'w':
249 if (set_windowarg(&rrd, RRA_window_len, optarg)) {
250 rrd_free(&rrd);
251 return -1;
252 }
253 break;
254 case 'x':
255 if (set_hwarg(&rrd, CF_HWPREDICT, RRA_hw_alpha, optarg)) {
256 if (set_hwarg(&rrd, CF_MHWPREDICT, RRA_hw_alpha, optarg)) {
257 rrd_free(&rrd);
258 return -1;
259 }
260 rrd_clear_error();
261 }
262 break;
263 case 'y':
264 if (set_hwarg(&rrd, CF_HWPREDICT, RRA_hw_beta, optarg)) {
265 if (set_hwarg(&rrd, CF_MHWPREDICT, RRA_hw_beta, optarg)) {
266 rrd_free(&rrd);
267 return -1;
268 }
269 rrd_clear_error();
270 }
271 break;
272 case 'z':
273 if (set_hwarg(&rrd, CF_SEASONAL, RRA_seasonal_gamma, optarg)) {
274 rrd_free(&rrd);
275 return -1;
276 }
277 break;
278 case 'v':
279 if (set_hwarg(&rrd, CF_DEVSEASONAL, RRA_seasonal_gamma, optarg)) {
280 rrd_free(&rrd);
281 return -1;
282 }
283 break;
284 case 'b':
285 if (sscanf(optarg, DS_NAM_FMT, ds_nam) != 1) {
286 rrd_set_error("invalid argument for aberrant-reset");
287 rrd_free(&rrd);
288 rrd_close(rrd_file);
289 return -1;
290 }
291 if ((ds = ds_match(&rrd, ds_nam)) == -1) {
292 /* ds_match handles it own errors */
293 rrd_free(&rrd);
294 rrd_close(rrd_file);
295 return -1;
296 }
297 reset_aberrant_coefficients(&rrd, rrd_file, (unsigned long) ds);
298 if (rrd_test_error()) {
299 rrd_free(&rrd);
300 rrd_close(rrd_file);
301 return -1;
302 }
303 break;
304 case 's':
305 strcpy(rrd.stat_head->version, RRD_VERSION); /* smoothing_window causes Version 4 */
306 if (set_hwarg
307 (&rrd, CF_SEASONAL, RRA_seasonal_smoothing_window, optarg)) {
308 rrd_free(&rrd);
309 return -1;
310 }
311 break;
312 case 'S':
313 strcpy(rrd.stat_head->version, RRD_VERSION); /* smoothing_window causes Version 4 */
314 if (set_hwarg
315 (&rrd, CF_DEVSEASONAL, RRA_seasonal_smoothing_window,
316 optarg)) {
317 rrd_free(&rrd);
318 return -1;
319 }
320 break;
321 case '?':
322 if (optopt != 0)
323 rrd_set_error("unknown option '%c'", optopt);
324 else
325 rrd_set_error("unknown option '%s'", argv[optind - 1]);
326 rrd_free(&rrd);
327 rrd_close(rrd_file);
328 return -1;
329 }
330 }
331 if (optcnt > 0) {
332 rrd_seek(rrd_file, 0, SEEK_SET);
333 rrd_write(rrd_file, rrd.stat_head, sizeof(stat_head_t) * 1);
334 rrd_write(rrd_file, rrd.ds_def,
335 sizeof(ds_def_t) * rrd.stat_head->ds_cnt);
336 /* need to write rra_defs for RRA parameter changes */
337 rrd_write(rrd_file, rrd.rra_def,
338 sizeof(rra_def_t) * rrd.stat_head->rra_cnt);
339 } else {
340 int i;
342 for (i = 0; i < (int) rrd.stat_head->ds_cnt; i++)
343 if (dst_conv(rrd.ds_def[i].dst) != DST_CDEF) {
344 printf("DS[%s] typ: %s\thbt: %ld\tmin: %1.4f\tmax: %1.4f\n",
345 rrd.ds_def[i].ds_nam,
346 rrd.ds_def[i].dst,
347 rrd.ds_def[i].par[DS_mrhb_cnt].u_cnt,
348 rrd.ds_def[i].par[DS_min_val].u_val,
349 rrd.ds_def[i].par[DS_max_val].u_val);
350 } else {
351 char *buffer = NULL;
353 rpn_compact2str((rpn_cdefds_t *) &
354 (rrd.ds_def[i].par[DS_cdef]), rrd.ds_def,
355 &buffer);
356 printf("DS[%s] typ: %s\tcdef: %s\n", rrd.ds_def[i].ds_nam,
357 rrd.ds_def[i].dst, buffer);
358 free(buffer);
359 }
360 }
361 rrd_close(rrd_file);
362 rrd_free(&rrd);
363 return 0;
364 }
366 int set_hwarg(
367 rrd_t *rrd,
368 enum cf_en cf,
369 enum rra_par_en rra_par,
370 char *arg)
371 {
372 double param;
373 unsigned long i;
374 signed short rra_idx = -1;
376 /* read the value */
377 param = atof(arg);
378 if (param <= 0.0 || param >= 1.0) {
379 rrd_set_error("Holt-Winters parameter must be between 0 and 1");
380 return -1;
381 }
382 /* does the appropriate RRA exist? */
383 for (i = 0; i < rrd->stat_head->rra_cnt; ++i) {
384 if (cf_conv(rrd->rra_def[i].cf_nam) == cf) {
385 rra_idx = i;
386 break;
387 }
388 }
389 if (rra_idx == -1) {
390 rrd_set_error("Holt-Winters RRA does not exist in this RRD");
391 return -1;
392 }
394 /* set the value */
395 rrd->rra_def[rra_idx].par[rra_par].u_val = param;
396 return 0;
397 }
399 int set_deltaarg(
400 rrd_t *rrd,
401 enum rra_par_en rra_par,
402 char *arg)
403 {
404 rrd_value_t param;
405 unsigned long i;
406 signed short rra_idx = -1;
408 param = atof(arg);
409 if (param < 0.1) {
410 rrd_set_error("Parameter specified is too small");
411 return -1;
412 }
413 /* does the appropriate RRA exist? */
414 for (i = 0; i < rrd->stat_head->rra_cnt; ++i) {
415 if (cf_conv(rrd->rra_def[i].cf_nam) == CF_FAILURES) {
416 rra_idx = i;
417 break;
418 }
419 }
420 if (rra_idx == -1) {
421 rrd_set_error("Failures RRA does not exist in this RRD");
422 return -1;
423 }
425 /* set the value */
426 rrd->rra_def[rra_idx].par[rra_par].u_val = param;
427 return 0;
428 }
430 int set_windowarg(
431 rrd_t *rrd,
432 enum rra_par_en rra_par,
433 char *arg)
434 {
435 unsigned long param;
436 unsigned long i, cdp_idx;
437 signed short rra_idx = -1;
439 /* read the value */
440 param = atoi(arg);
441 if (param < 1 || param > MAX_FAILURES_WINDOW_LEN) {
442 rrd_set_error("Parameter must be between %d and %d",
443 1, MAX_FAILURES_WINDOW_LEN);
444 return -1;
445 }
446 /* does the appropriate RRA exist? */
447 for (i = 0; i < rrd->stat_head->rra_cnt; ++i) {
448 if (cf_conv(rrd->rra_def[i].cf_nam) == CF_FAILURES) {
449 rra_idx = i;
450 break;
451 }
452 }
453 if (rra_idx == -1) {
454 rrd_set_error("Failures RRA does not exist in this RRD");
455 return -1;
456 }
458 /* set the value */
459 rrd->rra_def[rra_idx].par[rra_par].u_cnt = param;
461 /* erase existing violations */
462 for (i = 0; i < rrd->stat_head->ds_cnt; i++) {
463 cdp_idx = rra_idx * (rrd->stat_head->ds_cnt) + i;
464 erase_violations(rrd, cdp_idx, rra_idx);
465 }
466 return 0;
467 }