5b7923915db798d1c62419c124addf136eecb56a
1 /**
2 * collectd - src/ceph.c
3 * Copyright (C) 2011 New Dream Network
4 * Copyright (C) 2015 Florian octo Forster
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Colin McCabe <cmccabe at alumni.cmu.edu>
21 * Dennis Zou <yunzou at cisco.com>
22 * Dan Ryder <daryder at cisco.com>
23 * Florian octo Forster <octo at collectd.org>
24 **/
26 #define _DEFAULT_SOURCE
27 #define _BSD_SOURCE
29 #include "collectd.h"
31 #include "common.h"
32 #include "plugin.h"
34 #include <arpa/inet.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <yajl/yajl_parse.h>
38 #if HAVE_YAJL_YAJL_VERSION_H
39 #include <yajl/yajl_version.h>
40 #endif
41 #ifdef HAVE_SYS_CAPABILITY_H
42 # include <sys/capability.h>
43 #endif
45 #include <limits.h>
46 #include <poll.h>
47 #include <stdint.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <strings.h>
52 #include <sys/time.h>
53 #include <sys/types.h>
54 #include <sys/un.h>
55 #include <unistd.h>
56 #include <math.h>
57 #include <inttypes.h>
59 #define RETRY_AVGCOUNT -1
61 #if defined(YAJL_MAJOR) && (YAJL_MAJOR > 1)
62 # define HAVE_YAJL_V2 1
63 #endif
65 #define RETRY_ON_EINTR(ret, expr) \
66 while(1) { \
67 ret = expr; \
68 if(ret >= 0) \
69 break; \
70 ret = -errno; \
71 if(ret != -EINTR) \
72 break; \
73 }
75 /** Timeout interval in seconds */
76 #define CEPH_TIMEOUT_INTERVAL 1
78 /** Maximum path length for a UNIX domain socket on this system */
79 #define UNIX_DOMAIN_SOCK_PATH_MAX (sizeof(((struct sockaddr_un*)0)->sun_path))
81 /** Yajl callback returns */
82 #define CEPH_CB_CONTINUE 1
83 #define CEPH_CB_ABORT 0
85 #if HAVE_YAJL_V2
86 typedef size_t yajl_len_t;
87 #else
88 typedef unsigned int yajl_len_t;
89 #endif
91 /** Number of types for ceph defined in types.db */
92 #define CEPH_DSET_TYPES_NUM 3
93 /** ceph types enum */
94 enum ceph_dset_type_d
95 {
96 DSET_LATENCY = 0,
97 DSET_BYTES = 1,
98 DSET_RATE = 2,
99 DSET_TYPE_UNFOUND = 1000
100 };
102 /** Valid types for ceph defined in types.db */
103 static const char * const ceph_dset_types [CEPH_DSET_TYPES_NUM] =
104 {"ceph_latency", "ceph_bytes", "ceph_rate"};
106 /******* ceph_daemon *******/
107 struct ceph_daemon
108 {
109 /** Version of the admin_socket interface */
110 uint32_t version;
111 /** daemon name **/
112 char name[DATA_MAX_NAME_LEN];
114 /** Path to the socket that we use to talk to the ceph daemon */
115 char asok_path[UNIX_DOMAIN_SOCK_PATH_MAX];
117 /** Number of counters */
118 int ds_num;
119 /** Track ds types */
120 uint32_t *ds_types;
121 /** Track ds names to match with types */
122 char **ds_names;
124 /**
125 * Keep track of last data for latency values so we can calculate rate
126 * since last poll.
127 */
128 struct last_data **last_poll_data;
129 /** index of last poll data */
130 int last_idx;
131 };
133 /******* JSON parsing *******/
134 typedef int (*node_handler_t)(void *, const char*, const char*);
136 /** Track state and handler while parsing JSON */
137 struct yajl_struct
138 {
139 node_handler_t handler;
140 void * handler_arg;
142 char *key;
143 char *stack[YAJL_MAX_DEPTH];
144 size_t depth;
145 };
146 typedef struct yajl_struct yajl_struct;
148 enum perfcounter_type_d
149 {
150 PERFCOUNTER_LATENCY = 0x4, PERFCOUNTER_DERIVE = 0x8,
151 };
153 /** Give user option to use default (long run = since daemon started) avg */
154 static int long_run_latency_avg = 0;
156 /**
157 * Give user option to use default type for special cases -
158 * filestore.journal_wr_bytes is currently only metric here. Ceph reports the
159 * type as a sum/count pair and will calculate it the same as a latency value.
160 * All other "bytes" metrics (excluding the used/capacity bytes for the OSD)
161 * use the DERIVE type. Unless user specifies to use given type, convert this
162 * metric to use DERIVE.
163 */
164 static int convert_special_metrics = 1;
166 /** Array of daemons to monitor */
167 static struct ceph_daemon **g_daemons = NULL;
169 /** Number of elements in g_daemons */
170 static size_t g_num_daemons = 0;
172 /**
173 * A set of data that we build up in memory while parsing the JSON.
174 */
175 struct values_tmp
176 {
177 /** ceph daemon we are processing data for*/
178 struct ceph_daemon *d;
179 /** track avgcount across counters for avgcount/sum latency pairs */
180 uint64_t avgcount;
181 /** current index of counters - used to get type of counter */
182 int index;
183 /** do we already have an avgcount for latency pair */
184 int avgcount_exists;
185 /**
186 * similar to index, but current index of latency type counters -
187 * used to get last poll data of counter
188 */
189 int latency_index;
190 /**
191 * values list - maintain across counters since
192 * host/plugin/plugin instance are always the same
193 */
194 value_list_t vlist;
195 };
197 /**
198 * A set of count/sum pairs to keep track of latency types and get difference
199 * between this poll data and last poll data.
200 */
201 struct last_data
202 {
203 char ds_name[DATA_MAX_NAME_LEN];
204 double last_sum;
205 uint64_t last_count;
206 };
208 /******* network I/O *******/
209 enum cstate_t
210 {
211 CSTATE_UNCONNECTED = 0,
212 CSTATE_WRITE_REQUEST,
213 CSTATE_READ_VERSION,
214 CSTATE_READ_AMT,
215 CSTATE_READ_JSON,
216 };
218 enum request_type_t
219 {
220 ASOK_REQ_VERSION = 0,
221 ASOK_REQ_DATA = 1,
222 ASOK_REQ_SCHEMA = 2,
223 ASOK_REQ_NONE = 1000,
224 };
226 struct cconn
227 {
228 /** The Ceph daemon that we're talking to */
229 struct ceph_daemon *d;
231 /** Request type */
232 uint32_t request_type;
234 /** The connection state */
235 enum cstate_t state;
237 /** The socket we use to talk to this daemon */
238 int asok;
240 /** The amount of data remaining to read / write. */
241 uint32_t amt;
243 /** Length of the JSON to read */
244 uint32_t json_len;
246 /** Buffer containing JSON data */
247 unsigned char *json;
249 /** Keep data important to yajl processing */
250 struct yajl_struct yajl;
251 };
253 static int ceph_cb_null(void *ctx)
254 {
255 return CEPH_CB_CONTINUE;
256 }
258 static int ceph_cb_boolean(void *ctx, int bool_val)
259 {
260 return CEPH_CB_CONTINUE;
261 }
263 #define BUFFER_ADD(dest, src) do { \
264 size_t dest_size = sizeof (dest); \
265 size_t dest_len = strlen (dest); \
266 if (dest_size > dest_len) { \
267 sstrncpy ((dest) + dest_len, (src), dest_size - dest_len); \
268 } \
269 (dest)[dest_size - 1] = 0; \
270 } while (0)
272 static int
273 ceph_cb_number(void *ctx, const char *number_val, yajl_len_t number_len)
274 {
275 yajl_struct *state = (yajl_struct*) ctx;
276 char buffer[number_len+1];
277 char key[2 * DATA_MAX_NAME_LEN] = { 0 };
278 _Bool latency_type = 0;
279 int status;
281 memcpy(buffer, number_val, number_len);
282 buffer[sizeof(buffer) - 1] = '\0';
284 for (size_t i = 0; i < state->depth; i++)
285 {
286 if (state->stack[i] == NULL)
287 continue;
289 if (strlen (key) != 0)
290 BUFFER_ADD (key, ".");
291 BUFFER_ADD (key, state->stack[i]);
292 }
294 /* Special case for latency metrics. */
295 if ((strcmp ("avgcount", state->key) == 0)
296 || (strcmp ("sum", state->key) == 0))
297 {
298 latency_type = 1;
300 /* Super-special case for filestore.journal_wr_bytes.avgcount: For
301 * some reason, Ceph schema encodes this as a count/sum pair while all
302 * other "Bytes" data (excluding used/capacity bytes for OSD space) uses
303 * a single "Derive" type. To spare further confusion, keep this KPI as
304 * the same type of other "Bytes". Instead of keeping an "average" or
305 * "rate", use the "sum" in the pair and assign that to the derive
306 * value. */
307 if (convert_special_metrics && (state->depth >= 2)
308 && (strcmp("filestore", state->stack[state->depth - 2]) == 0)
309 && (strcmp("journal_wr_bytes", state->stack[state->depth - 1]) == 0)
310 && (strcmp("avgcount", state->key) == 0))
311 {
312 DEBUG("ceph plugin: Skipping avgcount for filestore.JournalWrBytes");
313 return CEPH_CB_CONTINUE;
314 }
315 }
316 else /* not a latency type */
317 {
318 BUFFER_ADD (key, ".");
319 BUFFER_ADD (key, state->key);
320 }
322 status = state->handler(state->handler_arg, buffer, key);
323 if((status == RETRY_AVGCOUNT) && latency_type)
324 {
325 /* Add previously skipped part of the key, either "avgcount" or "sum",
326 * and try again. */
327 BUFFER_ADD (key, ".");
328 BUFFER_ADD (key, state->key);
330 status = state->handler(state->handler_arg, buffer, key);
331 }
333 if (status != 0)
334 {
335 ERROR("ceph plugin: JSON handler failed with status %d.", status);
336 return CEPH_CB_ABORT;
337 }
339 return CEPH_CB_CONTINUE;
340 }
342 static int ceph_cb_string(void *ctx, const unsigned char *string_val,
343 yajl_len_t string_len)
344 {
345 return CEPH_CB_CONTINUE;
346 }
348 static int ceph_cb_start_map(void *ctx)
349 {
350 yajl_struct *state = (yajl_struct*) ctx;
352 /* Push key to the stack */
353 if (state->depth == YAJL_MAX_DEPTH)
354 return CEPH_CB_ABORT;
356 state->stack[state->depth] = state->key;
357 state->depth++;
358 state->key = NULL;
360 return CEPH_CB_CONTINUE;
361 }
363 static int ceph_cb_end_map(void *ctx)
364 {
365 yajl_struct *state = (yajl_struct*) ctx;
367 /* Pop key from the stack */
368 if (state->depth == 0)
369 return CEPH_CB_ABORT;
371 sfree (state->key);
372 state->depth--;
373 state->key = state->stack[state->depth];
374 state->stack[state->depth] = NULL;
376 return CEPH_CB_CONTINUE;
377 }
379 static int
380 ceph_cb_map_key(void *ctx, const unsigned char *key, yajl_len_t string_len)
381 {
382 yajl_struct *state = (yajl_struct*) ctx;
383 size_t sz = ((size_t) string_len) + 1;
385 sfree (state->key);
386 state->key = malloc (sz);
387 if (state->key == NULL)
388 {
389 ERROR ("ceph plugin: malloc failed.");
390 return CEPH_CB_ABORT;
391 }
393 memmove (state->key, key, sz - 1);
394 state->key[sz - 1] = 0;
396 return CEPH_CB_CONTINUE;
397 }
399 static int ceph_cb_start_array(void *ctx)
400 {
401 return CEPH_CB_CONTINUE;
402 }
404 static int ceph_cb_end_array(void *ctx)
405 {
406 return CEPH_CB_CONTINUE;
407 }
409 static yajl_callbacks callbacks = {
410 ceph_cb_null,
411 ceph_cb_boolean,
412 NULL,
413 NULL,
414 ceph_cb_number,
415 ceph_cb_string,
416 ceph_cb_start_map,
417 ceph_cb_map_key,
418 ceph_cb_end_map,
419 ceph_cb_start_array,
420 ceph_cb_end_array
421 };
423 static void ceph_daemon_print(const struct ceph_daemon *d)
424 {
425 DEBUG("ceph plugin: name=%s, asok_path=%s", d->name, d->asok_path);
426 }
428 static void ceph_daemons_print(void)
429 {
430 for(size_t i = 0; i < g_num_daemons; ++i)
431 {
432 ceph_daemon_print(g_daemons[i]);
433 }
434 }
436 static void ceph_daemon_free(struct ceph_daemon *d)
437 {
438 for(int i = 0; i < d->last_idx; i++)
439 {
440 sfree(d->last_poll_data[i]);
441 }
442 sfree(d->last_poll_data);
443 d->last_poll_data = NULL;
444 d->last_idx = 0;
446 for(int i = 0; i < d->ds_num; i++)
447 {
448 sfree(d->ds_names[i]);
449 }
450 sfree(d->ds_types);
451 sfree(d->ds_names);
452 sfree(d);
453 }
455 /* compact_ds_name removed the special characters ":", "_", "-" and "+" from the
456 * intput string. Characters following these special characters are capitalized.
457 * Trailing "+" and "-" characters are replaces with the strings "Plus" and
458 * "Minus". */
459 static int compact_ds_name (char *buffer, size_t buffer_size, char const *src)
460 {
461 char *src_copy;
462 size_t src_len;
463 char *ptr = buffer;
464 size_t ptr_size = buffer_size;
465 _Bool append_plus = 0;
466 _Bool append_minus = 0;
468 if ((buffer == NULL) || (buffer_size <= strlen ("Minus")) || (src == NULL))
469 return EINVAL;
471 src_copy = strdup (src);
472 src_len = strlen(src);
474 /* Remove trailing "+" and "-". */
475 if (src_copy[src_len - 1] == '+')
476 {
477 append_plus = 1;
478 src_len--;
479 src_copy[src_len] = 0;
480 }
481 else if (src_copy[src_len - 1] == '-')
482 {
483 append_minus = 1;
484 src_len--;
485 src_copy[src_len] = 0;
486 }
488 /* Split at special chars, capitalize first character, append to buffer. */
489 char *dummy = src_copy;
490 char *token;
491 char *save_ptr = NULL;
492 while ((token = strtok_r (dummy, ":_-+", &save_ptr)) != NULL)
493 {
494 size_t len;
496 dummy = NULL;
498 token[0] = toupper ((int) token[0]);
500 assert (ptr_size > 1);
502 len = strlen (token);
503 if (len >= ptr_size)
504 len = ptr_size - 1;
506 assert (len > 0);
507 assert (len < ptr_size);
509 sstrncpy (ptr, token, len + 1);
510 ptr += len;
511 ptr_size -= len;
513 assert (*ptr == 0);
514 if (ptr_size <= 1)
515 break;
516 }
518 /* Append "Plus" or "Minus" if "+" or "-" has been stripped above. */
519 if (append_plus || append_minus)
520 {
521 char const *append = "Plus";
522 if (append_minus)
523 append = "Minus";
525 size_t offset = buffer_size - (strlen (append) + 1);
526 if (offset > strlen (buffer))
527 offset = strlen (buffer);
529 sstrncpy (buffer + offset, append, buffer_size - offset);
530 }
532 sfree (src_copy);
533 return 0;
534 }
536 static _Bool has_suffix (char const *str, char const *suffix)
537 {
538 size_t str_len = strlen (str);
539 size_t suffix_len = strlen (suffix);
540 size_t offset;
542 if (suffix_len > str_len)
543 return 0;
544 offset = str_len - suffix_len;
546 if (strcmp (str + offset, suffix) == 0)
547 return 1;
549 return 0;
550 }
552 /* count_parts returns the number of elements a "foo.bar.baz" style key has. */
553 static size_t count_parts (char const *key)
554 {
555 size_t parts_num = 0;
557 for (const char *ptr = key; ptr != NULL; ptr = strchr (ptr + 1, '.'))
558 parts_num++;
560 return parts_num;
561 }
563 /**
564 * Parse key to remove "type" if this is for schema and initiate compaction
565 */
566 static int parse_keys (char *buffer, size_t buffer_size, const char *key_str)
567 {
568 char tmp[2 * buffer_size];
570 if (buffer == NULL || buffer_size == 0 || key_str == NULL || strlen (key_str) == 0)
571 return EINVAL;
573 if ((count_parts (key_str) > 2) && has_suffix (key_str, ".type"))
574 {
575 /* strip ".type" suffix iff the key has more than two parts. */
576 size_t sz = strlen (key_str) - strlen (".type") + 1;
578 if (sz > sizeof (tmp))
579 sz = sizeof (tmp);
580 sstrncpy (tmp, key_str, sz);
581 }
582 else
583 {
584 sstrncpy (tmp, key_str, sizeof (tmp));
585 }
587 return compact_ds_name (buffer, buffer_size, tmp);
588 }
590 /**
591 * while parsing ceph admin socket schema, save counter name and type for later
592 * data processing
593 */
594 static int ceph_daemon_add_ds_entry(struct ceph_daemon *d, const char *name,
595 int pc_type)
596 {
597 uint32_t type;
598 char ds_name[DATA_MAX_NAME_LEN];
600 if(convert_special_metrics)
601 {
602 /**
603 * Special case for filestore:JournalWrBytes. For some reason, Ceph
604 * schema encodes this as a count/sum pair while all other "Bytes" data
605 * (excluding used/capacity bytes for OSD space) uses a single "Derive"
606 * type. To spare further confusion, keep this KPI as the same type of
607 * other "Bytes". Instead of keeping an "average" or "rate", use the
608 * "sum" in the pair and assign that to the derive value.
609 */
610 if((strcmp(name,"filestore.journal_wr_bytes.type") == 0))
611 {
612 pc_type = 10;
613 }
614 }
616 d->ds_names = realloc(d->ds_names, sizeof(char *) * (d->ds_num + 1));
617 if(!d->ds_names)
618 {
619 return -ENOMEM;
620 }
622 d->ds_types = realloc(d->ds_types, sizeof(uint32_t) * (d->ds_num + 1));
623 if(!d->ds_types)
624 {
625 return -ENOMEM;
626 }
628 d->ds_names[d->ds_num] = malloc(DATA_MAX_NAME_LEN);
629 if(!d->ds_names[d->ds_num])
630 {
631 return -ENOMEM;
632 }
634 type = (pc_type & PERFCOUNTER_DERIVE) ? DSET_RATE :
635 ((pc_type & PERFCOUNTER_LATENCY) ? DSET_LATENCY : DSET_BYTES);
636 d->ds_types[d->ds_num] = type;
638 if (parse_keys(ds_name, sizeof (ds_name), name))
639 {
640 return 1;
641 }
643 sstrncpy(d->ds_names[d->ds_num], ds_name, DATA_MAX_NAME_LEN -1);
644 d->ds_num = (d->ds_num + 1);
646 return 0;
647 }
649 /******* ceph_config *******/
650 static int cc_handle_str(struct oconfig_item_s *item, char *dest, int dest_len)
651 {
652 const char *val;
653 if(item->values_num != 1)
654 {
655 return -ENOTSUP;
656 }
657 if(item->values[0].type != OCONFIG_TYPE_STRING)
658 {
659 return -ENOTSUP;
660 }
661 val = item->values[0].value.string;
662 if(snprintf(dest, dest_len, "%s", val) > (dest_len - 1))
663 {
664 ERROR("ceph plugin: configuration parameter '%s' is too long.\n",
665 item->key);
666 return -ENAMETOOLONG;
667 }
668 return 0;
669 }
671 static int cc_handle_bool(struct oconfig_item_s *item, int *dest)
672 {
673 if(item->values_num != 1)
674 {
675 return -ENOTSUP;
676 }
678 if(item->values[0].type != OCONFIG_TYPE_BOOLEAN)
679 {
680 return -ENOTSUP;
681 }
683 *dest = (item->values[0].value.boolean) ? 1 : 0;
684 return 0;
685 }
687 static int cc_add_daemon_config(oconfig_item_t *ci)
688 {
689 int ret;
690 struct ceph_daemon *nd, cd = { 0 };
691 struct ceph_daemon **tmp;
693 if((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
694 {
695 WARNING("ceph plugin: `Daemon' blocks need exactly one string "
696 "argument.");
697 return (-1);
698 }
700 ret = cc_handle_str(ci, cd.name, DATA_MAX_NAME_LEN);
701 if(ret)
702 {
703 return ret;
704 }
706 for(int i=0; i < ci->children_num; i++)
707 {
708 oconfig_item_t *child = ci->children + i;
710 if(strcasecmp("SocketPath", child->key) == 0)
711 {
712 ret = cc_handle_str(child, cd.asok_path, sizeof(cd.asok_path));
713 if(ret)
714 {
715 return ret;
716 }
717 }
718 else
719 {
720 WARNING("ceph plugin: ignoring unknown option %s", child->key);
721 }
722 }
723 if(cd.name[0] == '\0')
724 {
725 ERROR("ceph plugin: you must configure a daemon name.\n");
726 return -EINVAL;
727 }
728 else if(cd.asok_path[0] == '\0')
729 {
730 ERROR("ceph plugin(name=%s): you must configure an administrative "
731 "socket path.\n", cd.name);
732 return -EINVAL;
733 }
734 else if(!((cd.asok_path[0] == '/') ||
735 (cd.asok_path[0] == '.' && cd.asok_path[1] == '/')))
736 {
737 ERROR("ceph plugin(name=%s): administrative socket paths must begin "
738 "with '/' or './' Can't parse: '%s'\n", cd.name, cd.asok_path);
739 return -EINVAL;
740 }
742 tmp = realloc(g_daemons, (g_num_daemons+1) * sizeof(*g_daemons));
743 if(tmp == NULL)
744 {
745 /* The positive return value here indicates that this is a
746 * runtime error, not a configuration error. */
747 return ENOMEM;
748 }
749 g_daemons = tmp;
751 nd = malloc(sizeof (*nd));
752 if(!nd)
753 {
754 return ENOMEM;
755 }
756 memcpy(nd, &cd, sizeof(*nd));
757 g_daemons[g_num_daemons] = nd;
758 g_num_daemons++;
759 return 0;
760 }
762 static int ceph_config(oconfig_item_t *ci)
763 {
764 int ret;
766 for(int i = 0; i < ci->children_num; ++i)
767 {
768 oconfig_item_t *child = ci->children + i;
769 if(strcasecmp("Daemon", child->key) == 0)
770 {
771 ret = cc_add_daemon_config(child);
772 if(ret == ENOMEM)
773 {
774 ERROR("ceph plugin: Couldn't allocate memory");
775 return ret;
776 }
777 else if(ret)
778 {
779 //process other daemons and ignore this one
780 continue;
781 }
782 }
783 else if(strcasecmp("LongRunAvgLatency", child->key) == 0)
784 {
785 ret = cc_handle_bool(child, &long_run_latency_avg);
786 if(ret)
787 {
788 return ret;
789 }
790 }
791 else if(strcasecmp("ConvertSpecialMetricTypes", child->key) == 0)
792 {
793 ret = cc_handle_bool(child, &convert_special_metrics);
794 if(ret)
795 {
796 return ret;
797 }
798 }
799 else
800 {
801 WARNING("ceph plugin: ignoring unknown option %s", child->key);
802 }
803 }
804 return 0;
805 }
807 /**
808 * Parse JSON and get error message if present
809 */
810 static int
811 traverse_json(const unsigned char *json, uint32_t json_len, yajl_handle hand)
812 {
813 yajl_status status = yajl_parse(hand, json, json_len);
814 unsigned char *msg;
816 switch(status)
817 {
818 case yajl_status_error:
819 msg = yajl_get_error(hand, /* verbose = */ 1,
820 /* jsonText = */ (unsigned char *) json,
821 (unsigned int) json_len);
822 ERROR ("ceph plugin: yajl_parse failed: %s", msg);
823 yajl_free_error(hand, msg);
824 return 1;
825 case yajl_status_client_canceled:
826 return 1;
827 default:
828 return 0;
829 }
830 }
832 /**
833 * Add entry for each counter while parsing schema
834 */
835 static int
836 node_handler_define_schema(void *arg, const char *val, const char *key)
837 {
838 struct ceph_daemon *d = (struct ceph_daemon *) arg;
839 int pc_type;
840 pc_type = atoi(val);
841 return ceph_daemon_add_ds_entry(d, key, pc_type);
842 }
844 /**
845 * Latency counter does not yet have an entry in last poll data - add it.
846 */
847 static int add_last(struct ceph_daemon *d, const char *ds_n, double cur_sum,
848 uint64_t cur_count)
849 {
850 d->last_poll_data[d->last_idx] = malloc(sizeof (*d->last_poll_data[d->last_idx]));
851 if(!d->last_poll_data[d->last_idx])
852 {
853 return -ENOMEM;
854 }
855 sstrncpy(d->last_poll_data[d->last_idx]->ds_name,ds_n,
856 sizeof(d->last_poll_data[d->last_idx]->ds_name));
857 d->last_poll_data[d->last_idx]->last_sum = cur_sum;
858 d->last_poll_data[d->last_idx]->last_count = cur_count;
859 d->last_idx = (d->last_idx + 1);
860 return 0;
861 }
863 /**
864 * Update latency counter or add new entry if it doesn't exist
865 */
866 static int update_last(struct ceph_daemon *d, const char *ds_n, int index,
867 double cur_sum, uint64_t cur_count)
868 {
869 if((d->last_idx > index) && (strcmp(d->last_poll_data[index]->ds_name, ds_n) == 0))
870 {
871 d->last_poll_data[index]->last_sum = cur_sum;
872 d->last_poll_data[index]->last_count = cur_count;
873 return 0;
874 }
876 if(!d->last_poll_data)
877 {
878 d->last_poll_data = malloc(sizeof (*d->last_poll_data));
879 if(!d->last_poll_data)
880 {
881 return -ENOMEM;
882 }
883 }
884 else
885 {
886 struct last_data **tmp_last = realloc(d->last_poll_data,
887 ((d->last_idx+1) * sizeof(struct last_data *)));
888 if(!tmp_last)
889 {
890 return -ENOMEM;
891 }
892 d->last_poll_data = tmp_last;
893 }
894 return add_last(d, ds_n, cur_sum, cur_count);
895 }
897 /**
898 * If using index guess failed (shouldn't happen, but possible if counters
899 * get rearranged), resort to searching for counter name
900 */
901 static int backup_search_for_last_avg(struct ceph_daemon *d, const char *ds_n)
902 {
903 for(int i = 0; i < d->last_idx; i++)
904 {
905 if(strcmp(d->last_poll_data[i]->ds_name, ds_n) == 0)
906 {
907 return i;
908 }
909 }
910 return -1;
911 }
913 /**
914 * Calculate average b/t current data and last poll data
915 * if last poll data exists
916 */
917 static double get_last_avg(struct ceph_daemon *d, const char *ds_n, int index,
918 double cur_sum, uint64_t cur_count)
919 {
920 double result = -1.1, sum_delt = 0.0;
921 uint64_t count_delt = 0;
922 int tmp_index = 0;
923 if(d->last_idx > index)
924 {
925 if(strcmp(d->last_poll_data[index]->ds_name, ds_n) == 0)
926 {
927 tmp_index = index;
928 }
929 //test previous index
930 else if((index > 0) && (strcmp(d->last_poll_data[index-1]->ds_name, ds_n) == 0))
931 {
932 tmp_index = (index - 1);
933 }
934 else
935 {
936 tmp_index = backup_search_for_last_avg(d, ds_n);
937 }
939 if((tmp_index > -1) && (cur_count > d->last_poll_data[tmp_index]->last_count))
940 {
941 sum_delt = (cur_sum - d->last_poll_data[tmp_index]->last_sum);
942 count_delt = (cur_count - d->last_poll_data[tmp_index]->last_count);
943 result = (sum_delt / count_delt);
944 }
945 }
947 if(result == -1.1)
948 {
949 result = NAN;
950 }
951 if(update_last(d, ds_n, tmp_index, cur_sum, cur_count) == -ENOMEM)
952 {
953 return -ENOMEM;
954 }
955 return result;
956 }
958 /**
959 * If using index guess failed, resort to searching for counter name
960 */
961 static uint32_t backup_search_for_type(struct ceph_daemon *d, char *ds_name)
962 {
963 for(int i = 0; i < d->ds_num; i++)
964 {
965 if(strcmp(d->ds_names[i], ds_name) == 0)
966 {
967 return d->ds_types[i];
968 }
969 }
970 return DSET_TYPE_UNFOUND;
971 }
973 /**
974 * Process counter data and dispatch values
975 */
976 static int node_handler_fetch_data(void *arg, const char *val, const char *key)
977 {
978 value_t uv;
979 double tmp_d;
980 uint64_t tmp_u;
981 struct values_tmp *vtmp = (struct values_tmp*) arg;
982 uint32_t type = DSET_TYPE_UNFOUND;
983 int index = vtmp->index;
985 char ds_name[DATA_MAX_NAME_LEN];
987 if (parse_keys (ds_name, sizeof (ds_name), key))
988 {
989 return 1;
990 }
992 if(index >= vtmp->d->ds_num)
993 {
994 //don't overflow bounds of array
995 index = (vtmp->d->ds_num - 1);
996 }
998 /**
999 * counters should remain in same order we parsed schema... we maintain the
1000 * index variable to keep track of current point in list of counters. first
1001 * use index to guess point in array for retrieving type. if that doesn't
1002 * work, use the old way to get the counter type
1003 */
1004 if(strcmp(ds_name, vtmp->d->ds_names[index]) == 0)
1005 {
1006 //found match
1007 type = vtmp->d->ds_types[index];
1008 }
1009 else if((index > 0) && (strcmp(ds_name, vtmp->d->ds_names[index-1]) == 0))
1010 {
1011 //try previous key
1012 type = vtmp->d->ds_types[index-1];
1013 }
1015 if(type == DSET_TYPE_UNFOUND)
1016 {
1017 //couldn't find right type by guessing, check the old way
1018 type = backup_search_for_type(vtmp->d, ds_name);
1019 }
1021 switch(type)
1022 {
1023 case DSET_LATENCY:
1024 if(vtmp->avgcount_exists == -1)
1025 {
1026 sscanf(val, "%" PRIu64, &vtmp->avgcount);
1027 vtmp->avgcount_exists = 0;
1028 //return after saving avgcount - don't dispatch value
1029 //until latency calculation
1030 return 0;
1031 }
1032 else
1033 {
1034 double sum, result;
1035 sscanf(val, "%lf", &sum);
1037 if(vtmp->avgcount == 0)
1038 {
1039 vtmp->avgcount = 1;
1040 }
1042 /** User wants latency values as long run avg */
1043 if(long_run_latency_avg)
1044 {
1045 result = (sum / vtmp->avgcount);
1046 }
1047 else
1048 {
1049 result = get_last_avg(vtmp->d, ds_name, vtmp->latency_index, sum, vtmp->avgcount);
1050 if(result == -ENOMEM)
1051 {
1052 return -ENOMEM;
1053 }
1054 }
1056 uv.gauge = result;
1057 vtmp->avgcount_exists = -1;
1058 vtmp->latency_index = (vtmp->latency_index + 1);
1059 }
1060 break;
1061 case DSET_BYTES:
1062 sscanf(val, "%lf", &tmp_d);
1063 uv.gauge = tmp_d;
1064 break;
1065 case DSET_RATE:
1066 sscanf(val, "%" PRIu64, &tmp_u);
1067 uv.derive = tmp_u;
1068 break;
1069 case DSET_TYPE_UNFOUND:
1070 default:
1071 ERROR("ceph plugin: ds %s was not properly initialized.", ds_name);
1072 return -1;
1073 }
1075 sstrncpy(vtmp->vlist.type, ceph_dset_types[type], sizeof(vtmp->vlist.type));
1076 sstrncpy(vtmp->vlist.type_instance, ds_name, sizeof(vtmp->vlist.type_instance));
1077 vtmp->vlist.values = &uv;
1078 vtmp->vlist.values_len = 1;
1080 vtmp->index = (vtmp->index + 1);
1081 plugin_dispatch_values(&vtmp->vlist);
1083 return 0;
1084 }
1086 static int cconn_connect(struct cconn *io)
1087 {
1088 struct sockaddr_un address = { 0 };
1089 int flags, fd, err;
1090 if(io->state != CSTATE_UNCONNECTED)
1091 {
1092 ERROR("ceph plugin: cconn_connect: io->state != CSTATE_UNCONNECTED");
1093 return -EDOM;
1094 }
1095 fd = socket(PF_UNIX, SOCK_STREAM, 0);
1096 if(fd < 0)
1097 {
1098 err = -errno;
1099 ERROR("ceph plugin: cconn_connect: socket(PF_UNIX, SOCK_STREAM, 0) "
1100 "failed: error %d", err);
1101 return err;
1102 }
1103 address.sun_family = AF_UNIX;
1104 snprintf(address.sun_path, sizeof(address.sun_path), "%s",
1105 io->d->asok_path);
1106 RETRY_ON_EINTR(err,
1107 connect(fd, (struct sockaddr *) &address, sizeof(struct sockaddr_un)));
1108 if(err < 0)
1109 {
1110 ERROR("ceph plugin: cconn_connect: connect(%d) failed: error %d",
1111 fd, err);
1112 close(fd);
1113 return err;
1114 }
1116 flags = fcntl(fd, F_GETFL, 0);
1117 if(fcntl(fd, F_SETFL, flags | O_NONBLOCK) != 0)
1118 {
1119 err = -errno;
1120 ERROR("ceph plugin: cconn_connect: fcntl(%d, O_NONBLOCK) error %d",
1121 fd, err);
1122 close(fd);
1123 return err;
1124 }
1125 io->asok = fd;
1126 io->state = CSTATE_WRITE_REQUEST;
1127 io->amt = 0;
1128 io->json_len = 0;
1129 io->json = NULL;
1130 return 0;
1131 }
1133 static void cconn_close(struct cconn *io)
1134 {
1135 io->state = CSTATE_UNCONNECTED;
1136 if(io->asok != -1)
1137 {
1138 int res;
1139 RETRY_ON_EINTR(res, close(io->asok));
1140 }
1141 io->asok = -1;
1142 io->amt = 0;
1143 io->json_len = 0;
1144 sfree(io->json);
1145 io->json = NULL;
1146 }
1148 /* Process incoming JSON counter data */
1149 static int
1150 cconn_process_data(struct cconn *io, yajl_struct *yajl, yajl_handle hand)
1151 {
1152 int ret;
1153 struct values_tmp *vtmp = calloc(1, sizeof(struct values_tmp) * 1);
1154 if(!vtmp)
1155 {
1156 return -ENOMEM;
1157 }
1159 vtmp->vlist = (value_list_t)VALUE_LIST_INIT;
1160 sstrncpy(vtmp->vlist.host, hostname_g, sizeof(vtmp->vlist.host));
1161 sstrncpy(vtmp->vlist.plugin, "ceph", sizeof(vtmp->vlist.plugin));
1162 sstrncpy(vtmp->vlist.plugin_instance, io->d->name, sizeof(vtmp->vlist.plugin_instance));
1164 vtmp->d = io->d;
1165 vtmp->avgcount_exists = -1;
1166 vtmp->latency_index = 0;
1167 vtmp->index = 0;
1168 yajl->handler_arg = vtmp;
1169 ret = traverse_json(io->json, io->json_len, hand);
1170 sfree(vtmp);
1171 return ret;
1172 }
1174 /**
1175 * Initiate JSON parsing and print error if one occurs
1176 */
1177 static int cconn_process_json(struct cconn *io)
1178 {
1179 if((io->request_type != ASOK_REQ_DATA) &&
1180 (io->request_type != ASOK_REQ_SCHEMA))
1181 {
1182 return -EDOM;
1183 }
1185 int result = 1;
1186 yajl_handle hand;
1187 yajl_status status;
1189 hand = yajl_alloc(&callbacks,
1190 #if HAVE_YAJL_V2
1191 /* alloc funcs = */ NULL,
1192 #else
1193 /* alloc funcs = */ NULL, NULL,
1194 #endif
1195 /* context = */ (void *)(&io->yajl));
1197 if(!hand)
1198 {
1199 ERROR ("ceph plugin: yajl_alloc failed.");
1200 return ENOMEM;
1201 }
1203 io->yajl.depth = 0;
1205 switch(io->request_type)
1206 {
1207 case ASOK_REQ_DATA:
1208 io->yajl.handler = node_handler_fetch_data;
1209 result = cconn_process_data(io, &io->yajl, hand);
1210 break;
1211 case ASOK_REQ_SCHEMA:
1212 //init daemon specific variables
1213 io->d->ds_num = 0;
1214 io->d->last_idx = 0;
1215 io->d->last_poll_data = NULL;
1216 io->yajl.handler = node_handler_define_schema;
1217 io->yajl.handler_arg = io->d;
1218 result = traverse_json(io->json, io->json_len, hand);
1219 break;
1220 }
1222 if(result)
1223 {
1224 goto done;
1225 }
1227 #if HAVE_YAJL_V2
1228 status = yajl_complete_parse(hand);
1229 #else
1230 status = yajl_parse_complete(hand);
1231 #endif
1233 if (status != yajl_status_ok)
1234 {
1235 unsigned char *errmsg = yajl_get_error (hand, /* verbose = */ 0,
1236 /* jsonText = */ NULL, /* jsonTextLen = */ 0);
1237 ERROR ("ceph plugin: yajl_parse_complete failed: %s",
1238 (char *) errmsg);
1239 yajl_free_error (hand, errmsg);
1240 yajl_free (hand);
1241 return 1;
1242 }
1244 done:
1245 yajl_free (hand);
1246 return result;
1247 }
1249 static int cconn_validate_revents(struct cconn *io, int revents)
1250 {
1251 if(revents & POLLERR)
1252 {
1253 ERROR("ceph plugin: cconn_validate_revents(name=%s): got POLLERR",
1254 io->d->name);
1255 return -EIO;
1256 }
1257 switch (io->state)
1258 {
1259 case CSTATE_WRITE_REQUEST:
1260 return (revents & POLLOUT) ? 0 : -EINVAL;
1261 case CSTATE_READ_VERSION:
1262 case CSTATE_READ_AMT:
1263 case CSTATE_READ_JSON:
1264 return (revents & POLLIN) ? 0 : -EINVAL;
1265 default:
1266 ERROR("ceph plugin: cconn_validate_revents(name=%s) got to "
1267 "illegal state on line %d", io->d->name, __LINE__);
1268 return -EDOM;
1269 }
1270 }
1272 /** Handle a network event for a connection */
1273 static int cconn_handle_event(struct cconn *io)
1274 {
1275 int ret;
1276 switch (io->state)
1277 {
1278 case CSTATE_UNCONNECTED:
1279 ERROR("ceph plugin: cconn_handle_event(name=%s) got to illegal "
1280 "state on line %d", io->d->name, __LINE__);
1282 return -EDOM;
1283 case CSTATE_WRITE_REQUEST:
1284 {
1285 char cmd[32];
1286 snprintf(cmd, sizeof(cmd), "%s%d%s", "{ \"prefix\": \"",
1287 io->request_type, "\" }\n");
1288 size_t cmd_len = strlen(cmd);
1289 RETRY_ON_EINTR(ret,
1290 write(io->asok, ((char*)&cmd) + io->amt, cmd_len - io->amt));
1291 DEBUG("ceph plugin: cconn_handle_event(name=%s,state=%d,amt=%d,ret=%d)",
1292 io->d->name, io->state, io->amt, ret);
1293 if(ret < 0)
1294 {
1295 return ret;
1296 }
1297 io->amt += ret;
1298 if(io->amt >= cmd_len)
1299 {
1300 io->amt = 0;
1301 switch (io->request_type)
1302 {
1303 case ASOK_REQ_VERSION:
1304 io->state = CSTATE_READ_VERSION;
1305 break;
1306 default:
1307 io->state = CSTATE_READ_AMT;
1308 break;
1309 }
1310 }
1311 return 0;
1312 }
1313 case CSTATE_READ_VERSION:
1314 {
1315 RETRY_ON_EINTR(ret,
1316 read(io->asok, ((char*)(&io->d->version)) + io->amt,
1317 sizeof(io->d->version) - io->amt));
1318 DEBUG("ceph plugin: cconn_handle_event(name=%s,state=%d,ret=%d)",
1319 io->d->name, io->state, ret);
1320 if(ret < 0)
1321 {
1322 return ret;
1323 }
1324 io->amt += ret;
1325 if(io->amt >= sizeof(io->d->version))
1326 {
1327 io->d->version = ntohl(io->d->version);
1328 if(io->d->version != 1)
1329 {
1330 ERROR("ceph plugin: cconn_handle_event(name=%s) not "
1331 "expecting version %d!", io->d->name, io->d->version);
1332 return -ENOTSUP;
1333 }
1334 DEBUG("ceph plugin: cconn_handle_event(name=%s): identified as "
1335 "version %d", io->d->name, io->d->version);
1336 io->amt = 0;
1337 cconn_close(io);
1338 io->request_type = ASOK_REQ_SCHEMA;
1339 }
1340 return 0;
1341 }
1342 case CSTATE_READ_AMT:
1343 {
1344 RETRY_ON_EINTR(ret,
1345 read(io->asok, ((char*)(&io->json_len)) + io->amt,
1346 sizeof(io->json_len) - io->amt));
1347 DEBUG("ceph plugin: cconn_handle_event(name=%s,state=%d,ret=%d)",
1348 io->d->name, io->state, ret);
1349 if(ret < 0)
1350 {
1351 return ret;
1352 }
1353 io->amt += ret;
1354 if(io->amt >= sizeof(io->json_len))
1355 {
1356 io->json_len = ntohl(io->json_len);
1357 io->amt = 0;
1358 io->state = CSTATE_READ_JSON;
1359 io->json = calloc(1, io->json_len + 1);
1360 if(!io->json)
1361 {
1362 ERROR("ceph plugin: error callocing io->json");
1363 return -ENOMEM;
1364 }
1365 }
1366 return 0;
1367 }
1368 case CSTATE_READ_JSON:
1369 {
1370 RETRY_ON_EINTR(ret,
1371 read(io->asok, io->json + io->amt, io->json_len - io->amt));
1372 DEBUG("ceph plugin: cconn_handle_event(name=%s,state=%d,ret=%d)",
1373 io->d->name, io->state, ret);
1374 if(ret < 0)
1375 {
1376 return ret;
1377 }
1378 io->amt += ret;
1379 if(io->amt >= io->json_len)
1380 {
1381 ret = cconn_process_json(io);
1382 if(ret)
1383 {
1384 return ret;
1385 }
1386 cconn_close(io);
1387 io->request_type = ASOK_REQ_NONE;
1388 }
1389 return 0;
1390 }
1391 default:
1392 ERROR("ceph plugin: cconn_handle_event(name=%s) got to illegal "
1393 "state on line %d", io->d->name, __LINE__);
1394 return -EDOM;
1395 }
1396 }
1398 static int cconn_prepare(struct cconn *io, struct pollfd* fds)
1399 {
1400 int ret;
1401 if(io->request_type == ASOK_REQ_NONE)
1402 {
1403 /* The request has already been serviced. */
1404 return 0;
1405 }
1406 else if((io->request_type == ASOK_REQ_DATA) && (io->d->ds_num == 0))
1407 {
1408 /* If there are no counters to report on, don't bother
1409 * connecting */
1410 return 0;
1411 }
1413 switch (io->state)
1414 {
1415 case CSTATE_UNCONNECTED:
1416 ret = cconn_connect(io);
1417 if(ret > 0)
1418 {
1419 return -ret;
1420 }
1421 else if(ret < 0)
1422 {
1423 return ret;
1424 }
1425 fds->fd = io->asok;
1426 fds->events = POLLOUT;
1427 return 1;
1428 case CSTATE_WRITE_REQUEST:
1429 fds->fd = io->asok;
1430 fds->events = POLLOUT;
1431 return 1;
1432 case CSTATE_READ_VERSION:
1433 case CSTATE_READ_AMT:
1434 case CSTATE_READ_JSON:
1435 fds->fd = io->asok;
1436 fds->events = POLLIN;
1437 return 1;
1438 default:
1439 ERROR("ceph plugin: cconn_prepare(name=%s) got to illegal state "
1440 "on line %d", io->d->name, __LINE__);
1441 return -EDOM;
1442 }
1443 }
1445 /** Returns the difference between two struct timevals in milliseconds.
1446 * On overflow, we return max/min int.
1447 */
1448 static int milli_diff(const struct timeval *t1, const struct timeval *t2)
1449 {
1450 int64_t ret;
1451 int sec_diff = t1->tv_sec - t2->tv_sec;
1452 int usec_diff = t1->tv_usec - t2->tv_usec;
1453 ret = usec_diff / 1000;
1454 ret += (sec_diff * 1000);
1455 return (ret > INT_MAX) ? INT_MAX : ((ret < INT_MIN) ? INT_MIN : (int)ret);
1456 }
1458 /** This handles the actual network I/O to talk to the Ceph daemons.
1459 */
1460 static int cconn_main_loop(uint32_t request_type)
1461 {
1462 int ret, some_unreachable = 0;
1463 struct timeval end_tv;
1464 struct cconn io_array[g_num_daemons];
1466 DEBUG("ceph plugin: entering cconn_main_loop(request_type = %d)", request_type);
1468 /* create cconn array */
1469 memset(io_array, 0, sizeof(io_array));
1470 for(size_t i = 0; i < g_num_daemons; ++i)
1471 {
1472 io_array[i].d = g_daemons[i];
1473 io_array[i].request_type = request_type;
1474 io_array[i].state = CSTATE_UNCONNECTED;
1475 }
1477 /** Calculate the time at which we should give up */
1478 gettimeofday(&end_tv, NULL);
1479 end_tv.tv_sec += CEPH_TIMEOUT_INTERVAL;
1481 while (1)
1482 {
1483 int nfds, diff;
1484 struct timeval tv;
1485 struct cconn *polled_io_array[g_num_daemons];
1486 struct pollfd fds[g_num_daemons];
1487 memset(fds, 0, sizeof(fds));
1488 nfds = 0;
1489 for(size_t i = 0; i < g_num_daemons; ++i)
1490 {
1491 struct cconn *io = io_array + i;
1492 ret = cconn_prepare(io, fds + nfds);
1493 if(ret < 0)
1494 {
1495 WARNING("ceph plugin: cconn_prepare(name=%s,i=%zu,st=%d)=%d",
1496 io->d->name, i, io->state, ret);
1497 cconn_close(io);
1498 io->request_type = ASOK_REQ_NONE;
1499 some_unreachable = 1;
1500 }
1501 else if(ret == 1)
1502 {
1503 polled_io_array[nfds++] = io_array + i;
1504 }
1505 }
1506 if(nfds == 0)
1507 {
1508 /* finished */
1509 ret = 0;
1510 goto done;
1511 }
1512 gettimeofday(&tv, NULL);
1513 diff = milli_diff(&end_tv, &tv);
1514 if(diff <= 0)
1515 {
1516 /* Timed out */
1517 ret = -ETIMEDOUT;
1518 WARNING("ceph plugin: cconn_main_loop: timed out.");
1519 goto done;
1520 }
1521 RETRY_ON_EINTR(ret, poll(fds, nfds, diff));
1522 if(ret < 0)
1523 {
1524 ERROR("ceph plugin: poll(2) error: %d", ret);
1525 goto done;
1526 }
1527 for(int i = 0; i < nfds; ++i)
1528 {
1529 struct cconn *io = polled_io_array[i];
1530 int revents = fds[i].revents;
1531 if(revents == 0)
1532 {
1533 /* do nothing */
1534 continue;
1535 }
1536 else if(cconn_validate_revents(io, revents))
1537 {
1538 WARNING("ceph plugin: cconn(name=%s,i=%d,st=%d): "
1539 "revents validation error: "
1540 "revents=0x%08x", io->d->name, i, io->state, revents);
1541 cconn_close(io);
1542 io->request_type = ASOK_REQ_NONE;
1543 some_unreachable = 1;
1544 }
1545 else
1546 {
1547 ret = cconn_handle_event(io);
1548 if(ret)
1549 {
1550 WARNING("ceph plugin: cconn_handle_event(name=%s,"
1551 "i=%d,st=%d): error %d", io->d->name, i, io->state, ret);
1552 cconn_close(io);
1553 io->request_type = ASOK_REQ_NONE;
1554 some_unreachable = 1;
1555 }
1556 }
1557 }
1558 }
1559 done: for(size_t i = 0; i < g_num_daemons; ++i)
1560 {
1561 cconn_close(io_array + i);
1562 }
1563 if(some_unreachable)
1564 {
1565 DEBUG("ceph plugin: cconn_main_loop: some Ceph daemons were unreachable.");
1566 }
1567 else
1568 {
1569 DEBUG("ceph plugin: cconn_main_loop: reached all Ceph daemons :)");
1570 }
1571 return ret;
1572 }
1574 static int ceph_read(void)
1575 {
1576 return cconn_main_loop(ASOK_REQ_DATA);
1577 }
1579 /******* lifecycle *******/
1580 static int ceph_init(void)
1581 {
1582 int ret;
1584 #if defined(HAVE_SYS_CAPABILITY_H) && defined(CAP_DAC_OVERRIDE)
1585 if (check_capability (CAP_DAC_OVERRIDE) != 0)
1586 {
1587 if (getuid () == 0)
1588 WARNING ("ceph plugin: Running collectd as root, but the "
1589 "CAP_DAC_OVERRIDE capability is missing. The plugin's read "
1590 "function will probably fail. Is your init system dropping "
1591 "capabilities?");
1592 else
1593 WARNING ("ceph plugin: collectd doesn't have the CAP_DAC_OVERRIDE "
1594 "capability. If you don't want to run collectd as root, try running "
1595 "\"setcap cap_dac_override=ep\" on the collectd binary.");
1596 }
1597 #endif
1599 ceph_daemons_print();
1601 ret = cconn_main_loop(ASOK_REQ_VERSION);
1603 return (ret) ? ret : 0;
1604 }
1606 static int ceph_shutdown(void)
1607 {
1608 for(size_t i = 0; i < g_num_daemons; ++i)
1609 {
1610 ceph_daemon_free(g_daemons[i]);
1611 }
1612 sfree(g_daemons);
1613 g_daemons = NULL;
1614 g_num_daemons = 0;
1615 DEBUG("ceph plugin: finished ceph_shutdown");
1616 return 0;
1617 }
1619 void module_register(void)
1620 {
1621 plugin_register_complex_config("ceph", ceph_config);
1622 plugin_register_init("ceph", ceph_init);
1623 plugin_register_read("ceph", ceph_read);
1624 plugin_register_shutdown("ceph", ceph_shutdown);
1625 }
1626 /* vim: set sw=4 sts=4 et : */