Ejemplo n.º 1
0
/* get_path: Return a path name appropriate to be sent to the daemon.
 *
 * When talking to a local daemon (thru a UNIX socket), relative path names
 * are resolved to absolute path names to allow for transparent integration
 * into existing solutions (as requested by Tobi). Else, absolute path names
 * are not allowed, since path name translation is done by the server.
 *
 * One must hold `lock' when calling this function. */
static const char *get_path (const char *path, char *resolved_path) /* {{{ */
{
  const char *ret = path;
  int is_unix = 0;

  if ((path == NULL) || (resolved_path == NULL) || (sd_path == NULL))
    return (NULL);

  if ((*sd_path == '/')
      || (strncmp ("unix:", sd_path, strlen ("unix:")) == 0))
    is_unix = 1;

  if (is_unix)
  {
    ret = realpath(path, resolved_path);
    if (ret == NULL)
      rrd_set_error("realpath(%s): %s", path, rrd_strerror(errno));
    return ret;
  }
  else
  {
    if (*path == '/') /* not absolute path */
    {
      rrd_set_error ("absolute path names not allowed when talking "
          "to a remote daemon");
      return NULL;
    }
  }

  return path;
} /* }}} char *get_path */
Ejemplo n.º 2
0
rrd_info_t *rrd_info(
    int argc,
    char **argv)
{
    struct optparse_long longopts[] = {
        {"daemon", 'd', OPTPARSE_REQUIRED},
        {"noflush", 'F', OPTPARSE_NONE},
        {0},
    };
    struct    optparse options;
    int       opt;
    rrd_info_t *info;
    char *opt_daemon = NULL;
    int status;
    int flushfirst = 1;

    optparse_init(&options, argc, argv);
    while ((opt = optparse_long(&options, longopts, NULL)) != -1) {
        switch (opt) {
        case 'd':
            if (opt_daemon != NULL)
                free (opt_daemon);
            opt_daemon = strdup(options.optarg);
            if (opt_daemon == NULL)
            {
                rrd_set_error ("strdup failed.");
                return NULL;
            }
            break;

        case 'F':
            flushfirst = 0;
            break;

        case '?':
            rrd_set_error("%s", options.errmsg);
            return NULL;
        }
    } /* while (opt != -1) */

    if (options.argc - options.optind != 1) {
        rrd_set_error ("Usage: rrdtool %s [--daemon |-d <addr> [--noflush|-F]] <file>",
                options.argv[0]);
        return NULL;
    }

    if (flushfirst) {
        status = rrdc_flush_if_daemon(opt_daemon, options.argv[options.optind]);
        if (status) return (NULL);
    }

    rrdc_connect (opt_daemon);
    if (rrdc_is_connected (opt_daemon))
        info = rrdc_info(options.argv[options.optind]);
    else
        info = rrd_info_r(options.argv[options.optind]);

    if (opt_daemon) free(opt_daemon);
    return (info);
} /* rrd_info_t *rrd_info */
Ejemplo n.º 3
0
static int request (const char *buffer, size_t buffer_size, /* {{{ */
    rrdc_response_t **ret_response)
{
  int status;
  rrdc_response_t *res;

  if (sh == NULL)
    return (ENOTCONN);

  status = (int) fwrite (buffer, buffer_size, /* nmemb = */ 1, sh);
  if (status != 1)
  {
    close_connection ();
    rrd_set_error("request: socket error (%d) while talking to rrdcached",
                  status);
    return (-1);
  }
  fflush (sh);

  res = NULL;
  status = response_read (&res);

  if (status != 0)
  {
    if (status < 0)
      rrd_set_error("request: internal error while talking to rrdcached");
    return (status);
  }

  *ret_response = res;
  return (0);
} /* }}} int request */
Ejemplo n.º 4
0
int set_deltaarg(
    rrd_t *rrd,
    enum rra_par_en rra_par,
    char *arg)
{
    rrd_value_t param;
    unsigned long i;
    signed short rra_idx = -1;

    param = atof(arg);
    if (param < 0.1) {
        rrd_set_error("Parameter specified is too small");
        return -1;
    }
    /* does the appropriate RRA exist?  */
    for (i = 0; i < rrd->stat_head->rra_cnt; ++i) {
        if (cf_conv(rrd->rra_def[i].cf_nam) == CF_FAILURES) {
            rra_idx = i;
            break;
        }
    }
    if (rra_idx == -1) {
        rrd_set_error("Failures RRA does not exist in this RRD");
        return -1;
    }

    /* set the value */
    rrd->rra_def[rra_idx].par[rra_par].u_val = param;
    return 0;
}
Ejemplo n.º 5
0
/* convenience function; if there is a daemon specified, or if we can
 * detect one from the environment, then flush the file.  Otherwise, no-op
 */
