src/rrd_lastupdate.c: Implement the `--daemon' option for `rrdtool lastupdate'.
[rrdtool.git] / src / rrd_tool.c
1 /*****************************************************************************
2  * RRDtool 1.3.0  Copyright by Tobi Oetiker, 1997-2008
3  *****************************************************************************
4  * rrd_tool.c  Startup wrapper
5  *****************************************************************************/
6
7 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__) && !defined(HAVE_CONFIG_H)
8 #include "../win32/config.h"
9 #else
10 #ifdef HAVE_CONFIG_H
11 #include "../rrd_config.h"
12 #endif
13 #endif
14
15 #include "rrd_tool.h"
16 #include "rrd_xport.h"
17 #include "rrd_i18n.h"
18
19 #ifdef HAVE_LOCALE_H
20 #include <locale.h>
21 #endif
22
23 void      PrintUsage(
24     char *cmd);
25 int       CountArgs(
26     char *aLine);
27 int       CreateArgs(
28     char *,
29     char *,
30     int,
31     char **);
32 int       HandleInputLine(
33     int,
34     char **,
35     FILE *);
36 int       RemoteMode = 0;
37 int       ChangeRoot = 0;
38
39 #define TRUE            1
40 #define FALSE           0
41 #define MAX_LENGTH      10000
42
43
44 void PrintUsage(
45     char *cmd)
46 {
47
48     const char *help_main =
49         N_("RRDtool %s"
50            "  Copyright 1997-2008 by Tobias Oetiker <tobi@oetiker.ch>\n"
51            "               Compiled %s %s\n\n"
52            "Usage: rrdtool [options] command command_options\n\n");
53
54     const char *help_list =
55         N_
56         ("Valid commands: create, update, updatev, graph, graphv,  dump, restore,\n"
57          "\t\tlast, lastupdate, first, info, fetch, tune,\n"
58          "\t\tresize, xport\n\n");
59
60     const char *help_listremote =
61         N_("Valid remote commands: quit, ls, cd, mkdir, pwd\n\n");
62
63
64     const char *help_create =
65         N_("* create - create a new RRD\n\n"
66            "\trrdtool create filename [--start|-b start time]\n"
67            "\t\t[--step|-s step]\n"
68            "\t\t[DS:ds-name:DST:dst arguments]\n"
69            "\t\t[RRA:CF:cf arguments]\n\n");
70
71     const char *help_dump =
72         N_("* dump - dump an RRD to XML\n\n"
73            "\trrdtool dump filename.rrd >filename.xml\n\n");
74
75     const char *help_info =
76         N_("* info - returns the configuration and status of the RRD\n\n"
77            "\trrdtool info filename.rrd\n\n");
78
79     const char *help_restore =
80         N_("* restore - restore an RRD file from its XML form\n\n"
81            "\trrdtool restore [--range-check|-r] [--force-overwrite|-f] filename.xml filename.rrd\n\n");
82
83     const char *help_last =
84         N_("* last - show last update time for RRD\n\n"
85            "\trrdtool last filename.rrd\n\n");
86
87     const char *help_lastupdate =
88         N_("* lastupdate - returns the most recent datum stored for\n"
89            "  each DS in an RRD\n\n" "\trrdtool lastupdate filename.rrd\n\n");
90
91     const char *help_first =
92         N_("* first - show first update time for RRA within an RRD\n\n"
93            "\trrdtool first filename.rrd [--rraindex number]\n\n");
94
95     const char *help_update =
96         N_("* update - update an RRD\n\n"
97            "\trrdtool update filename\n"
98            "\t\t[--template|-t ds-name:ds-name:...]\n"
99            "\t\t[--daemon <address>]\n"
100            "\t\ttime|N:value[:value...]\n\n"
101            "\t\tat-time@value[:value...]\n\n"
102            "\t\t[ time:value[:value...] ..]\n\n");
103
104     const char *help_updatev =
105         N_("* updatev - a verbose version of update\n"
106            "\treturns information about values, RRAs, and datasources updated\n\n"
107            "\trrdtool updatev filename\n"
108            "\t\t[--template|-t ds-name:ds-name:...]\n"
109            "\t\ttime|N:value[:value...]\n\n"
110            "\t\tat-time@value[:value...]\n\n"
111            "\t\t[ time:value[:value...] ..]\n\n");
112
113     const char *help_fetch =
114         N_("* fetch - fetch data out of an RRD\n\n"
115            "\trrdtool fetch filename.rrd CF\n"
116            "\t\t[-r|--resolution resolution]\n"
117            "\t\t[-s|--start start] [-e|--end end]\n"
118            "\t\t[--daemon <address>]\n\n");
119
120 /* break up very large strings (help_graph, help_tune) for ISO C89 compliance*/
121
122     const char *help_graph0 =
123         N_("* graph - generate a graph from one or several RRD\n\n"
124            "\trrdtool graph filename [-s|--start seconds] [-e|--end seconds]\n");
125     const char *help_graphv0 =
126         N_("* graphv - generate a graph from one or several RRD\n"
127            "           with meta data printed before the graph\n\n"
128            "\trrdtool graphv filename [-s|--start seconds] [-e|--end seconds]\n");
129     const char *help_graph1 =
130         N_("\t\t[-x|--x-grid x-axis grid and label]\n"
131            "\t\t[-Y|--alt-y-grid]\n"
132            "\t\t[-y|--y-grid y-axis grid and label]\n"
133            "\t\t[-v|--vertical-label string] [-w|--width pixels]\n"
134            "\t\t[-h|--height pixels] [-o|--logarithmic]\n"
135            "\t\t[-u|--upper-limit value] [-z|--lazy]\n"
136            "\t\t[-l|--lower-limit value] [-r|--rigid]\n"
137            "\t\t[-g|--no-legend] [--daemon <address>]\n"
138            "\t\t[-F|--force-rules-legend]\n" "\t\t[-j|--only-graph]\n");
139     const char *help_graph2 =
140         N_("\t\t[-n|--font FONTTAG:size:font]\n"
141            "\t\t[-m|--zoom factor]\n"
142            "\t\t[-A|--alt-autoscale]\n"
143            "\t\t[-M|--alt-autoscale-max]\n"
144            "\t\t[-R|--font-render-mode {normal,light,mono}]\n"
145            "\t\t[-B|--font-smoothing-threshold size]\n"
146            "\t\t[-T|--tabwidth width]\n"
147            "\t\t[-E|--slope-mode]\n"
148            "\t\t[-N|--no-gridfit]\n"
149            "\t\t[-X|--units-exponent value]\n"
150            "\t\t[-L|--units-length value]\n"
151            "\t\t[-S|--step seconds]\n"
152            "\t\t[-f|--imginfo printfstr]\n"
153            "\t\t[-a|--imgformat PNG]\n"
154            "\t\t[-c|--color COLORTAG#rrggbb[aa]] [-t|--title string]\n"
155            "\t\t[-W|--watermark string]\n"
156            "\t\t[DEF:vname=rrd:ds-name:CF]\n");
157     const char *help_graph3 =
158         N_("\t\t[CDEF:vname=rpn-expression]\n"
159            "\t\t[VDEF:vdefname=rpn-expression]\n"
160            "\t\t[PRINT:vdefname:format]\n"
161            "\t\t[GPRINT:vdefname:format]\n" "\t\t[COMMENT:text]\n"
162            "\t\t[SHIFT:vname:offset]\n"
163            "\t\t[TICK:vname#rrggbb[aa][:[fraction][:legend]]]\n"
164            "\t\t[HRULE:value#rrggbb[aa][:legend]]\n"
165            "\t\t[VRULE:value#rrggbb[aa][:legend]]\n"
166            "\t\t[LINE[width]:vname[#rrggbb[aa][:[legend][:STACK]]]]\n"
167            "\t\t[AREA:vname[#rrggbb[aa][:[legend][:STACK]]]]\n"
168            "\t\t[PRINT:vname:CF:format] (deprecated)\n"
169            "\t\t[GPRINT:vname:CF:format] (deprecated)\n"
170            "\t\t[STACK:vname[#rrggbb[aa][:legend]]] (deprecated)\n\n");
171     const char *help_tune1 =
172         N_(" * tune -  Modify some basic properties of an RRD\n\n"
173            "\trrdtool tune filename\n"
174            "\t\t[--heartbeat|-h ds-name:heartbeat]\n"
175            "\t\t[--data-source-type|-d ds-name:DST]\n"
176            "\t\t[--data-source-rename|-r old-name:new-name]\n"
177            "\t\t[--minimum|-i ds-name:min] [--maximum|-a ds-name:max]\n"
178            "\t\t[--deltapos scale-value] [--deltaneg scale-value]\n"
179            "\t\t[--failure-threshold integer]\n"
180            "\t\t[--window-length integer]\n"
181            "\t\t[--alpha adaptation-parameter]\n");
182     const char *help_tune2 =
183         N_(" * tune -  Modify some basic properties of an RRD\n\n"
184            "\t\t[--beta adaptation-parameter]\n"
185            "\t\t[--gamma adaptation-parameter]\n"
186            "\t\t[--gamma-deviation adaptation-parameter]\n"
187            "\t\t[--aberrant-reset ds-name]\n\n");
188     const char *help_resize =
189         N_
190         (" * resize - alter the length of one of the RRAs in an RRD\n\n"
191          "\trrdtool resize filename rranum GROW|SHRINK rows\n\n");
192     const char *help_xport =
193         N_("* xport - generate XML dump from one or several RRD\n\n"
194            "\trrdtool xport [-s|--start seconds] [-e|--end seconds]\n"
195            "\t\t[-m|--maxrows rows]\n" "\t\t[--step seconds]\n"
196            "\t\t[--enumds]\n" "\t\t[DEF:vname=rrd:ds-name:CF]\n"
197            "\t\t[CDEF:vname=rpn-expression]\n"
198            "\t\t[XPORT:vname:legend]\n\n");
199     const char *help_quit =
200         N_(" * quit - closing a session in remote mode\n\n"
201            "\trrdtool quit\n\n");
202     const char *help_ls =
203         N_(" * ls - lists all *.rrd files in current directory\n\n"
204            "\trrdtool ls\n\n");
205     const char *help_cd =
206         N_(" * cd - changes the current directory\n\n"
207            "\trrdtool cd new directory\n\n");
208     const char *help_mkdir =
209         N_(" * mkdir - creates a new directory\n\n"
210            "\trrdtool mkdir newdirectoryname\n\n");
211     const char *help_pwd =
212         N_(" * pwd - returns the current working directory\n\n"
213            "\trrdtool pwd\n\n");
214     const char *help_lic =
215         N_("RRDtool is distributed under the Terms of the GNU General\n"
216            "Public License Version 2. (www.gnu.org/copyleft/gpl.html)\n\n"
217            "For more information read the RRD manpages\n\n");
218     enum { C_NONE, C_CREATE, C_DUMP, C_INFO, C_RESTORE, C_LAST,
219         C_LASTUPDATE, C_FIRST, C_UPDATE, C_FETCH, C_GRAPH, C_GRAPHV,
220         C_TUNE,
221         C_RESIZE, C_XPORT, C_QUIT, C_LS, C_CD, C_MKDIR, C_PWD,
222         C_UPDATEV
223     };
224     int       help_cmd = C_NONE;
225
226     if (cmd) {
227         if (!strcmp(cmd, "create"))
228             help_cmd = C_CREATE;
229         else if (!strcmp(cmd, "dump"))
230             help_cmd = C_DUMP;
231         else if (!strcmp(cmd, "info"))
232             help_cmd = C_INFO;
233         else if (!strcmp(cmd, "restore"))
234             help_cmd = C_RESTORE;
235         else if (!strcmp(cmd, "last"))
236             help_cmd = C_LAST;
237         else if (!strcmp(cmd, "lastupdate"))
238             help_cmd = C_LASTUPDATE;
239         else if (!strcmp(cmd, "first"))
240             help_cmd = C_FIRST;
241         else if (!strcmp(cmd, "update"))
242             help_cmd = C_UPDATE;
243         else if (!strcmp(cmd, "updatev"))
244             help_cmd = C_UPDATEV;
245         else if (!strcmp(cmd, "fetch"))
246             help_cmd = C_FETCH;
247         else if (!strcmp(cmd, "graph"))
248             help_cmd = C_GRAPH;
249         else if (!strcmp(cmd, "graphv"))
250             help_cmd = C_GRAPHV;
251         else if (!strcmp(cmd, "tune"))
252             help_cmd = C_TUNE;
253         else if (!strcmp(cmd, "resize"))
254             help_cmd = C_RESIZE;
255         else if (!strcmp(cmd, "xport"))
256             help_cmd = C_XPORT;
257         else if (!strcmp(cmd, "quit"))
258             help_cmd = C_QUIT;
259         else if (!strcmp(cmd, "ls"))
260             help_cmd = C_LS;
261         else if (!strcmp(cmd, "cd"))
262             help_cmd = C_CD;
263         else if (!strcmp(cmd, "mkdir"))
264             help_cmd = C_MKDIR;
265         else if (!strcmp(cmd, "pwd"))
266             help_cmd = C_PWD;
267     }
268     fprintf(stdout, _(help_main), PACKAGE_VERSION, __DATE__, __TIME__);
269     fflush(stdout);
270     switch (help_cmd) {
271     case C_NONE:
272         fputs(_(help_list), stdout);
273         if (RemoteMode) {
274             fputs(_(help_listremote), stdout);
275         }
276         break;
277     case C_CREATE:
278         fputs(_(help_create), stdout);
279         break;
280     case C_DUMP:
281         fputs(_(help_dump), stdout);
282         break;
283     case C_INFO:
284         fputs(_(help_info), stdout);
285         break;
286     case C_RESTORE:
287         fputs(_(help_restore), stdout);
288         break;
289     case C_LAST:
290         fputs(_(help_last), stdout);
291         break;
292     case C_LASTUPDATE:
293         fputs(_(help_lastupdate), stdout);
294         break;
295     case C_FIRST:
296         fputs(_(help_first), stdout);
297         break;
298     case C_UPDATE:
299         fputs(_(help_update), stdout);
300         break;
301     case C_UPDATEV:
302         fputs(_(help_updatev), stdout);
303         break;
304     case C_FETCH:
305         fputs(_(help_fetch), stdout);
306         break;
307     case C_GRAPH:
308         fputs(_(help_graph0), stdout);
309         fputs(_(help_graph1), stdout);
310         fputs(_(help_graph2), stdout);
311         fputs(_(help_graph3), stdout);
312         break;
313     case C_GRAPHV:
314         fputs(_(help_graphv0), stdout);
315         fputs(_(help_graph1), stdout);
316         fputs(_(help_graph2), stdout);
317         fputs(_(help_graph3), stdout);
318         break;
319     case C_TUNE:
320         fputs(_(help_tune1), stdout);
321         fputs(_(help_tune2), stdout);
322         break;
323     case C_RESIZE:
324         fputs(_(help_resize), stdout);
325         break;
326     case C_XPORT:
327         fputs(_(help_xport), stdout);
328         break;
329     case C_QUIT:
330         fputs(_(help_quit), stdout);
331         break;
332     case C_LS:
333         fputs(_(help_ls), stdout);
334         break;
335     case C_CD:
336         fputs(_(help_cd), stdout);
337         break;
338     case C_MKDIR:
339         fputs(_(help_mkdir), stdout);
340         break;
341     case C_PWD:
342         fputs(_(help_pwd), stdout);
343         break;
344     }
345     fputs(_(help_lic), stdout);
346 }
347
348 static char *fgetslong(
349     char **aLinePtr,
350     FILE * stream)
351 {
352     char     *linebuf;
353     size_t    bufsize = MAX_LENGTH;
354     int       eolpos = 0;
355
356     if (feof(stream))
357         return *aLinePtr = 0;
358     if (!(linebuf = malloc(bufsize))) {
359         perror("fgetslong: malloc");
360         exit(1);
361     }
362     linebuf[0] = '\0';
363     while (fgets(linebuf + eolpos, MAX_LENGTH, stream)) {
364         eolpos += strlen(linebuf + eolpos);
365         if (linebuf[eolpos - 1] == '\n')
366             return *aLinePtr = linebuf;
367         bufsize += MAX_LENGTH;
368         if (!(linebuf = realloc(linebuf, bufsize))) {
369             perror("fgetslong: realloc");
370             exit(1);
371         }
372     }
373     return *aLinePtr = linebuf[0] ? linebuf : 0;
374 }
375
376 int main(
377     int argc,
378     char *argv[])
379 {
380     char    **myargv;
381     char     *aLine;
382     char     *firstdir = "";
383
384 #ifdef MUST_DISABLE_SIGFPE
385     signal(SIGFPE, SIG_IGN);
386 #endif
387 #ifdef MUST_DISABLE_FPMASK
388     fpsetmask(0);
389 #endif
390 #ifdef HAVE_LOCALE_H
391     setlocale(LC_ALL, "");
392 #endif
393
394 #if defined(HAVE_LIBINTL_H) && defined(BUILD_LIBINTL)
395     bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
396     bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
397     textdomain(GETTEXT_PACKAGE);
398 #endif
399     if (argc == 1) {
400         PrintUsage("");
401         return 0;
402     }
403
404     if (((argc == 2) || (argc == 3)) && !strcmp("-", argv[1])) {
405 #if HAVE_GETRUSAGE
406         struct rusage myusage;
407         struct timeval starttime;
408         struct timeval currenttime;
409
410         gettimeofday(&starttime, NULL);
411 #endif
412         RemoteMode = 1;
413         if ((argc == 3) && strcmp("", argv[2])) {
414
415             if (
416 #ifdef HAVE_GETUID
417                    getuid()
418 #else
419                    1
420 #endif
421                    == 0) {
422
423 #ifdef HAVE_CHROOT
424                 chroot(argv[2]);
425                 if (errno != 0) {
426                     fprintf(stderr,
427                             "ERROR: can't change root to '%s' errno=%d\n",
428                             argv[2], errno);
429                     exit(errno);
430                 }
431                 ChangeRoot = 1;
432                 firstdir = "/";
433 #else
434                 fprintf(stderr,
435                         "ERROR: change root is not supported by your OS "
436                         "or at least by this copy of rrdtool\n");
437                 exit(1);
438 #endif
439             } else {
440                 firstdir = argv[2];
441             }
442         }
443         if (strcmp(firstdir, "")) {
444             chdir(firstdir);
445             if (errno != 0) {
446                 fprintf(stderr, "ERROR: %s\n", rrd_strerror(errno));
447                 exit(errno);
448             }
449         }
450
451         while (fgetslong(&aLine, stdin)) {
452             if ((argc = CountArgs(aLine)) == 0) {
453                 printf("ERROR: not enough arguments\n");
454             }
455             if ((myargv = (char **) malloc((argc + 1) *
456                                            sizeof(char *))) == NULL) {
457                 perror("malloc");
458                 exit(1);
459             }
460             if ((argc = CreateArgs(argv[0], aLine, argc, myargv)) < 0) {
461                 printf("ERROR: creating arguments\n");
462             } else {
463                 int       ret = HandleInputLine(argc, myargv, stdout);
464
465                 free(myargv);
466                 if (ret == 0) {
467 #if HAVE_GETRUSAGE
468                     getrusage(RUSAGE_SELF, &myusage);
469                     gettimeofday(&currenttime, NULL);
470                     printf("OK u:%1.2f s:%1.2f r:%1.2f\n",
471                            (double) myusage.ru_utime.tv_sec +
472                            (double) myusage.ru_utime.tv_usec / 1000000.0,
473                            (double) myusage.ru_stime.tv_sec +
474                            (double) myusage.ru_stime.tv_usec / 1000000.0,
475                            (double) (currenttime.tv_sec - starttime.tv_sec)
476                            + (double) (currenttime.tv_usec -
477                                        starttime.tv_usec)
478                            / 1000000.0);
479 #else
480                     printf("OK\n");
481 #endif
482                 }
483             }
484             fflush(stdout); /* this is important for pipes to work */
485             free(aLine);
486         }
487     } else if (argc == 2) {
488         PrintUsage(argv[1]);
489         exit(0);
490     } else if (argc == 3 && !strcmp(argv[1], "help")) {
491         PrintUsage(argv[2]);
492         exit(0);
493     } else {
494         exit(HandleInputLine(argc, argv, stderr));
495     }
496     return 0;
497 }
498
499 /* HandleInputLine is NOT thread safe - due to readdir issues,
500    resolving them portably is not really simple. */
501 int HandleInputLine(
502     int argc,
503     char **argv,
504     FILE * out)
505 {
506 #if defined(HAVE_OPENDIR) && defined (HAVE_READDIR)
507     DIR      *curdir;   /* to read current dir with ls */
508     struct dirent *dent;
509 #endif
510 #if defined(HAVE_SYS_STAT_H)
511     struct stat st;
512 #endif
513     char     *cwd;      /* To hold current working dir on call to pwd */
514
515     /* Reset errno to 0 before we start.
516      */
517     errno = 0;
518     if (RemoteMode) {
519         if (argc > 1 && strcmp("quit", argv[1]) == 0) {
520             if (argc > 2) {
521                 printf("ERROR: invalid parameter count for quit\n");
522                 return (1);
523             }
524             exit(0);
525         }
526 #if defined(HAVE_OPENDIR) && defined(HAVE_READDIR) && defined(HAVE_CHDIR)
527         if (argc > 1 && strcmp("cd", argv[1]) == 0) {
528             if (argc > 3) {
529                 printf("ERROR: invalid parameter count for cd\n");
530                 return (1);
531             }
532 #if ! defined(HAVE_CHROOT) || ! defined(HAVE_GETUID)
533             if (getuid() == 0 && !ChangeRoot) {
534                 printf
535                     ("ERROR: chdir security problem - rrdtool is running as "
536                      "root but not chroot!\n");
537                 return (1);
538             }
539 #endif
540             chdir(argv[2]);
541             if (errno != 0) {
542                 printf("ERROR: %s\n", rrd_strerror(errno));
543                 return (1);
544             }
545             return (0);
546         }
547         if (argc > 1 && strcmp("pwd", argv[1]) == 0) {
548             if (argc > 2) {
549                 printf("ERROR: invalid parameter count for pwd\n");
550                 return (1);
551             }
552             cwd = getcwd(NULL, MAXPATH);
553             if (cwd == NULL) {
554                 printf("ERROR: %s\n", rrd_strerror(errno));
555                 return (1);
556             }
557             printf("%s\n", cwd);
558             free(cwd);
559             return (0);
560         }
561         if (argc > 1 && strcmp("mkdir", argv[1]) == 0) {
562             if (argc > 3) {
563                 printf("ERROR: invalid parameter count for mkdir\n");
564                 return (1);
565             }
566 #if ! defined(HAVE_CHROOT) || ! defined(HAVE_GETUID)
567             if (getuid() == 0 && !ChangeRoot) {
568                 printf
569                     ("ERROR: mkdir security problem - rrdtool is running as "
570                      "root but not chroot!\n");
571                 return (1);
572             }
573 #endif
574             mkdir(argv[2], 0777);
575             if (errno != 0) {
576                 printf("ERROR: %s\n", rrd_strerror(errno));
577                 return (1);
578             }
579             return (0);
580         }
581         if (argc > 1 && strcmp("ls", argv[1]) == 0) {
582             if (argc > 2) {
583                 printf("ERROR: invalid parameter count for ls\n");
584                 return (1);
585             }
586             if ((curdir = opendir(".")) != NULL) {
587                 while ((dent = readdir(curdir)) != NULL) {
588                     if (!stat(dent->d_name, &st)) {
589                         if (S_ISDIR(st.st_mode)) {
590                             printf("d %s\n", dent->d_name);
591                         }
592                         if (strlen(dent->d_name) > 4 && S_ISREG(st.st_mode)) {
593                             if (!strcmp
594                                 (dent->d_name + NAMLEN(dent) - 4, ".rrd")
595                                 || !strcmp(dent->d_name + NAMLEN(dent) - 4,
596                                            ".RRD")) {
597                                 printf("- %s\n", dent->d_name);
598                             }
599                         }
600                     }
601                 }
602                 closedir(curdir);
603             } else {
604                 printf("ERROR: %s\n", rrd_strerror(errno));
605                 return (errno);
606             }
607             return (0);
608         }
609 #endif                          /* opendir and readdir */
610
611     }
612     if (argc < 3
613         || strcmp("help", argv[1]) == 0
614         || strcmp("--help", argv[1]) == 0
615         || strcmp("-help", argv[1]) == 0
616         || strcmp("-?", argv[1]) == 0 || strcmp("-h", argv[1]) == 0) {
617         PrintUsage("");
618         return 0;
619     }
620
621     if (strcmp("create", argv[1]) == 0)
622         rrd_create(argc - 1, &argv[1]);
623     else if (strcmp("dump", argv[1]) == 0)
624         rrd_dump(argc - 1, &argv[1]);
625     else if (strcmp("info", argv[1]) == 0 || strcmp("updatev", argv[1]) == 0) {
626         rrd_info_t *data;
627
628         if (strcmp("info", argv[1]) == 0)
629
630             data = rrd_info(argc - 1, &argv[1]);
631         else
632             data = rrd_update_v(argc - 1, &argv[1]);
633         rrd_info_print(data);
634         rrd_info_free(data);
635     }
636
637     else if (strcmp("--version", argv[1]) == 0 ||
638              strcmp("version", argv[1]) == 0 ||
639              strcmp("v", argv[1]) == 0 ||
640              strcmp("-v", argv[1]) == 0 || strcmp("-version", argv[1]) == 0)
641         printf("RRDtool " PACKAGE_VERSION
642                "  Copyright by Tobi Oetiker, 1997-2008 (%f)\n",
643                rrd_version());
644     else if (strcmp("restore", argv[1]) == 0)
645         rrd_restore(argc - 1, &argv[1]);
646     else if (strcmp("resize", argv[1]) == 0)
647         rrd_resize(argc - 1, &argv[1]);
648     else if (strcmp("last", argv[1]) == 0)
649         printf("%ld\n", rrd_last(argc - 1, &argv[1]));
650     else if (strcmp("lastupdate", argv[1]) == 0) {
651         rrd_lastupdate(argc - 1, &argv[1]);
652     } else if (strcmp("first", argv[1]) == 0)
653         printf("%ld\n", rrd_first(argc - 1, &argv[1]));
654     else if (strcmp("update", argv[1]) == 0)
655         rrd_update(argc - 1, &argv[1]);
656     else if (strcmp("fetch", argv[1]) == 0) {
657         time_t    start, end, ti;
658         unsigned long step, ds_cnt, i, ii;
659         rrd_value_t *data, *datai;
660         char    **ds_namv;
661
662         if (rrd_fetch
663             (argc - 1, &argv[1], &start, &end, &step, &ds_cnt, &ds_namv,
664              &data) != -1) {
665             datai = data;
666             printf("           ");
667             for (i = 0; i < ds_cnt; i++)
668                 printf("%20s", ds_namv[i]);
669             printf("\n\n");
670             for (ti = start + step; ti <= end; ti += step) {
671                 printf("%10lu:", ti);
672                 for (ii = 0; ii < ds_cnt; ii++)
673                     printf(" %0.10e", *(datai++));
674                 printf("\n");
675             }
676             for (i = 0; i < ds_cnt; i++)
677                 free(ds_namv[i]);
678             free(ds_namv);
679             free(data);
680         }
681     } else if (strcmp("xport", argv[1]) == 0) {
682         int       xxsize;
683         unsigned long int j = 0;
684         time_t    start, end, ti;
685         unsigned long step, col_cnt, row_cnt;
686         rrd_value_t *data, *ptr;
687         char    **legend_v;
688         int       enumds = 0;
689         int       i;
690         size_t    vtag_s = strlen(COL_DATA_TAG) + 10;
691         char     *vtag = malloc(vtag_s);
692
693         for (i = 2; i < argc; i++) {
694             if (strcmp("--enumds", argv[i]) == 0)
695                 enumds = 1;
696         }
697
698         if (rrd_xport
699             (argc - 1, &argv[1], &xxsize, &start, &end, &step, &col_cnt,
700              &legend_v, &data) != -1) {
701             row_cnt = (end - start) / step;
702             ptr = data;
703             printf("<?xml version=\"1.0\" encoding=\"%s\"?>\n\n",
704                    XML_ENCODING);
705             printf("<%s>\n", ROOT_TAG);
706             printf("  <%s>\n", META_TAG);
707             printf("    <%s>%lu</%s>\n", META_START_TAG,
708                    (unsigned long) start + step, META_START_TAG);
709             printf("    <%s>%lu</%s>\n", META_STEP_TAG, step, META_STEP_TAG);
710             printf("    <%s>%lu</%s>\n", META_END_TAG, (unsigned long) end,
711                    META_END_TAG);
712             printf("    <%s>%lu</%s>\n", META_ROWS_TAG, row_cnt,
713                    META_ROWS_TAG);
714             printf("    <%s>%lu</%s>\n", META_COLS_TAG, col_cnt,
715                    META_COLS_TAG);
716             printf("    <%s>\n", LEGEND_TAG);
717             for (j = 0; j < col_cnt; j++) {
718                 char     *entry = NULL;
719
720                 entry = legend_v[j];
721                 printf("      <%s>%s</%s>\n", LEGEND_ENTRY_TAG, entry,
722                        LEGEND_ENTRY_TAG);
723                 free(entry);
724             }
725             free(legend_v);
726             printf("    </%s>\n", LEGEND_TAG);
727             printf("  </%s>\n", META_TAG);
728             printf("  <%s>\n", DATA_TAG);
729             for (ti = start + step; ti <= end; ti += step) {
730                 printf("    <%s>", DATA_ROW_TAG);
731                 printf("<%s>%lu</%s>", COL_TIME_TAG, ti, COL_TIME_TAG);
732                 for (j = 0; j < col_cnt; j++) {
733                     rrd_value_t newval = DNAN;
734
735                     if (enumds == 1)
736
737                         snprintf(vtag, vtag_s, "%s%lu", COL_DATA_TAG, j);
738                     else
739                         snprintf(vtag, vtag_s, "%s", COL_DATA_TAG);
740                     newval = *ptr;
741                     if (isnan(newval)) {
742                         printf("<%s>NaN</%s>", vtag, vtag);
743                     } else {
744                         printf("<%s>%0.10e</%s>", vtag, newval, vtag);
745                     };
746                     ptr++;
747                 }
748                 printf("</%s>\n", DATA_ROW_TAG);
749             }
750             free(data);
751             printf("  </%s>\n", DATA_TAG);
752             printf("</%s>\n", ROOT_TAG);
753         }
754         free(vtag);
755     } else if (strcmp("graph", argv[1]) == 0) {
756         char    **calcpr;
757
758 #ifdef notused /*XXX*/
759         const char *imgfile = argv[2];  /* rrd_graph changes argv pointer */
760 #endif
761         int       xsize, ysize;
762         double    ymin, ymax;
763         int       i;
764         int       tostdout = (strcmp(argv[2], "-") == 0);
765         int       imginfo = 0;
766
767         for (i = 2; i < argc; i++) {
768             if (strcmp(argv[i], "--imginfo") == 0
769                 || strcmp(argv[i], "-f") == 0) {
770                 imginfo = 1;
771                 break;
772             }
773         }
774         if (rrd_graph
775             (argc - 1, &argv[1], &calcpr, &xsize, &ysize, NULL, &ymin,
776              &ymax) != -1) {
777             if (!tostdout && !imginfo)
778                 printf("%dx%d\n", xsize, ysize);
779             if (calcpr) {
780                 for (i = 0; calcpr[i]; i++) {
781                     if (!tostdout)
782                         printf("%s\n", calcpr[i]);
783                     free(calcpr[i]);
784                 }
785                 free(calcpr);
786             }
787         }
788
789     } else if (strcmp("graphv", argv[1]) == 0) {
790         rrd_info_t *grinfo = NULL;  /* 1 to distinguish it from the NULL that rrd_graph sends in */
791
792         grinfo = rrd_graph_v(argc - 1, &argv[1]);
793         if (grinfo) {
794             rrd_info_print(grinfo);
795             rrd_info_free(grinfo);
796         }
797
798     } else if (strcmp("tune", argv[1]) == 0)
799         rrd_tune(argc - 1, &argv[1]);
800     else {
801         rrd_set_error("unknown function '%s'", argv[1]);
802     }
803     if (rrd_test_error()) {
804         fprintf(out, "ERROR: %s\n", rrd_get_error());
805         rrd_clear_error();
806         return 1;
807     }
808     return (0);
809 }
810
811 int CountArgs(
812     char *aLine)
813 {
814     int       i = 0;
815     int       aCount = 0;
816     int       inarg = 0;
817
818     while (aLine[i] == ' ')
819         i++;
820     while (aLine[i] != 0) {
821         if ((aLine[i] == ' ') && inarg) {
822             inarg = 0;
823         }
824         if ((aLine[i] != ' ') && !inarg) {
825             inarg = 1;
826             aCount++;
827         }
828         i++;
829     }
830     return aCount;
831 }
832
833 /*
834  * CreateArgs - take a string (aLine) and tokenize
835  */
836 int CreateArgs(
837     char *pName,
838     char *aLine,
839     int argc,
840     char **argv)
841 {
842     char     *getP, *putP;
843     char    **pargv = argv;
844     char      Quote = 0;
845     int       inArg = 0;
846     int       len;
847
848     len = strlen(aLine);
849     /* remove trailing space and newlines */
850     while (len && aLine[len] <= ' ') {
851         aLine[len] = 0;
852         len--;
853     }
854     /* sikp leading blanks */
855     while (*aLine && *aLine <= ' ')
856         aLine++;
857     pargv[0] = pName;
858     argc = 1;
859     getP = aLine;
860     putP = aLine;
861     while (*getP) {
862         switch (*getP) {
863         case ' ':
864             if (Quote) {
865                 *(putP++) = *getP;
866             } else if (inArg) {
867                 *(putP++) = 0;
868                 inArg = 0;
869             }
870             break;
871         case '"':
872         case '\'':
873             if (Quote != 0) {
874                 if (Quote == *getP)
875                     Quote = 0;
876                 else {
877                     *(putP++) = *getP;
878                 }
879             } else {
880                 if (!inArg) {
881                     pargv[argc++] = putP;
882                     inArg = 1;
883                 }
884                 Quote = *getP;
885             }
886             break;
887         default:
888             if (!inArg) {
889                 pargv[argc++] = putP;
890                 inArg = 1;
891             }
892             *(putP++) = *getP;
893             break;
894         }
895         getP++;
896     }
897
898     *putP = '\0';
899     if (Quote)
900         return -1;
901     else
902         return argc;
903 }