Code

d457d1f2acbc1af96cd539e40c6a6d385f107fc5
[rrdtool-all.git] / program / src / rrd_tool.c
1 /*****************************************************************************
2  * RRDtool 1.0.33  Copyright Tobias Oetiker, 1997 - 2001
3  *****************************************************************************
4  * rrd_tool.c  Startup wrapper
5  *****************************************************************************/
7 #include "rrd_tool.h"
9 void PrintUsage(char *cmd);
10 int CountArgs(char *aLine);
11 int CreateArgs(char *, char *, int, char **);
12 int HandleInputLine(int, char **, FILE*);
13 #define TRUE            1
14 #define FALSE           0
15 #define MAX_LENGTH      10000
18 void PrintUsage(char *cmd)
19 {
21     char help_main[] =
22            "RRDtool 1.0.33  Copyright 1997-2001 by Tobias Oetiker <tobi@oetiker.ch>\n\n"
23            "Usage: rrdtool [options] command command_options\n\n";
25     char help_list[] =
26            "Valid commands: create, update, graph, dump, restore,\n"
27            "\t\tlast, info, fetch, tune, resize\n\n";
29     char help_create[] =
30            "* create - create a new RRD\n\n"
31            "\trrdtool create filename [--start|-b start time]\n"
32            "\t\t[--step|-s step]\n"
33            "\t\t[DS:ds-name:DST:dst arguments]\n" 
34            "\t\t[RRA:CF:cf arguments]\n\n";
36     char help_dump[] =
37            "* dump - dump an RRD to XML\n\n"
38            "\trrdtool dump filename.rrd >filename.xml\n\n";
40     char help_info[] =
41            "* info - returns the configuration and status of the RRD\n\n"
42            "\trrdtool info filename.rrd\n\n";
44     char help_restore[] =
45            "* restore - restore an RRD file from its XML form\n\n"
46            "\trrdtool restore [--range-check|-r] filename.xml filename.rrd\n\n";
48     char help_last[] =
49            "* last - show last update time for RRD\n\n"
50            "\trrdtool last filename.rrd\n\n";
52     char help_update[] =
53            "* update - update an RRD\n\n"
54            "\trrdtool update filename\n"
55            "\t\t--template|-t ds-name:ds-name:...\n"
56            "\t\ttime|N:value[:value...]\n\n"
57            "\t\tat-time@value[:value...]\n\n"
58            "\t\t[ time:value[:value...] ..]\n\n";
60     char help_fetch[] =
61            "* fetch - fetch data out of an RRD\n\n"
62            "\trrdtool fetch filename.rrd CF\n"
63            "\t\t[--resolution|-r resolution]\n"
64            "\t\t[--start|-s start] [--end|-e end]\n\n";
66     char help_graph[] =
67            "* graph - generate a graph from one or several RRD\n\n"
68            "\trrdtool graph filename [-s|--start seconds] [-e|--end seconds]\n"
69            "\t\t[-x|--x-grid x-axis grid and label]\n"
70            "\t\t[--alt-y-grid]\n"
71            "\t\t[-y|--y-grid y-axis grid and label]\n"
72            "\t\t[-v|--vertical-label string] [-w|--width pixels]\n"
73            "\t\t[-h|--height pixels] [-o|--logarithmic]\n"
74            "\t\t[-u|--upper-limit value] [-z|--lazy]\n"
75            "\t\t[-l|--lower-limit value] [-r|--rigid]\n"
76            "\t\t[-g|--no-legend]\n"
77            "\t\t[--alt-autoscale]\n"
78            "\t\t[--alt-autoscale-max]\n"
79            "\t\t[--units-exponent value]\n"        
80            "\t\t[--step seconds]\n"        
81            "\t\t[-f|--imginfo printfstr]\n"
82            "\t\t[-a|--imgformat GIF|PNG]\n"
83            "\t\t[-c|--color COLORTAG#rrggbb] [-t|--title string]\n"
84            "\t\t[DEF:vname=rrd:ds-name:CF]\n"
85            "\t\t[CDEF:vname=rpn-expression]\n"
86            "\t\t[PRINT:vname:CF:format]\n"
87            "\t\t[GPRINT:vname:CF:format]\n"
88            "\t\t[HRULE:value#rrggbb[:legend]]\n"
89            "\t\t[VRULE:value#rrggbb[:legend]]\n"
90            "\t\t[LINE{1|2|3}:vname[#rrggbb[:legend]]]\n"
91            "\t\t[AREA:vname[#rrggbb[:legend]]]\n"
92            "\t\t[STACK:vname[#rrggbb[:legend]]]\n\n";
94     char help_tune[] =
95            " * tune -  Modify some basic properties of an RRD\n\n"
96            "\trrdtool tune filename\n"
97            "\t\t[--heartbeat|-h ds-name:heartbeat]\n"
98            "\t\t[--data-source-type|-d ds-name:DST]\n"
99            "\t\t[--data-source-rename|-r old-name:new-name]\n"
100            "\t\t[--minimum|-i ds-name:min] [--maximum|-a ds-name:max]\n"
101            "\t\t[--deltapos scale-value] [--deltaneg scale-value]\n"
102            "\t\t[--failure-threshold integer]\n"
103            "\t\t[--window-length integer]\n"
104            "\t\t[--alpha adaptation-parameter]\n"
105            "\t\t[--beta adaptation-parameter]\n"
106            "\t\t[--gamma adaptation-parameter]\n"
107            "\t\t[--gamma-deviation adaptation-parameter]\n"
108            "\t\t[--aberrant-reset ds-name]\n\n";
110     char help_resize[] =
111            " * resize - alter the lenght of one of the RRAs in an RRD\n\n"
112            "\trrdtool resize filename rranum GROW|SHRINK rows\n\n";
114     char help_lic[] =
115            "RRDtool is distributed under the Terms of the GNU General\n"
116            "Public License Version 2. (www.gnu.org/copyleft/gpl.html)\n\n"
118            "For more information read the RRD manpages\n\n";
120     enum { C_NONE, C_CREATE, C_DUMP, C_INFO, C_RESTORE, C_LAST,
121            C_UPDATE, C_FETCH, C_GRAPH, C_TUNE, C_RESIZE };
123     int help_cmd = C_NONE;
125     if (cmd)
126         {
127             if (!strcmp(cmd,"create"))
128                 help_cmd = C_CREATE;
129             else if (!strcmp(cmd,"dump"))
130                 help_cmd = C_DUMP;
131             else if (!strcmp(cmd,"info"))
132                 help_cmd = C_INFO;
133             else if (!strcmp(cmd,"restore"))
134                 help_cmd = C_RESTORE;
135             else if (!strcmp(cmd,"last"))
136                 help_cmd = C_LAST;
137             else if (!strcmp(cmd,"update"))
138                 help_cmd = C_UPDATE;
139             else if (!strcmp(cmd,"fetch"))
140                 help_cmd = C_FETCH;
141             else if (!strcmp(cmd,"graph"))
142                 help_cmd = C_GRAPH;
143             else if (!strcmp(cmd,"tune"))
144                 help_cmd = C_TUNE;
145             else if (!strcmp(cmd,"resize"))
146                 help_cmd = C_RESIZE;
147         }
148     fputs(help_main, stdout);
149     switch (help_cmd)
150         {
151             case C_NONE:
152                 fputs(help_list, stdout);
153                 break;
154             case C_CREATE:
155                 fputs(help_create, stdout);
156                 break;
157             case C_DUMP:
158                 fputs(help_dump, stdout);
159                 break;
160             case C_INFO:
161                 fputs(help_info, stdout);
162                 break;
163             case C_RESTORE:
164                 fputs(help_restore, stdout);
165                 break;
166             case C_LAST:
167                 fputs(help_last, stdout);
168                 break;
169             case C_UPDATE:
170                 fputs(help_update, stdout);
171                 break;
172             case C_FETCH:
173                 fputs(help_fetch, stdout);
174                 break;
175             case C_GRAPH:
176                 fputs(help_graph, stdout);
177                 break;
178             case C_TUNE:
179                 fputs(help_tune, stdout);
180                 break;
181             case C_RESIZE:
182                 fputs(help_resize, stdout);
183                 break;
184         }
185     fputs(help_lic, stdout);
189 int main(int argc, char *argv[])
191     char **myargv;
192     char aLine[MAX_LENGTH];
193 #ifdef MUST_DISABLE_SIGFPE
194     signal(SIGFPE,SIG_IGN);
195 #endif
196 #ifdef MUST_DISABLE_FPMASK
197     fpsetmask(0);
198 #endif
199     if (argc == 1)
200         {
201             PrintUsage("");
202             return 0;
203         }
204     
205     if ((argc == 2) && !strcmp("-",argv[1]))
206         {
207 #if HAVE_GETRUSAGE
208           struct rusage  myusage;
209           struct timeval starttime;
210           struct timeval currenttime;
211           struct timezone tz;
213             tz.tz_minuteswest =0;
214             tz.tz_dsttime=0;
215             gettimeofday(&starttime,&tz);
216 #endif
218             while (fgets(aLine, sizeof(aLine)-1, stdin)){
219                 if ((argc = CountArgs(aLine)) == 0)  {
220                     fprintf(stderr,"ERROR: not enough arguments\n");                
221                 }
222                 if ((myargv = (char **) malloc((argc+1) * 
223                                                sizeof(char *))) == NULL)   {
224                     perror("malloc");
225                     return -1;
226                 }
227                 if ((argc=CreateArgs(argv[0], aLine, argc, myargv)) < 0) {
228                     fprintf(stderr, "ERROR: creating arguments\n");
229                     return -1;
230                 }
232                 if (HandleInputLine(argc, myargv, stdout))
233                     return -1;
234                 free(myargv);
236 #if HAVE_GETRUSAGE
237                 getrusage(RUSAGE_SELF,&myusage);
238                 gettimeofday(&currenttime,&tz);
239                 printf("OK u:%1.2f s:%1.2f r:%1.2f\n",
240                        (double)myusage.ru_utime.tv_sec+
241                        (double)myusage.ru_utime.tv_usec/1000000.0,
242                        (double)myusage.ru_stime.tv_sec+
243                        (double)myusage.ru_stime.tv_usec/1000000.0,
244                        (double)(currenttime.tv_sec-starttime.tv_sec)
245                        +(double)(currenttime.tv_usec-starttime.tv_usec)
246                        /1000000.0);
247 #else
248                 printf("OK\n");
249 #endif
250                 fflush(stdout); /* this is important for pipes to work */
251             }
252         }
253     else if (argc == 2)
254         {
255                 PrintUsage(argv[1]);
256                 exit(0);
257         }
258     else
259         HandleInputLine(argc, argv, stderr);    
260     return 0;
263 int HandleInputLine(int argc, char **argv, FILE* out)
265     optind=0; /* reset gnu getopt */
266     opterr=0; /* no error messages */
268     if (argc < 3 
269         || strcmp("help", argv[1]) == 0
270         || strcmp("--help", argv[1]) == 0
271         || strcmp("-help", argv[1]) == 0
272         || strcmp("-?", argv[1]) == 0
273         || strcmp("-h", argv[1]) == 0 ) {
274         PrintUsage("");
275         return 0;
276     }
277     
278     if (strcmp("create", argv[1]) == 0) 
279         rrd_create(argc-1, &argv[1]);
280     else if (strcmp("dump", argv[1]) == 0)
281         rrd_dump(argc-1, &argv[1]);
282     else if (strcmp("info", argv[1]) == 0){
283         info_t *data,*save;
284         data=rrd_info(argc-1, &argv[1]);
285         while (data) {
286             save=data;
287             printf ("%s = ", data->key);
288             free(data->key);
289             
290             switch (data->type) {
291             case RD_I_VAL:
292                 if (isnan (data->value.u_val))
293                     printf("NaN");
294                 else
295                     printf ("%0.10e", data->value.u_val);
296                 break;
297             case RD_I_CNT:
298                 printf ("%lu", data->value.u_cnt);
299                 break;
300             case RD_I_STR:
301                 printf ("\"%s\"", data->value.u_str);
302                 free(data->value.u_str);
303                 break;
304             }
305             data = data->next;
306             free(save);
307             printf ("\n");
308         }
309         free(data);
310     }
311         
312     else if (strcmp("--version", argv[1]) == 0 ||
313              strcmp("version", argv[1]) == 0 || 
314              strcmp("v", argv[1]) == 0 ||
315              strcmp("-v", argv[1]) == 0  ||
316              strcmp("-version", argv[1]) == 0  )
317         printf("RRDtool 1.0.33  Copyright (C) 1997-2001 by Tobias Oetiker <tobi@oetiker.ch>\n");
318     else if (strcmp("restore", argv[1]) == 0)
319         rrd_restore(argc-1, &argv[1]);
320     else if (strcmp("resize", argv[1]) == 0)
321         rrd_resize(argc-1, &argv[1]);
322     else if (strcmp("last", argv[1]) == 0)
323         printf("%ld\n",rrd_last(argc-1, &argv[1]));
324     else if (strcmp("update", argv[1]) == 0)
325         rrd_update(argc-1, &argv[1]);
326     else if (strcmp("fetch", argv[1]) == 0) {
327         time_t        start,end;
328         unsigned long step, ds_cnt,i,ii;
329         rrd_value_t   *data,*datai;
330         char          **ds_namv;
331         if (rrd_fetch(argc-1, &argv[1],&start,&end,&step,&ds_cnt,&ds_namv,&data) != -1) {
332             datai=data;
333             printf("           ");
334             for (i = 0; i<ds_cnt;i++)
335                 printf("%14s",ds_namv[i]);
336             printf ("\n\n");
337             for (i = start; i <= end; i += step){
338                 printf("%10lu:", i);
339                 for (ii = 0; ii < ds_cnt; ii++)
340                     printf(" %0.10e", *(datai++));
341                 printf("\n");
342             }
343             for (i=0;i<ds_cnt;i++)
344                   free(ds_namv[i]);
345             free(ds_namv);
346             free (data);
347         }
348     }
349     else if (strcmp("graph", argv[1]) == 0) {
350         char **calcpr;
351         int xsize, ysize;
352         int i;
353         if( rrd_graph(argc-1, &argv[1], &calcpr, &xsize, &ysize) != -1 ) {
354             if (strcmp(argv[2],"-") != 0) 
355                 printf ("%dx%d\n",xsize,ysize);
356             if (calcpr) {
357                 for(i=0;calcpr[i];i++){
358                     if (strcmp(argv[2],"-") != 0) 
359                         printf("%s\n",calcpr[i]);
360                     free(calcpr[i]);
361                 } 
362                 free(calcpr);
363             }
364         }
365         
366     } else if (strcmp("tune", argv[1]) == 0) 
367                 rrd_tune(argc-1, &argv[1]);
368     else {
369                 rrd_set_error("unknown function '%s'",argv[1]);
370     }
371     if (rrd_test_error()) {
372         fprintf(out, "ERROR: %s\n",rrd_get_error());
373         rrd_clear_error();
374     }
375     return(0);
378 int CountArgs(char *aLine)
380     int i=0;
381     int aCount = 0;
382     int inarg = 0;
383     while (aLine[i] == ' ') i++;
384     while (aLine[i] != 0){       
385         if((aLine[i]== ' ') && inarg){
386             inarg = 0;
387         }
388         if((aLine[i]!= ' ') && ! inarg){
389             inarg = 1;
390             aCount++;
391         }
392         i++;
393     }
394     return aCount;
397 /*
398  * CreateArgs - take a string (aLine) and tokenize
399  */
400 int CreateArgs(char *pName, char *aLine, int argc, char **argv)
402     char        *getP, *putP;
403     char        **pargv = argv;
404     char        Quote = 0;
405     int inArg = 0;
406     int len;
408     len = strlen(aLine);
409     /* remove trailing space and newlines */
410     while (len && aLine[len] <= ' ') {
411         aLine[len] = 0 ; len--;
412     }
413     /* sikp leading blanks */
414     while (*aLine && *aLine <= ' ') aLine++;
416     pargv[0] = pName;
417     argc = 1;
418     getP = aLine;
419     putP = aLine;
420     while (*getP){
421         switch (*getP) {
422         case ' ': 
423             if (Quote){
424                 *(putP++)=*getP;
425             } else 
426                 if(inArg) {
427                     *(putP++) = 0;
428                     inArg = 0;
429                 }
430             break;
431         case '"':
432         case '\'':
433             if (Quote != 0) {
434                 if (Quote == *getP) 
435                     Quote = 0;
436                 else {
437                     *(putP++)=*getP;
438                 }
439             } else {
440                 if(!inArg){
441                     pargv[argc++] = putP;
442                     inArg=1;
443                 }           
444                 Quote = *getP;
445             }
446             break;
447         default:
448             if(!inArg){
449                 pargv[argc++] = putP;
450                 inArg=1;
451             }
452             *(putP++)=*getP;
453             break;
454         }
455         getP++;
456     }
458     *putP = '\0';
460     if (Quote) 
461         return -1;
462     else
463         return argc;