int rrdc_flush_if_daemon (const char *opt_daemon, const char *filename) /* {{{ */
{
  int status = 0;

  rrdc_connect(opt_daemon);

  if (rrdc_is_connected(opt_daemon))
  {
    rrd_clear_error();
    status = rrdc_flush (filename);

    if (status != 0 && !rrd_test_error())
    {
      if (status > 0)
      {
        rrd_set_error("rrdc_flush (%s) failed: %s",
                      filename, rrd_strerror(status));
      }
      else if (status < 0)
      {
        rrd_set_error("rrdc_flush (%s) failed with status %i.",
                      filename, status);
      }
    }
  } /* if (rrdc_is_connected(..)) */

  return status;
} /* }}} int rrdc_flush_if_daemon */
Ejemplo n.º 6
0
static int parse_xport(enum gf_en gf,parsedargs_t* pa,image_desc_t *const im) {
  /* get new graph that we fill */
  graph_desc_t *gdp=newGraphDescription(im,gf,pa,PARSE_VNAMECOLORLEGEND);
  if (!gdp) { return 1;}
  /* check for cdef */
  /* and check that it is a CDEF */
  switch (im->gdes[gdp->vidx].gf) {
  case GF_DEF:
  case GF_CDEF:
    dprintf("- vname is of type DEF or CDEF, OK\n");
    break;
  case GF_VDEF:
    rrd_set_error("Cannot shift a VDEF: '%s' in line '%s'\n",
		  im->gdes[gdp->vidx].vname, pa->arg_orig);
    return 1;
  default:
    rrd_set_error("Encountered unknown type variable '%s' in line '%s'",
		  im->gdes[gdp->vidx].vname, pa->arg_orig);
    return 1;
  }

  /* debug output */
  dprintf("=================================\n");
  dprintf("LINE  : %s\n",pa->arg_orig);
  dprintf("VNAME : %s (%li)\n",gdp->vname,gdp->vidx);
  dprintf("LEGEND: \"%s\"\n",gdp->legend);
  dprintf("=================================\n");

  return 0;
}
Ejemplo n.º 7
0
int parse_textalign(enum gf_en gf,parsedargs_t* pa,image_desc_t *const im) {
  /* get new graph that we fill */
  graph_desc_t *gdp=newGraphDescription(im,gf,pa,0); 
  if (!gdp) { return 1;}

  /* get align */
  char* align=getKeyValueArgument("align",1,pa);
  if (!align) align=getFirstUnusedArgument(1,pa)->value;
  if (!align) { rrd_set_error("No alignment given"); return 1; }

  /* parse align */
  if (strcmp(align, "left") == 0) {
    gdp->txtalign = TXA_LEFT;
  } else if (strcmp(align, "right") == 0) {
    gdp->txtalign = TXA_RIGHT;
  } else if (strcmp(align, "justified") == 0) {
    gdp->txtalign = TXA_JUSTIFIED;
  } else if (strcmp(align, "center") == 0) {
    gdp->txtalign = TXA_CENTER;
  } else {
    rrd_set_error("Unknown alignement type '%s'", align);
    return 1;
  }

  /* debug output */
  dprintf("=================================\n");
  dprintf("TEXTALIGN : %s\n",pa->arg_orig);
  dprintf("ALIGNMENT : %s (%u)\n",align,gdp->txtalign);  
  dprintf("=================================\n");
  /* and return */
  return 0;
}
Ejemplo n.º 8
0
static int _inline_unescape (char* string) {
  char *src=string;
  char *dst=string;
  char c,h1,h2;
  while((c= *src)) {
    src++;
    if (c == '%') {
      if (*src == '%') { 
	/* increase src pointer by 1 skiping second % */
	src+=1;
      } else {
	/* try to calculate hex value from the next 2 values*/
	h1=_hexcharhelper(*src);
	if (h1 == (char)-1) {
	  rrd_set_error("string escape error at: %s\n",string);
	  return(1);
	}
	h2=_hexcharhelper(*(src+1));
	if (h1 == (char)-1) {
	  rrd_set_error("string escape error at: %s\n",string);
	  return(1);
	}
	c=h2+(h1<<4);
	/* increase src pointer by 2 skiping 2 chars */
	src+=2;
      } 
    }
    *dst=c;
    dst++;
  }
  *dst=0;
  return 0;
}
Ejemplo n.º 9
0
int rrd_restore(
    int argc,
    char **argv)
{
    rrd_t    *rrd;
    /* init rrd clean */
    optind = 0;
    opterr = 0;         /* initialize getopt */
    while (42) {
        int       opt;
        int       option_index = 0;
        static struct option long_options[] = {
            {"range-check", no_argument, 0, 'r'},
            {"force-overwrite", no_argument, 0, 'f'},
            {0, 0, 0, 0}
        };

        opt = getopt_long(argc, argv, "rf", long_options, &option_index);

        if (opt == EOF)
            break;

        switch (opt) {
        case 'r':
            opt_range_check = 1;
            break;

        case 'f':
            opt_force_overwrite = 1;
            break;

        default:
            rrd_set_error("usage rrdtool %s [--range-check|-r] "
                          "[--force-overwrite/-f]  file.xml file.rrd",
                          argv[0]);
            return (-1);
            break;
        }
    }                   /* while (42) */

    if ((argc - optind) != 2) {
        rrd_set_error("usage rrdtool %s [--range-check/-r] "
                      "[--force-overwrite/-f] file.xml file.rrd", argv[0]);
        return (-1);
    }

    rrd = parse_file(argv[optind]);

    if (rrd == NULL)
        return (-1);
    
    if (write_file(argv[optind + 1], rrd) != 0) {
        local_rrd_free(rrd);
        return (-1);
    }
    local_rrd_free(rrd);


    return (0);
}                       /* int rrd_restore */
Ejemplo n.º 10
0
int
rrd_parse_vdef(char *line, unsigned int *eaten, graph_desc_t *gdp, image_desc_t *im) {
    char tmpstr[MAX_VNAME_LEN+1];	/* vname\0 */
    int i=0;

    dprintf("- parsing '%s'\n",&line[*eaten]);
    if (rrd_parse_vname(line,eaten,gdp,im)) return 1;

    sscanf(&line[*eaten], DEF_NAM_FMT ",%n", tmpstr,&i);
    if (!i) {
	rrd_set_error("Cannot parse line '%s'",line);
	return 1;
    }
    if ((gdp->vidx=find_var(im,tmpstr))<0) {
	rrd_set_error("Not a valid vname: %s in line %s",tmpstr,line);
	return 1;
    }
    if (   im->gdes[gdp->vidx].gf != GF_DEF
	&& im->gdes[gdp->vidx].gf != GF_CDEF) {
	rrd_set_error("variable '%s' not DEF nor "
			"CDEF in VDEF '%s'", tmpstr,gdp->vname);
	return 1;
    }
    dprintf("- found vname: '%s' vidx %li\n",tmpstr,gdp->vidx);
    (*eaten)+=i;

    dprintf("- calling vdef_parse with param '%s'\n",&line[*eaten]);
    vdef_parse(gdp,&line[*eaten]);
    while (line[*eaten]!='\0'&&line[*eaten]!=':')
	(*eaten)++;

    return 0;
}
Ejemplo n.º 11
0
int set_hwsmootharg(
    rrd_t *rrd,
    enum cf_en cf,
    enum rra_par_en rra_par,
    char *arg)
{
    double    param;
    unsigned long i;
    signed short rra_idx = -1;

    /* read the value */
    param = atof(arg);
    /* in order to avoid smoothing of SEASONAL or DEVSEASONAL, we need to 
     * the 0.0 value*/
    if (param < 0.0 || param > 1.0) {
        rrd_set_error("Holt-Winters parameter must be between 0 and 1");
        return -1;
    }
    /* does the appropriate RRA exist?  */
    for (i = 0; i < rrd->stat_head->rra_cnt; ++i) {
        if (cf_conv(rrd->rra_def[i].cf_nam) == cf) {
            rra_idx = i;
            break;
        }
    }
    if (rra_idx == -1) {
        rrd_set_error("Holt-Winters RRA does not exist in this RRD");
        return -1;
    }

    /* set the value */
    rrd->rra_def[rra_idx].par[rra_par].u_val = param;
    return 0;
}
Ejemplo n.º 12
0
int rrd_close(
    rrd_file_t *rrd_file)
{
    rrd_simple_file_t *rrd_simple_file;
    rrd_simple_file = (rrd_simple_file_t *)rrd_file->pvt;
    int       ret;

#ifdef HAVE_MMAP
    ret = munmap(rrd_simple_file->file_start, rrd_file->file_len);
    if (ret != 0)
        rrd_set_error("munmap rrd_file: %s", rrd_strerror(errno));
#endif
#ifdef HAVE_LIBRADOS
    if (rrd_file->rados)
        ret = rrd_rados_close(rrd_file->rados);
    else
#endif
    ret = close(rrd_simple_file->fd);

    if (ret != 0)
        rrd_set_error("closing file: %s", rrd_strerror(errno));
    free(rrd_file->pvt);
    free(rrd_file);
    rrd_file = NULL;
    return ret;
}
Ejemplo n.º 13
0
time_t rrd_last(
    int argc,
    char **argv)
{
    char *opt_daemon = NULL;
    time_t lastupdate;

    optind = 0;
    opterr = 0;         /* initialize getopt */

    while (42) {
        int       opt;
        int       option_index = 0;
        static struct option long_options[] = {
            {"daemon", required_argument, 0, 'd'},
            {0, 0, 0, 0}
        };

        opt = getopt_long(argc, argv, "d:", long_options, &option_index);

        if (opt == EOF)
            break;

        switch (opt) {
        case 'd':
            if (opt_daemon != NULL)
                    free (opt_daemon);
            opt_daemon = strdup (optarg);
            if (opt_daemon == NULL)
            {
                rrd_set_error ("strdup failed.");
                return (-1);
            }
            break;

        default:
            rrd_set_error ("Usage: rrdtool %s [--daemon <addr>] <file>",
                    argv[0]);
            return (-1);
            break;
        }
    }                   /* while (42) */

    if ((argc - optind) != 1) {
        rrd_set_error ("Usage: rrdtool %s [--daemon <addr>] <file>",
                argv[0]);
        return (-1);
    }

    rrdc_connect (opt_daemon);
    if (rrdc_is_connected (opt_daemon))
        lastupdate = rrdc_last (argv[optind]);

    else
        lastupdate = rrd_last_r(argv[optind]);

    if (opt_daemon) free(opt_daemon);
    return (lastupdate);
}
Ejemplo n.º 14
0
int write_file(
    const char *file_name,
    rrd_t *rrd)
{
    FILE     *fh;

#ifdef HAVE_LIBRADOS
    if (strncmp("ceph//", file_name, 6) == 0) {
      return rrd_rados_create(file_name + 6, rrd);
    }
#endif

    if (strcmp("-", file_name) == 0)
        fh = stdout;
    else {
        int       fd_flags = O_WRONLY | O_CREAT;
        int       fd;

#if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
        fd_flags |= O_BINARY;
#endif

        if (opt_force_overwrite == 0)
            fd_flags |= O_EXCL;

        fd = open(file_name, fd_flags, 0666);
        if (fd == -1) {
            rrd_set_error("creating '%s': %s", file_name,
                          rrd_strerror(errno));
            return (-1);
        }

        fh = fdopen(fd, "wb");
        if (fh == NULL) {
            rrd_set_error("fdopen failed: %s", rrd_strerror(errno));
            close(fd);
            return (-1);
        }
    }

    int rc = write_fh(fh, rrd);

    /* lets see if we had an error */
    if (ferror(fh)) {
        rrd_set_error("a file error occurred while creating '%s': %s", file_name,
            rrd_strerror(errno));
        fclose(fh);
        if (strcmp("-", file_name) != 0)
            unlink(file_name);
        return (-1);
    }

    fclose(fh);
    
    return rc;
}
Ejemplo n.º 15
0
int lookup_seasonal(
    rrd_t *rrd,
    unsigned long rra_idx,
    unsigned long rra_start,
    rrd_file_t *rrd_file,
    unsigned long offset,
    rrd_value_t **seasonal_coef)
{
    unsigned long pos_tmp;

    /* rra_ptr[].cur_row points to the rra row to be written; this function
     * reads cur_row + offset */
    unsigned long row_idx = rrd->rra_ptr[rra_idx].cur_row + offset;

    /* handle wrap around */
    if (row_idx >= rrd->rra_def[rra_idx].row_cnt)
        row_idx = row_idx % (rrd->rra_def[rra_idx].row_cnt);

    /* rra_start points to the appropriate rra block in the file */
    /* compute the pointer to the appropriate location in the file */
    pos_tmp =
        rra_start +
        (row_idx) * (rrd->stat_head->ds_cnt) * sizeof(rrd_value_t);

    /* allocate memory if need be */
    if (*seasonal_coef == NULL)
        *seasonal_coef =
            (rrd_value_t *) malloc((rrd->stat_head->ds_cnt) *
                                   sizeof(rrd_value_t));
    if (*seasonal_coef == NULL) {
        rrd_set_error("memory allocation failure: seasonal coef");
        return -1;
    }

    if (!rrd_seek(rrd_file, pos_tmp, SEEK_SET)) {
        if (rrd_read
            (rrd_file, *seasonal_coef,
             sizeof(rrd_value_t) * rrd->stat_head->ds_cnt)
            == (ssize_t) (sizeof(rrd_value_t) * rrd->stat_head->ds_cnt)) {
            /* success! */
            /* we can safely ignore the rule requiring a seek operation between read
             * and write, because this read moves the file pointer to somewhere
             * in the file other than the next write location.
             * */
            return 0;
        } else {
            rrd_set_error("read operation failed in lookup_seasonal(): %lu\n",
                          pos_tmp);
        }
    } else {
        rrd_set_error("seek operation failed in lookup_seasonal(): %lu\n",
                      pos_tmp);
    }

    return -1;
}
Ejemplo n.º 16
0
static xmlChar* get_xml_text (
    xmlTextReaderPtr reader
    )
{
    while(xmlTextReaderRead(reader)){
        xmlChar  *ret;    
        xmlChar  *text;
        xmlChar  *begin_ptr;
        xmlChar  *end_ptr;
        int type;        
        type = xmlTextReaderNodeType(reader);
        if (type == XML_READER_TYPE_ELEMENT){
            xmlChar *name;
            name = xmlTextReaderName(reader);
            rrd_set_error("line %d: expected a value but found a <%s> element",
                          xmlTextReaderGetParserLineNumber(reader),
                          name);
            xmlFree(name);            
            return NULL;            
        }

        /* trying to read text from <a></a> we end up here
           lets return an empty string instead. This is a tad optimistic
           since we do not check if it is actually </a> and not </b>
           we got, but first we do not know if we expect </a> and second
           we the whole implementation is on the optimistic side. */
        if (type == XML_READER_TYPE_END_ELEMENT){
            return  xmlStrdup(BAD_CAST "");
        }        

        /* skip all other non-text */
        if (type != XML_READER_TYPE_TEXT)
            continue;
        
        text = xmlTextReaderValue(reader);

        begin_ptr = text;
        while ((begin_ptr[0] != 0) && (isspace(begin_ptr[0])))
            begin_ptr++;
        if (begin_ptr[0] == 0) {
            xmlFree(text);
            return xmlStrdup(BAD_CAST "");
        }        
        end_ptr = begin_ptr;
        while ((end_ptr[0] != 0) && (!isspace(end_ptr[0])))
            end_ptr++;
        end_ptr[0] = 0;
        
        ret = xmlStrdup(begin_ptr);
        xmlFree(text);
        return ret;
    }
    rrd_set_error("file ended while looking for text");
    return NULL;
}  /* get_xml_text */ 
Ejemplo n.º 17
0
static int _sql_setparam(struct sql_table_helper* th,char* key, char* value) {
    char* dbi_errstr=NULL;
    dbi_driver driver;
    /* if not connected */
    if (! th->conn) {
        /* initialize some stuff */
        th->table_next=th->table_start;
        th->result=NULL;
        th->connected=0;
        /* initialize db */
        if (getenv("RRDDEBUGSQL")) {
            fprintf(stderr,"RRDDEBUGSQL: %li: initialize libDBI\n",time(NULL) );
        }
        dbi_initialize(NULL);
        /* load the driver */
        driver=dbi_driver_open(th->dbdriver);
        if (! driver) {
            rrd_set_error( "libdbi - no such driver: %s (possibly a dynamic link problem of the driver being linked without -ldbi)",th->dbdriver);
            return -1;
        }
        /* and connect to driver */
        th->conn=dbi_conn_open(driver);
        /* and handle errors */
        if (! th->conn) {
            rrd_set_error( "libdbi - could not open connection to driver %s",th->dbdriver);
            dbi_shutdown();
            return -1;
        }
    }
    if (th->connected) {
        rrd_set_error( "we are already connected - can not set parameter %s=%s",key,value);
        _sql_close(th);
        return -1;
    }
    if (getenv("RRDDEBUGSQL")) {
        fprintf(stderr,"RRDDEBUGSQL: %li: setting option %s to %s\n",time(NULL),key,value );
    }
    if (strcmp(key, "port") == 0) {
        if (dbi_conn_set_option_numeric(th->conn,key,atoi(value))) {
            dbi_conn_error(th->conn,(const char**)&dbi_errstr);
            rrd_set_error( "libdbi: problems setting %s to %d - %s",key,value,dbi_errstr);
            _sql_close(th);
            return -1;
        }
    } else {
        if (dbi_conn_set_option(th->conn,key,value)) {
            dbi_conn_error(th->conn,(const char**)&dbi_errstr);
            rrd_set_error( "libdbi: problems setting %s to %s - %s",key,value,dbi_errstr);
            _sql_close(th);
            return -1;
        }
    }
    return 0;
}
Ejemplo n.º 18
0
time_t rrd_first(
    int argc,
    char **argv)
{
    struct optparse_long longopts[] = {
        {"rraindex", 129, OPTPARSE_REQUIRED},
        {"daemon", 'd', OPTPARSE_REQUIRED},
        {0},
    };
    struct    optparse options;
    int       opt;
    int       target_rraindex = 0;
    char     *endptr;
    char *opt_daemon = NULL;

    optparse_init(&options, argc, argv);
    while ((opt = optparse_long(&options, longopts, NULL)) != -1) {
        switch (opt) {
        case 129:
            target_rraindex = strtol(options.optarg, &endptr, 0);
            if (target_rraindex < 0) {
                rrd_set_error("invalid rraindex number");
                return (-1);
            }
            break;
        case 'd':
            if (opt_daemon != NULL)
                    free (opt_daemon);
            opt_daemon = strdup(options.optarg);
            if (opt_daemon == NULL)
            {
                rrd_set_error ("strdup failed.");
                return -1;
            }
            break;
        case '?':
            rrd_set_error("%s", options.errmsg);
            return -1;
        }
    }

    if (options.optind >= options.argc) {
        rrd_set_error("usage rrdtool %s [--rraindex number] [--daemon|-d <addr>] file.rrd",
                      options.argv[0]);
        return -1;
    }

    rrdc_connect (opt_daemon);
    if (rrdc_is_connected (opt_daemon)) {
      return rrdc_first(options.argv[options.optind], target_rraindex);
    } else {
      return rrd_first_r(options.argv[options.optind], target_rraindex);
	}
}
Ejemplo n.º 19
0
int
rrd_parse_find_gf(char *line, unsigned int *eaten, graph_desc_t *gdp) {
    char funcname[11],c1=0;
    int i=0;

    /* start an argument with DEBUG to be able to see how it is parsed */
    sscanf(&line[*eaten], "DEBUG%n", &i);
    if (i) {
	gdp->debug=1;
	(*eaten)+=i;
	i=0;
	dprintf("Scanning line '%s'\n",&line[*eaten]);
    }
    sscanf(&line[*eaten], "%10[A-Z]%n%c", funcname, &i, &c1);
    if (!i) {
	rrd_set_error("Could not make sense out of '%s'",line);
	return 1;
    }
    if ((int)(gdp->gf=gf_conv(funcname)) == -1) {
	rrd_set_error("'%s' is not a valid function name", funcname);
	return 1;
    } else {
	dprintf("- found function name '%s'\n",funcname);
    }

    if (gdp->gf == GF_LINE) {
	if (c1 == ':') {
	    gdp->linewidth=1;
	    dprintf("- - using default width of 1\n");
	} else {
	    double width;
	    (*eaten)+=i;
	    if (sscanf(&line[*eaten],"%lf%n:",&width,&i)) {
		if (width < 0 || isnan(width) || isinf(width) ) {
		    rrd_set_error("LINE width is %lf. It must be finite and >= 0 though",width);
		    return 1;
		}
		gdp->linewidth=width;
		dprintf("- - using width %f\n",width);
	    } else {
		rrd_set_error("LINE width: %s",line);
		return 1;
	    }
	}
    } else {
	if (c1 != ':') {
	    rrd_set_error("Malformed %s command: %s",funcname,line);
	    return 1;
	}
    }
    (*eaten)+=++i;
    return 0;
}
Ejemplo n.º 20
0
int addToBuffer(stringbuffer_t * sb,char* data,size_t len) {
    /* if len <= 0  we assume a string and calculate the length ourself */
    if (len<=0) {
        len=strlen(data);
    }
    /* if we have got a file, then take the shortcut */
    if (sb->file) {
        sb->len+=len;
        fwrite(data,len,1,sb->file);
        return 0;
    }
    /* if buffer is 0, then initialize */
    if (! sb->data) {
        /* make buffer a multiple of 8192 */
        sb->allocated+=8192;
        sb->allocated-=(sb->allocated%8192);
        /* and allocate it */
        sb->data = (unsigned char *) malloc(sb->allocated);
        if (! sb->data) {
            rrd_set_error("malloc issue");
            return 1;
        }
        /* and initialize the buffer */
        sb->len=0;
        sb->data[0]=0;
    }
    /* and figure out if we need to extend the buffer */
    if (sb->len+len+1>=sb->allocated) {
        /* add so many pages until we have a buffer big enough */
        while(sb->len+len+1>=sb->allocated) {
            sb->allocated+=8192;
        }
        /* try to resize it */
        unsigned char* resized=(unsigned char*)realloc(sb->data,sb->allocated);
        if (resized) {
            sb->data=resized;
        } else {
            free(sb->data);
            sb->data=NULL;
            sb->allocated=0;
            rrd_set_error("realloc issue");
            return -1;
        }
    }
    /* and finally add to the buffer */
    memcpy(sb->data+sb->len,data,len);
    sb->len+=len;
    /* and 0 terminate it */
    sb->data[sb->len]=0;
    /* and return */
    return 0;
}
Ejemplo n.º 21
0
static int parse_cvdef(enum gf_en gf,parsedargs_t*pa,image_desc_t *const im){
  /* get new graph that we fill */
  graph_desc_t *gdp=newGraphDescription(im,gf,pa,
					PARSE_VNAMERPN
					);
  if (!gdp) { return 1;}

  /* handle RPN parsing */
  if (gf==GF_CDEF) {
    /* parse rpn */
    if ((gdp->rpnp= rpn_parse((void *) im, gdp->rpn, &find_var_wrapper)) == NULL) {
      return 1; }
  } else { /* VDEF */
    /* parse vdef, as vdef_parse is a bit "stupid" right now we have to touch things here */
    /* so find first , */
    char*c=strchr(gdp->rpn,',');
    char vname[MAX_VNAME_LEN+1];
    if (! c) { rrd_set_error("Comma expected in VDEF definition %s",gdp->rpn); return 1;}
    /* found a comma, so copy the first part to ds_nam (re/abusing it) */
    *c=0; /* yes now it seems as if the string ended here */
    strncpy(vname,gdp->rpn,MAX_VNAME_LEN);
    *c=','; /* and now all is back to normal ... shudder */
    /* trying to find the vidx for that name */
    gdp->vidx = find_var(im, vname);
    if (gdp->vidx<0) { *c=',';
      rrd_set_error("Not a valid vname: %s in line %s", vname, gdp->rpn);
      return 1;}
    if (im->gdes[gdp->vidx].gf != GF_DEF && im->gdes[gdp->vidx].gf != GF_CDEF) {
      rrd_set_error("variable '%s' not DEF nor "
		    "CDEF in VDEF '%s'",vname, gdp->rpn);
      return 1;
    }
    /* and parsing the rpn */
    int r=vdef_parse(gdp, c+1);
    /* original code does not check here for some reason */
    if (r) { return 1; }
  }

  /* debugging output */
  dprintf("=================================\n");
  if (gf==GF_CDEF) {
    dprintf("CDEF  : %s\n",pa->arg_orig);
  } else {
    dprintf("VDEF  : %s\n",pa->arg_orig);
  }
  dprintf("VNAME : %s\n",gdp->vname);
  dprintf("RPN   : %s\n",gdp->rpn);
  dprintf("=================================\n");

  /* and return fine */
  return 0;
}
Ejemplo n.º 22
0
int rrd_restore(
    int argc,
    char **argv)
{
    struct optparse_long longopts[] = {
        {"range-check", 'r', OPTPARSE_NONE},
        {"force-overwrite", 'f', OPTPARSE_NONE},
        {0},
    };
    struct    optparse options;
    int       opt;
    rrd_t    *rrd;

    optparse_init(&options, argc, argv);
    while ((opt = optparse_long(&options, longopts, NULL)) != -1) {
        switch (opt) {
        case 'r':
            opt_range_check = 1;
            break;

        case 'f':
            opt_force_overwrite = 1;
            break;

        case '?':
            rrd_set_error("%s", options.errmsg);
            return -1;
        }
    } /* while (opt != -1) */

    if (options.argc - options.optind != 2) {
        rrd_set_error("usage rrdtool %s [--range-check|-r] "
                      "[--force-overwrite|-f] file.xml file.rrd",
                      options.argv[0]);
        return -1;
    }

    rrd = parse_file(options.argv[options.optind]);

    if (rrd == NULL)
        return (-1);
    
    if (write_file(options.argv[options.optind + 1], rrd) != 0) {
        local_rrd_free(rrd);
        return (-1);
    }
    local_rrd_free(rrd);


    return (0);
}                       /* int rrd_restore */
Ejemplo n.º 23
0
char* printstrftime(long argc, const char **args){
	struct	rrd_time_value start_tv, end_tv;
	char    *parsetime_error = NULL;
	char	formatted[MAX_STRFTIME_SIZE];
	struct tm *the_tm;
	time_t	start_tmp, end_tmp;

	/* Make sure that we were given the right number of args */
	if( argc != 4) {
		rrd_set_error( "wrong number of args %d", argc);
		return stralloc("");
	}

	/* Init start and end time */
	parsetime("end-24h", &start_tv);
	parsetime("now", &end_tv);

	/* Parse the start and end times we were given */
	if( (parsetime_error = parsetime( args[1], &start_tv))) {
		rrd_set_error( "start time: %s", parsetime_error);
		return stralloc("");
	}
	if( (parsetime_error = parsetime( args[2], &end_tv))) {
		rrd_set_error( "end time: %s", parsetime_error);
		return stralloc("");
	}
	if( proc_start_end( &start_tv, &end_tv, &start_tmp, &end_tmp) == -1) {
		return stralloc("");
	}

	/* Do we do the start or end */
	if( strcasecmp( args[0], "START") == 0) {
		the_tm = localtime( &start_tmp);
	}
	else if( strcasecmp( args[0], "END") == 0) {
		the_tm = localtime( &end_tmp);
	}
	else {
		rrd_set_error( "start/end not found in '%s'", args[0]);
		return stralloc("");
	}

	/* now format it */
	if( strftime( formatted, MAX_STRFTIME_SIZE, args[3], the_tm)) {
		return( stralloc( formatted));
	}
	else {
		rrd_set_error( "strftime failed");
		return stralloc("");
	}
}
Ejemplo n.º 24
0
rpnp_t   *rpn_expand(
    rpn_cdefds_t *rpnc)
{
    short     i;
    rpnp_t   *rpnp;

    /* DS_CDEF_MAX_RPN_NODES is small, so at the expense of some wasted
     * memory we avoid any reallocs */
    rpnp = (rpnp_t *) calloc(DS_CDEF_MAX_RPN_NODES, sizeof(rpnp_t));
    if (rpnp == NULL) {
        rrd_set_error("failed allocating rpnp array");
        return NULL;
    }
    for (i = 0; rpnc[i].op != OP_END; ++i) {
        rpnp[i].op = (enum op_en)rpnc[i].op;
	rpnp[i].extra = NULL;
	rpnp[i].free_extra = NULL;
        if (rpnp[i].op == OP_NUMBER) {
            rpnp[i].val = (double) rpnc[i].val;
        } else if (rpnp[i].op == OP_VARIABLE || rpnp[i].op == OP_PREV_OTHER) {
            rpnp[i].ptr = (long) rpnc[i].val;
        }
    }
    /* terminate the sequence */
    rpnp[i].op = OP_END;
    return rpnp;
}
Ejemplo n.º 25
0
static int get_xml_time_t(
    xmlTextReaderPtr reader,
    time_t *value)
{    
    xmlChar *text;
    time_t temp;    
    if ((text = get_xml_text(reader)) != NULL){
        errno = 0;        
#if SIZEOF_TIME_T == 4
        temp = strtol((char *)text,NULL, 0);
#elif SIZEOF_TIME_T == 8
        temp = strtoll((char *)text,NULL, 0);        
#else
#error "Don't know how to deal with TIME_T other than 4 or 8 bytes"
#endif    
        if (errno>0){
            rrd_set_error("ling %d: get_xml_time_t from '%s' %s",
                          xmlTextReaderGetParserLineNumber(reader),
                          text,rrd_strerror(errno));
            xmlFree(text);            
            return -1;
        }
        xmlFree(text);            
        *value = temp;
        return 0;
    }
    return -1;
} /* get_xml_time_t */
Ejemplo n.º 26
0
static int expect_element_end (
    xmlTextReaderPtr reader,
    char *exp_name)
{
    xmlChar *name;
    /* maybe we are already on the end element ... lets see */
    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT){
         xmlChar *temp;
         xmlChar *temp2;            
         temp = xmlTextReaderName(reader);
         temp2 = (xmlChar*)sprintf_alloc("/%s", temp);
         name = xmlStrdup(temp2);
         xmlFree(temp);
         free(temp2);            
    } else {     
         name = get_xml_element(reader);
    }

    if (name == NULL)
        return -1;    
    if (xmlStrcasecmp(name+1,(xmlChar *)exp_name) != 0 || name[0] != '/'){
        rrd_set_error("line %d: expected </%s> end element but found <%s>",
                      xmlTextReaderGetParserLineNumber(reader),exp_name,name);
        xmlFree(name);            
        return -1;            
    }
    xmlFree(name);    
    return 0;    
} /* expect_element_end */
Ejemplo n.º 27
0
int rrd_dump_opt_r(
    const char *filename,
    char *outname,
    int opt_noheader)
{
    FILE     *out_file;
    int       res;

    out_file = NULL;
    if (outname) {
        if (!(out_file = fopen(outname, "w"))) {
            return (-1);
        }
    } else {
        out_file = stdout;
    }

    res = rrd_dump_cb_r(filename, opt_noheader, rrd_dump_opt_cb_fileout, (void *)out_file);

    if (fflush(out_file) != 0) {
        rrd_set_error("error flushing output: %s", rrd_strerror(errno));
        res = -1;
    }
    if (out_file != stdout) {
        fclose(out_file);
        if (res != 0)
            unlink(outname);
    }

    return res;
}
Ejemplo n.º 28
0
/* allocate memory for string */
char     *sprintf_alloc(
    char *fmt,
    ...)
{
    char     *str = NULL;
    va_list   argp;
#ifdef HAVE_VASPRINTF
    va_start( argp, fmt );
    if (vasprintf( &str, fmt, argp ) == -1){
        va_end(argp);
        rrd_set_error ("vasprintf failed.");
        return(NULL);
    }
#else
    int       maxlen = 1024 + strlen(fmt);
    str = (char*)malloc(sizeof(char) * (maxlen + 1));
    if (str != NULL) {
        va_start(argp, fmt);
#ifdef HAVE_VSNPRINTF
        vsnprintf(str, maxlen, fmt, argp);
#else
        vsprintf(str, fmt, argp);
#endif
    }
#endif /* HAVE_VASPRINTF */
    va_end(argp);
    return str;
}
Ejemplo n.º 29
0
ssize_t rrd_write(
    rrd_file_t *rrd_file,
    const void *buf,
    size_t count)
{
    rrd_simple_file_t *rrd_simple_file = (rrd_simple_file_t *)rrd_file->pvt;
#ifdef HAVE_MMAP
    size_t old_size = rrd_file->file_len;
    if (count == 0)
        return 0;
    if (buf == NULL)
        return -1;      /* EINVAL */
    
    if((rrd_file->pos + count) > old_size)
    {
        rrd_set_error("attempting to write beyond end of file (%ld + %ld > %ld)",rrd_file->pos, count, old_size);
        return -1;
    }
    memcpy(rrd_simple_file->file_start + rrd_file->pos, buf, count);
    rrd_file->pos += count;
    return count;       /* mimmic write() semantics */
#else
    ssize_t   _sz = write(rrd_simple_file->fd, buf, count);

    if (_sz > 0)
        rrd_file->pos += _sz;
    return _sz;
#endif
}
Ejemplo n.º 30
0
off_t rrd_seek(
    rrd_file_t *rrd_file,
    off_t off,
    int whence)
{
    off_t     ret = 0;
#ifndef HAVE_MMAP
    rrd_simple_file_t *rrd_simple_file;
    rrd_simple_file = (rrd_simple_file_t *)rrd_file->pvt;
#endif
 
#ifdef HAVE_MMAP
    if (whence == SEEK_SET)
        rrd_file->pos = off;
    else if (whence == SEEK_CUR)
        rrd_file->pos += off;
    else if (whence == SEEK_END)
        rrd_file->pos = rrd_file->file_len + off;
#else
    ret = lseek(rrd_simple_file->fd, off, whence);
    if (ret < 0)
        rrd_set_error("lseek: %s", rrd_strerror(errno));
    rrd_file->pos = ret;
#endif
    /* mimic fseek, which returns 0 upon success */
    return ret < 0;     /*XXX: or just ret to mimic lseek */
}