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