示例#1
0
文件: rrdstats.c 项目: pepito987/sgr
int update_rrd(char* file, long int last_up, data_t* dt)
{
	if ( dt )
	{
		char data[256];
		update_params[1] = file;
		sprintf(data, "%ld:%u:%u:%u:%u:%u:%u",
				last_up,
				dt->bytes,
				dt->ip_v4_bytes,
				dt->not_ip_bytes,
				dt->tcp_bytes,
				dt->udp_bytes,
				dt->other_bytes);
		update_params[2] = data;
		if (rrd_update(3, update_params))
		{
			fprintf(stderr, "rrd update failed: %s\n", rrd_get_error());
			rrd_clear_error();
		}
		else
				printf("%s updated!\n", file);
		bzero(dt, sizeof(data_t));
	}
	else
	    fprintf(stderr, "\nerror\n");
	return 0;
}
示例#2
0
文件: rrdstats.c 项目: pepito987/sgr
int create_rrd(char* file)
{
	struct stat st;
	if (file)
	{
		
		if (stat(file, &st))
		{   /* stats fails*/
			if (errno != ENOENT)
			{ /* error occurred */
				perror(file);
				return -1;
			}
		}
    /*  file exists  */
		create_args[3] = file;
		if (rrd_create(CREATE_ARGS, create_args))
		{
			fprintf(stderr, "unable to create file: %s\n", file);
			fprintf(stderr, "%s\n", rrd_get_error());
			rrd_clear_error();
			return -1;
		}
		printf("file %s created\n", file);
	}
	return 0;
}
示例#3
0
  Full documentation can be found at (graphv section):\n\
  http://oss.oetiker.ch/rrdtool/doc/rrdgraph.en.html";

static PyObject *
_rrdtool_graphv(PyObject *Py_UNUSED(self), PyObject *args)
{
    PyObject *ret;
    rrd_info_t *data;

    if (convert_args("graphv", args) == -1)
        return NULL;

    Py_BEGIN_ALLOW_THREADS
    data = rrd_graph_v(rrdtool_argc, rrdtool_argv);
    Py_END_ALLOW_THREADS

    if (data == NULL) {
        PyErr_SetString(rrdtool_OperationalError, rrd_get_error());
        rrd_clear_error();
        ret = NULL;
    } else {
        ret = _rrdtool_util_info2dict(data);
        rrd_info_free(data);
    }

    destroy_args();
    return ret;
}
示例#4
0
char     *drawgraph(
    long argc,
    const char **args)
{
    int       i, xsize, ysize;
    double    ymin, ymax;

    for (i = 0; i < argc; i++)
        if (strcmp(args[i], "--imginfo") == 0 || strcmp(args[i], "-g") == 0)
            break;
    if (i == argc) {
        args[argc++] = "--imginfo";
        args[argc++] = "<IMG SRC=\"./%s\" WIDTH=\"%lu\" HEIGHT=\"%lu\">";
    }
    calfree();
    if (rrd_graph
        (argc + 1, (char **) args - 1, &calcpr, &xsize, &ysize, NULL, &ymin,
         &ymax) != -1) {
        return stralloc(calcpr[0]);
    } else {
        if (rrd_test_error()) {
            const size_t len = strlen(rrd_get_error()) + DS_NAM_SIZE;
            char *err = malloc(len);
            snprintf(err, len, "[ERROR: %s]", rrd_get_error());
            rrd_clear_error();
            return err;
        }
    }
    return NULL;
}
示例#5
0
文件: Lua.cpp 项目: ctcble/ntopng
static int rrd_common_call (lua_State *L, const char *cmd,
			    RRD_FUNCTION rrd_function) {
  char **argv;
  int argc = lua_gettop(L) + 1;

  if(ntop->getGlobals()->isShutdown()) return(CONST_LUA_PARAM_ERROR);

  ntop->rrdLock(__FILE__, __LINE__);
  rrd_clear_error();
  argv = make_argv(cmd, L);
  reset_rrd_state();
  rrd_function(argc, argv);
  free(argv);
  if(rrd_test_error()) {
    char *err =  rrd_get_error();

    if(err != NULL) {
      /*
	IMPORTANT

	It is important to unlock now as if luaL_error is called the
	function returns and no unlock will take place
      */
      ntop->rrdUnlock(__FILE__, __LINE__);
      luaL_error(L, err);
    }
  }

  ntop->rrdUnlock(__FILE__, __LINE__);

  return 0;
}
示例#6
0
/**
 * \brief Update RRD stats file
 *
 * \param[in] stats Stats data
 * \param[in] templ RRD template
 */
