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