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