|
|
|
@ -48,7 +48,7 @@ struct rra_cb_data {
|
|
|
|
|
int *pos; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
static int append_rra_config_cb(const char *parameter, void *privdata) |
|
|
|
|
static int append_rra_config(const char *parameter, void *privdata) |
|
|
|
|
{ |
|
|
|
|
struct rra_cb_data *data = (struct rra_cb_data *)privdata; |
|
|
|
|
|
|
|
|
@ -62,114 +62,78 @@ static int append_rra_config_cb(const char *parameter, void *privdata)
|
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static int append_rra_config(char *buffer, int size, int *pos) |
|
|
|
|
static int do_rrd(int (*rrd_func)(int, char **), char *cmd) |
|
|
|
|
{ |
|
|
|
|
struct rra_cb_data data = { |
|
|
|
|
.buffer = buffer, |
|
|
|
|
.size = size, |
|
|
|
|
.pos = pos, |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
return config_get_strings("global", "rra", append_rra_config_cb, &data); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static int do_rrd(int mode, const char *cmd) |
|
|
|
|
{ |
|
|
|
|
int argc; |
|
|
|
|
char *argv[ARGCMAX]; |
|
|
|
|
|
|
|
|
|
argc = strsplit(cmd, " \t\n", argv, ARGCMAX -1); |
|
|
|
|
int argc = strsplit(cmd, " \t\n", argv, ARGCMAX -1); |
|
|
|
|
argv[argc] = NULL; |
|
|
|
|
|
|
|
|
|
optind = 0; |
|
|
|
|
rrd_clear_error(); |
|
|
|
|
|
|
|
|
|
if (mode == RRDTOOL_CREATE) { |
|
|
|
|
if (rrd_create(argc, argv) == -1) { |
|
|
|
|
errno = 0; |
|
|
|
|
log_print(LOG_ERROR, "rrd_create failed: %s: %s", |
|
|
|
|
argv[1], rrd_get_error()); |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} else if (mode == RRDTOOL_UPDATE) { |
|
|
|
|
if (rrd_update(argc, argv) == -1) { |
|
|
|
|
errno = 0; |
|
|
|
|
log_print(LOG_ERROR, "rrd_update failed: %s: %s", |
|
|
|
|
argv[1], rrd_get_error()); |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
int retval = rrd_func(argc, argv); |
|
|
|
|
if (retval == -1) { |
|
|
|
|
errno = 0; |
|
|
|
|
log_print(LOG_ERROR, "rrd_func failed: %s", rrd_get_error()); |
|
|
|
|
} |
|
|
|
|
return 0; |
|
|
|
|
|
|
|
|
|
free(cmd); |
|
|
|
|
return retval; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static int rrd_create_file(const char *filename, const char *plugin_name, int ds_id) |
|
|
|
|
static int rrd_create_file(const char *filename, struct sammler_plugin *plugin, int ds_id) |
|
|
|
|
{ |
|
|
|
|
struct sammler_plugin *plugin; |
|
|
|
|
int pos, step, retval; |
|
|
|
|
const char *ds_def; |
|
|
|
|
char *buffer; |
|
|
|
|
|
|
|
|
|
plugin = plugin_lookup(plugin_name); |
|
|
|
|
if (plugin == NULL) { |
|
|
|
|
log_print(LOG_ERROR, "Plugin not found (%s)", plugin_name); |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ds_def = plugin->get_ds(ds_id); |
|
|
|
|
const char *ds_def = plugin->get_ds(ds_id); |
|
|
|
|
if (ds_def == NULL) { |
|
|
|
|
log_print(LOG_ERROR, "No vaild DS found (%s:%d)", plugin->name, ds_id); |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
buffer = malloc(ARGVSIZE); |
|
|
|
|
char *buffer = malloc(ARGVSIZE); |
|
|
|
|
if (buffer == NULL) { |
|
|
|
|
log_print(LOG_ERROR, "append_ds_config: out of memory"); |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
step = plugin->interval; |
|
|
|
|
|
|
|
|
|
pos = snprintf(buffer, ARGVSIZE, "create %s -s %d %s ", filename, step, ds_def); |
|
|
|
|
int step = plugin->interval; |
|
|
|
|
int pos = snprintf(buffer, ARGVSIZE, "create %s -s %d %s ", filename, step, ds_def); |
|
|
|
|
if (pos < 0 || pos >= ARGVSIZE) { |
|
|
|
|
log_print(LOG_ERROR, "rrd_create_file: arguments too long"); |
|
|
|
|
free(buffer); |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (append_rra_config(buffer, ARGVSIZE, &pos) <= 0) { |
|
|
|
|
struct rra_cb_data data = { |
|
|
|
|
.buffer = buffer, |
|
|
|
|
.size = ARGVSIZE, |
|
|
|
|
.pos = &pos, |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
int cnt = config_get_strings("global", "rra", append_rra_config, &data); |
|
|
|
|
if (cnt <= 0) { |
|
|
|
|
free(buffer); |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
retval = do_rrd(RRDTOOL_CREATE, buffer); |
|
|
|
|
free(buffer); |
|
|
|
|
|
|
|
|
|
return retval; |
|
|
|
|
return do_rrd(&rrd_create, buffer); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static int rrd_update_file(const char *filename, const char *values) |
|
|
|
|
{ |
|
|
|
|
int pos, retval; |
|
|
|
|
char *buffer; |
|
|
|
|
|
|
|
|
|
buffer = malloc(ARGVSIZE); |
|
|
|
|
char *buffer = malloc(ARGVSIZE); |
|
|
|
|
if (buffer == NULL) { |
|
|
|
|
log_print(LOG_ERROR, "append_ds_config: out of memory"); |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pos = snprintf(buffer, ARGVSIZE, "update %s %lu:%s", filename, time(NULL), values); |
|
|
|
|
int pos = snprintf(buffer, ARGVSIZE, "update %s %lu:%s", filename, time(NULL), values); |
|
|
|
|
if (pos < 0 || pos >= ARGVSIZE) { |
|
|
|
|
log_print(LOG_ERROR, "rrd_update_file: arguments too long"); |
|
|
|
|
free(buffer); |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
retval = do_rrd(RRDTOOL_UPDATE, buffer); |
|
|
|
|
free(buffer); |
|
|
|
|
|
|
|
|
|
return retval; |
|
|
|
|
return do_rrd(&rrd_update, buffer); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static int check_create_dir(const char *dir) |
|
|
|
@ -197,9 +161,9 @@ static int check_create_dir(const char *dir)
|
|
|
|
|
|
|
|
|
|
static int create_parent_dirs(char *filename) |
|
|
|
|
{ |
|
|
|
|
char *lastslash, *nextslash = filename; |
|
|
|
|
char *nextslash = filename; |
|
|
|
|
|
|
|
|
|
lastslash = strrchr(filename, '/'); |
|
|
|
|
char *lastslash = strrchr(filename, '/'); |
|
|
|
|
if (lastslash == NULL) { |
|
|
|
|
log_print(LOG_ERROR, "create_parent_dirs: invalid file name"); |
|
|
|
|
return -1; |
|
|
|
@ -229,30 +193,26 @@ static int create_parent_dirs(char *filename)
|
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int rrd_submit(const char *hostname, const char *plugin, const char *filename, int ds_id, const char *data) |
|
|
|
|
int rrd_submit(const char *hostname, struct sammler_plugin *plugin, const char *filename, int ds_id, const char *data) |
|
|
|
|
{ |
|
|
|
|
struct stat statbuf; |
|
|
|
|
static const char *rrd_dir = NULL; |
|
|
|
|
|
|
|
|
|
char *fullfile; |
|
|
|
|
int len; |
|
|
|
|
|
|
|
|
|
if (rrd_dir == NULL) |
|
|
|
|
rrd_dir = config_get_string("global", "rrd_dir", "."); |
|
|
|
|
|
|
|
|
|
fullfile = malloc(BUFSIZE); |
|
|
|
|
char *fullfile = malloc(BUFSIZE); |
|
|
|
|
if (fullfile == NULL) { |
|
|
|
|
log_print(LOG_ERROR, "rrd_submit: out of memory"); |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
len = snprintf(fullfile, BUFSIZE, "%s/%s/%s", rrd_dir, hostname, filename); |
|
|
|
|
int len = snprintf(fullfile, BUFSIZE, "%s/%s/%s", rrd_dir, hostname, filename); |
|
|
|
|
if (len < 0 || len >= BUFSIZE) { |
|
|
|
|
log_print(LOG_ERROR, "rrd_submit: arguments too long"); |
|
|
|
|
free(fullfile); |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
struct stat statbuf; |
|
|
|
|
if (stat(fullfile, &statbuf) == -1) { |
|
|
|
|
if (errno == ENOENT) { |
|
|
|
|
errno = 0; |
|
|
|
|