~ubuntu-branches/ubuntu/precise/rrdtool/precise-updates

« back to all changes in this revision

Viewing changes to src/rrd_tool.c

  • Committer: Package Import Robot
  • Author(s): Bernd Zeimetz
  • Date: 2012-01-24 23:35:35 UTC
  • mto: (1.2.9) (3.1.9 sid)
  • mto: This revision was merged to the branch mainline in revision 33.
  • Revision ID: package-import@ubuntu.com-20120124233535-4kqu7lb3afynd13h
ImportĀ upstreamĀ versionĀ 1.4.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*****************************************************************************
2
 
 * RRDtool 1.4.3  Copyright by Tobi Oetiker, 1997-2010
 
2
 * RRDtool 1.4.7  Copyright by Tobi Oetiker, 1997-2012
3
3
 *****************************************************************************
4
4
 * rrd_tool.c  Startup wrapper
5
5
 *****************************************************************************/
6
6
 
7
 
#if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__) && !defined(HAVE_CONFIG_H)
 
7
#if defined(WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__) && !defined(HAVE_CONFIG_H)
8
8
#include "../win32/config.h"
9
9
#include <stdlib.h>
10
10
#include <sys/stat.h>
 
11
#include <io.h>
 
12
#include <fcntl.h>
11
13
#else
12
14
#ifdef HAVE_CONFIG_H
13
15
#include "../rrd_config.h"
47
49
 
48
50
    const char *help_main =
49
51
        N_("RRDtool %s"
50
 
           "  Copyright 1997-2009 by Tobias Oetiker <tobi@oetiker.ch>\n"
 
52
           "  Copyright 1997-2012 by Tobias Oetiker <tobi@oetiker.ch>\n"
51
53
           "               Compiled %s %s\n\n"
52
54
           "Usage: rrdtool [options] command command_options\n");
53
55
 
205
207
        N_("* xport - generate XML dump from one or several RRD\n\n"
206
208
           "\trrdtool xport [-s|--start seconds] [-e|--end seconds]\n"
207
209
           "\t\t[-m|--maxrows rows]\n" "\t\t[--step seconds]\n"
208
 
           "\t\t[--enumds]\n" "\t\t[DEF:vname=rrd:ds-name:CF]\n"
 
210
           "\t\t[--enumds] [--json]\n" "\t\t[DEF:vname=rrd:ds-name:CF]\n"
209
211
           "\t\t[CDEF:vname=rpn-expression]\n"
210
212
           "\t\t[XPORT:vname:legend]\n");
211
213
    const char *help_quit =
414
416
       according to localeconv(3) */       
415
417
    setlocale(LC_ALL, "");
416
418
 
 
419
#if defined(WIN32) && !defined(__CYGWIN__)
 
420
    setmode(fileno(stdout), O_BINARY);
 
421
    setmode(fileno(stdin), O_BINARY);
 
422
#endif
 
423
 
 
424
 
417
425
#if defined(HAVE_LIBINTL_H) && defined(BUILD_LIBINTL)
418
426
    bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
419
427
    textdomain(GETTEXT_PACKAGE);
534
542
     */
