2016-03-19 15:14:35 +00:00
|
|
|
#include "first.h"
|
|
|
|
|
2017-06-20 03:00:45 +00:00
|
|
|
#include "base.h"
|
|
|
|
#include "fdevent.h"
|
2009-10-11 14:31:42 +00:00
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
#include "plugin.h"
|
2005-02-20 14:27:00 +00:00
|
|
|
#include <sys/types.h>
|
2017-06-20 03:00:45 +00:00
|
|
|
#include <sys/wait.h>
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
buffer *path_rrdtool_bin;
|
|
|
|
buffer *path_rrd;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
double requests, *requests_ptr;
|
|
|
|
double bytes_written, *bytes_written_ptr;
|
|
|
|
double bytes_read, *bytes_read_ptr;
|
|
|
|
} plugin_config;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
PLUGIN_DATA;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
buffer *cmd;
|
|
|
|
buffer *resp;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
int read_fd, write_fd;
|
|
|
|
pid_t rrdtool_pid;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
int rrdtool_running;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
plugin_config **config_storage;
|
|
|
|
plugin_config conf;
|
|
|
|
} plugin_data;
|
|
|
|
|
|
|
|
INIT_FUNC(mod_rrd_init) {
|
|
|
|
plugin_data *p;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
p = calloc(1, sizeof(*p));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
p->resp = buffer_init();
|
|
|
|
p->cmd = buffer_init();
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
FREE_FUNC(mod_rrd_free) {
|
|
|
|
plugin_data *p = p_d;
|
|
|
|
size_t i;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (!p) return HANDLER_GO_ON;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (p->config_storage) {
|
|
|
|
for (i = 0; i < srv->config_context->used; i++) {
|
|
|
|
plugin_config *s = p->config_storage[i];
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-05-14 09:38:33 +00:00
|
|
|
if (NULL == s) continue;
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
buffer_free(s->path_rrdtool_bin);
|
|
|
|
buffer_free(s->path_rrd);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buffer_free(p->cmd);
|
|
|
|
buffer_free(p->resp);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
free(p->config_storage);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2017-06-20 03:00:45 +00:00
|
|
|
if (p->rrdtool_pid > 0) {
|
2005-02-20 14:27:00 +00:00
|
|
|
close(p->read_fd);
|
|
|
|
close(p->write_fd);
|
|
|
|
/* collect status */
|
2017-04-26 15:48:51 +00:00
|
|
|
while (-1 == waitpid(p->rrdtool_pid, NULL, 0) && errno == EINTR) ;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
free(p);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|
|
|
|
|
2009-03-07 21:05:37 +00:00
|
|
|
static int mod_rrd_create_pipe(server *srv, plugin_data *p) {
|
2017-06-20 03:00:45 +00:00
|
|
|
char *args[3];
|
2005-02-20 14:27:00 +00:00
|
|
|
int to_rrdtool_fds[2];
|
|
|
|
int from_rrdtool_fds[2];
|
|
|
|
if (pipe(to_rrdtool_fds)) {
|
2006-10-04 13:26:23 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ss",
|
2005-02-20 14:27:00 +00:00
|
|
|
"pipe failed: ", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (pipe(from_rrdtool_fds)) {
|
2006-10-04 13:26:23 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ss",
|
2005-02-20 14:27:00 +00:00
|
|
|
"pipe failed: ", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2017-06-20 03:00:45 +00:00
|
|
|
fdevent_setfd_cloexec(to_rrdtool_fds[1]);
|
|
|
|
fdevent_setfd_cloexec(from_rrdtool_fds[0]);
|
|
|
|
*(const char **)&args[0] = p->conf.path_rrdtool_bin->ptr;
|
|
|
|
*(const char **)&args[1] = "-";
|
|
|
|
args[2] = NULL;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2017-06-20 03:00:45 +00:00
|
|
|
p->rrdtool_pid = fdevent_fork_execve(args[0], args, NULL, to_rrdtool_fds[0], from_rrdtool_fds[1], -1, -1);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2017-06-20 03:00:45 +00:00
|
|
|
if (-1 != p->rrdtool_pid) {
|
2005-02-20 14:27:00 +00:00
|
|
|
close(from_rrdtool_fds[1]);
|
|
|
|
close(to_rrdtool_fds[0]);
|
|
|
|
p->write_fd = to_rrdtool_fds[1];
|
|
|
|
p->read_fd = from_rrdtool_fds[0];
|
2017-06-20 03:00:45 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "SBss", "fork/exec(", p->conf.path_rrdtool_bin, "):", strerror(errno));
|
|
|
|
close(to_rrdtool_fds[0]);
|
|
|
|
close(to_rrdtool_fds[1]);
|
|
|
|
close(from_rrdtool_fds[0]);
|
|
|
|
close(from_rrdtool_fds[1]);
|
|
|
|
return -1;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-19 13:13:58 +00:00
|
|
|
/* read/write wrappers to catch EINTR */
|
|
|
|
|
|
|
|
/* write to blocking socket; blocks until all data is sent, write returns 0 or an error (apart from EINTR) occurs. */
|
|
|
|
static ssize_t safe_write(int fd, const void *buf, size_t count) {
|
|
|
|
ssize_t res, sum = 0;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
res = write(fd, buf, count);
|
|
|
|
if (res >= 0) {
|
|
|
|
sum += res;
|
|
|
|
/* do not try again if res == 0 */
|
|
|
|
if (res == 0 || (size_t) res == count) return sum;
|
|
|
|
count -= res;
|
|
|
|
buf = (const char*) buf + res;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
switch (errno) {
|
|
|
|
case EINTR:
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this assumes we get enough data on a successful read */
|
2017-08-12 19:39:12 +00:00
|
|
|
static ssize_t safe_read(int fd, buffer *b) {
|
2009-02-19 13:13:58 +00:00
|
|
|
ssize_t res;
|
|
|
|
|
2017-08-12 19:39:12 +00:00
|
|
|
buffer_string_prepare_copy(b, 4095);
|
|
|
|
|
|
|
|
do {
|
|
|
|
res = read(fd, b->ptr, b->size-1);
|
|
|
|
} while (-1 == res && errno == EINTR);
|
|
|
|
|
|
|
|
if (res >= 0) buffer_commit(b, res);
|
|
|
|
return res;
|
2009-02-19 13:13:58 +00:00
|
|
|
}
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
static int mod_rrdtool_create_rrd(server *srv, plugin_data *p, plugin_config *s) {
|
|
|
|
struct stat st;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* check if DB already exists */
|
|
|
|
if (0 == stat(s->path_rrd->ptr, &st)) {
|
|
|
|
/* check if it is plain file */
|
|
|
|
if (!S_ISREG(st.st_mode)) {
|
2006-10-04 13:26:23 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sb",
|
2005-02-20 14:27:00 +00:00
|
|
|
"not a regular file:", s->path_rrd);
|
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
2009-03-11 21:45:17 +00:00
|
|
|
|
2009-10-31 09:54:13 +00:00
|
|
|
/* still create DB if it's empty file */
|
|
|
|
if (st.st_size > 0) {
|
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|
2009-03-11 21:45:17 +00:00
|
|
|
}
|
|
|
|
|
2009-03-11 21:45:49 +00:00
|
|
|
/* create a new one */
|
|
|
|
buffer_copy_string_len(p->cmd, CONST_STR_LEN("create "));
|
|
|
|
buffer_append_string_buffer(p->cmd, s->path_rrd);
|
|
|
|
buffer_append_string_len(p->cmd, CONST_STR_LEN(
|
|
|
|
" --step 60 "
|
|
|
|
"DS:InOctets:ABSOLUTE:600:U:U "
|
|
|
|
"DS:OutOctets:ABSOLUTE:600:U:U "
|
|
|
|
"DS:Requests:ABSOLUTE:600:U:U "
|
|
|
|
"RRA:AVERAGE:0.5:1:600 "
|
|
|
|
"RRA:AVERAGE:0.5:6:700 "
|
|
|
|
"RRA:AVERAGE:0.5:24:775 "
|
|
|
|
"RRA:AVERAGE:0.5:288:797 "
|
|
|
|
"RRA:MAX:0.5:1:600 "
|
|
|
|
"RRA:MAX:0.5:6:700 "
|
|
|
|
"RRA:MAX:0.5:24:775 "
|
|
|
|
"RRA:MAX:0.5:288:797 "
|
|
|
|
"RRA:MIN:0.5:1:600 "
|
|
|
|
"RRA:MIN:0.5:6:700 "
|
|
|
|
"RRA:MIN:0.5:24:775 "
|
|
|
|
"RRA:MIN:0.5:288:797\n"));
|
|
|
|
|
2015-02-08 19:10:44 +00:00
|
|
|
if (-1 == (safe_write(p->write_fd, CONST_BUF_LEN(p->cmd)))) {
|
2009-03-11 21:45:49 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ss",
|
|
|
|
"rrdtool-write: failed", strerror(errno));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2009-03-11 21:45:49 +00:00
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2017-08-12 19:39:12 +00:00
|
|
|
if (-1 == safe_read(p->read_fd, p->resp)) {
|
2009-03-11 21:45:49 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ss",
|
|
|
|
"rrdtool-read: failed", strerror(errno));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2009-03-11 21:45:49 +00:00
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2009-03-11 21:45:49 +00:00
|
|
|
if (p->resp->ptr[0] != 'O' ||
|
|
|
|
p->resp->ptr[1] != 'K') {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sbb",
|
|
|
|
"rrdtool-response:", p->cmd, p->resp);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2009-03-11 21:45:49 +00:00
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define PATCH(x) \
|
|
|
|
p->conf.x = s->x;
|
2005-08-08 10:27:07 +00:00
|
|
|
static int mod_rrd_patch_connection(server *srv, connection *con, plugin_data *p) {
|
2005-02-20 14:27:00 +00:00
|
|
|
size_t i, j;
|
2005-08-08 10:27:07 +00:00
|
|
|
plugin_config *s = p->config_storage[0];
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-08 10:27:07 +00:00
|
|
|
PATCH(path_rrdtool_bin);
|
|
|
|
PATCH(path_rrd);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-08 10:27:07 +00:00
|
|
|
p->conf.bytes_written_ptr = &(s->bytes_written);
|
|
|
|
p->conf.bytes_read_ptr = &(s->bytes_read);
|
|
|
|
p->conf.requests_ptr = &(s->requests);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* skip the first, the global context */
|
|
|
|
for (i = 1; i < srv->config_context->used; i++) {
|
|
|
|
data_config *dc = (data_config *)srv->config_context->data[i];
|
2005-08-08 10:27:07 +00:00
|
|
|
s = p->config_storage[i];
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* condition didn't match */
|
|
|
|
if (!config_check_cond(srv, con, dc)) continue;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* merge config */
|
|
|
|
for (j = 0; j < dc->value->used; j++) {
|
|
|
|
data_unset *du = dc->value->data[j];
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (buffer_is_equal_string(du->key, CONST_STR_LEN("rrdtool.db-name"))) {
|
|
|
|
PATCH(path_rrd);
|
|
|
|
/* get pointers to double values */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
p->conf.bytes_written_ptr = &(s->bytes_written);
|
|
|
|
p->conf.bytes_read_ptr = &(s->bytes_read);
|
|
|
|
p->conf.requests_ptr = &(s->requests);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#undef PATCH
|
|
|
|
|
|
|
|
SETDEFAULTS_FUNC(mod_rrd_set_defaults) {
|
|
|
|
plugin_data *p = p_d;
|
|
|
|
size_t i;
|
2017-01-24 21:31:36 +00:00
|
|
|
int activate = 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
config_values_t cv[] = {
|
2015-11-07 12:51:11 +00:00
|
|
|
{ "rrdtool.binary", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
|
|
|
|
{ "rrdtool.db-name", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
|
|
|
|
{ NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
|
2005-02-20 14:27:00 +00:00
|
|
|
};
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (!p) return HANDLER_ERROR;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2014-02-16 13:08:20 +00:00
|
|
|
force_assert(srv->config_context->used > 0);
|
2013-11-13 11:43:26 +00:00
|
|
|
p->config_storage = calloc(1, srv->config_context->used * sizeof(plugin_config *));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
for (i = 0; i < srv->config_context->used; i++) {
|
2015-11-07 12:51:11 +00:00
|
|
|
data_config const* config = (data_config const*)srv->config_context->data[i];
|
2005-02-20 14:27:00 +00:00
|
|
|
plugin_config *s;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-16 13:07:46 +00:00
|
|
|
s = calloc(1, sizeof(plugin_config));
|
2005-02-20 14:27:00 +00:00
|
|
|
s->path_rrdtool_bin = buffer_init();
|
|
|
|
s->path_rrd = buffer_init();
|
|
|
|
s->requests = 0;
|
|
|
|
s->bytes_written = 0;
|
|
|
|
s->bytes_read = 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
cv[0].destination = s->path_rrdtool_bin;
|
|
|
|
cv[1].destination = s->path_rrd;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
p->config_storage[i] = s;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-11-07 12:51:11 +00:00
|
|
|
if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
|
2005-02-20 14:27:00 +00:00
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
if (i > 0 && !buffer_string_is_empty(s->path_rrdtool_bin)) {
|
2005-02-20 14:27:00 +00:00
|
|
|
/* path_rrdtool_bin is a global option */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s",
|
2005-02-20 14:27:00 +00:00
|
|
|
"rrdtool.binary can only be set as a global option.");
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2017-01-24 21:31:36 +00:00
|
|
|
if (!buffer_string_is_empty(s->path_rrd)) activate = 1;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
p->conf.path_rrdtool_bin = p->config_storage[0]->path_rrdtool_bin;
|
|
|
|
p->rrdtool_running = 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2017-01-24 21:31:36 +00:00
|
|
|
if (!activate) return HANDLER_GO_ON;
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* check for dir */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
if (buffer_string_is_empty(p->conf.path_rrdtool_bin)) {
|
2006-10-04 13:26:23 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s",
|
2005-02-20 14:27:00 +00:00
|
|
|
"rrdtool.binary has to be set");
|
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* open the pipe to rrdtool */
|
|
|
|
if (mod_rrd_create_pipe(srv, p)) {
|
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
p->rrdtool_running = 1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|
|
|
|
|
|
|
|
TRIGGER_FUNC(mod_rrd_trigger) {
|
|
|
|
plugin_data *p = p_d;
|
|
|
|
size_t i;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (!p->rrdtool_running) return HANDLER_GO_ON;
|
|
|
|
if ((srv->cur_ts % 60) != 0) return HANDLER_GO_ON;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
for (i = 0; i < srv->config_context->used; i++) {
|
|
|
|
plugin_config *s = p->config_storage[i];
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
if (buffer_string_is_empty(s->path_rrd)) continue;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* write the data down every minute */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (HANDLER_GO_ON != mod_rrdtool_create_rrd(srv, p, s)) return HANDLER_ERROR;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2008-07-30 19:38:32 +00:00
|
|
|
buffer_copy_string_len(p->cmd, CONST_STR_LEN("update "));
|
2005-02-20 14:27:00 +00:00
|
|
|
buffer_append_string_buffer(p->cmd, s->path_rrd);
|
2008-07-30 19:38:32 +00:00
|
|
|
buffer_append_string_len(p->cmd, CONST_STR_LEN(" N:"));
|
2015-02-08 12:37:10 +00:00
|
|
|
buffer_append_int(p->cmd, s->bytes_read);
|
2008-07-30 19:38:32 +00:00
|
|
|
buffer_append_string_len(p->cmd, CONST_STR_LEN(":"));
|
2015-02-08 12:37:10 +00:00
|
|
|
buffer_append_int(p->cmd, s->bytes_written);
|
2008-07-30 19:38:32 +00:00
|
|
|
buffer_append_string_len(p->cmd, CONST_STR_LEN(":"));
|
2015-02-08 12:37:10 +00:00
|
|
|
buffer_append_int(p->cmd, s->requests);
|
2008-07-30 19:38:32 +00:00
|
|
|
buffer_append_string_len(p->cmd, CONST_STR_LEN("\n"));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2017-08-12 19:39:12 +00:00
|
|
|
if (-1 == safe_write(p->write_fd, CONST_BUF_LEN(p->cmd))) {
|
2005-02-20 14:27:00 +00:00
|
|
|
p->rrdtool_running = 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ss",
|
2005-02-20 14:27:00 +00:00
|
|
|
"rrdtool-write: failed", strerror(errno));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2017-08-12 19:39:12 +00:00
|
|
|
if (-1 == safe_read(p->read_fd, p->resp)) {
|
2005-02-20 14:27:00 +00:00
|
|
|
p->rrdtool_running = 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ss",
|
2005-02-20 14:27:00 +00:00
|
|
|
"rrdtool-read: failed", strerror(errno));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (p->resp->ptr[0] != 'O' ||
|
|
|
|
p->resp->ptr[1] != 'K') {
|
2009-02-19 13:14:02 +00:00
|
|
|
/* don't fail on this error if we just started (graceful restart, the old one might have just updated too) */
|
|
|
|
if (!(strstr(p->resp->ptr, "(minimum one second step)") && (srv->cur_ts - srv->startup_ts < 3))) {
|
|
|
|
p->rrdtool_running = 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2009-02-19 13:14:02 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sbb",
|
2005-02-20 14:27:00 +00:00
|
|
|
"rrdtool-response:", p->cmd, p->resp);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2009-02-19 13:14:02 +00:00
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
s->requests = 0;
|
|
|
|
s->bytes_written = 0;
|
|
|
|
s->bytes_read = 0;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|
|
|
|
|
|
|
|
REQUESTDONE_FUNC(mod_rrd_account) {
|
|
|
|
plugin_data *p = p_d;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-08 10:27:07 +00:00
|
|
|
mod_rrd_patch_connection(srv, con, p);
|
2017-01-24 21:31:36 +00:00
|
|
|
if (!p->rrdtool_running) return HANDLER_GO_ON;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
*(p->conf.requests_ptr) += 1;
|
|
|
|
*(p->conf.bytes_written_ptr) += con->bytes_written;
|
|
|
|
*(p->conf.bytes_read_ptr) += con->bytes_read;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|
|
|
|
|
2009-03-07 21:05:37 +00:00
|
|
|
int mod_rrdtool_plugin_init(plugin *p);
|
2005-02-20 14:27:00 +00:00
|
|
|
int mod_rrdtool_plugin_init(plugin *p) {
|
|
|
|
p->version = LIGHTTPD_VERSION_ID;
|
|
|
|
p->name = buffer_init_string("rrd");
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
p->init = mod_rrd_init;
|
|
|
|
p->cleanup = mod_rrd_free;
|
|
|
|
p->set_defaults= mod_rrd_set_defaults;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
p->handle_trigger = mod_rrd_trigger;
|
|
|
|
p->handle_request_done = mod_rrd_account;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
p->data = NULL;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|