Code

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