/* A summary RRD has a "num" and a "sum" DS (datasource) whereas the host rrds only have "sum" (since num is always 1) */ static int push_data_to_rrd( char *rrd, const char *sum, const char *num, unsigned int step, unsigned int process_time, ganglia_slope_t slope) { int rval; int summary; struct stat st; /* if process_time is undefined, we set it to the current time */ if (!process_time) process_time = time(0); if (num) summary=1; else summary=0; if( stat(rrd, &st) ) { rval = RRD_create( rrd, summary, step, process_time, slope); if( rval ) return rval; } return RRD_update( rrd, sum, num, process_time ); }
/** * @brief 写数据到RRD数据库中 * @param host host名字 * @param metric metric的名字 * @param value 数值 * @param num 个数 * @param step 抽取间隔 * @return 成功返回0,失败返回-1 */ int c_rrd_handler::write_data_to_rrd( const char *host, const char *metric, const char *value, const char *num, unsigned int step) { if (!m_inited) { ERROR_LOG("c_rrd_handler has not been inited."); return -1; } if (NULL == host || NULL == metric || NULL == value) { ERROR_LOG("Parameter ERROR: host[%s], metric[%s], value[%s]", NULL == host ? "NULL" : host, NULL == metric ? "NULL" : metric, NULL == value ? "NULL" : value); return -1; } char rrd_file_path[PATH_MAX] = {'\0'}; int len = 0; int remain_len = PATH_MAX - 1; char *write_pos = rrd_file_path; //先把根目录拷进来 len = snprintf(write_pos, remain_len, "%s", m_rrd_base_dir); write_pos += len; remain_len -= len; // char * relative_name = write_pos - 1; //len = snprintf(write_pos, remain_len, "/clusters-data"); //write_pos += len; //remain_len -= len; //rrd_mkdir(rrd_file_path); len = snprintf(write_pos, remain_len, "/%s", host); write_pos += len; remain_len -= len; rrd_mkdir(rrd_file_path); snprintf(write_pos, remain_len, "/%s.rrd", metric); if(0 != access(rrd_file_path, F_OK)) { bool summary = (num != NULL) ? true : false; if(0 != RRD_create(rrd_file_path, summary, step)) { return -1; } } // *relative_name = '.'; // return RRD_update(relative_name, value, num); return RRD_update(rrd_file_path, value, num); }
/* A summary RRD has a "num" and a "sum" DS (datasource) whereas the host rrds only have "sum" (since num is always 1) */ static int push_data_to_rrd( char *rrd, const char *sum, const char *num, unsigned int step, unsigned int process_time, ganglia_slope_t slope) { int rval; int summary; struct stat st; /* if process_time is undefined, we set it to the current time */ if (!process_time) process_time = time(0); if (num) summary=1; else summary=0; /* XXX: cache the results of the stat once hash table is fixed. */ if( stat(rrd, &st) ) { rval = RRD_create( rrd, summary, step, process_time, slope); if( rval ) return rval; } if (gmetad_config.rrdcached_addrstr != NULL) { ganglia_scoreboard_inc(METS_SENT_RRDCACHED); ganglia_scoreboard_inc(METS_SENT_ALL); last_rrdcached = apr_time_now(); //Updating global variable return RRD_update_cached( rrd, sum, num, process_time ); } else { ganglia_scoreboard_inc(METS_SENT_RRDTOOL); ganglia_scoreboard_inc(METS_SENT_ALL); last_rrdtool = apr_time_now(); //Updating global variable return RRD_update( rrd, sum, num, process_time ); } }
int main(int argc, char **argv) { char dbpath[PATH_MAX] = "/tmp/tesla.rrd"; libusb_context *ctx = NULL; libusb_device_handle *dev_handle = NULL; signal(SIGINT, sigint_handler); int c = 0; while ((c = getopt(argc, argv, "hd")) != -1) { switch (c) { case 'd': ++_debug; break; case 'h': default: usage(); return 0; } } argc -= optind; argv += optind; if (argc == 1) realpath(argv[0], dbpath); if (access(dbpath, F_OK) == -1) { DPRINTF(1, "Creating db: %s\n", dbpath); if (RRD_create(dbpath, 60)) return -1; } DPRINTF(2, "Using DB: %s\n", dbpath); DPRINTF(1, "Please plug your CM160 device...\n"); if (scan_usb(ctx, &dev_handle)) return 1; if (prepare_device(dev_handle)) return 1; DPRINTF(1, "Start acquiring data...\n"); struct record_history *hist; if (init_history(&hist)) return 1; while (!stop) if (get_data(dbpath, hist, dev_handle)) stop = 1; DPRINTF(2, "\nClosing connection with the device\n"); if (libusb_release_interface(dev_handle, INTERFACE)) fprintf(stderr, "Cannot release interface.\n"); libusb_reset_device(dev_handle); libusb_close(dev_handle); libusb_exit(ctx); free_history(&hist); return 0; }