void stats_update(stats_data *stats, std::string templ)
{
	std::vector<std::string> argv;

	/* Set RRD file */
	argv.push_back("update");
	argv.push_back(stats->file);

	/* Set template */
	argv.push_back("--template");
	argv.push_back(templ);

	/* Add counters */
	argv.push_back(stats_counters_to_string(stats->last, stats->fields));

	/* Create C style argv */
	const char **c_argv = new const char*[argv.size()];
	for (u_int16_t i = 0; i < argv.size(); ++i) {
		c_argv[i] = argv[i].c_str();
	}

	/* Update database */
	if (rrd_update(argv.size(), (char **) c_argv)) {
		MSG_ERROR(msg_module, "RRD Insert Error: %s", rrd_get_error());
		rrd_clear_error();
	}

	delete c_argv;
}
示例#7
0
void temperhum_rrd_update(char *fname, time_t tv_sec, struct sht1x_readings readings) {
	char update[4096];
	char buf[512];
	const char *args[1];

	args[0] = update;
	snprintf(update, 4096, "%llu", (unsigned long long)tv_sec);

	if (isnan(readings.temperature_celsius))
		strcat(update, ":U");
	else {
		snprintf(buf, 512, ":%.2f", readings.temperature_celsius);
		strcat(update, buf);
	}

	if (isnan(readings.relative_humidity))
		strcat(update, ":U");
	else {
		snprintf(buf, 512, ":%.2f", readings.relative_humidity);
		strcat(update, buf);
	}

	if (isnan(readings.dew_point))
		strcat(update, ":U");
	else {
		snprintf(buf, 512, ":%.2f", readings.dew_point);
		strcat(update, buf);
	}

	rrd_clear_error();
	if (rrd_update_r(fname, "tc:rh:dp", 1, args) != 0)
		fprintf(stderr, "%s: (%s) %s\n", fname, args[0], rrd_get_error());
}
示例#8
0
char     *printtimelast(
    long argc,
    const char **args)
{
    time_t    last;
    struct tm tm_last;
    char     *buf;

    if (argc == 2) {
        buf = malloc(255);
        if (buf == NULL) {
            return stralloc("[ERROR: allocating strftime buffer]");
        };
        /* not raising argc in step with args - 1 since the last argument
           will be used below for strftime  */

        last = rrd_last(argc, (char **) args - 1);
        if (rrd_test_error()) {
            const size_t len = strlen(rrd_get_error()) + DS_NAM_SIZE;
            char *err = malloc(len);
            snprintf(err, len, "[ERROR: %s]", rrd_get_error());
            rrd_clear_error();
            return err;
        }
        tm_last = *localtime(&last);
        strftime(buf, 254, args[1], &tm_last);
        return buf;
    }
    return stralloc("[ERROR: expected <RRD::TIME::LAST file.rrd strftime-format>]");
}
示例#9
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 */
示例#10
0
char* printtimelast(long argc, const char **args) {
  time_t last;
  struct tm tm_last;
  char *buf;
  if ( argc == 2 ) {
    buf = malloc(255);
    if (buf == NULL){	
	return stralloc("[ERROR: allocating strftime buffer]");
    };
    last = rrd_last(argc+1, (char **) args-1); 
    if (rrd_test_error()) {
      char *err = malloc((strlen(rrd_get_error())+DS_NAM_SIZE)*sizeof(char));
      sprintf(err, "[ERROR: %s]",rrd_get_error());
      rrd_clear_error();
      return err;
    }
    tm_last = *localtime(&last);
    strftime(buf,254,args[1],&tm_last);
    return buf;
  }
  if ( argc < 2 ) {
    return stralloc("[ERROR: too few arguments for RRD::TIME::LAST]");
  }
  return stralloc("[ERROR: not enough arguments for RRD::TIME::LAST]");
}
示例#11
0
static PyObject *
_rrdtool_updatev(PyObject *Py_UNUSED(self), PyObject *args)
{
    PyObject *ret;
    rrd_info_t *data;

    if (convert_args("updatev", args) == -1)
        return NULL;

    Py_BEGIN_ALLOW_THREADS
    data = rrd_update_v(rrdtool_argc, rrdtool_argv);
    Py_END_ALLOW_THREADS

    if (data == NULL) {
        PyErr_SetString(rrdtool_OperationalError, rrd_get_error());
        rrd_clear_error();
        ret = NULL;
    } else {
        ret = _rrdtool_util_info2dict(data);
        rrd_info_free(data);
    }

    destroy_args();
    return ret;
}
示例#12
0
文件: rrd.c 项目: paivaspol/tstat
void
rrdtool_create_command (const char *name, const char *what)
{
  // rrd_create needs the time of the first entry, so it
  // called at the time of the first update; therefore, we
  // need to backup the original rrd entry -- in order not 
  // overwrite the update command contained in rrd.cmd  
  rrd_struct temp_rrd = rrd;

  if (RRD_DEBUG)
    fprintf (fp_stderr, "rrdtool: create(%s,%s)\n", name, what);
#ifdef RRD_TREE
  sprintf (temp_rrd.file, "%s/%s", rrd.conf.path, name, what);
  struct stat fbuf;
  if (!((stat (temp_rrd.file, &fbuf) == 0) && S_ISDIR (fbuf.st_mode)))
    {
      mkdir (temp_rrd.file, 0775);
      if (debug > 1)
	fprintf (fp_stderr, "RRDtool database path <%s> created\n",
		 temp_rrd.file);
    }
  sprintf (temp_rrd.file, "%s/%s/%s.rrd", rrd.conf.path, name, what);
#else
  sprintf (temp_rrd.file, "%s/%s.%s.rrd", rrd.conf.path, name, what);
#endif

  rrd.time_update = (unsigned long) current_time.tv_sec;

  if (stat (temp_rrd.file, &temp_rrd.fbuf) == 0)
    {
      if (debug > 1)
	fprintf (fp_stderr, "rrdtool: skip create <%s> ... already existent\n",
		 temp_rrd.file);
      return;			/* already called ? */
    }


  /* MTRG-like behavior for on-line usage */
  sprintf (temp_rrd.cmd,
        "create %s --step %lu --start %ld DS:%s:GAUGE:%lu:U:U %s %s %s %s",
        temp_rrd.file, (unsigned long) (GLOBALS.Max_Time_Step / 1000000),
        (long) rrd.time_update - 10, name, (unsigned long) (GLOBALS.Max_Time_Step / 500000),
	   RRA_DAILY, RRA_WEEKLY, RRA_MONTHLY, RRA_YEARLY);
  if (debug > 1)
    fprintf (fp_stderr, "rrdtool: rrd_create('%s')\n", temp_rrd.cmd);

  optind = 0;
  opterr = 0;
  rrdtool_str2argv (temp_rrd.cmd);
  rrd_create (rrd.argc, rrd.argv);

  if (rrd_test_error ())
    {
      fprintf (fp_stderr, "rrdtool: create command:\n%s\n", temp_rrd.cmd);
      fprintf (fp_stderr, "rrdtool: create error!\n%s\n", rrd_get_error ());
      if (temp_rrd.fatal)
	exit (1);
      rrd_clear_error ();
    }
}
示例#13
0
static void throwException
(JNIEnv *env, char *exception, char *msg) {
  int rc = (*env)->ThrowNew(env, (*env)->FindClass(env, exception), msg);
  if(rc < 0)  /* couldn't find exception class */
    (*env)->ThrowNew(env, (*env)->FindClass(env, "java/lang/Exception"), msg);
  rrd_clear_error();
}
示例#14
0
文件: test-rrd.c 项目: tjyang/abmon
int main(int argc, char *argv[])
{
    char *rrdargs[] = {
        "rrdgraph",
        "bbgen.png",
        "-s", "e - 48d",
        "--title", "bbgen runtime last 48 days",
        "-w576",
        "-v", "Seconds",
        "-a", "PNG",
        "DEF:rt=bbgen.rrd:runtime:AVERAGE",
        "AREA:rt#00CCCC:Run Time",
        "COMMENT: Timestamp",
        NULL
    };
    char **calcpr=NULL;

    int pcount, result, xsize, ysize;
    double ymin, ymax;

    for (pcount = 0; (rrdargs[pcount]); pcount++);
    rrd_clear_error();
#ifdef RRDTOOL12
    result = rrd_graph(pcount, rrdargs, &calcpr, &xsize, &ysize, NULL, &ymin, &ymax);
#else
    result = rrd_graph(pcount, rrdargs, &calcpr, &xsize, &ysize);
#endif

    return 0;
}
示例#15
0
static int
RRD_update( char *rrd, const char *sum, const char *num, unsigned int process_time )
{
    char *argv[3];
    int   argc = 3;
    char val[128];

    /* If we are a host RRD, we "sum" over only one host. */
    if (num)
        sprintf(val, "%d:%s:%s", process_time, sum, num);
    else
        sprintf(val, "%d:%s", process_time, sum);

    argv[0] = "dummy";
    argv[1] = rrd;
    argv[2] = val;

    pthread_mutex_lock( &rrd_mutex );
    optind=0;
    opterr=0;
    rrd_clear_error();
    rrd_update(argc, argv);
    if(rrd_test_error())
    {
        err_msg("RRD_update (%s): %s", rrd, rrd_get_error());
        pthread_mutex_unlock( &rrd_mutex );
        return 0;
    }
    /* debug_msg("Updated rrd %s with value %s", rrd, val); */
    pthread_mutex_unlock( &rrd_mutex );
    return 0;
}
示例#16
0
文件: main.c 项目: Distrotech/rrdtool
void reset_rrd_state(
    )
{
    optind = 0;
    opterr = 0;
    rrd_clear_error();
}
示例#17
0
static int srrd_create(const char *filename, /* {{{ */
                       unsigned long pdp_step, time_t last_up, int argc,
                       const char **argv) {
  int status;
  char *filename_copy;

  if ((filename == NULL) || (argv == NULL))
    return -EINVAL;

  /* Some versions of librrd don't have the `const' qualifier for the first
   * argument, so we have to copy the pointer here to avoid warnings. It sucks,
   * but what else can we do? :(  -octo */
  filename_copy = strdup(filename);
  if (filename_copy == NULL) {
    ERROR("srrd_create: strdup failed.");
    return -ENOMEM;
  }

  optind = 0; /* bug in librrd? */
  rrd_clear_error();

  status = rrd_create_r(filename_copy, pdp_step, last_up, argc, (void *)argv);

  if (status != 0) {
    P_WARNING("srrd_create: rrd_create_r (%s) failed: %s", filename,
              rrd_get_error());
  }

  sfree(filename_copy);

  return status;
} /* }}} int srrd_create */
示例#18
0
/** 
 * @brief  创建RRD数据库
 * @param   rrd_file     rrd数据库路径
 * @param   summary 标志位是否summary
 * @param   step    步长
 * @return  成功返回0,失败返回-1
 */
