1 /*****************************************************************************
2 * RRDtool 1.2.99907080300 Copyright by Tobi Oetiker, 1997-2007
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 {"aberrant-reset", required_argument, 0, 'b'},
92 {0, 0, 0, 0}
93 };
95 optind = 0;
96 opterr = 0; /* initialize getopt */
99 rrd_file = rrd_open(argv[1], &rrd, RRD_READWRITE);
100 if (rrd_file == NULL) {
101 rrd_free(&rrd);
102 return -1;
103 }
105 while (1) {
106 int option_index = 0;
107 int opt;
108 char *old_locale = "";
110 opt = getopt_long(argc, argv, "h:i:a:d:r:p:n:w:f:x:y:z:v:b:",
111 long_options, &option_index);
112 if (opt == EOF)
113 break;
115 optcnt++;
116 switch (opt) {
117 case 'h':
118 old_locale = setlocale(LC_NUMERIC, "C");
119 if ((matches =
120 sscanf(optarg, DS_NAM_FMT ":%ld", ds_nam,
121 &heartbeat)) != 2) {
122 rrd_set_error("invalid arguments for heartbeat");
123 rrd_free(&rrd);
124 rrd_close(rrd_file);
125 setlocale(LC_NUMERIC, old_locale);
126 return -1;
127 }
128 setlocale(LC_NUMERIC, old_locale);
129 if ((ds = ds_match(&rrd, ds_nam)) == -1) {
130 rrd_free(&rrd);
131 rrd_close(rrd_file);
132 return -1;
133 }
134 rrd.ds_def[ds].par[DS_mrhb_cnt].u_cnt = heartbeat;
135 break;
137 case 'i':
138 old_locale = setlocale(LC_NUMERIC, "C");
139 if ((matches =
140 sscanf(optarg, DS_NAM_FMT ":%lf", ds_nam, &min)) < 1) {
141 rrd_set_error("invalid arguments for minimum ds value");
142 rrd_free(&rrd);
143 rrd_close(rrd_file);
144 setlocale(LC_NUMERIC, old_locale);
145 return -1;
146 }
147 setlocale(LC_NUMERIC, old_locale);
148 if ((ds = ds_match(&rrd, ds_nam)) == -1) {
149 rrd_free(&rrd);
150 rrd_close(rrd_file);
151 return -1;
152 }
154 if (matches == 1)
155 min = DNAN;
156 rrd.ds_def[ds].par[DS_min_val].u_val = min;
157 break;
159 case 'a':
160 old_locale = setlocale(LC_NUMERIC, "C");
161 if ((matches =
162 sscanf(optarg, DS_NAM_FMT ":%lf", ds_nam, &max)) < 1) {
163 rrd_set_error("invalid arguments for maximum ds value");
164 rrd_free(&rrd);
165 rrd_close(rrd_file);
166 setlocale(LC_NUMERIC, old_locale);
167 return -1;
168 }
169 setlocale(LC_NUMERIC, old_locale);
170 if ((ds = ds_match(&rrd, ds_nam)) == -1) {
171 rrd_free(&rrd);
172 rrd_close(rrd_file);
173 return -1;
174 }
175 if (matches == 1)
176 max = DNAN;
177 rrd.ds_def[ds].par[DS_max_val].u_val = max;
178 break;
180 case 'd':
181 if ((matches =
182 sscanf(optarg, DS_NAM_FMT ":" DST_FMT, ds_nam, dst)) != 2) {
183 rrd_set_error("invalid arguments for data source type");
184 rrd_free(&rrd);
185 rrd_close(rrd_file);
186 return -1;
187 }
188 if ((ds = ds_match(&rrd, ds_nam)) == -1) {
189 rrd_free(&rrd);
190 rrd_close(rrd_file);
191 return -1;
192 }
193 if ((int) dst_conv(dst) == -1) {
194 rrd_free(&rrd);
195 rrd_close(rrd_file);
196 return -1;
197 }
198 strncpy(rrd.ds_def[ds].dst, dst, DST_SIZE - 1);
199 rrd.ds_def[ds].dst[DST_SIZE - 1] = '\0';
201 rrd.pdp_prep[ds].last_ds[0] = 'U';
202 rrd.pdp_prep[ds].last_ds[1] = 'N';
203 rrd.pdp_prep[ds].last_ds[2] = 'K';
204 rrd.pdp_prep[ds].last_ds[3] = 'N';
205 rrd.pdp_prep[ds].last_ds[4] = '\0';
207 break;
208 case 'r':
209 if ((matches =
210 sscanf(optarg, DS_NAM_FMT ":" DS_NAM_FMT, ds_nam,
211 ds_new)) != 2) {
212 rrd_set_error("invalid arguments for data source type");
213 rrd_free(&rrd);
214 rrd_close(rrd_file);
215 return -1;
216 }
217 if ((ds = ds_match(&rrd, ds_nam)) == -1) {
218 rrd_free(&rrd);
219 rrd_close(rrd_file);
220 return -1;
221 }
222 strncpy(rrd.ds_def[ds].ds_nam, ds_new, DS_NAM_SIZE - 1);
223 rrd.ds_def[ds].ds_nam[DS_NAM_SIZE - 1] = '\0';
224 break;
225 case 'p':
226 if (set_deltaarg(&rrd, RRA_delta_pos, optarg)) {
227 rrd_free(&rrd);
228 return -1;
229 }
230 break;
231 case 'n':
232 if (set_deltaarg(&rrd, RRA_delta_neg, optarg)) {
233 rrd_free(&rrd);
234 return -1;
235 }
236 break;
237 case 'f':
238 if (set_windowarg(&rrd, RRA_failure_threshold, optarg)) {
239 rrd_free(&rrd);
240 return -1;
241 }
242 break;
243 case 'w':
244 if (set_windowarg(&rrd, RRA_window_len, optarg)) {
245 rrd_free(&rrd);
246 return -1;
247 }
248 break;
249 case 'x':
250 if (set_hwarg(&rrd, CF_HWPREDICT, RRA_hw_alpha, optarg)) {
251 if (set_hwarg(&rrd, CF_MHWPREDICT, RRA_hw_alpha, optarg)) {
252 rrd_free(&rrd);
253 return -1;
254 }
255 rrd_clear_error();
256 }
257 break;
258 case 'y':
259 if (set_hwarg(&rrd, CF_HWPREDICT, RRA_hw_beta, optarg)) {
260 if (set_hwarg(&rrd, CF_MHWPREDICT, RRA_hw_beta, optarg)) {
261 rrd_free(&rrd);
262 return -1;
263 }
264 rrd_clear_error();
265 }
266 break;
267 case 'z':
268 if (set_hwarg(&rrd, CF_SEASONAL, RRA_seasonal_gamma, optarg)) {
269 rrd_free(&rrd);
270 return -1;
271 }
272 break;
273 case 'v':
274 if (set_hwarg(&rrd, CF_DEVSEASONAL, RRA_seasonal_gamma, optarg)) {
275 rrd_free(&rrd);
276 return -1;
277 }
278 break;
279 case 'b':
280 if (sscanf(optarg, DS_NAM_FMT, ds_nam) != 1) {
281 rrd_set_error("invalid argument for aberrant-reset");
282 rrd_free(&rrd);
283 rrd_close(rrd_file);
284 return -1;
285 }
286 if ((ds = ds_match(&rrd, ds_nam)) == -1) {
287 /* ds_match handles it own errors */
288 rrd_free(&rrd);
289 rrd_close(rrd_file);
290 return -1;
291 }
292 reset_aberrant_coefficients(&rrd, rrd_file, (unsigned long) ds);
293 if (rrd_test_error()) {
294 rrd_free(&rrd);
295 rrd_close(rrd_file);
296 return -1;
297 }
298 break;
299 case '?':
300 if (optopt != 0)
301 rrd_set_error("unknown option '%c'", optopt);
302 else
303 rrd_set_error("unknown option '%s'", argv[optind - 1]);
304 rrd_free(&rrd);
305 rrd_close(rrd_file);
306 return -1;
307 }
308 }
309 if (optcnt > 0) {
310 rrd_seek(rrd_file, 0, SEEK_SET);
311 rrd_write(rrd_file, rrd.stat_head, sizeof(stat_head_t) * 1);
312 rrd_write(rrd_file, rrd.ds_def,
313 sizeof(ds_def_t) * rrd.stat_head->ds_cnt);
314 /* need to write rra_defs for RRA parameter changes */
315 rrd_write(rrd_file, rrd.rra_def,
316 sizeof(rra_def_t) * rrd.stat_head->rra_cnt);
317 } else {
318 int i;
320 for (i = 0; i < (int) rrd.stat_head->ds_cnt; i++)
321 if (dst_conv(rrd.ds_def[i].dst) != DST_CDEF) {
322 printf("DS[%s] typ: %s\thbt: %ld\tmin: %1.4f\tmax: %1.4f\n",
323 rrd.ds_def[i].ds_nam,
324 rrd.ds_def[i].dst,
325 rrd.ds_def[i].par[DS_mrhb_cnt].u_cnt,
326 rrd.ds_def[i].par[DS_min_val].u_val,
327 rrd.ds_def[i].par[DS_max_val].u_val);
328 } else {
329 char *buffer = NULL;
331 rpn_compact2str((rpn_cdefds_t *) &
332 (rrd.ds_def[i].par[DS_cdef]), rrd.ds_def,
333 &buffer);
334 printf("DS[%s] typ: %s\tcdef: %s\n", rrd.ds_def[i].ds_nam,
335 rrd.ds_def[i].dst, buffer);
336 free(buffer);
337 }
338 }
339 rrd_close(rrd_file);
340 rrd_free(&rrd);
341 return 0;
342 }
344 int set_hwarg(
345 rrd_t *rrd,
346 enum cf_en cf,
347 enum rra_par_en rra_par,
348 char *arg)
349 {
350 double param;
351 unsigned long i;
352 signed short rra_idx = -1;
354 /* read the value */
355 param = atof(arg);
356 if (param <= 0.0 || param >= 1.0) {
357 rrd_set_error("Holt-Winters parameter must be between 0 and 1");
358 return -1;
359 }
360 /* does the appropriate RRA exist? */
361 for (i = 0; i < rrd->stat_head->rra_cnt; ++i) {
362 if (cf_conv(rrd->rra_def[i].cf_nam) == cf) {
363 rra_idx = i;
364 break;
365 }
366 }
367 if (rra_idx == -1) {
368 rrd_set_error("Holt-Winters RRA does not exist in this RRD");
369 return -1;
370 }
372 /* set the value */
373 rrd->rra_def[rra_idx].par[rra_par].u_val = param;
374 return 0;
375 }
377 int set_deltaarg(
378 rrd_t *rrd,
379 enum rra_par_en rra_par,
380 char *arg)
381 {
382 rrd_value_t param;
383 unsigned long i;
384 signed short rra_idx = -1;
386 param = atof(arg);
387 if (param < 0.1) {
388 rrd_set_error("Parameter specified is too small");
389 return -1;
390 }
391 /* does the appropriate RRA exist? */
392 for (i = 0; i < rrd->stat_head->rra_cnt; ++i) {
393 if (cf_conv(rrd->rra_def[i].cf_nam) == CF_FAILURES) {
394 rra_idx = i;
395 break;
396 }
397 }
398 if (rra_idx == -1) {
399 rrd_set_error("Failures RRA does not exist in this RRD");
400 return -1;
401 }
403 /* set the value */
404 rrd->rra_def[rra_idx].par[rra_par].u_val = param;
405 return 0;
406 }
408 int set_windowarg(
409 rrd_t *rrd,
410 enum rra_par_en rra_par,
411 char *arg)
412 {
413 unsigned long param;
414 unsigned long i, cdp_idx;
415 signed short rra_idx = -1;
417 /* read the value */
418 param = atoi(arg);
419 if (param < 1 || param > MAX_FAILURES_WINDOW_LEN) {
420 rrd_set_error("Parameter must be between %d and %d",
421 1, MAX_FAILURES_WINDOW_LEN);
422 return -1;
423 }
424 /* does the appropriate RRA exist? */
425 for (i = 0; i < rrd->stat_head->rra_cnt; ++i) {
426 if (cf_conv(rrd->rra_def[i].cf_nam) == CF_FAILURES) {
427 rra_idx = i;
428 break;
429 }
430 }
431 if (rra_idx == -1) {
432 rrd_set_error("Failures RRA does not exist in this RRD");
433 return -1;
434 }
436 /* set the value */
437 rrd->rra_def[rra_idx].par[rra_par].u_cnt = param;
439 /* erase existing violations */
440 for (i = 0; i < rrd->stat_head->ds_cnt; i++) {
441 cdp_idx = rra_idx * (rrd->stat_head->ds_cnt) + i;
442 erase_violations(rrd, cdp_idx, rra_idx);
443 }
444 return 0;
445 }