Code

in remote mode, make argument count for remote commmands strict as suggested by Dave...
[rrdtool.git] / src / rrd_tool.c
index b5d856d3fca154c6a4ad6504abb983206e0e1019..454907386cb0c66a468aa49dbf7a83a044ef76bf 100644 (file)
@@ -1,11 +1,13 @@
 /*****************************************************************************
- * RRDtool 1.2.99907080300  Copyright by Tobi Oetiker, 1997-2007
+ * RRDtool 1.3.9  Copyright by Tobi Oetiker, 1997-2009
  *****************************************************************************
  * rrd_tool.c  Startup wrapper
  *****************************************************************************/
 
 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__) && !defined(HAVE_CONFIG_H)
 #include "../win32/config.h"
+#include <stdlib.h>
+#include <sys/stat.h>
 #else
 #ifdef HAVE_CONFIG_H
 #include "../rrd_config.h"
@@ -27,7 +29,6 @@ int       CountArgs(
 int       CreateArgs(
     char *,
     char *,
-    int,
     char **);
 int       HandleInputLine(
     int,
@@ -47,14 +48,15 @@ void PrintUsage(
 
     const char *help_main =
         N_("RRDtool %s"
-           "  Copyright 1997-2007 by Tobias Oetiker <tobi@oetiker.ch>\n"
+           "  Copyright 1997-2009 by Tobias Oetiker <tobi@oetiker.ch>\n"
            "               Compiled %s %s\n\n"
            "Usage: rrdtool [options] command command_options\n\n");
 
     const char *help_list =
-        N_("Valid commands: create, update, updatev, graph, dump, restore,\n"
-           "\t\tlast, lastupdate, first, info, fetch, tune,\n"
-           "\t\tresize, xport\n\n");
+        N_
+        ("Valid commands: create, update, updatev, graph, graphv,  dump, restore,\n"
+         "\t\tlast, lastupdate, first, info, fetch, tune,\n"
+         "\t\tresize, xport\n\n");
 
     const char *help_listremote =
         N_("Valid remote commands: quit, ls, cd, mkdir, pwd\n\n");
@@ -64,6 +66,7 @@ void PrintUsage(
         N_("* create - create a new RRD\n\n"
            "\trrdtool create filename [--start|-b start time]\n"
            "\t\t[--step|-s step]\n"
+           "\t\t[--no-overwrite|-O]\n"
            "\t\t[DS:ds-name:DST:dst arguments]\n"
            "\t\t[RRA:CF:cf arguments]\n\n");
 
@@ -116,25 +119,34 @@ void PrintUsage(
 
 /* break up very large strings (help_graph, help_tune) for ISO C89 compliance*/
 
-    const char *help_graph1 =
+    const char *help_graph0 =
         N_("* graph - generate a graph from one or several RRD\n\n"
-           "\trrdtool graph filename [-s|--start seconds] [-e|--end seconds]\n"
-           "\t\t[-x|--x-grid x-axis grid and label]\n"
+           "\trrdtool graph filename [-s|--start seconds] [-e|--end seconds]\n");
+    const char *help_graphv0 =
+        N_("* graphv - generate a graph from one or several RRD\n"
+           "           with meta data printed before the graph\n\n"
+           "\trrdtool graphv filename [-s|--start seconds] [-e|--end seconds]\n");
+    const char *help_graph1 =
+        N_("\t\t[-x|--x-grid x-axis grid and label]\n"
            "\t\t[-Y|--alt-y-grid]\n"
            "\t\t[-y|--y-grid y-axis grid and label]\n"
            "\t\t[-v|--vertical-label string] [-w|--width pixels]\n"
+           "\t\t[--right-axis scale:shift] [--right-axis-label label]\n"
+           "\t\t[--right-axis-format format]\n"          
            "\t\t[-h|--height pixels] [-o|--logarithmic]\n"
            "\t\t[-u|--upper-limit value] [-z|--lazy]\n"
            "\t\t[-l|--lower-limit value] [-r|--rigid]\n"
-           "\t\t[-g|--no-legend]\n"
+           "\t\t[-g|--no-legend] [--full-size-mode]\n"
            "\t\t[-F|--force-rules-legend]\n" "\t\t[-j|--only-graph]\n");
     const char *help_graph2 =
         N_("\t\t[-n|--font FONTTAG:size:font]\n"
            "\t\t[-m|--zoom factor]\n"
            "\t\t[-A|--alt-autoscale]\n"
            "\t\t[-M|--alt-autoscale-max]\n"
+           "\t\t[-G|--graph-render-mode {normal,mono}]\n"
            "\t\t[-R|--font-render-mode {normal,light,mono}]\n"
            "\t\t[-B|--font-smoothing-threshold size]\n"
+           "\t\t[-T|--tabwidth width]\n"
            "\t\t[-E|--slope-mode]\n"
            "\t\t[-N|--no-gridfit]\n"
            "\t\t[-X|--units-exponent value]\n"
@@ -148,8 +160,9 @@ void PrintUsage(
     const char *help_graph3 =
         N_("\t\t[CDEF:vname=rpn-expression]\n"
            "\t\t[VDEF:vdefname=rpn-expression]\n"
-           "\t\t[PRINT:vdefname:format]\n" "\t\t[GPRINT:vdefname:format]\n"
-           "\t\t[COMMENT:text]\n" "\t\t[SHIFT:vname:offset]\n"
+           "\t\t[PRINT:vdefname:format]\n"
+           "\t\t[GPRINT:vdefname:format]\n" "\t\t[COMMENT:text]\n"
+           "\t\t[SHIFT:vname:offset]\n"
            "\t\t[TICK:vname#rrggbb[aa][:[fraction][:legend]]]\n"
            "\t\t[HRULE:value#rrggbb[aa][:legend]]\n"
            "\t\t[VRULE:value#rrggbb[aa][:legend]]\n"
@@ -158,7 +171,6 @@ void PrintUsage(
            "\t\t[PRINT:vname:CF:format] (deprecated)\n"
            "\t\t[GPRINT:vname:CF:format] (deprecated)\n"
            "\t\t[STACK:vname[#rrggbb[aa][:legend]]] (deprecated)\n\n");
-
     const char *help_tune1 =
         N_(" * tune -  Modify some basic properties of an RRD\n\n"
            "\trrdtool tune filename\n"
@@ -171,57 +183,46 @@ void PrintUsage(
            "\t\t[--window-length integer]\n"
            "\t\t[--alpha adaptation-parameter]\n");
     const char *help_tune2 =
-        N_(" * tune -  Modify some basic properties of an RRD\n\n"
-           "\t\t[--beta adaptation-parameter]\n"
+        N_("\t\t[--beta adaptation-parameter]\n"
            "\t\t[--gamma adaptation-parameter]\n"
            "\t\t[--gamma-deviation adaptation-parameter]\n"
            "\t\t[--aberrant-reset ds-name]\n\n");
-
     const char *help_resize =
-        N_(" * resize - alter the length of one of the RRAs in an RRD\n\n"
-           "\trrdtool resize filename rranum GROW|SHRINK rows\n\n");
-
+        N_
+        (" * resize - alter the length of one of the RRAs in an RRD\n\n"
+         "\trrdtool resize filename rranum GROW|SHRINK rows\n\n");
     const char *help_xport =
         N_("* xport - generate XML dump from one or several RRD\n\n"
            "\trrdtool xport [-s|--start seconds] [-e|--end seconds]\n"
-           "\t\t[-m|--maxrows rows]\n"
-           "\t\t[--step seconds]\n"
-           "\t\t[--enumds]\n"
-           "\t\t[DEF:vname=rrd:ds-name:CF]\n"
+           "\t\t[-m|--maxrows rows]\n" "\t\t[--step seconds]\n"
+           "\t\t[--enumds]\n" "\t\t[DEF:vname=rrd:ds-name:CF]\n"
            "\t\t[CDEF:vname=rpn-expression]\n"
            "\t\t[XPORT:vname:legend]\n\n");
-
     const char *help_quit =
         N_(" * quit - closing a session in remote mode\n\n"
            "\trrdtool quit\n\n");
-
     const char *help_ls =
         N_(" * ls - lists all *.rrd files in current directory\n\n"
            "\trrdtool ls\n\n");
-
     const char *help_cd =
         N_(" * cd - changes the current directory\n\n"
            "\trrdtool cd new directory\n\n");
-
     const char *help_mkdir =
         N_(" * mkdir - creates a new directory\n\n"
            "\trrdtool mkdir newdirectoryname\n\n");
-
     const char *help_pwd =
         N_(" * pwd - returns the current working directory\n\n"
            "\trrdtool pwd\n\n");
-
     const char *help_lic =
         N_("RRDtool is distributed under the Terms of the GNU General\n"
            "Public License Version 2. (www.gnu.org/copyleft/gpl.html)\n\n"
            "For more information read the RRD manpages\n\n");
-
     enum { C_NONE, C_CREATE, C_DUMP, C_INFO, C_RESTORE, C_LAST,
-        C_LASTUPDATE, C_FIRST, C_UPDATE, C_FETCH, C_GRAPH, C_TUNE,
+        C_LASTUPDATE, C_FIRST, C_UPDATE, C_FETCH, C_GRAPH, C_GRAPHV,
+        C_TUNE,
         C_RESIZE, C_XPORT, C_QUIT, C_LS, C_CD, C_MKDIR, C_PWD,
         C_UPDATEV
     };
-
     int       help_cmd = C_NONE;
 
     if (cmd) {
@@ -247,6 +248,8 @@ void PrintUsage(
             help_cmd = C_FETCH;
         else if (!strcmp(cmd, "graph"))
             help_cmd = C_GRAPH;
+        else if (!strcmp(cmd, "graphv"))
+            help_cmd = C_GRAPHV;
         else if (!strcmp(cmd, "tune"))
             help_cmd = C_TUNE;
         else if (!strcmp(cmd, "resize"))
@@ -304,9 +307,16 @@ void PrintUsage(
         fputs(_(help_fetch), stdout);
         break;
     case C_GRAPH:
+        fputs(_(help_graph0), stdout);
         fputs(_(help_graph1), stdout);
-        fputs(help_graph2, stdout);
-        fputs(help_graph3, stdout);
+        fputs(_(help_graph2), stdout);
+        fputs(_(help_graph3), stdout);
+        break;
+    case C_GRAPHV:
+        fputs(_(help_graphv0), stdout);
+        fputs(_(help_graph1), stdout);
+        fputs(_(help_graph2), stdout);
+        fputs(_(help_graph3), stdout);
         break;
     case C_TUNE:
         fputs(_(help_tune1), stdout);
@@ -358,11 +368,16 @@ static char *fgetslong(
             return *aLinePtr = linebuf;
         bufsize += MAX_LENGTH;
         if (!(linebuf = realloc(linebuf, bufsize))) {
+            free(linebuf);
             perror("fgetslong: realloc");
             exit(1);
         }
     }
-    return *aLinePtr = linebuf[0] ? linebuf : 0;
+    if (linebuf[0]){
+        return  *aLinePtr = linebuf;
+    }
+    free(linebuf);
+    return *aLinePtr = 0;
 }
 
 int main(
@@ -382,7 +397,8 @@ int main(
 #ifdef HAVE_LOCALE_H
     setlocale(LC_ALL, "");
 #endif
-#ifdef HAVE_LIBINTL_H
+
+#if defined(HAVE_LIBINTL_H) && defined(BUILD_LIBINTL)
     bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
     bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
     textdomain(GETTEXT_PACKAGE);
@@ -412,11 +428,8 @@ int main(
                    == 0) {
 
 #ifdef HAVE_CHROOT
-                chroot(argv[2]);
-                if (errno != 0) {
-                    fprintf(stderr,
-                            "ERROR: can't change root to '%s' errno=%d\n",
-                            argv[2], errno);
+                if (chroot(argv[2]) != 0){
+                    fprintf(stderr, "ERROR: chroot %s: %s\n", argv[2],rrd_strerror(errno));
                     exit(errno);
                 }
                 ChangeRoot = 1;
@@ -432,29 +445,28 @@ int main(
             }
         }
         if (strcmp(firstdir, "")) {
-            chdir(firstdir);
-            if (errno != 0) {
-                fprintf(stderr, "ERROR: %s\n", rrd_strerror(errno));
+            if (chdir(firstdir) != 0){
+                fprintf(stderr, "ERROR: chdir %s %s\n", firstdir,rrd_strerror(errno));
                 exit(errno);
             }
         }
 
         while (fgetslong(&aLine, stdin)) {
+            char *aLineOrig = aLine;
             if ((argc = CountArgs(aLine)) == 0) {
+                free(aLine);
                 printf("ERROR: not enough arguments\n");
+                continue;                
             }
             if ((myargv = (char **) malloc((argc + 1) *
                                            sizeof(char *))) == NULL) {
                 perror("malloc");
                 exit(1);
             }
-            if ((argc = CreateArgs(argv[0], aLine, argc, myargv)) < 0) {
+            if ((argc = CreateArgs(argv[0], aLine, myargv)) < 0) {
                 printf("ERROR: creating arguments\n");
             } else {
-                int       ret = HandleInputLine(argc, myargv, stdout);
-
-                free(myargv);
-                if (ret == 0) {
+                if ( HandleInputLine(argc, myargv, stdout) == 0 ){
 #if HAVE_GETRUSAGE
                     getrusage(RUSAGE_SELF, &myusage);
                     gettimeofday(&currenttime, NULL);
@@ -469,12 +481,12 @@ int main(
                            / 1000000.0);
 #else
                     printf("OK\n");
-
 #endif
                 }
             }
             fflush(stdout); /* this is important for pipes to work */
-            free(aLine);
+            free(myargv);
+            free(aLineOrig);
         }
     } else if (argc == 2) {
         PrintUsage(argv[1]);
@@ -506,11 +518,9 @@ int HandleInputLine(
 
     /* Reset errno to 0 before we start.
      */
-    errno = 0;
-
     if (RemoteMode) {
         if (argc > 1 && strcmp("quit", argv[1]) == 0) {
-            if (argc > 2) {
+            if (argc != 2) {
                 printf("ERROR: invalid parameter count for quit\n");
                 return (1);
             }
@@ -518,7 +528,7 @@ int HandleInputLine(
         }
 #if defined(HAVE_OPENDIR) && defined(HAVE_READDIR) && defined(HAVE_CHDIR)
         if (argc > 1 && strcmp("cd", argv[1]) == 0) {
-            if (argc > 3) {
+            if (argc != 3) {
                 printf("ERROR: invalid parameter count for cd\n");
                 return (1);
             }
@@ -530,21 +540,20 @@ int HandleInputLine(
                 return (1);
             }
 #endif
-            chdir(argv[2]);
-            if (errno != 0) {
-                printf("ERROR: %s\n", rrd_strerror(errno));
+            if (chdir(argv[2]) != 0){
+                printf("ERROR: chdir %s %s\n", argv[2], rrd_strerror(errno));
                 return (1);
             }
             return (0);
         }
         if (argc > 1 && strcmp("pwd", argv[1]) == 0) {
-            if (argc > 2) {
+            if (argc != 2) {
                 printf("ERROR: invalid parameter count for pwd\n");
                 return (1);
             }
             cwd = getcwd(NULL, MAXPATH);
             if (cwd == NULL) {
-                printf("ERROR: %s\n", rrd_strerror(errno));
+                printf("ERROR: getcwd %s\n", rrd_strerror(errno));
                 return (1);
             }
             printf("%s\n", cwd);
@@ -552,7 +561,7 @@ int HandleInputLine(
             return (0);
         }
         if (argc > 1 && strcmp("mkdir", argv[1]) == 0) {
-            if (argc > 3) {
+            if (argc != 3) {
                 printf("ERROR: invalid parameter count for mkdir\n");
                 return (1);
             }
@@ -564,15 +573,14 @@ int HandleInputLine(
                 return (1);
             }
 #endif
-            mkdir(argv[2], 0777);
-            if (errno != 0) {
-                printf("ERROR: %s\n", rrd_strerror(errno));
+            if(mkdir(argv[2], 0777)!=0){
+                printf("ERROR: mkdir %s: %s\n", argv[2],rrd_strerror(errno));
                 return (1);
             }
             return (0);
         }
         if (argc > 1 && strcmp("ls", argv[1]) == 0) {
-            if (argc > 2) {
+            if (argc != 2) {
                 printf("ERROR: invalid parameter count for ls\n");
                 return (1);
             }
@@ -594,7 +602,7 @@ int HandleInputLine(
                 }
                 closedir(curdir);
             } else {
-                printf("ERROR: %s\n", rrd_strerror(errno));
+                printf("ERROR: opendir .: %s\n", rrd_strerror(errno));
                 return (errno);
             }
             return (0);
@@ -616,40 +624,15 @@ int HandleInputLine(
     else if (strcmp("dump", argv[1]) == 0)
         rrd_dump(argc - 1, &argv[1]);
     else if (strcmp("info", argv[1]) == 0 || strcmp("updatev", argv[1]) == 0) {
-        info_t   *data, *save;
+        rrd_info_t *data;
 
         if (strcmp("info", argv[1]) == 0)
+
             data = rrd_info(argc - 1, &argv[1]);
         else
             data = rrd_update_v(argc - 1, &argv[1]);
-        while (data) {
-            save = data;
-            printf("%s = ", data->key);
-            free(data->key);
-
-            switch (data->type) {
-            case RD_I_VAL:
-                if (isnan(data->value.u_val))
-                    printf("NaN");
-                else
-                    printf("%0.10e", data->value.u_val);
-                break;
-            case RD_I_CNT:
-                printf("%lu", data->value.u_cnt);
-                break;
-            case RD_I_INT:
-                printf("%d", data->value.u_int);
-                break;
-            case RD_I_STR:
-                printf("\"%s\"", data->value.u_str);
-                free(data->value.u_str);
-                break;
-            }
-            data = data->next;
-            free(save);
-            printf("\n");
-        }
-        free(data);
+        rrd_info_print(data);
+        rrd_info_free(data);
     }
 
     else if (strcmp("--version", argv[1]) == 0 ||
@@ -657,7 +640,7 @@ int HandleInputLine(
              strcmp("v", argv[1]) == 0 ||
              strcmp("-v", argv[1]) == 0 || strcmp("-version", argv[1]) == 0)
         printf("RRDtool " PACKAGE_VERSION
-               "  Copyright by Tobi Oetiker, 1997-2005 (%f)\n",
+               "  Copyright by Tobi Oetiker, 1997-2008 (%f)\n",
                rrd_version());
     else if (strcmp("restore", argv[1]) == 0)
         rrd_restore(argc - 1, &argv[1]);
@@ -735,16 +718,17 @@ int HandleInputLine(
         if (rrd_xport
             (argc - 1, &argv[1], &xxsize, &start, &end, &step, &col_cnt,
              &legend_v, &data) != -1) {
+            char *old_locale = setlocale(LC_NUMERIC, "C");
             row_cnt = (end - start) / step;
             ptr = data;
             printf("<?xml version=\"1.0\" encoding=\"%s\"?>\n\n",
                    XML_ENCODING);
             printf("<%s>\n", ROOT_TAG);
             printf("  <%s>\n", META_TAG);
-            printf("    <%s>%lu</%s>\n", META_START_TAG,
-                   (unsigned long) start + step, META_START_TAG);
+            printf("    <%s>%lld</%s>\n", META_START_TAG,
+                   (long long int) start + step, META_START_TAG);
             printf("    <%s>%lu</%s>\n", META_STEP_TAG, step, META_STEP_TAG);
-            printf("    <%s>%lu</%s>\n", META_END_TAG, (unsigned long) end,
+            printf("    <%s>%lld</%s>\n", META_END_TAG, (long long int) end,
                    META_END_TAG);
             printf("    <%s>%lu</%s>\n", META_ROWS_TAG, row_cnt,
                    META_ROWS_TAG);
@@ -765,15 +749,15 @@ int HandleInputLine(
             printf("  <%s>\n", DATA_TAG);
             for (ti = start + step; ti <= end; ti += step) {
                 printf("    <%s>", DATA_ROW_TAG);
-                printf("<%s>%lu</%s>", COL_TIME_TAG, ti, COL_TIME_TAG);
+                printf("<%s>%lld</%s>", COL_TIME_TAG, (long long int)ti, COL_TIME_TAG);
                 for (j = 0; j < col_cnt; j++) {
                     rrd_value_t newval = DNAN;
 
                     if (enumds == 1)
+
                         snprintf(vtag, vtag_s, "%s%lu", COL_DATA_TAG, j);
                     else
                         snprintf(vtag, vtag_s, "%s", COL_DATA_TAG);
-
                     newval = *ptr;
                     if (isnan(newval)) {
                         printf("<%s>NaN</%s>", vtag, vtag);
@@ -787,8 +771,10 @@ int HandleInputLine(
             free(data);
             printf("  </%s>\n", DATA_TAG);
             printf("</%s>\n", ROOT_TAG);
+            setlocale(LC_NUMERIC, old_locale);
         }
         free(vtag);
+        
     } else if (strcmp("graph", argv[1]) == 0) {
         char    **calcpr;
 
@@ -823,6 +809,15 @@ int HandleInputLine(
             }
         }
 
+    } else if (strcmp("graphv", argv[1]) == 0) {
+        rrd_info_t *grinfo = NULL;  /* 1 to distinguish it from the NULL that rrd_graph sends in */
+
+        grinfo = rrd_graph_v(argc - 1, &argv[1]);
+        if (grinfo) {
+            rrd_info_print(grinfo);
+            rrd_info_free(grinfo);
+        }
+
     } else if (strcmp("tune", argv[1]) == 0)
         rrd_tune(argc - 1, &argv[1]);
     else {
@@ -864,7 +859,6 @@ int CountArgs(
 int CreateArgs(
     char *pName,
     char *aLine,
-    int argc,
     char **argv)
 {
     char     *getP, *putP;
@@ -872,6 +866,7 @@ int CreateArgs(
     char      Quote = 0;
     int       inArg = 0;
     int       len;
+    int       argc = 1;
 
     len = strlen(aLine);
     /* remove trailing space and newlines */
@@ -882,7 +877,6 @@ int CreateArgs(
     /* sikp leading blanks */
     while (*aLine && *aLine <= ' ')
         aLine++;
-
     pargv[0] = pName;
     argc = 1;
     getP = aLine;
@@ -925,7 +919,6 @@ int CreateArgs(
     }
 
     *putP = '\0';
-
     if (Quote)
         return -1;
     else