int c_rrd_handler::RRD_create(const char *rrd_file, bool summary, unsigned int step)
{
    /* Warning: RRD_create will overwrite a RRD if it already exists */
    int  argc = 0;
    const char *argv[128] = {NULL};
    const char *data_source_type = "GAUGE";
    int  heartbeat = 30;
    char sstep[16] = {'\0'};
    char sstart[64] = {'\0'};
    char ssum[64] = {'\0'};
    char snum[64] = {'\0'};

    /* Our heartbeat is twice the step interval. */
    heartbeat = 2 * step;


    argv[argc++] = "dummy";
    argv[argc++] = rrd_file;
    argv[argc++] = "--step";
    sprintf(sstep, "%u", step);
    argv[argc++] = sstep;
    argv[argc++] = "--start";
    sprintf(sstart, "%lu", time(NULL) - 1);
    argv[argc++] = sstart;
    sprintf(ssum, "DS:sum:%s:%d:U:U", data_source_type, heartbeat);
    argv[argc++] = ssum;

    if(summary)
    {
        sprintf(snum, "DS:num:%s:%d:U:U", data_source_type, heartbeat);
        argv[argc++] = snum;
    }

    if (g_is_switch_rrd)
    {
        GEN_SWITCH_RRA(argc, argv);
    }
    else
    {
        GEN_SERVER_RRA(argc, argv);
    }

    optind = 0;
    optopt = 0;
    opterr = 0;
    optarg = NULL;
    rrd_clear_error();

    rrd_create(argc, (char**)argv);

    if(rrd_test_error())
    {
        ERROR_LOG("RRD_create error: %s", rrd_get_error());
        return -1;
    }

    DEBUG_LOG("CREATE RRD[%s]", rrd_file);
    return 0;
}
示例#19
0
文件: rrd.c 项目: paivaspol/tstat
void * call_rrd_update(void*arg){
	char buffer [2000];
	int i = 0;
	time_t t;
	srand((unsigned) time(&t));
	
	/* Set Cpu and I/O low priority 
	struct sched_param sc_par;
	sc_par.sched_priority = 0;
	pthread_setschedparam(pthread_self(), SCHED_OTHER, &sc_par);
	setpriority(PRIO_PROCESS, 0, 19);
	syscall(SYS_ioprio_set,1, 0,  (2<< 13)| 7);*/

	/* Open pipe as a stream */
	FILE * buffer_pipe = fdopen(command_pipe[0], "r");
	rrd_clear_error ();
	/* Infinite loop on pipe */
	while(1){
		usleep(10000);
		fgets(buffer,2000,buffer_pipe);

		/* Replace \n with \0 */
		buffer[strlen(buffer)-1]= '\0';

		/* Execute update */
		rrdtool_str2argv(buffer);
		optind = 0; 
		opterr = 0;
		rrd_update (rrd.argc, rrd.argv);

		//if (i++%1000 == 0)printf("RRD\n"); 
	
		/* Check errors */
		if (rrd_test_error ())
		{
			fprintf (fp_stderr, "rrdtool: update command:\n'%s'\n", buffer);
			fprintf (fp_stderr, "rrdtool: update error!\n%s\n", rrd_get_error ());
			if (rrd.fatal)
				exit (1);
			rrd_clear_error ();
		}
		
	}
	return NULL;
}
/*
 *  Handles the GET requests by getting RRD to graph and sending the result
 * as an HTTP response.
 */
