Code

ceph plugin: Refactor ceph_cb_number().
[collectd.git] / src / ceph.c
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"
30 #include "common.h"
31 #include "plugin.h"
33 #include <arpa/inet.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <yajl/yajl_parse.h>
37 #if HAVE_YAJL_YAJL_VERSION_H
38 #include <yajl/yajl_version.h>
39 #endif
41 #include <limits.h>
42 #include <poll.h>
43 #include <stdint.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <strings.h>
48 #include <sys/socket.h>
49 #include <sys/time.h>
50 #include <sys/types.h>
51 #include <sys/un.h>
52 #include <unistd.h>
53 #include <math.h>
54 #include <inttypes.h>
56 #define RETRY_AVGCOUNT -1
58 #if defined(YAJL_MAJOR) && (YAJL_MAJOR > 1)
59 # define HAVE_YAJL_V2 1
60 #endif
62 #define RETRY_ON_EINTR(ret, expr) \
63     while(1) { \
64         ret = expr; \
65         if(ret >= 0) \
66             break; \
67         ret = -errno; \
68         if(ret != -EINTR) \
69             break; \
70     }
72 /** Timeout interval in seconds */
73 #define CEPH_TIMEOUT_INTERVAL 1
75 /** Maximum path length for a UNIX domain socket on this system */
76 #define UNIX_DOMAIN_SOCK_PATH_MAX (sizeof(((struct sockaddr_un*)0)->sun_path))
78 /** Yajl callback returns */
79 #define CEPH_CB_CONTINUE 1
80 #define CEPH_CB_ABORT 0
82 #if HAVE_YAJL_V2
83 typedef size_t yajl_len_t;
84 #else
85 typedef unsigned int yajl_len_t;
86 #endif
88 /** Number of types for ceph defined in types.db */
89 #define CEPH_DSET_TYPES_NUM 3
90 /** ceph types enum */
91 enum ceph_dset_type_d
92 {
93     DSET_LATENCY = 0,
94     DSET_BYTES = 1,
95     DSET_RATE = 2,
96     DSET_TYPE_UNFOUND = 1000
97 };
99 /** Valid types for ceph defined in types.db */
100 const char * ceph_dset_types [CEPH_DSET_TYPES_NUM] =
101                                    {"ceph_latency", "ceph_bytes", "ceph_rate"};
103 /******* ceph_daemon *******/
104 struct ceph_daemon
106     /** Version of the admin_socket interface */
107     uint32_t version;
108     /** daemon name **/
109     char name[DATA_MAX_NAME_LEN];
111     /** Path to the socket that we use to talk to the ceph daemon */
112     char asok_path[UNIX_DOMAIN_SOCK_PATH_MAX];
114     /** Number of counters */
115     int ds_num;
116     /** Track ds types */
117     uint32_t *ds_types;
118     /** Track ds names to match with types */
119     char **ds_names;
121     /**
122      * Keep track of last data for latency values so we can calculate rate
123      * since last poll.
124      */
125     struct last_data **last_poll_data;
126     /** index of last poll data */
127     int last_idx;
128 };
130 /******* JSON parsing *******/
131 typedef int (*node_handler_t)(void *, const char*, const char*);
133 /** Track state and handler while parsing JSON */
134 struct yajl_struct
136     node_handler_t handler;
137     void * handler_arg;
138     struct {
139       char key[DATA_MAX_NAME_LEN];
140       int key_len;
141     } state[YAJL_MAX_DEPTH];
142     int depth;
143 };
144 typedef struct yajl_struct yajl_struct;
146 enum perfcounter_type_d
148     PERFCOUNTER_LATENCY = 0x4, PERFCOUNTER_DERIVE = 0x8,
149 };
151 /** Give user option to use default (long run = since daemon started) avg */
152 static int long_run_latency_avg = 0;
154 /**
155  * Give user option to use default type for special cases -
156  * filestore.journal_wr_bytes is currently only metric here. Ceph reports the
157  * type as a sum/count pair and will calculate it the same as a latency value.
158  * All other "bytes" metrics (excluding the used/capacity bytes for the OSD)
159  * use the DERIVE type. Unless user specifies to use given type, convert this
160  * metric to use DERIVE.
161  */
162 static int convert_special_metrics = 1;
164 /** Array of daemons to monitor */
165 static struct ceph_daemon **g_daemons = NULL;
167 /** Number of elements in g_daemons */
168 static int g_num_daemons = 0;
170 /**
171  * A set of data that we build up in memory while parsing the JSON.
172  */
173 struct values_tmp
175     /** ceph daemon we are processing data for*/
176     struct ceph_daemon *d;
177     /** track avgcount across counters for avgcount/sum latency pairs */
178     uint64_t avgcount;
179     /** current index of counters - used to get type of counter */
180     int index;
181     /** do we already have an avgcount for latency pair */
182     int avgcount_exists;
183     /**
184      * similar to index, but current index of latency type counters -
185      * used to get last poll data of counter
186      */
187     int latency_index;
188     /**
189      * values list - maintain across counters since
190      * host/plugin/plugin instance are always the same
191      */
192     value_list_t vlist;
193 };
195 /**
196  * A set of count/sum pairs to keep track of latency types and get difference
197  * between this poll data and last poll data.
198  */
199 struct last_data
201     char ds_name[DATA_MAX_NAME_LEN];
202     double last_sum;
203     uint64_t last_count;
204 };
206 /******* network I/O *******/
207 enum cstate_t
209     CSTATE_UNCONNECTED = 0,
210     CSTATE_WRITE_REQUEST,
211     CSTATE_READ_VERSION,
212     CSTATE_READ_AMT,
213     CSTATE_READ_JSON,
214 };
216 enum request_type_t
218     ASOK_REQ_VERSION = 0,
219     ASOK_REQ_DATA = 1,
220     ASOK_REQ_SCHEMA = 2,
221     ASOK_REQ_NONE = 1000,
222 };
224 struct cconn
226     /** The Ceph daemon that we're talking to */
227     struct ceph_daemon *d;
229     /** Request type */
230     uint32_t request_type;
232     /** The connection state */
233     enum cstate_t state;
235     /** The socket we use to talk to this daemon */
236     int asok;
238     /** The amount of data remaining to read / write. */
239     uint32_t amt;
241     /** Length of the JSON to read */
242     uint32_t json_len;
244     /** Buffer containing JSON data */
245     unsigned char *json;
247     /** Keep data important to yajl processing */
248     struct yajl_struct yajl;
249 };
251 static int ceph_cb_null(void *ctx)
253     return CEPH_CB_CONTINUE;
256 static int ceph_cb_boolean(void *ctx, int bool_val)
258     return CEPH_CB_CONTINUE;
261 #define BUFFER_ADD(dest, src) do { \
262     size_t dest_size = sizeof (dest); \
263     strncat ((dest), (src), dest_size - strlen (dest)); \
264     (dest)[dest_size - 1] = 0; \
265 } while (0)
267 static int
268 ceph_cb_number(void *ctx, const char *number_val, yajl_len_t number_len)
270     yajl_struct *yajl = (yajl_struct*)ctx;
271     char buffer[number_len+1];
272     int i, status;
273     char key[2 * DATA_MAX_NAME_LEN];
274     _Bool latency_type = 0;
276     memcpy(buffer, number_val, number_len);
277     buffer[sizeof(buffer) - 1] = 0;
279     sstrncpy (key, yajl->state[0].key, sizeof (key));
280     for (i = 1; i < yajl->depth; i++)
281     {
282         /* Special case for latency metrics. */
283         if ((i == yajl->depth-1)
284                 && ((strcmp(yajl->state[i].key,"avgcount") == 0)
285                     || (strcmp(yajl->state[i].key,"sum") == 0)))
286         {
287             /* Super-special case for filestore:JournalWrBytes. For some
288              * reason, Ceph schema encodes this as a count/sum pair while all
289              * other "Bytes" data (excluding used/capacity bytes for OSD space)
290              * uses a single "Derive" type. To spare further confusion, keep
291              * this KPI as the same type of other "Bytes". Instead of keeping
292              * an "average" or "rate", use the "sum" in the pair and assign
293              * that to the derive value. */
294             if (convert_special_metrics && (i >= 2)
295                     && (strcmp("filestore", yajl->state[i-2].key) == 0)
296                     && (strcmp("journal_wr_bytes", yajl->state[i-1].key) == 0)
297                     && (strcmp("avgcount", yajl->state[i].key) == 0))
298             {
299                 DEBUG("ceph plugin: Skipping avgcount for filestore.JournalWrBytes");
300                 yajl->depth -= 1;
301                 return CEPH_CB_CONTINUE;
302             }
304             /* Don't add "avgcount" or "sum" to the key just yet. If the
305              * handler function signals RETRY_AVGCOUNT, we'll add it and try
306              * again. */
307             latency_type = 1;
308             break;
309         }
311         BUFFER_ADD (key, ".");
312         BUFFER_ADD (key, yajl->state[i].key);
313     }
315     status = yajl->handler(yajl->handler_arg, buffer, key);
316     if((status == RETRY_AVGCOUNT) && latency_type)
317     {
318         /* Add previously skipped part of the key, either "avgcount" or "sum",
319          * and try again. */
320         BUFFER_ADD (key, ".");
321         BUFFER_ADD (key, yajl->state[yajl->depth-1].key);
323         status = yajl->handler(yajl->handler_arg, buffer, key);
324     }
326     if(status == -ENOMEM)
327     {
328         ERROR("ceph plugin: memory allocation failed");
329         return CEPH_CB_ABORT;
330     }
332     yajl->depth -= 1;
333     return CEPH_CB_CONTINUE;
336 static int ceph_cb_string(void *ctx, const unsigned char *string_val,
337         yajl_len_t string_len)
339     return CEPH_CB_CONTINUE;
342 static int ceph_cb_start_map(void *ctx)
344     return CEPH_CB_CONTINUE;
347 static int
348 ceph_cb_map_key(void *ctx, const unsigned char *key, yajl_len_t string_len)
350     yajl_struct *yajl = (yajl_struct*)ctx;
352     if((yajl->depth+1)  >= YAJL_MAX_DEPTH)
353     {
354         ERROR("ceph plugin: depth exceeds max, aborting.");
355         return CEPH_CB_ABORT;
356     }
358     char buffer[string_len+1];
360     memcpy(buffer, key, string_len);
361     buffer[sizeof(buffer) - 1] = 0;
363     snprintf(yajl->state[yajl->depth].key, sizeof(buffer), "%s", buffer);
364     yajl->state[yajl->depth].key_len = sizeof(buffer);
365     yajl->depth = (yajl->depth + 1);
367     return CEPH_CB_CONTINUE;
370 static int ceph_cb_end_map(void *ctx)
372     yajl_struct *yajl = (yajl_struct*)ctx;
374     yajl->depth = (yajl->depth - 1);
375     return CEPH_CB_CONTINUE;
378 static int ceph_cb_start_array(void *ctx)
380     return CEPH_CB_CONTINUE;
383 static int ceph_cb_end_array(void *ctx)
385     return CEPH_CB_CONTINUE;
388 static yajl_callbacks callbacks = {
389         ceph_cb_null,
390         ceph_cb_boolean,
391         NULL,
392         NULL,
393         ceph_cb_number,
394         ceph_cb_string,
395         ceph_cb_start_map,
396         ceph_cb_map_key,
397         ceph_cb_end_map,
398         ceph_cb_start_array,
399         ceph_cb_end_array
400 };
402 static void ceph_daemon_print(const struct ceph_daemon *d)
404     DEBUG("ceph plugin: name=%s, asok_path=%s", d->name, d->asok_path);
407 static void ceph_daemons_print(void)
409     int i;
410     for(i = 0; i < g_num_daemons; ++i)
411     {
412         ceph_daemon_print(g_daemons[i]);
413     }
416 static void ceph_daemon_free(struct ceph_daemon *d)
418     int i = 0;
419     for(; i < d->last_idx; i++)
420     {
421         sfree(d->last_poll_data[i]);
422     }
423     sfree(d->last_poll_data);
424     d->last_poll_data = NULL;
425     d->last_idx = 0;
426     for(i = 0; i < d->ds_num; i++)
427     {
428         sfree(d->ds_names[i]);
429     }
430     sfree(d->ds_types);
431     sfree(d->ds_names);
432     sfree(d);
435 /* compact_ds_name removed the special characters ":", "_", "-" and "+" from the
436  * intput string. Characters following these special characters are capitalized.
437  * Trailing "+" and "-" characters are replaces with the strings "Plus" and
438  * "Minus". */
439 static int compact_ds_name (char *buffer, size_t buffer_size, char const *src)
441     char *src_copy;
442     size_t src_len;
443     char *ptr = buffer;
444     size_t ptr_size = buffer_size;
445     _Bool append_plus = 0;
446     _Bool append_minus = 0;
448     if ((buffer == NULL) || (buffer_size <= strlen ("Minus")) || (src == NULL))
449       return EINVAL;
451     src_copy = strdup (src);
452     src_len = strlen(src);
454     /* Remove trailing "+" and "-". */
455     if (src_copy[src_len - 1] == '+')
456     {
457         append_plus = 1;
458         src_len--;
459         src_copy[src_len] = 0;
460     }
461     else if (src_copy[src_len - 1] == '-')
462     {
463         append_minus = 1;
464         src_len--;
465         src_copy[src_len] = 0;
466     }
468     /* Split at special chars, capitalize first character, append to buffer. */
469     char *dummy = src_copy;
470     char *token;
471     char *save_ptr = NULL;
472     while ((token = strtok_r (dummy, ":_-+", &save_ptr)) != NULL)
473     {
474         size_t len;
476         dummy = NULL;
478         token[0] = toupper ((int) token[0]);
480         assert (ptr_size > 1);
482         len = strlen (token);
483         if (len >= ptr_size)
484             len = ptr_size - 1;
486         assert (len > 0);
487         assert (len < ptr_size);
489         sstrncpy (ptr, token, len + 1);
490         ptr += len;
491         ptr_size -= len;
493         assert (*ptr == 0);
494         if (ptr_size <= 1)
495             break;
496     }
498     /* Append "Plus" or "Minus" if "+" or "-" has been stripped above. */
499     if (append_plus || append_minus)
500     {
501         char const *append = "Plus";
502         if (append_minus)
503             append = "Minus";
505         size_t offset = buffer_size - (strlen (append) + 1);
506         if (offset > strlen (buffer))
507             offset = strlen (buffer);
509         sstrncpy (buffer + offset, append, buffer_size - offset);
510     }
512     sfree (src_copy);
513     return 0;
516 static _Bool has_suffix (char const *str, char const *suffix)
518     size_t str_len = strlen (str);
519     size_t suffix_len = strlen (suffix);
520     size_t offset;
522     if (suffix_len > str_len)
523         return 0;
524     offset = str_len - suffix_len;
526     if (strcmp (str + offset, suffix) == 0)
527         return 1;
529     return 0;
532 /* count_parts returns the number of elements a "foo.bar.baz" style key has. */
533 static size_t count_parts (char const *key)
535     char const *ptr;
536     size_t parts_num = 0;
538     for (ptr = key; ptr != NULL; ptr = strchr (ptr + 1, '.'))
539         parts_num++;
541     return parts_num;
544 /**
545  * Parse key to remove "type" if this is for schema and initiate compaction
546  */
547 static int parse_keys (char *buffer, size_t buffer_size, const char *key_str)
549     char tmp[2 * buffer_size];
551     if (buffer == NULL || buffer_size == 0 || key_str == NULL || strlen (key_str) == 0)
552         return EINVAL;
554     if ((count_parts (key_str) > 2) && has_suffix (key_str, ".type"))
555     {
556         /* strip ".type" suffix iff the key has more than two parts. */
557         size_t sz = strlen (key_str) - strlen (".type") + 1;
559         if (sz > sizeof (tmp))
560             sz = sizeof (tmp);
561         sstrncpy (tmp, key_str, sz);
562     }
563     else
564     {
565         sstrncpy (tmp, key_str, sizeof (tmp));
566     }
568     return compact_ds_name (buffer, buffer_size, tmp);
571 /**
572  * while parsing ceph admin socket schema, save counter name and type for later
573  * data processing
574  */
575 static int ceph_daemon_add_ds_entry(struct ceph_daemon *d, const char *name,
576         int pc_type)
578     uint32_t type;
579     char ds_name[DATA_MAX_NAME_LEN];
580     memset(ds_name, 0, sizeof(ds_name));
582     if(convert_special_metrics)
583     {
584         /**
585          * Special case for filestore:JournalWrBytes. For some reason, Ceph
586          * schema encodes this as a count/sum pair while all other "Bytes" data
587          * (excluding used/capacity bytes for OSD space) uses a single "Derive"
588          * type. To spare further confusion, keep this KPI as the same type of
589          * other "Bytes". Instead of keeping an "average" or "rate", use the
590          * "sum" in the pair and assign that to the derive value.
591          */
592         if((strcmp(name,"filestore.journal_wr_bytes.type") == 0))
593         {
594             pc_type = 10;
595         }
596     }
598     d->ds_names = realloc(d->ds_names, sizeof(char *) * (d->ds_num + 1));
599     if(!d->ds_names)
600     {
601         return -ENOMEM;
602     }
604     d->ds_types = realloc(d->ds_types, sizeof(uint32_t) * (d->ds_num + 1));
605     if(!d->ds_types)
606     {
607         return -ENOMEM;
608     }
610     d->ds_names[d->ds_num] = malloc(sizeof(char) * DATA_MAX_NAME_LEN);
611     if(!d->ds_names[d->ds_num])
612     {
613         return -ENOMEM;
614     }
616     type = (pc_type & PERFCOUNTER_DERIVE) ? DSET_RATE :
617             ((pc_type & PERFCOUNTER_LATENCY) ? DSET_LATENCY : DSET_BYTES);
618     d->ds_types[d->ds_num] = type;
620     if (parse_keys(ds_name, sizeof (ds_name), name))
621     {
622         return 1;
623     }
625     sstrncpy(d->ds_names[d->ds_num], ds_name, DATA_MAX_NAME_LEN -1);
626     d->ds_num = (d->ds_num + 1);
628     return 0;
631 /******* ceph_config *******/
632 static int cc_handle_str(struct oconfig_item_s *item, char *dest, int dest_len)
634     const char *val;
635     if(item->values_num != 1)
636     {
637         return -ENOTSUP;
638     }
639     if(item->values[0].type != OCONFIG_TYPE_STRING)
640     {
641         return -ENOTSUP;
642     }
643     val = item->values[0].value.string;
644     if(snprintf(dest, dest_len, "%s", val) > (dest_len - 1))
645     {
646         ERROR("ceph plugin: configuration parameter '%s' is too long.\n",
647                 item->key);
648         return -ENAMETOOLONG;
649     }
650     return 0;
653 static int cc_handle_bool(struct oconfig_item_s *item, int *dest)
655     if(item->values_num != 1)
656     {
657         return -ENOTSUP;
658     }
660     if(item->values[0].type != OCONFIG_TYPE_BOOLEAN)
661     {
662         return -ENOTSUP;
663     }
665     *dest = (item->values[0].value.boolean) ? 1 : 0;
666     return 0;
669 static int cc_add_daemon_config(oconfig_item_t *ci)
671     int ret, i;
672     struct ceph_daemon *nd, cd;
673     struct ceph_daemon **tmp;
674     memset(&cd, 0, sizeof(struct ceph_daemon));
676     if((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
677     {
678         WARNING("ceph plugin: `Daemon' blocks need exactly one string "
679                 "argument.");
680         return (-1);
681     }
683     ret = cc_handle_str(ci, cd.name, DATA_MAX_NAME_LEN);
684     if(ret)
685     {
686         return ret;
687     }
689     for(i=0; i < ci->children_num; i++)
690     {
691         oconfig_item_t *child = ci->children + i;
693         if(strcasecmp("SocketPath", child->key) == 0)
694         {
695             ret = cc_handle_str(child, cd.asok_path, sizeof(cd.asok_path));
696             if(ret)
697             {
698                 return ret;
699             }
700         }
701         else
702         {
703             WARNING("ceph plugin: ignoring unknown option %s", child->key);
704         }
705     }
706     if(cd.name[0] == '\0')
707     {
708         ERROR("ceph plugin: you must configure a daemon name.\n");
709         return -EINVAL;
710     }
711     else if(cd.asok_path[0] == '\0')
712     {
713         ERROR("ceph plugin(name=%s): you must configure an administrative "
714         "socket path.\n", cd.name);
715         return -EINVAL;
716     }
717     else if(!((cd.asok_path[0] == '/') ||
718             (cd.asok_path[0] == '.' && cd.asok_path[1] == '/')))
719     {
720         ERROR("ceph plugin(name=%s): administrative socket paths must begin "
721                 "with '/' or './' Can't parse: '%s'\n", cd.name, cd.asok_path);
722         return -EINVAL;
723     }
725     tmp = realloc(g_daemons, (g_num_daemons+1) * sizeof(*g_daemons));
726     if(tmp == NULL)
727     {
728         /* The positive return value here indicates that this is a
729          * runtime error, not a configuration error.  */
730         return ENOMEM;
731     }
732     g_daemons = tmp;
734     nd = malloc(sizeof(*nd));
735     if(!nd)
736     {
737         return ENOMEM;
738     }
739     memcpy(nd, &cd, sizeof(*nd));
740     g_daemons[g_num_daemons++] = nd;
741     return 0;
744 static int ceph_config(oconfig_item_t *ci)
746     int ret, i;
748     for(i = 0; i < ci->children_num; ++i)
749     {
750         oconfig_item_t *child = ci->children + i;
751         if(strcasecmp("Daemon", child->key) == 0)
752         {
753             ret = cc_add_daemon_config(child);
754             if(ret == ENOMEM)
755             {
756                 ERROR("ceph plugin: Couldn't allocate memory");
757                 return ret;
758             }
759             else if(ret)
760             {
761                 //process other daemons and ignore this one
762                 continue;
763             }
764         }
765         else if(strcasecmp("LongRunAvgLatency", child->key) == 0)
766         {
767             ret = cc_handle_bool(child, &long_run_latency_avg);
768             if(ret)
769             {
770                 return ret;
771             }
772         }
773         else if(strcasecmp("ConvertSpecialMetricTypes", child->key) == 0)
774         {
775             ret = cc_handle_bool(child, &convert_special_metrics);
776             if(ret)
777             {
778                 return ret;
779             }
780         }
781         else
782         {
783             WARNING("ceph plugin: ignoring unknown option %s", child->key);
784         }
785     }
786     return 0;
789 /**
790  * Parse JSON and get error message if present
791  */
792 static int
793 traverse_json(const unsigned char *json, uint32_t json_len, yajl_handle hand)
795     yajl_status status = yajl_parse(hand, json, json_len);
796     unsigned char *msg;
798     switch(status)
799     {
800         case yajl_status_error:
801             msg = yajl_get_error(hand, /* verbose = */ 1,
802                                        /* jsonText = */ (unsigned char *) json,
803                                                       (unsigned int) json_len);
804             ERROR ("ceph plugin: yajl_parse failed: %s", msg);
805             yajl_free_error(hand, msg);
806             return 1;
807         case yajl_status_client_canceled:
808             return 1;
809         default:
810             return 0;
811     }
814 /**
815  * Add entry for each counter while parsing schema
816  */
817 static int
818 node_handler_define_schema(void *arg, const char *val, const char *key)
820     struct ceph_daemon *d = (struct ceph_daemon *) arg;
821     int pc_type;
822     pc_type = atoi(val);
823     return ceph_daemon_add_ds_entry(d, key, pc_type);
826 /**
827  * Latency counter does not yet have an entry in last poll data - add it.
828  */
829 static int add_last(struct ceph_daemon *d, const char *ds_n, double cur_sum,
830         uint64_t cur_count)
832     d->last_poll_data[d->last_idx] = malloc(1 * sizeof(struct last_data));
833     if(!d->last_poll_data[d->last_idx])
834     {
835         return -ENOMEM;
836     }
837     sstrncpy(d->last_poll_data[d->last_idx]->ds_name,ds_n,
838             sizeof(d->last_poll_data[d->last_idx]->ds_name));
839     d->last_poll_data[d->last_idx]->last_sum = cur_sum;
840     d->last_poll_data[d->last_idx]->last_count = cur_count;
841     d->last_idx = (d->last_idx + 1);
842     return 0;
845 /**
846  * Update latency counter or add new entry if it doesn't exist
847  */
848 static int update_last(struct ceph_daemon *d, const char *ds_n, int index,
849         double cur_sum, uint64_t cur_count)
851     if((d->last_idx > index) && (strcmp(d->last_poll_data[index]->ds_name, ds_n) == 0))
852     {
853         d->last_poll_data[index]->last_sum = cur_sum;
854         d->last_poll_data[index]->last_count = cur_count;
855         return 0;
856     }
858     if(!d->last_poll_data)
859     {
860         d->last_poll_data = malloc(1 * sizeof(struct last_data *));
861         if(!d->last_poll_data)
862         {
863             return -ENOMEM;
864         }
865     }
866     else
867     {
868         struct last_data **tmp_last = realloc(d->last_poll_data,
869                 ((d->last_idx+1) * sizeof(struct last_data *)));
870         if(!tmp_last)
871         {
872             return -ENOMEM;
873         }
874         d->last_poll_data = tmp_last;
875     }
876     return add_last(d, ds_n, cur_sum, cur_count);
879 /**
880  * If using index guess failed (shouldn't happen, but possible if counters
881  * get rearranged), resort to searching for counter name
882  */
883 static int backup_search_for_last_avg(struct ceph_daemon *d, const char *ds_n)
885     int i = 0;
886     for(; i < d->last_idx; i++)
887     {
888         if(strcmp(d->last_poll_data[i]->ds_name, ds_n) == 0)
889         {
890             return i;
891         }
892     }
893     return -1;
896 /**
897  * Calculate average b/t current data and last poll data
898  * if last poll data exists
899  */
900 static double get_last_avg(struct ceph_daemon *d, const char *ds_n, int index,
901         double cur_sum, uint64_t cur_count)
903     double result = -1.1, sum_delt = 0.0;
904     uint64_t count_delt = 0;
905     int tmp_index = 0;
906     if(d->last_idx > index)
907     {
908         if(strcmp(d->last_poll_data[index]->ds_name, ds_n) == 0)
909         {
910             tmp_index = index;
911         }
912         //test previous index
913         else if((index > 0) && (strcmp(d->last_poll_data[index-1]->ds_name, ds_n) == 0))
914         {
915             tmp_index = (index - 1);
916         }
917         else
918         {
919             tmp_index = backup_search_for_last_avg(d, ds_n);
920         }
922         if((tmp_index > -1) && (cur_count > d->last_poll_data[tmp_index]->last_count))
923         {
924             sum_delt = (cur_sum - d->last_poll_data[tmp_index]->last_sum);
925             count_delt = (cur_count - d->last_poll_data[tmp_index]->last_count);
926             result = (sum_delt / count_delt);
927         }
928     }
930     if(result == -1.1)
931     {
932         result = NAN;
933     }
934     if(update_last(d, ds_n, tmp_index, cur_sum, cur_count) == -ENOMEM)
935     {
936         return -ENOMEM;
937     }
938     return result;
941 /**
942  * If using index guess failed, resort to searching for counter name
943  */
944 static uint32_t backup_search_for_type(struct ceph_daemon *d, char *ds_name)
946     int idx = 0;
947     for(; idx < d->ds_num; idx++)
948     {
949         if(strcmp(d->ds_names[idx], ds_name) == 0)
950         {
951             return d->ds_types[idx];
952         }
953     }
954     return DSET_TYPE_UNFOUND;
957 /**
958  * Process counter data and dispatch values
959  */
960 static int node_handler_fetch_data(void *arg, const char *val, const char *key)
962     value_t uv;
963     double tmp_d;
964     uint64_t tmp_u;
965     struct values_tmp *vtmp = (struct values_tmp*) arg;
966     uint32_t type = DSET_TYPE_UNFOUND;
967     int index = vtmp->index;
969     char ds_name[DATA_MAX_NAME_LEN];
970     memset(ds_name, 0, sizeof(ds_name));
972     if (parse_keys (ds_name, sizeof (ds_name), key))
973     {
974         return 1;
975     }
977     if(index >= vtmp->d->ds_num)
978     {
979         //don't overflow bounds of array
980         index = (vtmp->d->ds_num - 1);
981     }
983     /**
984      * counters should remain in same order we parsed schema... we maintain the
985      * index variable to keep track of current point in list of counters. first
986      * use index to guess point in array for retrieving type. if that doesn't
987      * work, use the old way to get the counter type
988      */
989     if(strcmp(ds_name, vtmp->d->ds_names[index]) == 0)
990     {
991         //found match
992         type = vtmp->d->ds_types[index];
993     }
994     else if((index > 0) && (strcmp(ds_name, vtmp->d->ds_names[index-1]) == 0))
995     {
996         //try previous key
997         type = vtmp->d->ds_types[index-1];
998     }
1000     if(type == DSET_TYPE_UNFOUND)
1001     {
1002         //couldn't find right type by guessing, check the old way
1003         type = backup_search_for_type(vtmp->d, ds_name);
1004     }
1006     switch(type)
1007     {
1008         case DSET_LATENCY:
1009             if(vtmp->avgcount_exists == -1)
1010             {
1011                 sscanf(val, "%" PRIu64, &vtmp->avgcount);
1012                 vtmp->avgcount_exists = 0;
1013                 //return after saving avgcount - don't dispatch value
1014                 //until latency calculation
1015                 return 0;
1016             }
1017             else
1018             {
1019                 double sum, result;
1020                 sscanf(val, "%lf", &sum);
1022                 if(vtmp->avgcount == 0)
1023                 {
1024                     vtmp->avgcount = 1;
1025                 }
1027                 /** User wants latency values as long run avg */
1028                 if(long_run_latency_avg)
1029                 {
1030                     result = (sum / vtmp->avgcount);
1031                 }
1032                 else
1033                 {
1034                     result = get_last_avg(vtmp->d, ds_name, vtmp->latency_index, sum, vtmp->avgcount);
1035                     if(result == -ENOMEM)
1036                     {
1037                         return -ENOMEM;
1038                     }
1039                 }
1041                 uv.gauge = result;
1042                 vtmp->avgcount_exists = -1;
1043                 vtmp->latency_index = (vtmp->latency_index + 1);
1044             }
1045             break;
1046         case DSET_BYTES:
1047             sscanf(val, "%lf", &tmp_d);
1048             uv.gauge = tmp_d;
1049             break;
1050         case DSET_RATE:
1051             sscanf(val, "%" PRIu64, &tmp_u);
1052             uv.derive = tmp_u;
1053             break;
1054         case DSET_TYPE_UNFOUND:
1055         default:
1056             ERROR("ceph plugin: ds %s was not properly initialized.", ds_name);
1057             return -1;
1058     }
1060     sstrncpy(vtmp->vlist.type, ceph_dset_types[type], sizeof(vtmp->vlist.type));
1061     sstrncpy(vtmp->vlist.type_instance, ds_name, sizeof(vtmp->vlist.type_instance));
1062     vtmp->vlist.values = &uv;
1063     vtmp->vlist.values_len = 1;
1065     vtmp->index = (vtmp->index + 1);
1066     plugin_dispatch_values(&vtmp->vlist);
1068     return 0;
1071 static int cconn_connect(struct cconn *io)
1073     struct sockaddr_un address;
1074     int flags, fd, err;
1075     if(io->state != CSTATE_UNCONNECTED)
1076     {
1077         ERROR("ceph plugin: cconn_connect: io->state != CSTATE_UNCONNECTED");
1078         return -EDOM;
1079     }
1080     fd = socket(PF_UNIX, SOCK_STREAM, 0);
1081     if(fd < 0)
1082     {
1083         int err = -errno;
1084         ERROR("ceph plugin: cconn_connect: socket(PF_UNIX, SOCK_STREAM, 0) "
1085             "failed: error %d", err);
1086         return err;
1087     }
1088     memset(&address, 0, sizeof(struct sockaddr_un));
1089     address.sun_family = AF_UNIX;
1090     snprintf(address.sun_path, sizeof(address.sun_path), "%s",
1091             io->d->asok_path);
1092     RETRY_ON_EINTR(err,
1093         connect(fd, (struct sockaddr *) &address, sizeof(struct sockaddr_un)));
1094     if(err < 0)
1095     {
1096         ERROR("ceph plugin: cconn_connect: connect(%d) failed: error %d",
1097             fd, err);
1098         close(fd);
1099         return err;
1100     }
1102     flags = fcntl(fd, F_GETFL, 0);
1103     if(fcntl(fd, F_SETFL, flags | O_NONBLOCK) != 0)
1104     {
1105         err = -errno;
1106         ERROR("ceph plugin: cconn_connect: fcntl(%d, O_NONBLOCK) error %d",
1107             fd, err);
1108         close(fd);
1109         return err;
1110     }
1111     io->asok = fd;
1112     io->state = CSTATE_WRITE_REQUEST;
1113     io->amt = 0;
1114     io->json_len = 0;
1115     io->json = NULL;
1116     return 0;
1119 static void cconn_close(struct cconn *io)
1121     io->state = CSTATE_UNCONNECTED;
1122     if(io->asok != -1)
1123     {
1124         int res;
1125         RETRY_ON_EINTR(res, close(io->asok));
1126     }
1127     io->asok = -1;
1128     io->amt = 0;
1129     io->json_len = 0;
1130     sfree(io->json);
1131     io->json = NULL;
1134 /* Process incoming JSON counter data */
1135 static int
1136 cconn_process_data(struct cconn *io, yajl_struct *yajl, yajl_handle hand)
1138     int ret;
1139     struct values_tmp *vtmp = calloc(1, sizeof(struct values_tmp) * 1);
1140     if(!vtmp)
1141     {
1142         return -ENOMEM;
1143     }
1145     vtmp->vlist = (value_list_t)VALUE_LIST_INIT;
1146     sstrncpy(vtmp->vlist.host, hostname_g, sizeof(vtmp->vlist.host));
1147     sstrncpy(vtmp->vlist.plugin, "ceph", sizeof(vtmp->vlist.plugin));
1148     sstrncpy(vtmp->vlist.plugin_instance, io->d->name, sizeof(vtmp->vlist.plugin_instance));
1150     vtmp->d = io->d;
1151     vtmp->avgcount_exists = -1;
1152     vtmp->latency_index = 0;
1153     vtmp->index = 0;
1154     yajl->handler_arg = vtmp;
1155     ret = traverse_json(io->json, io->json_len, hand);
1156     sfree(vtmp);
1157     return ret;
1160 /**
1161  * Initiate JSON parsing and print error if one occurs
1162  */
1163 static int cconn_process_json(struct cconn *io)
1165     if((io->request_type != ASOK_REQ_DATA) &&
1166             (io->request_type != ASOK_REQ_SCHEMA))
1167     {
1168         return -EDOM;
1169     }
1171     int result = 1;
1172     yajl_handle hand;
1173     yajl_status status;
1175     hand = yajl_alloc(&callbacks,
1176 #if HAVE_YAJL_V2
1177       /* alloc funcs = */ NULL,
1178 #else
1179       /* alloc funcs = */ NULL, NULL,
1180 #endif
1181       /* context = */ (void *)(&io->yajl));
1183     if(!hand)
1184     {
1185         ERROR ("ceph plugin: yajl_alloc failed.");
1186         return ENOMEM;
1187     }
1189     io->yajl.depth = 0;
1191     switch(io->request_type)
1192     {
1193         case ASOK_REQ_DATA:
1194             io->yajl.handler = node_handler_fetch_data;
1195             result = cconn_process_data(io, &io->yajl, hand);
1196             break;
1197         case ASOK_REQ_SCHEMA:
1198             //init daemon specific variables
1199             io->d->ds_num = 0;
1200             io->d->last_idx = 0;
1201             io->d->last_poll_data = NULL;
1202             io->yajl.handler = node_handler_define_schema;
1203             io->yajl.handler_arg = io->d;
1204             result = traverse_json(io->json, io->json_len, hand);
1205             break;
1206     }
1208     if(result)
1209     {
1210         goto done;
1211     }
1213 #if HAVE_YAJL_V2
1214     status = yajl_complete_parse(hand);
1215 #else
1216     status = yajl_parse_complete(hand);
1217 #endif
1219     if (status != yajl_status_ok)
1220     {
1221       unsigned char *errmsg = yajl_get_error (hand, /* verbose = */ 0,
1222           /* jsonText = */ NULL, /* jsonTextLen = */ 0);
1223       ERROR ("ceph plugin: yajl_parse_complete failed: %s",
1224           (char *) errmsg);
1225       yajl_free_error (hand, errmsg);
1226       yajl_free (hand);
1227       return 1;
1228     }
1230     done:
1231     yajl_free (hand);
1232     return result;
1235 static int cconn_validate_revents(struct cconn *io, int revents)
1237     if(revents & POLLERR)
1238     {
1239         ERROR("ceph plugin: cconn_validate_revents(name=%s): got POLLERR",
1240             io->d->name);
1241         return -EIO;
1242     }
1243     switch (io->state)
1244     {
1245         case CSTATE_WRITE_REQUEST:
1246             return (revents & POLLOUT) ? 0 : -EINVAL;
1247         case CSTATE_READ_VERSION:
1248         case CSTATE_READ_AMT:
1249         case CSTATE_READ_JSON:
1250             return (revents & POLLIN) ? 0 : -EINVAL;
1251         default:
1252             ERROR("ceph plugin: cconn_validate_revents(name=%s) got to "
1253                 "illegal state on line %d", io->d->name, __LINE__);
1254             return -EDOM;
1255     }
1258 /** Handle a network event for a connection */
1259 static int cconn_handle_event(struct cconn *io)
1261     int ret;
1262     switch (io->state)
1263     {
1264         case CSTATE_UNCONNECTED:
1265             ERROR("ceph plugin: cconn_handle_event(name=%s) got to illegal "
1266                 "state on line %d", io->d->name, __LINE__);
1268             return -EDOM;
1269         case CSTATE_WRITE_REQUEST:
1270         {
1271             char cmd[32];
1272             snprintf(cmd, sizeof(cmd), "%s%d%s", "{ \"prefix\": \"",
1273                     io->request_type, "\" }\n");
1274             size_t cmd_len = strlen(cmd);
1275             RETRY_ON_EINTR(ret,
1276                   write(io->asok, ((char*)&cmd) + io->amt, cmd_len - io->amt));
1277             DEBUG("ceph plugin: cconn_handle_event(name=%s,state=%d,amt=%d,ret=%d)",
1278                     io->d->name, io->state, io->amt, ret);
1279             if(ret < 0)
1280             {
1281                 return ret;
1282             }
1283             io->amt += ret;
1284             if(io->amt >= cmd_len)
1285             {
1286                 io->amt = 0;
1287                 switch (io->request_type)
1288                 {
1289                     case ASOK_REQ_VERSION:
1290                         io->state = CSTATE_READ_VERSION;
1291                         break;
1292                     default:
1293                         io->state = CSTATE_READ_AMT;
1294                         break;
1295                 }
1296             }
1297             return 0;
1298         }
1299         case CSTATE_READ_VERSION:
1300         {
1301             RETRY_ON_EINTR(ret,
1302                     read(io->asok, ((char*)(&io->d->version)) + io->amt,
1303                             sizeof(io->d->version) - io->amt));
1304             DEBUG("ceph plugin: cconn_handle_event(name=%s,state=%d,ret=%d)",
1305                     io->d->name, io->state, ret);
1306             if(ret < 0)
1307             {
1308                 return ret;
1309             }
1310             io->amt += ret;
1311             if(io->amt >= sizeof(io->d->version))
1312             {
1313                 io->d->version = ntohl(io->d->version);
1314                 if(io->d->version != 1)
1315                 {
1316                     ERROR("ceph plugin: cconn_handle_event(name=%s) not "
1317                         "expecting version %d!", io->d->name, io->d->version);
1318                     return -ENOTSUP;
1319                 }
1320                 DEBUG("ceph plugin: cconn_handle_event(name=%s): identified as "
1321                         "version %d", io->d->name, io->d->version);
1322                 io->amt = 0;
1323                 cconn_close(io);
1324                 io->request_type = ASOK_REQ_SCHEMA;
1325             }
1326             return 0;
1327         }
1328         case CSTATE_READ_AMT:
1329         {
1330             RETRY_ON_EINTR(ret,
1331                     read(io->asok, ((char*)(&io->json_len)) + io->amt,
1332                             sizeof(io->json_len) - io->amt));
1333             DEBUG("ceph plugin: cconn_handle_event(name=%s,state=%d,ret=%d)",
1334                     io->d->name, io->state, ret);
1335             if(ret < 0)
1336             {
1337                 return ret;
1338             }
1339             io->amt += ret;
1340             if(io->amt >= sizeof(io->json_len))
1341             {
1342                 io->json_len = ntohl(io->json_len);
1343                 io->amt = 0;
1344                 io->state = CSTATE_READ_JSON;
1345                 io->json = calloc(1, io->json_len + 1);
1346                 if(!io->json)
1347                 {
1348                     ERROR("ceph plugin: error callocing io->json");
1349                     return -ENOMEM;
1350                 }
1351             }
1352             return 0;
1353         }
1354         case CSTATE_READ_JSON:
1355         {
1356             RETRY_ON_EINTR(ret,
1357                    read(io->asok, io->json + io->amt, io->json_len - io->amt));
1358             DEBUG("ceph plugin: cconn_handle_event(name=%s,state=%d,ret=%d)",
1359                     io->d->name, io->state, ret);
1360             if(ret < 0)
1361             {
1362                 return ret;
1363             }
1364             io->amt += ret;
1365             if(io->amt >= io->json_len)
1366             {
1367                 ret = cconn_process_json(io);
1368                 if(ret)
1369                 {
1370                     return ret;
1371                 }
1372                 cconn_close(io);
1373                 io->request_type = ASOK_REQ_NONE;
1374             }
1375             return 0;
1376         }
1377         default:
1378             ERROR("ceph plugin: cconn_handle_event(name=%s) got to illegal "
1379                 "state on line %d", io->d->name, __LINE__);
1380             return -EDOM;
1381     }
1384 static int cconn_prepare(struct cconn *io, struct pollfd* fds)
1386     int ret;
1387     if(io->request_type == ASOK_REQ_NONE)
1388     {
1389         /* The request has already been serviced. */
1390         return 0;
1391     }
1392     else if((io->request_type == ASOK_REQ_DATA) && (io->d->ds_num == 0))
1393     {
1394         /* If there are no counters to report on, don't bother
1395          * connecting */
1396         return 0;
1397     }
1399     switch (io->state)
1400     {
1401         case CSTATE_UNCONNECTED:
1402             ret = cconn_connect(io);
1403             if(ret > 0)
1404             {
1405                 return -ret;
1406             }
1407             else if(ret < 0)
1408             {
1409                 return ret;
1410             }
1411             fds->fd = io->asok;
1412             fds->events = POLLOUT;
1413             return 1;
1414         case CSTATE_WRITE_REQUEST:
1415             fds->fd = io->asok;
1416             fds->events = POLLOUT;
1417             return 1;
1418         case CSTATE_READ_VERSION:
1419         case CSTATE_READ_AMT:
1420         case CSTATE_READ_JSON:
1421             fds->fd = io->asok;
1422             fds->events = POLLIN;
1423             return 1;
1424         default:
1425             ERROR("ceph plugin: cconn_prepare(name=%s) got to illegal state "
1426                 "on line %d", io->d->name, __LINE__);
1427             return -EDOM;
1428     }
1431 /** Returns the difference between two struct timevals in milliseconds.
1432  * On overflow, we return max/min int.
1433  */
1434 static int milli_diff(const struct timeval *t1, const struct timeval *t2)
1436     int64_t ret;
1437     int sec_diff = t1->tv_sec - t2->tv_sec;
1438     int usec_diff = t1->tv_usec - t2->tv_usec;
1439     ret = usec_diff / 1000;
1440     ret += (sec_diff * 1000);
1441     return (ret > INT_MAX) ? INT_MAX : ((ret < INT_MIN) ? INT_MIN : (int)ret);
1444 /** This handles the actual network I/O to talk to the Ceph daemons.
1445  */
1446 static int cconn_main_loop(uint32_t request_type)
1448     int i, ret, some_unreachable = 0;
1449     struct timeval end_tv;
1450     struct cconn io_array[g_num_daemons];
1452     DEBUG("ceph plugin: entering cconn_main_loop(request_type = %d)", request_type);
1454     /* create cconn array */
1455     memset(io_array, 0, sizeof(io_array));
1456     for(i = 0; i < g_num_daemons; ++i)
1457     {
1458         io_array[i].d = g_daemons[i];
1459         io_array[i].request_type = request_type;
1460         io_array[i].state = CSTATE_UNCONNECTED;
1461     }
1463     /** Calculate the time at which we should give up */
1464     gettimeofday(&end_tv, NULL);
1465     end_tv.tv_sec += CEPH_TIMEOUT_INTERVAL;
1467     while (1)
1468     {
1469         int nfds, diff;
1470         struct timeval tv;
1471         struct cconn *polled_io_array[g_num_daemons];
1472         struct pollfd fds[g_num_daemons];
1473         memset(fds, 0, sizeof(fds));
1474         nfds = 0;
1475         for(i = 0; i < g_num_daemons; ++i)
1476         {
1477             struct cconn *io = io_array + i;
1478             ret = cconn_prepare(io, fds + nfds);
1479             if(ret < 0)
1480             {
1481                 WARNING("ceph plugin: cconn_prepare(name=%s,i=%d,st=%d)=%d",
1482                         io->d->name, i, io->state, ret);
1483                 cconn_close(io);
1484                 io->request_type = ASOK_REQ_NONE;
1485                 some_unreachable = 1;
1486             }
1487             else if(ret == 1)
1488             {
1489                 polled_io_array[nfds++] = io_array + i;
1490             }
1491         }
1492         if(nfds == 0)
1493         {
1494             /* finished */
1495             ret = 0;
1496             goto done;
1497         }
1498         gettimeofday(&tv, NULL);
1499         diff = milli_diff(&end_tv, &tv);
1500         if(diff <= 0)
1501         {
1502             /* Timed out */
1503             ret = -ETIMEDOUT;
1504             WARNING("ceph plugin: cconn_main_loop: timed out.");
1505             goto done;
1506         }
1507         RETRY_ON_EINTR(ret, poll(fds, nfds, diff));
1508         if(ret < 0)
1509         {
1510             ERROR("ceph plugin: poll(2) error: %d", ret);
1511             goto done;
1512         }
1513         for(i = 0; i < nfds; ++i)
1514         {
1515             struct cconn *io = polled_io_array[i];
1516             int revents = fds[i].revents;
1517             if(revents == 0)
1518             {
1519                 /* do nothing */
1520             }
1521             else if(cconn_validate_revents(io, revents))
1522             {
1523                 WARNING("ceph plugin: cconn(name=%s,i=%d,st=%d): "
1524                 "revents validation error: "
1525                 "revents=0x%08x", io->d->name, i, io->state, revents);
1526                 cconn_close(io);
1527                 io->request_type = ASOK_REQ_NONE;
1528                 some_unreachable = 1;
1529             }
1530             else
1531             {
1532                 int ret = cconn_handle_event(io);
1533                 if(ret)
1534                 {
1535                     WARNING("ceph plugin: cconn_handle_event(name=%s,"
1536                     "i=%d,st=%d): error %d", io->d->name, i, io->state, ret);
1537                     cconn_close(io);
1538                     io->request_type = ASOK_REQ_NONE;
1539                     some_unreachable = 1;
1540                 }
1541             }
1542         }
1543     }
1544     done: for(i = 0; i < g_num_daemons; ++i)
1545     {
1546         cconn_close(io_array + i);
1547     }
1548     if(some_unreachable)
1549     {
1550         DEBUG("ceph plugin: cconn_main_loop: some Ceph daemons were unreachable.");
1551     }
1552     else
1553     {
1554         DEBUG("ceph plugin: cconn_main_loop: reached all Ceph daemons :)");
1555     }
1556     return ret;
1559 static int ceph_read(void)
1561     return cconn_main_loop(ASOK_REQ_DATA);
1564 /******* lifecycle *******/
1565 static int ceph_init(void)
1567     int ret;
1568     ceph_daemons_print();
1570     ret = cconn_main_loop(ASOK_REQ_VERSION);
1572     return (ret) ? ret : 0;
1575 static int ceph_shutdown(void)
1577     int i;
1578     for(i = 0; i < g_num_daemons; ++i)
1579     {
1580         ceph_daemon_free(g_daemons[i]);
1581     }
1582     sfree(g_daemons);
1583     g_daemons = NULL;
1584     g_num_daemons = 0;
1585     DEBUG("ceph plugin: finished ceph_shutdown");
1586     return 0;
1589 void module_register(void)
1591     plugin_register_complex_config("ceph", ceph_config);
1592     plugin_register_init("ceph", ceph_init);
1593     plugin_register_read("ceph", ceph_read);
1594     plugin_register_shutdown("ceph", ceph_shutdown);
1596 /* vim: set sw=4 sts=4 et : */