X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fceph.c;h=e6d3767b3140d786698f054796b23b4ed903b78f;hb=8422e7e438bf34c075af3b1b6380fe8c1fb82bf9;hp=b186a276335d1b7e8867a1b170ff864845f4a83e;hpb=f91dc17b98157dfd785759cf71e009a0e9c1a520;p=collectd.git diff --git a/src/ceph.c b/src/ceph.c index b186a276..e6d3767b 100644 --- a/src/ceph.c +++ b/src/ceph.c @@ -167,7 +167,7 @@ static int convert_special_metrics = 1; static struct ceph_daemon **g_daemons = NULL; /** Number of elements in g_daemons */ -static int g_num_daemons = 0; +static size_t g_num_daemons = 0; /** * A set of data that we build up in memory while parsing the JSON. @@ -262,8 +262,11 @@ static int ceph_cb_boolean(void *ctx, int bool_val) #define BUFFER_ADD(dest, src) do { \ size_t dest_size = sizeof (dest); \ - strncat ((dest), (src), dest_size - strlen (dest)); \ - (dest)[dest_size - 1] = '\0'; \ + size_t dest_len = strlen (dest); \ + if (dest_size > dest_len) { \ + sstrncpy ((dest) + dest_len, (src), dest_size - dest_len); \ + } \ + (dest)[dest_size - 1] = 0; \ } while (0) static int @@ -271,11 +274,10 @@ ceph_cb_number(void *ctx, const char *number_val, yajl_len_t number_len) { yajl_struct *state = (yajl_struct*) ctx; char buffer[number_len+1]; - char key[2 * DATA_MAX_NAME_LEN]; + char key[2 * DATA_MAX_NAME_LEN] = { 0 }; _Bool latency_type = 0; int status; - key[0] = '\0'; memcpy(buffer, number_val, number_len); buffer[sizeof(buffer) - 1] = '\0'; @@ -295,6 +297,11 @@ ceph_cb_number(void *ctx, const char *number_val, yajl_len_t number_len) { latency_type = 1; + /* depth >= 2 => (stack[-1] != NULL && stack[-2] != NULL) */ + assert ((state->depth < 2) + || ((state->stack[state->depth - 1] != NULL) + && (state->stack[state->depth - 2] != NULL))); + /* Super-special case for filestore.journal_wr_bytes.avgcount: For * some reason, Ceph schema encodes this as a count/sum pair while all * other "Bytes" data (excluding used/capacity bytes for OSD space) uses @@ -425,7 +432,7 @@ static void ceph_daemon_print(const struct ceph_daemon *d) static void ceph_daemons_print(void) { - for(int i = 0; i < g_num_daemons; ++i) + for(size_t i = 0; i < g_num_daemons; ++i) { ceph_daemon_print(g_daemons[i]); } @@ -752,7 +759,8 @@ static int cc_add_daemon_config(oconfig_item_t *ci) return ENOMEM; } memcpy(nd, &cd, sizeof(*nd)); - g_daemons[g_num_daemons++] = nd; + g_daemons[g_num_daemons] = nd; + g_num_daemons++; return 0; } @@ -1464,7 +1472,7 @@ static int cconn_main_loop(uint32_t request_type) /* create cconn array */ memset(io_array, 0, sizeof(io_array)); - for(int i = 0; i < g_num_daemons; ++i) + for(size_t i = 0; i < g_num_daemons; ++i) { io_array[i].d = g_daemons[i]; io_array[i].request_type = request_type; @@ -1483,13 +1491,13 @@ static int cconn_main_loop(uint32_t request_type) struct pollfd fds[g_num_daemons]; memset(fds, 0, sizeof(fds)); nfds = 0; - for(int i = 0; i < g_num_daemons; ++i) + for(size_t i = 0; i < g_num_daemons; ++i) { struct cconn *io = io_array + i; ret = cconn_prepare(io, fds + nfds); if(ret < 0) { - WARNING("ceph plugin: cconn_prepare(name=%s,i=%d,st=%d)=%d", + WARNING("ceph plugin: cconn_prepare(name=%s,i=%zu,st=%d)=%d", io->d->name, i, io->state, ret); cconn_close(io); io->request_type = ASOK_REQ_NONE; @@ -1553,7 +1561,7 @@ static int cconn_main_loop(uint32_t request_type) } } } - done: for(int i = 0; i < g_num_daemons; ++i) + done: for(size_t i = 0; i < g_num_daemons; ++i) { cconn_close(io_array + i); } @@ -1576,8 +1584,6 @@ static int ceph_read(void) /******* lifecycle *******/ static int ceph_init(void) { - int ret; - #if defined(HAVE_SYS_CAPABILITY_H) && defined(CAP_DAC_OVERRIDE) if (check_capability (CAP_DAC_OVERRIDE) != 0) { @@ -1595,14 +1601,18 @@ static int ceph_init(void) ceph_daemons_print(); - ret = cconn_main_loop(ASOK_REQ_VERSION); + if (g_num_daemons < 1) + { + ERROR ("ceph plugin: No daemons configured. See the \"Daemon\" config option."); + return ENOENT; + } - return (ret) ? ret : 0; + return cconn_main_loop(ASOK_REQ_VERSION); } static int ceph_shutdown(void) { - for(int i = 0; i < g_num_daemons; ++i) + for(size_t i = 0; i < g_num_daemons; ++i) { ceph_daemon_free(g_daemons[i]); }