static ngx_int_t ngx_http_rrd_show_graph(ngx_http_request_t *r)
{
    ngx_log_t* log = r->connection->log;
    ngx_http_rrd_loc_conf_t *rrd_conf;
    rrd_conf = ngx_http_get_module_loc_conf(r, ngx_http_rrd_module);

    /* Prepare args for rrdgraph */
    int rrd_argc = -1;
    char** rrd_arg = ngx_http_rrd_create_graph_arg(&rrd_argc, r,
                                                   rrd_conf->db_name_cstyle);
    if (NULL == rrd_arg) {
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }
    rrd_clear_error();
    rrd_info_t* const rrd_graph_info = rrd_graph_v(rrd_argc, rrd_arg);
    ngx_log_debug(NGX_LOG_DEBUG_HTTP, log, 0,
                  "rrd_graph (%s, %s, %s, %s) returned %p.",
                  rrd_arg[0], rrd_arg[1], rrd_arg[2], rrd_arg[3],
                  rrd_graph_info);
    if (NULL == rrd_graph_info) {
        ngx_http_rrd_free_graph_arg(r->pool, rrd_argc, rrd_arg);
        return ngx_http_rrd_outprintf(r, NGX_HTTP_INTERNAL_SERVER_ERROR,
                            "Error graphing DB %s: %s", rrd_conf->db_name_cstyle,
                            rrd_get_error());
    }
    rrd_info_t* it;
    u_char* image_data = NULL;
    size_t image_size = 0;
    for(it = rrd_graph_info; NULL == image_data && it != NULL; it = it->next) {
        if (it->key != NULL
            && ngx_strncmp(it->key, "image", sizeof("image")) == 0) {
            image_data = it->value.u_blo.ptr;
            image_size = it->value.u_blo.size;
        }
    }
    if (NULL == image_data) {
        ngx_http_rrd_free_graph_arg(r->pool, rrd_argc, rrd_arg);
        rrd_info_free(rrd_graph_info);
        return ngx_http_rrd_outprintf(r, NGX_HTTP_INTERNAL_SERVER_ERROR,
                            "No image returned graphing DB %s.",
                            rrd_conf->db_name_cstyle);
    }
    ngx_chain_t* out_chain = ngx_http_rrd_get_picture_outchain(r->pool,
                                                      image_data, image_size);
    if (NULL == out_chain) {
        ngx_http_rrd_free_graph_arg(r->pool, rrd_argc, rrd_arg);
        rrd_info_free(rrd_graph_info);
        return ngx_http_rrd_outprintf(r, NGX_HTTP_INTERNAL_SERVER_ERROR,
                            "Problem returning image for DB %s.",
                            rrd_conf->db_name_cstyle);
    }
    ngx_http_rrd_free_graph_arg(r->pool, rrd_argc, rrd_arg);
    rrd_info_free(rrd_graph_info);
    return ngx_http_rrd_png_file_200(r, out_chain);
}
示例#21
0
int rrdc_connect (const char *addr) /* {{{ */
{
  int status = 0;

  if (addr == NULL) {
    addr = getenv (ENV_RRDCACHED_ADDRESS);
  }

  if (addr == NULL || strcmp(addr,"") == 0 ) {
    addr = NULL;
    return 0;   
  }

  pthread_mutex_lock(&lock);

  if (sd >= 0 && sd_path != NULL && strcmp(addr, sd_path) == 0)
  {
    /* connection to the same daemon; use cached connection */
    pthread_mutex_unlock (&lock);
    return (0);
  }
  else
  {
    close_connection();
  }

  rrd_clear_error ();
  if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
    status = rrdc_connect_unix (addr + strlen ("unix:"));
  else if (addr[0] == '/')
    status = rrdc_connect_unix (addr);
  else
    status = rrdc_connect_network(addr);

  if (status == 0 && sd >= 0)
    sd_path = strdup(addr);
  else
  {
    char *err = rrd_test_error () ? rrd_get_error () : "Internal error";
    /* err points the string that gets written to by rrd_set_error(), thus we
     * cannot pass it to that function */
    err = strdup (err);
    rrd_set_error("Unable to connect to rrdcached: %s",
                  (status < 0)
                  ? (err ? err : "Internal error")
                  : rrd_strerror (status));
    if (err != NULL)
      free (err);
  }

  pthread_mutex_unlock (&lock);
  return (status);
} /* }}} int rrdc_connect */
示例#22
0
/* #endif HAVE_THREADSAFE_LIBRRD */