535
543
    if (RemoteMode) {
536
544
        if (argc > 1 && strcmp("quit", argv[1]) == 0) {
537
 
            if (argc > 2) {
 
545
            if (argc != 2) {
538
546
                printf("ERROR: invalid parameter count for quit\n");
539
547
                return (1);
540
548
            }
542
550
        }
543
551
#if defined(HAVE_OPENDIR) && defined(HAVE_READDIR) && defined(HAVE_CHDIR)
544
552
        if (argc > 1 && strcmp("cd", argv[1]) == 0) {
545
 
            if (argc > 3) {
 
553
            if (argc != 3) {
546
554
                printf("ERROR: invalid parameter count for cd\n");
547
555
                return (1);
548
556
            }
562
570
        }
563
571
        if (argc > 1 && strcmp("pwd", argv[1]) == 0) {
564
572
            char     *cwd;      /* To hold current working dir on call to pwd */
565
 
            if (argc > 2) {
 
573
            if (argc != 2) {
566
574
                printf("ERROR: invalid parameter count for pwd\n");
567
575
                return (1);
568
576
            }
576
584
            return (0);
577
585
        }
578
586
        if (argc > 1 && strcmp("mkdir", argv[1]) == 0) {
579
 
            if (argc > 3) {
 
587
            if (argc != 3) {
580
588
                printf("ERROR: invalid parameter count for mkdir\n");
581
589
                return (1);
582
590
            }
595
603
            return (0);
596
604
        }
597
605
        if (argc > 1 && strcmp("ls", argv[1]) == 0) {
598
 
            if (argc > 2) {
 
606
            if (argc != 2) {
599
607
                printf("ERROR: invalid parameter count for ls\n");
600
608
                return (1);
601
609
            }
677
685
 
678
686
        if (rrd_fetch
679
687
            (argc - 1, &argv[1], &start, &end, &step, &ds_cnt, &ds_namv,
680
 
             &data) != -1) {
 
688
             &data) == 0) {
681
689
            datai = data;
682
690
            printf("           ");
683
691
            for (i = 0; i < ds_cnt; i++)
695
703
            free(data);
696
704
        }
697
705
    } else if (strcmp("xport", argv[1]) == 0) {
 
706
#ifdef HAVE_RRD_GRAPH
698
707
        int       xxsize;
699
708
        unsigned long int j = 0;
700
709
        time_t    start, end, ti;
702
711
        rrd_value_t *data, *ptr;
703
712
        char    **legend_v;
704
713
        int       enumds = 0;
 
714
        int       json = 0;
705
715
        int       i;
706
716
        size_t    vtag_s = strlen(COL_DATA_TAG) + 10;
707
717
        char     *vtag = malloc(vtag_s);
709
719
        for (i = 2; i < argc; i++) {
710
720
            if (strcmp("--enumds", argv[i]) == 0)
711
721
                enumds = 1;
 
722
            if (strcmp("--json", argv[i]) == 0)
 
723
                json = 1;
712
724
        }
713
725
 
714
726
        if (rrd_xport
715
727
            (argc - 1, &argv[1], &xxsize, &start, &end, &step, &col_cnt,
716
 
             &legend_v, &data) != -1) {
717
 
            char *old_locale = setlocale(LC_NUMERIC, "C");
 
728
             &legend_v, &data) == 0) {
 
729
            char *old_locale = setlocale(LC_NUMERIC,NULL);
 
730
            setlocale(LC_NUMERIC, "C");
718
731
            row_cnt = (end - start) / step;
719
732
            ptr = data;
720
 
            printf("<?xml version=\"1.0\" encoding=\"%s\"?>\n\n",
721
 
                   XML_ENCODING);
722
 
            printf("<%s>\n", ROOT_TAG);
723
 
            printf("  <%s>\n", META_TAG);
724
 
            printf("    <%s>%lu</%s>\n", META_START_TAG,
725
 
                   (unsigned long) start + step, META_START_TAG);
726
 
            printf("    <%s>%lu</%s>\n", META_STEP_TAG, step, META_STEP_TAG);
727
 
            printf("    <%s>%lu</%s>\n", META_END_TAG, (unsigned long) end,
728
 
                   META_END_TAG);
729
 
            printf("    <%s>%lu</%s>\n", META_ROWS_TAG, row_cnt,
730
 
                   META_ROWS_TAG);
731
 
            printf("    <%s>%lu</%s>\n", META_COLS_TAG, col_cnt,
732
 
                   META_COLS_TAG);
733
 
            printf("    <%s>\n", LEGEND_TAG);
 
733
            if (json == 0){
 
734
                printf("<?xml version=\"1.0\" encoding=\"%s\"?>\n\n",
 
735
                    XML_ENCODING);
 
736
                printf("<%s>\n", ROOT_TAG);
 
737
                printf("  <%s>\n", META_TAG);
 
738
            }
 
739
            else {
 
740
                printf("{ about: 'RRDtool xport JSON output',\n  meta: {\n");
 
741
            }
 
742
 
 
743
 
 
744
#define pXJV(indent,fmt,tag,value) \
 
745
            if (json) { \
 
746
               printf(indent "%s: " fmt ",\n",tag,value); \
 
747
            } else { \
 
748
               printf(indent "<%s>" fmt "</%s>\n",tag,value,tag); \
 
749
            }
 
750
        
 
751
            pXJV("    ","%lld",META_START_TAG,(long long int) start + step);
 
752
            pXJV("    ","%lu", META_STEP_TAG, step);
 
753
            pXJV("    ","%lld",META_END_TAG,(long long int) start + step);
 
754
            if (! json){
 
755
                    pXJV("    ","%lu", META_ROWS_TAG, row_cnt);
 
756
                    pXJV("    ","%lu", META_COLS_TAG, col_cnt);
 
757
            }
 
758
             
 
759
            if (json){
 
760
                printf("    %s: [\n", LEGEND_TAG);
 
761
            }
 
762
            else {
 
763
                printf("    <%s>\n", LEGEND_TAG);
 
764
            }
734
765
            for (j = 0; j < col_cnt; j++) {
735
766
                char     *entry = NULL;
736
 
 
737
767
                entry = legend_v[j];
738
 
                printf("      <%s>%s</%s>\n", LEGEND_ENTRY_TAG, entry,
 
768
                if (json){
 
769
                    printf("      '%s'", entry);
 
770
                    if (j < col_cnt -1){
 
771
                        printf(",");
 
772
                    }
 
773
                    printf("\n");
 
774
                }
 
775
                else {
 
776
                    printf("      <%s>%s</%s>\n", LEGEND_ENTRY_TAG, entry,
739
777
                       LEGEND_ENTRY_TAG);
 
778
                }
740
779
                free(entry);
741
780
            }
742
781
            free(legend_v);
743
 
            printf("    </%s>\n", LEGEND_TAG);
744
 
            printf("  </%s>\n", META_TAG);
745
 
            printf("  <%s>\n", DATA_TAG);
 
782
            if (json){
 
783
                printf("          ]\n     },\n");
 
784
            }
 
785
            else {
 
786
                printf("    </%s>\n", LEGEND_TAG);
 
787
                printf("  </%s>\n", META_TAG);
 
788
            }
 
789
            
 
790
            if (json){
 
791
                printf("  %s: [\n",DATA_TAG);
 
792
            } else {
 
793
                printf("  <%s>\n", DATA_TAG);
 
794
            }
746
795
            for (ti = start + step; ti <= end; ti += step) {
747
 
                printf("    <%s>", DATA_ROW_TAG);
748
 
                printf("<%s>%lu</%s>", COL_TIME_TAG, ti, COL_TIME_TAG);
 
796
                if (json){
 
797
                    printf("    [ ");
 
798
                }
 
799
                else {
 
800
                    printf("    <%s>", DATA_ROW_TAG);
 
801
                    printf("<%s>%lld</%s>", COL_TIME_TAG, (long long int)ti, COL_TIME_TAG);
 
802
                }
749
803
                for (j = 0; j < col_cnt; j++) {
750
804
                    rrd_value_t newval = DNAN;
751
 
 
752
 
                    if (enumds == 1)
753
 
 
754
 
                        snprintf(vtag, vtag_s, "%s%lu", COL_DATA_TAG, j);
755
 
                    else
756
 
                        snprintf(vtag, vtag_s, "%s", COL_DATA_TAG);
757
805
                    newval = *ptr;
758
 
                    if (isnan(newval)) {
759
 
                        printf("<%s>NaN</%s>", vtag, vtag);
760
 
                    } else {
761
 
                        printf("<%s>%0.10e</%s>", vtag, newval, vtag);
762
 
                    };
 
806
                    if (json){
 
807
                        if (isnan(newval)){
 
808
                            printf("null");                        
 
809
                        } else {
 
810
                            printf("%0.10e",newval);
 
811
                        }
 
812
                        if (j < col_cnt -1){
 
813
                            printf(", ");
 
814
                        }
 
815
                    }
 
816
                    else {
 
817
                        if (enumds == 1)
 
818
                            snprintf(vtag, vtag_s, "%s%lu", COL_DATA_TAG, j);
 
819
                        else
 
820
                           snprintf(vtag, vtag_s, "%s", COL_DATA_TAG);
 
821
                        if (isnan(newval)) {
 
822
                           printf("<%s>NaN</%s>", vtag, vtag);
 
823
                        } else {
 
824
                           printf("<%s>%0.10e</%s>", vtag, newval, vtag);
 
825
                        };
 
826
                    }
763
827
                    ptr++;
764
 
                }
765
 
                printf("</%s>\n", DATA_ROW_TAG);
 
828
                }                
 
829
                if (json){
 
830
                    printf(ti < end ? " ],\n" : "  ]\n");
 
831
                }
 
832
                else {                
 
833
                    printf("</%s>\n", DATA_ROW_TAG);
 
834
                }
766
835
            }
767
836
            free(data);
768
 
            printf("  </%s>\n", DATA_TAG);
769
 
            printf("</%s>\n", ROOT_TAG);
 
837
            if (json){
 
838
                printf("  ]\n}\n");
 
839
            }
 
840
            else {
 
841
                printf("  </%s>\n", DATA_TAG);
 
842
                printf("</%s>\n", ROOT_TAG);
 
843
            }
770
844
            setlocale(LC_NUMERIC, old_locale);
771
845
        }
772
846
        free(vtag);
 
847
#else
 
848
        rrd_set_error("the instance of rrdtool has been compiled without graphics");
 
849
#endif
773
850
    } else if (strcmp("graph", argv[1]) == 0) {
 
851
#ifdef HAVE_RRD_GRAPH
774
852
        char    **calcpr;
775
853
 
776
854
#ifdef notused /*XXX*/
791
869
        }
792
870
        if (rrd_graph
793
871
            (argc - 1, &argv[1], &calcpr, &xsize, &ysize, NULL, &ymin,
794
 
             &ymax) != -1) {
 
872
             &ymax) == 0) {
795
873
            if (!tostdout && !imginfo)
796
874
                printf("%dx%d\n", xsize, ysize);
797
875
            if (calcpr) {
804
882
            }
805
883
        }
806
884
 
 
885
#else
 
886
       rrd_set_error("the instance of rrdtool has been compiled without graphics");
 
887
#endif
807
888
    } else if (strcmp("graphv", argv[1]) == 0) {
 
889
#ifdef HAVE_RRD_GRAPH
808
890
        rrd_info_t *grinfo = NULL;  /* 1 to distinguish it from the NULL that rrd_graph sends in */
809
891
 
810
892
        grinfo = rrd_graph_v(argc - 1, &argv[1]);
812
894
            rrd_info_print(grinfo);
813
895
            rrd_info_free(grinfo);
814
896
        }
815
 
 
 
897
#else
 
898
       rrd_set_error("the instance of rrdtool has been compiled without graphics");
 
899
#endif
816
900
    } else if (strcmp("tune", argv[1]) == 0)
817
901
        rrd_tune(argc - 1, &argv[1]);
 
902
#ifndef WIN32
818
903
    else if (strcmp("flushcached", argv[1]) == 0)
819
904
        rrd_flushcached(argc - 1, &argv[1]);
 
905
#endif
820
906
    else {
821
907
        rrd_set_error("unknown function '%s'", argv[1]);
822
908
    }