Ensure that response_read() always calls fflush() or fclose().
[rrdtool.git] / src / rrd_tune.c
index 3048b79..52651fd 100644 (file)
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * RRDtool 1.3rc7  Copyright by Tobi Oetiker, 1997-2008
+ * RRDtool 1.4.3  Copyright by Tobi Oetiker, 1997-2010
  *****************************************************************************
  * change header parameters of an rrd
  *****************************************************************************
  *
  *****************************************************************************/
 
+#include <stdlib.h>
+#include <locale.h>
+
 #include "rrd_tool.h"
 #include "rrd_rpncalc.h"
 #include "rrd_hw.h"
-#include <locale.h>
 
 int       set_hwarg(
     rrd_t *rrd,
@@ -58,6 +60,12 @@ int       set_windowarg(
     enum rra_par_en,
     char *arg);
 
+int set_hwsmootharg(
+    rrd_t *rrd,
+    enum cf_en cf,
+    enum rra_par_en rra_par,
+    char *arg);
+
 int rrd_tune(
     int argc,
     char **argv)
@@ -98,6 +106,7 @@ int rrd_tune(
     opterr = 0;         /* initialize getopt */
 
 
+    rrd_init(&rrd);
     rrd_file = rrd_open(argv[1], &rrd, RRD_READWRITE);
     if (rrd_file == NULL) {
         rrd_free(&rrd);
@@ -117,7 +126,8 @@ int rrd_tune(
         optcnt++;
         switch (opt) {
         case 'h':
-            old_locale = setlocale(LC_NUMERIC, "C");
+            old_locale = setlocale(LC_NUMERIC, NULL);
+            setlocale(LC_NUMERIC, "C");
             if ((matches =
                  sscanf(optarg, DS_NAM_FMT ":%ld", ds_nam,
                         &heartbeat)) != 2) {
@@ -137,7 +147,8 @@ int rrd_tune(
             break;
 
         case 'i':
-            old_locale = setlocale(LC_NUMERIC, "C");
+            old_locale = setlocale(LC_NUMERIC, NULL);
+            setlocale(LC_NUMERIC, "C");
             if ((matches =
                  sscanf(optarg, DS_NAM_FMT ":%lf", ds_nam, &min)) < 1) {
                 rrd_set_error("invalid arguments for minimum ds value");
@@ -159,7 +170,8 @@ int rrd_tune(
             break;
 
         case 'a':
-            old_locale = setlocale(LC_NUMERIC, "C");
+            old_locale = setlocale(LC_NUMERIC, NULL);
+            setlocale(LC_NUMERIC, "C");
             if ((matches =
                  sscanf(optarg, DS_NAM_FMT ":%lf", ds_nam, &max)) < 1) {
                 rrd_set_error("invalid arguments for maximum ds value");
@@ -302,7 +314,7 @@ int rrd_tune(
             break;
         case 's':
             strcpy(rrd.stat_head->version, RRD_VERSION);    /* smoothing_window causes Version 4 */
-            if (set_hwarg
+            if (set_hwsmootharg
                 (&rrd, CF_SEASONAL, RRA_seasonal_smoothing_window, optarg)) {
                 rrd_free(&rrd);
                 return -1;
@@ -310,7 +322,7 @@ int rrd_tune(
             break;
         case 'S':
             strcpy(rrd.stat_head->version, RRD_VERSION);    /* smoothing_window causes Version 4 */
-            if (set_hwarg
+            if (set_hwsmootharg
                 (&rrd, CF_DEVSEASONAL, RRA_seasonal_smoothing_window,
                  optarg)) {
                 rrd_free(&rrd);
@@ -395,6 +407,41 @@ int set_hwarg(
     return 0;
 }
 
+int set_hwsmootharg(
+    rrd_t *rrd,
+    enum cf_en cf,
+    enum rra_par_en rra_par,
+    char *arg)
+{
+    double    param;
+    unsigned long i;
+    signed short rra_idx = -1;
+
+    /* read the value */
+    param = atof(arg);
+    /* in order to avoid smoothing of SEASONAL or DEVSEASONAL, we need to 
+     * the 0.0 value*/
+    if (param < 0.0 || param > 1.0) {
+        rrd_set_error("Holt-Winters parameter must be between 0 and 1");
+        return -1;
+    }
+    /* does the appropriate RRA exist?  */
+    for (i = 0; i < rrd->stat_head->rra_cnt; ++i) {
+        if (cf_conv(rrd->rra_def[i].cf_nam) == cf) {
+            rra_idx = i;
+            break;
+        }
+    }
+    if (rra_idx == -1) {
+        rrd_set_error("Holt-Winters RRA does not exist in this RRD");
+        return -1;
+    }
+
+    /* set the value */
+    rrd->rra_def[rra_idx].par[rra_par].u_val = param;
+    return 0;
+}
+
 int set_deltaarg(
     rrd_t *rrd,
     enum rra_par_en rra_par,