#else /* !HAVE_THREADSAFE_LIBRRD */
static int srrd_create (const char *filename, /* {{{ */
                        unsigned long pdp_step, time_t last_up,
                        int argc, const char **argv)
{
    int status;

    int new_argc;
    char **new_argv;

    char pdp_step_str[16];
    char last_up_str[16];

    new_argc = 6 + argc;
    new_argv = (char **) malloc ((new_argc + 1) * sizeof (char *));
    if (new_argv == NULL)
    {
        ERROR ("rrdtool plugin: malloc failed.");
        return (-1);
    }

    if (last_up == 0)
        last_up = time (NULL) - 10;

    ssnprintf (pdp_step_str, sizeof (pdp_step_str), "%lu", pdp_step);
    ssnprintf (last_up_str, sizeof (last_up_str), "%u", (unsigned int) last_up);

    new_argv[0] = "create";
    new_argv[1] = (void *) filename;
    new_argv[2] = "-s";
    new_argv[3] = pdp_step_str;
    new_argv[4] = "-b";
    new_argv[5] = last_up_str;

    memcpy (new_argv + 6, argv, argc * sizeof (char *));
    new_argv[new_argc] = NULL;

    pthread_mutex_lock (&librrd_lock);
    optind = 0; /* bug in librrd? */
    rrd_clear_error ();

    status = rrd_create (new_argc, new_argv);
    pthread_mutex_unlock (&librrd_lock);

    if (status != 0)
    {
        WARNING ("rrdtool plugin: rrd_create (%s) failed: %s",
                 filename, rrd_get_error ());
    }

    sfree (new_argv);

    return (status);
} /* }}} int srrd_create */
示例#23
0
int main(
    int argc,
    char **argv)
{
    char *name=basename(argv[0]);
    rrd_info_t *info;

    if (!strcmp(name, "rrdcreate")) {
        rrd_create(argc, argv);
    }
    else if (!strcmp(name, "rrdinfo")) {
         info=rrd_info(argc, argv);
         rrd_info_print(info);
         rrd_info_free(info);
    }
    else {
        rrd_update(argc, argv);
    }

    if (rrd_test_error()) {
         printf("RRDtool " PACKAGE_VERSION
               "  Copyright by Tobi Oetiker, 1997-2010\n\n");
        if (!strcmp(name, "rrdcreate")) {
            printf("Usage: rrdcreate <filename>\n"
                   "\t\t\t[--start|-b start time]\n"
                   "\t\t\t[--step|-s step]\n"
                   "\t\t\t[--no-overwrite]\n"
                   "\t\t\t[DS:ds-name:DST:dst arguments]\n"
                   "\t\t\t[RRA:CF:cf arguments]\n\n");
       }
       else if (!strcmp(name, "rrdinfo")) {
           printf("Usage: rrdinfo <filename>\n");
       }
       else {
            printf("Usage: rrdupdate <filename>\n"
                   "\t\t\t[--template|-t ds-name[:ds-name]...]\n"
                   "\t\t\t[--skip-past-updates]\n"
                   "\t\t\ttime|N:value[:value...]\n\n"
                   "\t\t\tat-time@value[:value...]\n\n"
                   "\t\t\t[ time:value[:value...] ..]\n\n");
       }

       printf("ERROR: %s\n", rrd_get_error());
       rrd_clear_error();
       return 1;
   }
    return 0;
}
示例#24
0
文件: rrd.c 项目: paivaspol/tstat
void
rrdtool_update_command (const char *name, const char *what, double val)
{
  if (RRD_DEBUG)
    fprintf (fp_stderr, "rrdtool: update(%s,%s,%f)\n", name, what, val);
#ifdef RRD_TREE
  sprintf (rrd.file, "%s/%s/%s.rrd", rrd.conf.path, name, what);
#else
  sprintf (rrd.file, "%s/%s.%s.rrd", rrd.conf.path, name, what);
#endif

  /* at the first call of this function, all the needed rrd 
     files are created with the start time set appropriately */
  if (stat (rrd.file, &rrd.fbuf) == -1)
    rrdtool_create_all ();

  if (rrd.time_update == 0)
    rrd.time_update = (unsigned long) current_time.tv_sec;

#ifdef RRD_THREADED
  sprintf (rrd.cmd, "update %s %ld:%f\n", rrd.file, rrd.time_update, val);
  if (RRD_DEBUG)
    fprintf (fp_stderr, "rrdtool: rrd_update('%s')\n", rrd.cmd);
  
  write (command_pipe[1],rrd.cmd, strlen(rrd.cmd) );
#else
  sprintf (rrd.cmd, "update %s %ld:%f", rrd.file, rrd.time_update, val);
  if (RRD_DEBUG)
    fprintf (fp_stderr, "rrdtool: rrd_update('%s')\n", rrd.cmd);

  optind = 0;
  opterr = 0;
  rrdtool_str2argv (rrd.cmd);
  rrd_update (rrd.argc, rrd.argv);

  if (rrd_test_error ())
    {
      fprintf (fp_stderr, "rrdtool: update command:\n%s\n", rrd.cmd);
      fprintf (fp_stderr, "rrdtool: update error!\n%s\n", rrd_get_error ());
      if (rrd.fatal)
	exit (1);
      rrd_clear_error ();
    }
#endif
  
}
示例#25
0
static PyObject * _RRD_call(PyObject *self, PyObject *args) {

    char **argv;
    int argc, rc;

    argv = parse_args(args, &argc);
    if (!argv)
        return NULL;

    if (!strcmp("create", argv[0])) 
        rc = rrd_create(argc, argv);
    else if (!strcmp("update", argv[0]))
        rc = rrd_update(argc, argv);
    else if (!strcmp("restore", argv[0]))
        rc = rrd_restore(argc, argv);
    else if (!strcmp("dump", argv[0]))
        rc = rrd_dump(argc, argv);
    else if (!strcmp("tune", argv[0]))
        rc = rrd_tune(argc, argv);
    else if (!strcmp("last", argv[0]))
        rc = rrd_last(argc, argv);
    else if (!strcmp("resize", argv[0]))
        rc = rrd_resize(argc, argv);
    else if (!strcmp("fetch", argv[0])) 
        return _RRD_fetch(argc, argv);
    else if (!strcmp("graph", argv[0])) {
        return _RRD_graph(argc, argv);
    }
    else {
        PyMem_DEL(argv);
        PyErr_SetString(PyExc_TypeError,
                        "invalid action");
        return NULL;
    }
    
    if (rc == -1) {
        PyMem_DEL(argv);
        PyErr_SetString(PyExc_ValueError,
                        rrd_get_error());
        rrd_clear_error();
        return NULL;
    }
    
    PyMem_DEL(argv);
    return PyLong_FromLong(rc);
}
示例#26
0
static PyObject *PyRRD_graph(
    PyObject UNUSED(*self),
    PyObject * args)
{
    PyObject *r;
    char    **argv, **calcpr;
    int       argc, xsize, ysize, i;
    double    ymin, ymax;

    if (create_args("graph", args, &argc, &argv) < 0)
        return NULL;

    if (rrd_graph(argc, argv, &calcpr, &xsize, &ysize, NULL, &ymin, &ymax) ==
        -1) {
        PyErr_SetString(ErrorObject, rrd_get_error());
        rrd_clear_error();
        r = NULL;
    } else {
        r = PyTuple_New(3);

        PyTuple_SET_ITEM(r, 0, PyInt_FromLong((long) xsize));
        PyTuple_SET_ITEM(r, 1, PyInt_FromLong((long) ysize));

        if (calcpr) {
            PyObject *e, *t;

            e = PyList_New(0);
            PyTuple_SET_ITEM(r, 2, e);

            for (i = 0; calcpr[i]; i++) {
                t = PyString_FromString(calcpr[i]);
                PyList_Append(e, t);
                Py_DECREF(t);
                rrd_freemem(calcpr[i]);
            }
            rrd_freemem(calcpr);
        } else {
            Py_INCREF(Py_None);
            PyTuple_SET_ITEM(r, 2, Py_None);
        }
    }

    destroy_args(&argv);
    return r;
}
示例#27
0
/** 
 * @brief   更新RRD数据库
 * @param   rrd_file  rrd数据库路径
 * @param   value  数值
 * @param   num  个数
 * @return  成功返回0,失败返回-1
 */
