2 * collectd - src/ceph.c
3 * Copyright (C) 2011 New Dream Network
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Colin McCabe <cmccabe@alumni.cmu.edu>
20 * Dennis Zou <yunzou@cisco.com>
21 * Dan Ryder <daryder@cisco.com>
30 #include <arpa/inet.h>
33 #include <yajl/yajl_parse.h>
34 #if HAVE_YAJL_YAJL_VERSION_H
35 #include <yajl/yajl_version.h>
45 #include <sys/socket.h>
47 #include <sys/types.h>
53 #define MAX_RRD_DS_NAME_LEN 20
55 #define RETRY_AVGCOUNT -1
57 #if defined(YAJL_MAJOR) && (YAJL_MAJOR > 1)
58 # define HAVE_YAJL_V2 1
61 #define RETRY_ON_EINTR(ret, expr) \
71 /** Timeout interval in seconds */
72 #define CEPH_TIMEOUT_INTERVAL 1
74 /** Maximum path length for a UNIX domain socket on this system */
75 #define UNIX_DOMAIN_SOCK_PATH_MAX (sizeof(((struct sockaddr_un*)0)->sun_path))
77 /** Yajl callback returns */
78 #define CEPH_CB_CONTINUE 1
79 #define CEPH_CB_ABORT 0
82 typedef size_t yajl_len_t;
84 typedef unsigned int yajl_len_t;
87 /******* ceph_daemon *******/
90 /** Version of the admin_socket interface */
93 char name[DATA_MAX_NAME_LEN];
97 /** Path to the socket that we use to talk to the ceph daemon */
98 char asok_path[UNIX_DOMAIN_SOCK_PATH_MAX];
100 /** The set of key/value pairs that this daemon reports
101 * dset.type The daemon name
102 * dset.ds_num Number of data sources (key/value pairs)
103 * dset.ds Dynamically allocated array of key/value pairs
105 /** Dynamically allocated array **/
106 struct data_set_s *dset;
110 /******* JSON parsing *******/
111 typedef int (*node_handler_t)(void *, const char*, const char*);
113 /** Track state and handler while parsing JSON */
116 node_handler_t handler;
119 char key[DATA_MAX_NAME_LEN];
121 } state[YAJL_MAX_DEPTH];
124 typedef struct yajl_struct yajl_struct;
127 * Keep track of last data for latency values so we can calculate rate
130 struct last_data **last_poll_data = NULL;
133 enum perfcounter_type_d
135 PERFCOUNTER_LATENCY = 0x4, PERFCOUNTER_DERIVE = 0x8,
138 /** Give user option to use default (long run = since daemon started) avg */
139 static int long_run_latency_avg = 0;
142 * Give user option to use default type for special cases -
143 * filestore.journal_wr_bytes is currently only metric here. Ceph reports the
144 * type as a sum/count pair and will calculate it the same as a latency value.
145 * All other "bytes" metrics (excluding the used/capacity bytes for the OSD)
146 * use the DERIVE type. Unless user specifies to use given type, convert this
147 * metric to use DERIVE.
149 static int convert_special_metrics = 1;
151 /** Array of daemons to monitor */
152 static struct ceph_daemon **g_daemons = NULL;
154 /** Number of elements in g_daemons */
155 static int g_num_daemons = 0;
164 * A set of values_t data that we build up in memory while parsing the JSON.
168 struct ceph_daemon *d;
170 struct values_holder vh[0];
175 * A set of count/sum pairs to keep track of latency types and get difference
176 * between this poll data and last poll data.
180 char dset_name[DATA_MAX_NAME_LEN];
181 char ds_name[MAX_RRD_DS_NAME_LEN];
187 /******* network I/O *******/
190 CSTATE_UNCONNECTED = 0,
191 CSTATE_WRITE_REQUEST,
199 ASOK_REQ_VERSION = 0,
202 ASOK_REQ_NONE = 1000,
207 /** The Ceph daemon that we're talking to */
208 struct ceph_daemon *d;
211 uint32_t request_type;
213 /** The connection state */
216 /** The socket we use to talk to this daemon */
219 /** The amount of data remaining to read / write. */
222 /** Length of the JSON to read */
225 /** Buffer containing JSON data */
228 /** Keep data important to yajl processing */
229 struct yajl_struct yajl;
232 static int ceph_cb_null(void *ctx)
234 return CEPH_CB_CONTINUE;
237 static int ceph_cb_boolean(void *ctx, int bool_val)
239 return CEPH_CB_CONTINUE;
243 ceph_cb_number(void *ctx, const char *number_val, yajl_len_t number_len)
245 yajl_struct *yajl = (yajl_struct*)ctx;
246 char buffer[number_len+1];
247 int i, latency_type = 0, result;
250 memcpy(buffer, number_val, number_len);
251 buffer[sizeof(buffer) - 1] = 0;
253 ssnprintf(key, yajl->state[0].key_len, "%s", yajl->state[0].key);
254 for(i = 1; i < yajl->depth; i++)
256 if((i == yajl->depth-1) && ((strcmp(yajl->state[i].key,"avgcount") == 0)
257 || (strcmp(yajl->state[i].key,"sum") == 0)))
259 if(convert_special_metrics)
262 * Special case for filestore:JournalWrBytes. For some reason,
263 * Ceph schema encodes this as a count/sum pair while all
264 * other "Bytes" data (excluding used/capacity bytes for OSD
265 * space) uses a single "Derive" type. To spare further
266 * confusion, keep this KPI as the same type of other "Bytes".
267 * Instead of keeping an "average" or "rate", use the "sum" in
268 * the pair and assign that to the derive value.
270 if((strcmp(yajl->state[i-1].key, "journal_wr_bytes") == 0) &&
271 (strcmp(yajl->state[i-2].key,"filestore") == 0) &&
272 (strcmp(yajl->state[i].key,"avgcount") == 0))
274 DEBUG("Skipping avgcount for filestore.JournalWrBytes");
275 yajl->depth = (yajl->depth - 1);
276 return CEPH_CB_CONTINUE;
279 //probably a avgcount/sum pair. if not - we'll try full key later
283 strncat(key, ".", 1);
284 strncat(key, yajl->state[i].key, yajl->state[i].key_len+1);
287 result = yajl->handler(yajl->handler_arg, buffer, key);
289 if((result == RETRY_AVGCOUNT) && latency_type)
291 strncat(key, ".", 1);
292 strncat(key, yajl->state[yajl->depth-1].key,
293 yajl->state[yajl->depth-1].key_len+1);
294 result = yajl->handler(yajl->handler_arg, buffer, key);
297 if(result == -ENOMEM)
299 ERROR("ceph plugin: memory allocation failed");
300 return CEPH_CB_ABORT;
303 yajl->depth = (yajl->depth - 1);
304 return CEPH_CB_CONTINUE;
307 static int ceph_cb_string(void *ctx, const unsigned char *string_val,
308 yajl_len_t string_len)
310 return CEPH_CB_CONTINUE;
313 static int ceph_cb_start_map(void *ctx)
315 return CEPH_CB_CONTINUE;
319 ceph_cb_map_key(void *ctx, const unsigned char *key, yajl_len_t string_len)
321 yajl_struct *yajl = (yajl_struct*)ctx;
323 if((yajl->depth+1) >= YAJL_MAX_DEPTH)
325 ERROR("ceph plugin: depth exceeds max, aborting.");
326 return CEPH_CB_ABORT;
329 char buffer[string_len+1];
331 memcpy(buffer, key, string_len);
332 buffer[sizeof(buffer) - 1] = 0;
334 snprintf(yajl->state[yajl->depth].key, sizeof(buffer), "%s", buffer);
335 yajl->state[yajl->depth].key_len = sizeof(buffer);
336 yajl->depth = (yajl->depth + 1);
338 return CEPH_CB_CONTINUE;
341 static int ceph_cb_end_map(void *ctx)
343 yajl_struct *yajl = (yajl_struct*)ctx;
345 yajl->depth = (yajl->depth - 1);
346 return CEPH_CB_CONTINUE;
349 static int ceph_cb_start_array(void *ctx)
351 return CEPH_CB_CONTINUE;
354 static int ceph_cb_end_array(void *ctx)
356 return CEPH_CB_CONTINUE;
359 static yajl_callbacks callbacks = {
373 static void ceph_daemon_print(const struct ceph_daemon *d)
375 DEBUG("name=%s, asok_path=%s", d->name, d->asok_path);
378 static void ceph_daemons_print(void)
381 for(i = 0; i < g_num_daemons; ++i)
383 ceph_daemon_print(g_daemons[i]);
387 static void ceph_daemon_free(struct ceph_daemon *d)
390 for(; i < d->dset_num; i++)
392 plugin_unregister_data_set((d->dset + i)->type);
394 sfree(d->pc_types[i]);
401 static void compact_ds_name(char *source, char *dest)
404 char *save_ptr = NULL, *tmp_ptr = source;
407 char tmp[DATA_MAX_NAME_LEN];
408 size_t key_chars_remaining = (DATA_MAX_NAME_LEN-1);
411 memset(tmp, 0, sizeof(tmp));
412 if(source == NULL || dest == NULL || source[0] == '\0' || dest[0] != '\0')
416 size_t src_len = strlen(source);
417 snprintf(len_str, sizeof(len_str), "%zu", src_len);
418 unsigned char append_status = 0x0;
419 append_status |= (source[src_len - 1] == '-') ? 0x1 : 0x0;
420 append_status |= (source[src_len - 1] == '+') ? 0x2 : 0x0;
421 while ((keys[keys_num] = strtok_r(tmp_ptr, ":_-+", &save_ptr)) != NULL)
424 /** capitalize 1st char **/
425 keys[keys_num][0] = toupper(keys[keys_num][0]);
432 /** concatenate each part of source string **/
433 for(i = 0; i < keys_num; i++)
435 strncat(tmp, keys[i], key_chars_remaining);
436 key_chars_remaining -= strlen(keys[i]);
438 /** to coordinate limitation of length of ds name from RRD
439 * we will truncate ds_name
440 * when the its length is more than
441 * MAX_RRD_DS_NAME_LEN
443 if(strlen(tmp) > MAX_RRD_DS_NAME_LEN - 1)
445 append_status |= 0x4;
446 /** we should reserve space for
451 if(append_status & 0x1)
453 /** we should reserve space for
458 if(append_status & 0x2)
460 /** we should reserve space for
465 snprintf(dest, MAX_RRD_DS_NAME_LEN - reserved, "%s", tmp);
466 offset = strlen(dest);
467 switch (append_status)
470 memcpy(dest + offset, "Minus", 5);
473 memcpy(dest + offset, "Plus", 5);
476 memcpy(dest + offset, len_str, 2);
479 memcpy(dest + offset, "Minus", 5);
480 memcpy(dest + offset + 5, len_str, 2);
483 memcpy(dest + offset, "Plus", 4);
484 memcpy(dest + offset + 4, len_str, 2);
490 static int parse_keys(const char *key_str, char *dset_name, char *ds_name)
493 size_t dset_name_len = 0;
494 size_t ds_name_len = 0;
495 char tmp_ds_name[DATA_MAX_NAME_LEN];
496 memset(tmp_ds_name, 0, sizeof(tmp_ds_name));
497 if(dset_name == NULL || ds_name == NULL || key_str == NULL ||
498 key_str[0] == '\0' || dset_name[0] != '\0' || ds_name[0] != '\0')
502 if((ptr = strchr(key_str, '.')) == NULL
503 || (rptr = strrchr(key_str, '.')) == NULL)
505 strncpy(dset_name, key_str, DATA_MAX_NAME_LEN - 1);
506 strncpy(tmp_ds_name, key_str, DATA_MAX_NAME_LEN - 1);
510 (ptr - key_str) > (DATA_MAX_NAME_LEN - 1) ?
511 (DATA_MAX_NAME_LEN - 1) : (ptr - key_str);
512 memcpy(dset_name, key_str, dset_name_len);
514 (rptr - ptr) > DATA_MAX_NAME_LEN ? DATA_MAX_NAME_LEN : (rptr - ptr);
516 { /** only have two keys **/
517 if(!strncmp(rptr + 1, "type", 4))
518 {/** if last key is "type",ignore **/
519 strncpy(tmp_ds_name, dset_name, DATA_MAX_NAME_LEN - 1);
522 {/** if last key isn't "type", copy last key **/
523 strncpy(tmp_ds_name, rptr + 1, DATA_MAX_NAME_LEN - 1);
526 else if(!strncmp(rptr + 1, "type", 4))
527 {/** more than two keys **/
528 memcpy(tmp_ds_name, ptr + 1, ds_name_len - 1);
531 {/** copy whole keys **/
532 strncpy(tmp_ds_name, ptr + 1, DATA_MAX_NAME_LEN - 1);
534 compact: compact_ds_name(tmp_ds_name, ds_name);
538 static int get_matching_dset(const struct ceph_daemon *d, const char *name)
541 for(idx = 0; idx < d->dset_num; ++idx)
543 if(strcmp(d->dset[idx].type, name) == 0)
551 static int get_matching_value(const struct data_set_s *dset, const char *name,
555 for(idx = 0; idx < num_values; ++idx)
557 if(strcmp(dset->ds[idx].name, name) == 0)
565 static int ceph_daemon_add_ds_entry(struct ceph_daemon *d, const char *name,
568 struct data_source_s *ds;
569 struct data_set_s *dset;
570 struct data_set_s *dset_array;
571 int **pc_types_array = NULL;
575 if(strlen(name) + 1 > DATA_MAX_NAME_LEN)
577 return -ENAMETOOLONG;
579 char dset_name[DATA_MAX_NAME_LEN];
580 char ds_name[MAX_RRD_DS_NAME_LEN];
581 memset(dset_name, 0, sizeof(dset_name));
582 memset(ds_name, 0, sizeof(ds_name));
583 if(parse_keys(name, dset_name, ds_name))
587 idx = get_matching_dset(d, dset_name);
589 {/* need to add a dset **/
590 dset_array = realloc(d->dset,
591 sizeof(struct data_set_s) * (d->dset_num + 1));
596 pc_types_array = realloc(d->pc_types,
597 sizeof(int *) * (d->dset_num + 1));
602 dset = &dset_array[d->dset_num];
603 /** this step is very important, otherwise,
604 * realloc for dset->ds will tricky because of
605 * a random addr in dset->ds
607 memset(dset, 0, sizeof(struct data_set_s));
609 snprintf(dset->type, DATA_MAX_NAME_LEN, "%s", dset_name);
610 pc_types = pc_types_array[d->dset_num] = NULL;
611 d->dset = dset_array;
615 dset = &d->dset[idx];
616 pc_types = d->pc_types[idx];
618 struct data_source_s *ds_array = realloc(dset->ds,
619 sizeof(struct data_source_s) * (dset->ds_num + 1));
624 pc_types_new = realloc(pc_types, sizeof(int) * (dset->ds_num + 1));
631 if(convert_special_metrics)
634 * Special case for filestore:JournalWrBytes. For some reason, Ceph
635 * schema encodes this as a count/sum pair while all other "Bytes" data
636 * (excluding used/capacity bytes for OSD space) uses a single "Derive"
637 * type. To spare further confusion, keep this KPI as the same type of
638 * other "Bytes". Instead of keeping an "average" or "rate", use the
639 * "sum" in the pair and assign that to the derive value.
641 if((strcmp(dset_name,"filestore") == 0) &&
642 strcmp(ds_name, "JournalWrBytes") == 0)
650 pc_types_array[d->dset_num] = pc_types_new;
651 d->pc_types = pc_types_array;
652 d->pc_types[d->dset_num][dset->ds_num] = pc_type;
657 d->pc_types[idx] = pc_types_new;
658 d->pc_types[idx][dset->ds_num] = pc_type;
660 ds = &ds_array[dset->ds_num++];
661 snprintf(ds->name, MAX_RRD_DS_NAME_LEN, "%s", ds_name);
662 ds->type = (pc_type & PERFCOUNTER_DERIVE) ? DS_TYPE_DERIVE : DS_TYPE_GAUGE;
665 * Use min of 0 for DERIVE types so we don't get negative values on Ceph
668 ds->min = (ds->type == DS_TYPE_DERIVE) ? 0 : NAN;
673 /******* ceph_config *******/
674 static int cc_handle_str(struct oconfig_item_s *item, char *dest, int dest_len)
677 if(item->values_num != 1)
681 if(item->values[0].type != OCONFIG_TYPE_STRING)
685 val = item->values[0].value.string;
686 if(snprintf(dest, dest_len, "%s", val) > (dest_len - 1))
688 ERROR("ceph plugin: configuration parameter '%s' is too long.\n",
690 return -ENAMETOOLONG;
695 static int cc_handle_bool(struct oconfig_item_s *item, int *dest)
697 if(item->values_num != 1)
702 if(item->values[0].type != OCONFIG_TYPE_BOOLEAN)
707 *dest = (item->values[0].value.boolean) ? 1 : 0;
711 static int cc_add_daemon_config(oconfig_item_t *ci)
714 struct ceph_daemon *array, *nd, cd;
715 memset(&cd, 0, sizeof(struct ceph_daemon));
717 if((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
719 WARNING("ceph plugin: `Daemon' blocks need exactly one string "
724 ret = cc_handle_str(ci, cd.name, DATA_MAX_NAME_LEN);
730 for(i=0; i < ci->children_num; i++)
732 oconfig_item_t *child = ci->children + i;
734 if(strcasecmp("SocketPath", child->key) == 0)
736 ret = cc_handle_str(child, cd.asok_path, sizeof(cd.asok_path));
744 WARNING("ceph plugin: ignoring unknown option %s", child->key);
747 if(cd.name[0] == '\0')
749 ERROR("ceph plugin: you must configure a daemon name.\n");
752 else if(cd.asok_path[0] == '\0')
754 ERROR("ceph plugin(name=%s): you must configure an administrative "
755 "socket path.\n", cd.name);
758 else if(!((cd.asok_path[0] == '/') ||
759 (cd.asok_path[0] == '.' && cd.asok_path[1] == '/')))
761 ERROR("ceph plugin(name=%s): administrative socket paths must begin "
762 "with '/' or './' Can't parse: '%s'\n", cd.name, cd.asok_path);
765 array = realloc(g_daemons,
766 sizeof(struct ceph_daemon *) * (g_num_daemons + 1));
769 /* The positive return value here indicates that this is a
770 * runtime error, not a configuration error. */
773 g_daemons = (struct ceph_daemon**) array;
774 nd = malloc(sizeof(struct ceph_daemon));
779 memcpy(nd, &cd, sizeof(struct ceph_daemon));
780 g_daemons[g_num_daemons++] = nd;
784 static int ceph_config(oconfig_item_t *ci)
788 for(i = 0; i < ci->children_num; ++i)
790 oconfig_item_t *child = ci->children + i;
791 if(strcasecmp("Daemon", child->key) == 0)
793 ret = cc_add_daemon_config(child);
799 else if(strcasecmp("LongRunAvgLatency", child->key) == 0)
801 ret = cc_handle_bool(child, &long_run_latency_avg);
807 else if(strcasecmp("ConvertSpecialMetricTypes", child->key) == 0)
809 ret = cc_handle_bool(child, &convert_special_metrics);
817 WARNING("ceph plugin: ignoring unknown option %s", child->key);
824 traverse_json(const unsigned char *json, uint32_t json_len, yajl_handle hand)
826 yajl_status status = yajl_parse(hand, json, json_len);
831 case yajl_status_error:
832 msg = yajl_get_error(hand, /* verbose = */ 1,
833 /* jsonText = */ (unsigned char *) json,
834 (unsigned int) json_len);
835 ERROR ("ceph plugin: yajl_parse failed: %s", msg);
836 yajl_free_error(hand, msg);
838 case yajl_status_client_canceled:
846 node_handler_define_schema(void *arg, const char *val, const char *key)
848 struct ceph_daemon *d = (struct ceph_daemon *) arg;
851 DEBUG("\nceph_daemon_add_ds_entry(d=%s,key=%s,pc_type=%04x)",
852 d->name, key, pc_type);
853 return ceph_daemon_add_ds_entry(d, key, pc_type);
856 static int add_last(const char *dset_n, const char *ds_n, double cur_sum,
859 last_poll_data[last_idx] = malloc(1 * sizeof(struct last_data));
860 if(!last_poll_data[last_idx])
864 sstrncpy(last_poll_data[last_idx]->dset_name,dset_n,
865 sizeof(last_poll_data[last_idx]->dset_name));
866 sstrncpy(last_poll_data[last_idx]->ds_name,ds_n,
867 sizeof(last_poll_data[last_idx]->ds_name));
868 last_poll_data[last_idx]->last_sum = cur_sum;
869 last_poll_data[last_idx]->last_count = cur_count;
874 static int update_last(const char *dset_n, const char *ds_n, double cur_sum,
878 for(i = 0; i < last_idx; i++)
880 if(strcmp(last_poll_data[i]->dset_name,dset_n) == 0 &&
881 (strcmp(last_poll_data[i]->ds_name,ds_n) == 0))
883 last_poll_data[i]->last_sum = cur_sum;
884 last_poll_data[i]->last_count = cur_count;
891 last_poll_data = malloc(1 * sizeof(struct last_data *));
899 struct last_data **tmp_last = realloc(last_poll_data,
900 ((last_idx+1) * sizeof(struct last_data *)));
905 last_poll_data = tmp_last;
907 return add_last(dset_n,ds_n,cur_sum,cur_count);
910 static double get_last_avg(const char *dset_n, const char *ds_n,
911 double cur_sum, uint64_t cur_count)
914 double result = -1.1, sum_delt = 0.0;
915 uint64_t count_delt = 0;
916 for(i = 0; i < last_idx; i++)
918 if((strcmp(last_poll_data[i]->dset_name,dset_n) == 0) &&
919 (strcmp(last_poll_data[i]->ds_name,ds_n) == 0))
921 if(cur_count < last_poll_data[i]->last_count)
925 sum_delt = (cur_sum - last_poll_data[i]->last_sum);
926 count_delt = (cur_count - last_poll_data[i]->last_count);
927 result = (sum_delt / count_delt);
936 if(update_last(dset_n,ds_n,cur_sum,cur_count) == -ENOMEM)
943 static int node_handler_fetch_data(void *arg, const char *val, const char *key)
945 int dset_idx, ds_idx;
947 char dset_name[DATA_MAX_NAME_LEN];
948 char ds_name[MAX_RRD_DS_NAME_LEN];
949 struct values_tmp *vtmp = (struct values_tmp*) arg;
950 memset(dset_name, 0, sizeof(dset_name));
951 memset(ds_name, 0, sizeof(ds_name));
952 if(parse_keys(key, dset_name, ds_name))
954 DEBUG("enter node_handler_fetch_data");
957 dset_idx = get_matching_dset(vtmp->d, dset_name);
962 ds_idx = get_matching_value(&vtmp->d->dset[dset_idx], ds_name,
963 vtmp->d->dset[dset_idx].ds_num);
966 DEBUG("DSet:%s, DS:%s, DSet idx:%d, DS idx:%d",
967 dset_name,ds_name,dset_idx,ds_idx);
968 return RETRY_AVGCOUNT;
970 uv = &(vtmp->vh[dset_idx].values[ds_idx]);
972 if(vtmp->d->pc_types[dset_idx][ds_idx] & PERFCOUNTER_LATENCY)
974 if(vtmp->avgcount == -1)
976 sscanf(val, "%" PRIu64, &vtmp->avgcount);
981 sscanf(val, "%lf", &sum);
982 DEBUG("avgcount:%ld",vtmp->avgcount);
983 DEBUG("sum:%lf",sum);
985 if(vtmp->avgcount == 0)
990 /** User wants latency values as long run avg */
991 if(long_run_latency_avg)
993 result = (sum / vtmp->avgcount);
994 DEBUG("uv->gauge = sumd / avgcounti = :%lf", result);
998 result = get_last_avg(dset_name, ds_name, sum, vtmp->avgcount);
999 if(result == -ENOMEM)
1003 DEBUG("uv->gauge = (sumd_now - sumd_last) / "
1004 "(avgcounti_now - avgcounti_last) = :%lf", result);
1008 vtmp->avgcount = -1;
1011 else if(vtmp->d->pc_types[dset_idx][ds_idx] & PERFCOUNTER_DERIVE)
1013 uint64_t derive_val;
1014 sscanf(val, "%" PRIu64, &derive_val);
1015 uv->derive = derive_val;
1016 DEBUG("uv->derive %" PRIu64 "",(uint64_t)uv->derive);
1021 sscanf(val, "%lf", &other_val);
1022 uv->gauge = other_val;
1023 DEBUG("uv->gauge %lf",uv->gauge);
1028 static int cconn_connect(struct cconn *io)
1030 struct sockaddr_un address;
1032 if(io->state != CSTATE_UNCONNECTED)
1034 ERROR("cconn_connect: io->state != CSTATE_UNCONNECTED");
1037 fd = socket(PF_UNIX, SOCK_STREAM, 0);
1041 ERROR("cconn_connect: socket(PF_UNIX, SOCK_STREAM, 0) failed: "
1045 memset(&address, 0, sizeof(struct sockaddr_un));
1046 address.sun_family = AF_UNIX;
1047 snprintf(address.sun_path, sizeof(address.sun_path), "%s",
1050 connect(fd, (struct sockaddr *) &address, sizeof(struct sockaddr_un)));
1053 ERROR("cconn_connect: connect(%d) failed: error %d", fd, err);
1057 flags = fcntl(fd, F_GETFL, 0);
1058 if(fcntl(fd, F_SETFL, flags | O_NONBLOCK) != 0)
1061 ERROR("cconn_connect: fcntl(%d, O_NONBLOCK) error %d", fd, err);
1065 io->state = CSTATE_WRITE_REQUEST;
1072 static void cconn_close(struct cconn *io)
1074 io->state = CSTATE_UNCONNECTED;
1078 RETRY_ON_EINTR(res, close(io->asok));
1087 /* Process incoming JSON counter data */
1089 cconn_process_data(struct cconn *io, yajl_struct *yajl, yajl_handle hand)
1092 struct values_tmp *vtmp = calloc(1, sizeof(struct values_tmp)
1093 + (sizeof(struct values_holder)) * io->d->dset_num);
1099 for(i = 0; i < io->d->dset_num; i++)
1101 value_t *val = calloc(1, (sizeof(value_t) * io->d->dset[i].ds_num));
1102 vtmp->vh[i].values = val;
1103 vtmp->vh[i].values_len = io->d->dset[i].ds_num;
1106 vtmp->holder_num = io->d->dset_num;
1107 vtmp->avgcount = -1;
1108 yajl->handler_arg = vtmp;
1109 ret = traverse_json(io->json, io->json_len, hand);
1114 for(i = 0; i < vtmp->holder_num; i++)
1116 value_list_t vl = VALUE_LIST_INIT;
1117 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
1118 sstrncpy(vl.plugin, "ceph", sizeof(vl.plugin));
1119 strncpy(vl.plugin_instance, io->d->name, sizeof(vl.plugin_instance));
1120 sstrncpy(vl.type, io->d->dset[i].type, sizeof(vl.type));
1121 vl.values = vtmp->vh[i].values;
1122 vl.values_len = io->d->dset[i].ds_num;
1123 DEBUG("cconn_process_data(io=%s): vl.values_len=%d, json=\"%s\"",
1124 io->d->name, vl.values_len, io->json);
1125 ret = plugin_dispatch_values(&vl);
1132 done: for(i = 0; i < vtmp->holder_num; i++)
1134 sfree(vtmp->vh[i].values);
1140 static int cconn_process_json(struct cconn *io)
1142 if((io->request_type != ASOK_REQ_DATA) &&
1143 (io->request_type != ASOK_REQ_SCHEMA))
1152 hand = yajl_alloc(&callbacks,
1154 /* alloc funcs = */ NULL,
1156 /* alloc funcs = */ NULL, NULL,
1158 /* context = */ (void *)(&io->yajl));
1162 ERROR ("ceph plugin: yajl_alloc failed.");
1168 switch(io->request_type)
1171 io->yajl.handler = node_handler_fetch_data;
1172 result = cconn_process_data(io, &io->yajl, hand);
1174 case ASOK_REQ_SCHEMA:
1175 io->yajl.handler = node_handler_define_schema;
1176 io->yajl.handler_arg = io->d;
1177 result = traverse_json(io->json, io->json_len, hand);
1187 status = yajl_complete_parse(hand);
1189 status = yajl_parse_complete(hand);
1192 if (status != yajl_status_ok)
1194 unsigned char *errmsg = yajl_get_error (hand, /* verbose = */ 0,
1195 /* jsonText = */ NULL, /* jsonTextLen = */ 0);
1196 ERROR ("ceph plugin: yajl_parse_complete failed: %s",
1198 yajl_free_error (hand, errmsg);
1208 static int cconn_validate_revents(struct cconn *io, int revents)
1210 if(revents & POLLERR)
1212 ERROR("cconn_validate_revents(name=%s): got POLLERR", io->d->name);
1217 case CSTATE_WRITE_REQUEST:
1218 return (revents & POLLOUT) ? 0 : -EINVAL;
1219 case CSTATE_READ_VERSION:
1220 case CSTATE_READ_AMT:
1221 case CSTATE_READ_JSON:
1222 return (revents & POLLIN) ? 0 : -EINVAL;
1223 return (revents & POLLIN) ? 0 : -EINVAL;
1225 ERROR("cconn_validate_revents(name=%s) got to illegal state on "
1226 "line %d", io->d->name, __LINE__);
1231 /** Handle a network event for a connection */
1232 static int cconn_handle_event(struct cconn *io)
1237 case CSTATE_UNCONNECTED:
1238 ERROR("cconn_handle_event(name=%s) got to illegal state on line "
1239 "%d", io->d->name, __LINE__);
1242 case CSTATE_WRITE_REQUEST:
1245 snprintf(cmd, sizeof(cmd), "%s%d%s", "{ \"prefix\": \"",
1246 io->request_type, "\" }\n");
1247 size_t cmd_len = strlen(cmd);
1249 write(io->asok, ((char*)&cmd) + io->amt, cmd_len - io->amt));
1250 DEBUG("cconn_handle_event(name=%s,state=%d,amt=%d,ret=%d)",
1251 io->d->name, io->state, io->amt, ret);
1257 if(io->amt >= cmd_len)
1260 switch (io->request_type)
1262 case ASOK_REQ_VERSION:
1263 io->state = CSTATE_READ_VERSION;
1266 io->state = CSTATE_READ_AMT;
1272 case CSTATE_READ_VERSION:
1275 read(io->asok, ((char*)(&io->d->version)) + io->amt,
1276 sizeof(io->d->version) - io->amt));
1277 DEBUG("cconn_handle_event(name=%s,state=%d,ret=%d)",
1278 io->d->name, io->state, ret);
1284 if(io->amt >= sizeof(io->d->version))
1286 io->d->version = ntohl(io->d->version);
1287 if(io->d->version != 1)
1289 ERROR("cconn_handle_event(name=%s) not "
1290 "expecting version %d!", io->d->name, io->d->version);
1292 }DEBUG("cconn_handle_event(name=%s): identified as "
1293 "version %d", io->d->name, io->d->version);
1296 io->request_type = ASOK_REQ_SCHEMA;
1300 case CSTATE_READ_AMT:
1303 read(io->asok, ((char*)(&io->json_len)) + io->amt,
1304 sizeof(io->json_len) - io->amt));
1305 DEBUG("cconn_handle_event(name=%s,state=%d,ret=%d)",
1306 io->d->name, io->state, ret);
1312 if(io->amt >= sizeof(io->json_len))
1314 io->json_len = ntohl(io->json_len);
1316 io->state = CSTATE_READ_JSON;
1317 io->json = calloc(1, io->json_len + 1);
1320 ERROR("ERR CALLOCING IO->JSON");
1326 case CSTATE_READ_JSON:
1329 read(io->asok, io->json + io->amt, io->json_len - io->amt));
1330 DEBUG("cconn_handle_event(name=%s,state=%d,ret=%d)",
1331 io->d->name, io->state, ret);
1337 if(io->amt >= io->json_len)
1339 ret = cconn_process_json(io);
1345 io->request_type = ASOK_REQ_NONE;
1350 ERROR("cconn_handle_event(name=%s) got to illegal state on "
1351 "line %d", io->d->name, __LINE__);
1356 static int cconn_prepare(struct cconn *io, struct pollfd* fds)
1359 if(io->request_type == ASOK_REQ_NONE)
1361 /* The request has already been serviced. */
1364 else if((io->request_type == ASOK_REQ_DATA) && (io->d->dset_num == 0))
1366 /* If there are no counters to report on, don't bother
1373 case CSTATE_UNCONNECTED:
1374 ret = cconn_connect(io);
1384 fds->events = POLLOUT;
1386 case CSTATE_WRITE_REQUEST:
1388 fds->events = POLLOUT;
1390 case CSTATE_READ_VERSION:
1391 case CSTATE_READ_AMT:
1392 case CSTATE_READ_JSON:
1394 fds->events = POLLIN;
1397 ERROR("cconn_prepare(name=%s) got to illegal state on line %d",
1398 io->d->name, __LINE__);
1403 /** Returns the difference between two struct timevals in milliseconds.
1404 * On overflow, we return max/min int.
1406 static int milli_diff(const struct timeval *t1, const struct timeval *t2)
1409 int sec_diff = t1->tv_sec - t2->tv_sec;
1410 int usec_diff = t1->tv_usec - t2->tv_usec;
1411 ret = usec_diff / 1000;
1412 ret += (sec_diff * 1000);
1413 return (ret > INT_MAX) ? INT_MAX : ((ret < INT_MIN) ? INT_MIN : (int)ret);
1416 /** This handles the actual network I/O to talk to the Ceph daemons.
1418 static int cconn_main_loop(uint32_t request_type)
1420 int i, ret, some_unreachable = 0;
1421 struct timeval end_tv;
1422 struct cconn io_array[g_num_daemons];
1424 DEBUG("entering cconn_main_loop(request_type = %d)", request_type);
1426 /* create cconn array */
1427 memset(io_array, 0, sizeof(io_array));
1428 for(i = 0; i < g_num_daemons; ++i)
1430 io_array[i].d = g_daemons[i];
1431 io_array[i].request_type = request_type;
1432 io_array[i].state = CSTATE_UNCONNECTED;
1435 /** Calculate the time at which we should give up */
1436 gettimeofday(&end_tv, NULL);
1437 end_tv.tv_sec += CEPH_TIMEOUT_INTERVAL;
1443 struct cconn *polled_io_array[g_num_daemons];
1444 struct pollfd fds[g_num_daemons];
1445 memset(fds, 0, sizeof(fds));
1447 for(i = 0; i < g_num_daemons; ++i)
1449 struct cconn *io = io_array + i;
1450 ret = cconn_prepare(io, fds + nfds);
1453 WARNING("ERROR: cconn_prepare(name=%s,i=%d,st=%d)=%d",
1454 io->d->name, i, io->state, ret);
1456 io->request_type = ASOK_REQ_NONE;
1457 some_unreachable = 1;
1461 DEBUG("did cconn_prepare(name=%s,i=%d,st=%d)",
1462 io->d->name, i, io->state);
1463 polled_io_array[nfds++] = io_array + i;
1470 DEBUG("cconn_main_loop: no more cconn to manage.");
1473 gettimeofday(&tv, NULL);
1474 diff = milli_diff(&end_tv, &tv);
1479 WARNING("ERROR: cconn_main_loop: timed out.\n");
1482 RETRY_ON_EINTR(ret, poll(fds, nfds, diff));
1485 ERROR("poll(2) error: %d", ret);
1488 for(i = 0; i < nfds; ++i)
1490 struct cconn *io = polled_io_array[i];
1491 int revents = fds[i].revents;
1496 else if(cconn_validate_revents(io, revents))
1498 WARNING("ERROR: cconn(name=%s,i=%d,st=%d): "
1499 "revents validation error: "
1500 "revents=0x%08x", io->d->name, i, io->state, revents);
1502 io->request_type = ASOK_REQ_NONE;
1503 some_unreachable = 1;
1507 int ret = cconn_handle_event(io);
1510 WARNING("ERROR: cconn_handle_event(name=%s,"
1511 "i=%d,st=%d): error %d", io->d->name, i, io->state, ret);
1513 io->request_type = ASOK_REQ_NONE;
1514 some_unreachable = 1;
1519 done: for(i = 0; i < g_num_daemons; ++i)
1521 cconn_close(io_array + i);
1523 if(some_unreachable)
1525 DEBUG("cconn_main_loop: some Ceph daemons were unreachable.");
1529 DEBUG("cconn_main_loop: reached all Ceph daemons :)");
1534 static int ceph_read(void)
1536 return cconn_main_loop(ASOK_REQ_DATA);
1539 /******* lifecycle *******/
1540 static int ceph_init(void)
1544 ceph_daemons_print();
1546 ret = cconn_main_loop(ASOK_REQ_VERSION);
1551 for(i = 0; i < g_num_daemons; ++i)
1553 struct ceph_daemon *d = g_daemons[i];
1554 for(j = 0; j < d->dset_num; j++)
1556 ret = plugin_register_data_set(d->dset + j);
1559 ERROR("plugin_register_data_set(%s) failed!", d->name);
1563 DEBUG("plugin_register_data_set(%s): "
1564 "(d->dset)[%d]->ds_num=%d",
1565 d->name, j, d->dset[j].ds_num);
1572 static int ceph_shutdown(void)
1575 for(i = 0; i < g_num_daemons; ++i)
1577 ceph_daemon_free(g_daemons[i]);
1582 for(i = 0; i < last_idx; i++)
1584 sfree(last_poll_data[i]);
1586 sfree(last_poll_data);
1587 last_poll_data = NULL;
1589 DEBUG("finished ceph_shutdown");
1593 void module_register(void)
1595 plugin_register_complex_config("ceph", ceph_config);
1596 plugin_register_init("ceph", ceph_init);
1597 plugin_register_read("ceph", ceph_read);
1598 plugin_register_shutdown("ceph", ceph_shutdown);