int c_rrd_handler::RRD_update(const char *rrd_file, const char *value, const char *num)
{
    int argc = 3;
    const char *argv[4] = {NULL};
    char val[128] = {'\0'};

    if(NULL == rrd_file || NULL == value)
    {
        ERROR_LOG("RRD path OR Value is NULL.");
        return -1;
    }

    // if(access(rrd_file, F_OK | R_OK | W_OK) != 0) {
        // ERROR_LOG("Can not access rrd_file[%s]: [%s]", rrd_file, strerror(errno));
        // return -1;
    // }

    if(num)
    {
        snprintf(val, sizeof(val) - 1, "N:%s:%s", value, num);
    }
    else
    {
        snprintf(val, sizeof(val) - 1, "N:%s", value);
    }

    argv[0] = "dummy";
    argv[1] = rrd_file;
    argv[2] = val; 

    optind = 0;
    optopt = 0;
    opterr = 0;
    optarg = NULL;
    rrd_clear_error();

    rrd_update(argc, (char**)argv);
    if(rrd_test_error())
    {
        ERROR_LOG("ERROR: RRD_update(%s): %s", rrd_file, rrd_get_error());
        return -1;
    } 

    return 0;
}
示例#28
0
void
rrdtool_init ()
{
  int i;

  rrd_clear_error ();
  rrd.fatal = 1;
  rrdtool_parse_config ();
  rrd.time_update = 0;

  for (i = 0; i < MAX_RRD_ARGV; i++)
    rrd.argv[i] = (char *) malloc (512);

  /* we rather want to create this now...    */
  /* but we need to wait for the first entry */
  /* so this is deferred to update           */
  /*            rrdtool_create_all();        */
}
示例#29
0
int eat_tag(char **buf, char *tag){ 
  if ((*buf)==NULL) return -1;   /* fall though clause */

  rrd_clear_error();
  skip(buf);
  if ((**buf)=='<' 
      && strncmp((*buf)+1,tag,strlen(tag)) == 0 
      && *((*buf)+strlen(tag)+1)=='>') {
    (*buf) += strlen(tag)+2;
  }
  else {
    rrd_set_error("No <%s> tag found",tag);
    (*buf) = NULL;
    return -1;
  }
  skip(buf);
  return 1;
}
示例#30
0
char* includefile(long argc, const char **args){
  char *buffer;
  if (argc >= 1) {
      const char* filename = args[0];
      readfile(filename, &buffer, 0);
      if (rrd_test_error()) {
	  	char *err = malloc((strlen(rrd_get_error())+DS_NAM_SIZE));
	  sprintf(err, "[ERROR: %s]",rrd_get_error());
	  rrd_clear_error();
	  return err;
      } else {
       return buffer;
      }
  }
  else
  {
      return stralloc("[ERROR: No Inclue file defined]");
  }
}