Merge pull request #947 from mfournier/avoid_using_curlopt_userpwd
authorMarc Fournier <marc.fournier@camptocamp.com>
Wed, 11 Mar 2015 21:42:11 +0000 (22:42 +0100)
committerMarc Fournier <marc.fournier@camptocamp.com>
Wed, 11 Mar 2015 21:42:11 +0000 (22:42 +0100)
avoid using CURLOPT_USERPWD when possible

17 files changed:
AUTHORS
README
clean.sh
configure.ac
contrib/redhat/collectd.spec
src/Makefile.am
src/amqp.c
src/ceph.c [new file with mode: 0644]
src/collectd-perl.pod
src/collectd-python.pod
src/collectd.conf.in
src/collectd.conf.pod
src/powerdns.c
src/snmp.c
src/types.db
src/utils_latency.c
src/write_tsdb.c

diff --git a/AUTHORS b/AUTHORS
index b35f35f..072e7ff 100644 (file)
--- a/AUTHORS
+++ b/AUTHORS
@@ -81,6 +81,9 @@ Cyril Feraudet <cyril at feraudet.com>
 Dan Berrange <berrange at redhat.com>
  - uuid plugin.
 
+Dan Ryder <daryder at cisco.com>
+ - ceph plugin.
+
 David Bacher <drbacher at gmail.com>
  - serial plugin.
 
diff --git a/README b/README
index c2c0873..9fb9004 100644 (file)
--- a/README
+++ b/README
@@ -45,6 +45,9 @@ Features
       Name server and resolver statistics from the `statistics-channel'
       interface of BIND 9.5, 9,6 and later.
 
+    - ceph
+      Statistics from the Ceph distributed storage system.
+
     - cgroups
       CPU accounting information for process groups under Linux.
 
@@ -803,8 +806,8 @@ Prerequisites
     <http://www.xmms.org/>
 
   * libyajl (optional)
-    Parse JSON data. This is needed for the `curl_json' and `log_logstash'
-    plugins.
+    Parse JSON data. This is needed for the `ceph', `curl_json' and
+    `log_logstash' plugins.
     <http://github.com/lloyd/yajl>
 
   * libvarnish (optional)
index 46e8c29..200091a 100755 (executable)
--- a/clean.sh
+++ b/clean.sh
@@ -27,7 +27,6 @@ true \
 && rm -f src/*.o \
 && rm -f src/*.la \
 && rm -f src/*.lo \
-&& rm -f src/collectd \
 && rm -f src/collectd.1 \
 && rm -f src/collectd.conf \
 && rm -f src/collectdctl \
@@ -44,6 +43,14 @@ true \
 && rm -f src/*.pb-c.c \
 && rm -f src/*.pb-c.h \
 && rm -f src/Makefile.in \
+&& rm -f -r src/daemon/.deps \
+&& rm -f -r src/daemon/.libs \
+&& rm -f src/daemon/*.o \
+&& rm -f src/daemon/*.la \
+&& rm -f src/daemon/*.lo \
+&& rm -f src/daemon/collectd \
+&& rm -f src/daemon/Makefile.in \
+&& rm -f src/daemon/Makefile \
 && rm -f src/liboconfig/*.o \
 && rm -f src/liboconfig/*.la \
 && rm -f src/liboconfig/*.lo \
index f2d7ebd..2af661e 100644 (file)
@@ -5103,6 +5103,7 @@ plugin_ascent="no"
 plugin_barometer="no"
 plugin_battery="no"
 plugin_bind="no"
+plugin_ceph="no"
 plugin_cgroups="no"
 plugin_conntrack="no"
 plugin_contextswitch="no"
@@ -5296,6 +5297,11 @@ then
        plugin_curl_xml="yes"
 fi
 
+if test "x$with_libyajl" = "xyes"
+then
+       plugin_ceph="yes"
+fi
+
 if test "x$have_processor_info" = "xyes"
 then
        plugin_cpu="yes"
@@ -5468,6 +5474,7 @@ AC_PLUGIN([ascent],      [$plugin_ascent],     [AscentEmu player statistics])
 AC_PLUGIN([barometer],   [$plugin_barometer],  [Barometer sensor on I2C])
 AC_PLUGIN([battery],     [$plugin_battery],    [Battery statistics])
 AC_PLUGIN([bind],        [$plugin_bind],       [ISC Bind nameserver statistics])
+AC_PLUGIN([ceph],        [$plugin_ceph],       [Ceph daemon statistics])
 AC_PLUGIN([conntrack],   [$plugin_conntrack],  [nf_conntrack statistics])
 AC_PLUGIN([contextswitch], [$plugin_contextswitch], [context switch statistics])
 AC_PLUGIN([cpufreq],     [$plugin_cpufreq],    [CPU frequency statistics])
@@ -5839,6 +5846,7 @@ Configuration:
     barometer . . . . . . $enable_barometer
     battery . . . . . . . $enable_battery
     bind  . . . . . . . . $enable_bind
+    ceph  . . . . . . . . $enable_ceph
     conntrack . . . . . . $enable_conntrack
     contextswitch . . . . $enable_contextswitch
     cgroups . . . . . . . $enable_cgroups
index 406ea48..0e5cd86 100644 (file)
@@ -75,6 +75,7 @@
 %define with_ascent 0%{!?_without_ascent:1}
 %define with_battery 0%{!?_without_battery:1}
 %define with_bind 0%{!?_without_bind:1}
+%define with_ceph 0%{!?_without_ceph:0%{?_has_libyajl}}
 %define with_cgroups 0%{!?_without_cgroups:1}
 %define with_conntrack 0%{!?_without_conntrack:1}
 %define with_contextswitch 0%{!?_without_contextswitch:1}
@@ -301,6 +302,16 @@ The BIND plugin retrieves this information that's encoded in XML and provided
 via HTTP and submits the values to collectd.
 %endif
 
+%if %{with_ceph}
+%package ceph
+Summary:       Ceph plugin for collectd
+Group:         System Environment/Daemons
+Requires:      %{name}%{?_isa} = %{version}-%{release}
+BuildRequires: yajl-devel
+%description ceph
+Ceph plugin for collectd
+%endif
+
 %if %{with_curl}
 %package curl
 Summary:       Curl plugin for collectd
@@ -908,6 +919,12 @@ Collectd utilities
 %define _with_csv --disable-csv
 %endif
 
+%if %{with_ceph}
+%define _with_ceph --enable-ceph
+%else
+%define _with_ceph --disable-ceph
+%endif
+
 %if %{with_curl}
 %define _with_curl --enable-curl
 %else
@@ -1554,6 +1571,7 @@ Collectd utilities
        %{?_with_barometer} \
        %{?_with_battery} \
        %{?_with_bind} \
+       %{?_with_ceph} \
        %{?_with_cgroups} \
        %{?_with_conntrack} \
        %{?_with_contextswitch} \
@@ -2034,6 +2052,11 @@ fi
 %{_libdir}/%{name}/bind.so
 %endif
 
+%if %{with_ceph}
+%files ceph
+%{_libdir}/%{name}/ceph.so
+%endif
+
 %if %{with_curl}
 %files curl
 %{_libdir}/%{name}/curl.so
@@ -2268,7 +2291,7 @@ fi
 %changelog
 # * TODO 5.5.0-1
 # - New upstream version
-# - New plugins enabled by default: drbd, log_logstash, write_tsdb, smart, openldap, redis, write_redis, zookeeper, write_log
+# - New plugins enabled by default: ceph, drbd, log_logstash, write_tsdb, smart, openldap, redis, write_redis, zookeeper, write_log
 # - New plugins disabled by default: barometer, write_kafka
 # - Enable zfs_arc, now supported on Linux
 # - Install disk plugin in a dedicated package, as it depends on libudev
index 004fc94..14708c0 100644 (file)
@@ -171,6 +171,15 @@ bind_la_CFLAGS = $(AM_CFLAGS) \
 bind_la_LIBADD = $(BUILD_WITH_LIBCURL_LIBS) $(BUILD_WITH_LIBXML2_LIBS)
 endif
 
+if BUILD_PLUGIN_CEPH
+pkglib_LTLIBRARIES += ceph.la
+ceph_la_SOURCES = ceph.c
+ceph_la_CFLAGS = $(AM_CFLAGS)
+ceph_la_LDFLAGS = $(PLUGIN_LDFLAGS) $(BUILD_WITH_LIBYAJL_LDFLAGS)
+ceph_la_CPPFLAGS = $(AM_CPPFLAGS) $(BUILD_WITH_LIBYAJL_CPPFLAGS)
+ceph_la_LIBADD = $(BUILD_WITH_LIBYAJL_LIBS)
+endif
+
 if BUILD_PLUGIN_CGROUPS
 pkglib_LTLIBRARIES += cgroups.la
 cgroups_la_SOURCES = cgroups.c \
index 1764129..cf2a448 100644 (file)
@@ -80,6 +80,9 @@ struct camqp_config_s
     char   *exchange;
     char   *routing_key;
 
+    /* Number of seconds to wait before connection is retried */
+    int     connection_retry_delay;
+
     /* publish only */
     uint8_t delivery_mode;
     _Bool   store_rates;
@@ -405,6 +408,8 @@ static int camqp_setup_queue (camqp_config_t *conf) /* {{{ */
 
 static int camqp_connect (camqp_config_t *conf) /* {{{ */
 {
+    static time_t last_connect_time = 0;
+
     amqp_rpc_reply_t reply;
     int status;
 #ifdef HAVE_AMQP_TCP_SOCKET
@@ -416,6 +421,19 @@ static int camqp_connect (camqp_config_t *conf) /* {{{ */
     if (conf->connection != NULL)
         return (0);
 
+    time_t now = time(NULL);
+    if (now < (last_connect_time + conf->connection_retry_delay))
+    {
+        DEBUG("amqp plugin: skipping connection retry, "
+            "ConnectionRetryDelay: %d", conf->connection_retry_delay);
+        return(1);
+    }
+    else
+    {
+        DEBUG ("amqp plugin: retrying connection");
+        last_connect_time = now;
+    }
+
     conf->connection = amqp_new_connection ();
     if (conf->connection == NULL)
     {
@@ -922,6 +940,8 @@ static int camqp_config_connection (oconfig_item_t *ci, /* {{{ */
     conf->password = NULL;
     conf->exchange = NULL;
     conf->routing_key = NULL;
+    conf->connection_retry_delay = 0;
+
     /* publish only */
     conf->delivery_mode = CAMQP_DM_VOLATILE;
     conf->store_rates = 0;
@@ -1017,6 +1037,8 @@ static int camqp_config_connection (oconfig_item_t *ci, /* {{{ */
             conf->escape_char = tmp_buff[0];
             sfree (tmp_buff);
         }
+        else if (strcasecmp ("ConnectionRetryDelay", child->key) == 0)
+            status = cf_util_get_int (child, &conf->connection_retry_delay);
         else
             WARNING ("amqp plugin: Ignoring unknown "
                     "configuration option \"%s\".", child->key);
diff --git a/src/ceph.c b/src/ceph.c
new file mode 100644 (file)
index 0000000..56e349c
--- /dev/null
@@ -0,0 +1,1580 @@
+/**
+ * collectd - src/ceph.c
+ * Copyright (C) 2011  New Dream Network
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; only version 2 of the License is applicable.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ *
+ * Authors:
+ *   Colin McCabe <cmccabe@alumni.cmu.edu>
+ *   Dennis Zou <yunzou@cisco.com>
+ *   Dan Ryder <daryder@cisco.com>
+ **/
+
+#define _DEFAULT_SOURCE
+#define _BSD_SOURCE
+
+#include "collectd.h"
+#include "common.h"
+#include "plugin.h"
+
+#include <arpa/inet.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <yajl/yajl_parse.h>
+#if HAVE_YAJL_YAJL_VERSION_H
+#include <yajl/yajl_version.h>
+#endif
+
+#include <limits.h>
+#include <poll.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <sys/socket.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <sys/un.h>
+#include <unistd.h>
+#include <math.h>
+#include <inttypes.h>
+
+#define RETRY_AVGCOUNT -1
+
+#if defined(YAJL_MAJOR) && (YAJL_MAJOR > 1)
+# define HAVE_YAJL_V2 1
+#endif
+
+#define RETRY_ON_EINTR(ret, expr) \
+    while(1) { \
+        ret = expr; \
+        if(ret >= 0) \
+            break; \
+        ret = -errno; \
+        if(ret != -EINTR) \
+            break; \
+    }
+
+/** Timeout interval in seconds */
+#define CEPH_TIMEOUT_INTERVAL 1
+
+/** Maximum path length for a UNIX domain socket on this system */
+#define UNIX_DOMAIN_SOCK_PATH_MAX (sizeof(((struct sockaddr_un*)0)->sun_path))
+
+/** Yajl callback returns */
+#define CEPH_CB_CONTINUE 1
+#define CEPH_CB_ABORT 0
+
+#if HAVE_YAJL_V2
+typedef size_t yajl_len_t;
+#else
+typedef unsigned int yajl_len_t;
+#endif
+
+/** Number of types for ceph defined in types.db */
+#define CEPH_DSET_TYPES_NUM 3
+/** ceph types enum */
+enum ceph_dset_type_d
+{
+    DSET_LATENCY = 0,
+    DSET_BYTES = 1,
+    DSET_RATE = 2,
+    DSET_TYPE_UNFOUND = 1000
+};
+
+/** Valid types for ceph defined in types.db */
+const char * ceph_dset_types [CEPH_DSET_TYPES_NUM] =
+                                   {"ceph_latency", "ceph_bytes", "ceph_rate"};
+
+/******* ceph_daemon *******/
+struct ceph_daemon
+{
+    /** Version of the admin_socket interface */
+    uint32_t version;
+    /** daemon name **/
+    char name[DATA_MAX_NAME_LEN];
+
+    /** Path to the socket that we use to talk to the ceph daemon */
+    char asok_path[UNIX_DOMAIN_SOCK_PATH_MAX];
+
+    /** Number of counters */
+    int ds_num;
+    /** Track ds types */
+    uint32_t *ds_types;
+    /** Track ds names to match with types */
+    char **ds_names;
+
+    /**
+     * Keep track of last data for latency values so we can calculate rate
+     * since last poll.
+     */
+    struct last_data **last_poll_data;
+    /** index of last poll data */
+    int last_idx;
+};
+
+/******* JSON parsing *******/
+typedef int (*node_handler_t)(void *, const char*, const char*);
+
+/** Track state and handler while parsing JSON */
+struct yajl_struct
+{
+    node_handler_t handler;
+    void * handler_arg;
+    struct {
+      char key[DATA_MAX_NAME_LEN];
+      int key_len;
+    } state[YAJL_MAX_DEPTH];
+    int depth;
+};
+typedef struct yajl_struct yajl_struct;
+
+enum perfcounter_type_d
+{
+    PERFCOUNTER_LATENCY = 0x4, PERFCOUNTER_DERIVE = 0x8,
+};
+
+/** Give user option to use default (long run = since daemon started) avg */
+static int long_run_latency_avg = 0;
+
+/**
+ * Give user option to use default type for special cases -
+ * filestore.journal_wr_bytes is currently only metric here. Ceph reports the
+ * type as a sum/count pair and will calculate it the same as a latency value.
+ * All other "bytes" metrics (excluding the used/capacity bytes for the OSD)
+ * use the DERIVE type. Unless user specifies to use given type, convert this
+ * metric to use DERIVE.
+ */
+static int convert_special_metrics = 1;
+
+/** Array of daemons to monitor */
+static struct ceph_daemon **g_daemons = NULL;
+
+/** Number of elements in g_daemons */
+static int g_num_daemons = 0;
+
+/**
+ * A set of data that we build up in memory while parsing the JSON.
+ */
+struct values_tmp
+{
+    /** ceph daemon we are processing data for*/
+    struct ceph_daemon *d;
+    /** track avgcount across counters for avgcount/sum latency pairs */
+    uint64_t avgcount;
+    /** current index of counters - used to get type of counter */
+    int index;
+    /** do we already have an avgcount for latency pair */
+    int avgcount_exists;
+    /**
+     * similar to index, but current index of latency type counters -
+     * used to get last poll data of counter
+     */
+    int latency_index;
+    /**
+     * values list - maintain across counters since
+     * host/plugin/plugin instance are always the same
+     */
+    value_list_t vlist;
+};
+
+/**
+ * A set of count/sum pairs to keep track of latency types and get difference
+ * between this poll data and last poll data.
+ */
+struct last_data
+{
+    char ds_name[DATA_MAX_NAME_LEN];
+    double last_sum;
+    uint64_t last_count;
+};
+
+/******* network I/O *******/
+enum cstate_t
+{
+    CSTATE_UNCONNECTED = 0,
+    CSTATE_WRITE_REQUEST,
+    CSTATE_READ_VERSION,
+    CSTATE_READ_AMT,
+    CSTATE_READ_JSON,
+};
+
+enum request_type_t
+{
+    ASOK_REQ_VERSION = 0,
+    ASOK_REQ_DATA = 1,
+    ASOK_REQ_SCHEMA = 2,
+    ASOK_REQ_NONE = 1000,
+};
+
+struct cconn
+{
+    /** The Ceph daemon that we're talking to */
+    struct ceph_daemon *d;
+
+    /** Request type */
+    uint32_t request_type;
+
+    /** The connection state */
+    enum cstate_t state;
+
+    /** The socket we use to talk to this daemon */
+    int asok;
+
+    /** The amount of data remaining to read / write. */
+    uint32_t amt;
+
+    /** Length of the JSON to read */
+    uint32_t json_len;
+
+    /** Buffer containing JSON data */
+    unsigned char *json;
+
+    /** Keep data important to yajl processing */
+    struct yajl_struct yajl;
+};
+
+static int ceph_cb_null(void *ctx)
+{
+    return CEPH_CB_CONTINUE;
+}
+
+static int ceph_cb_boolean(void *ctx, int bool_val)
+{
+    return CEPH_CB_CONTINUE;
+}
+
+static int
+ceph_cb_number(void *ctx, const char *number_val, yajl_len_t number_len)
+{
+    yajl_struct *yajl = (yajl_struct*)ctx;
+    char buffer[number_len+1];
+    int i, latency_type = 0, result;
+    char key[128];
+
+    memcpy(buffer, number_val, number_len);
+    buffer[sizeof(buffer) - 1] = 0;
+
+    ssnprintf(key, yajl->state[0].key_len, "%s", yajl->state[0].key);
+    for(i = 1; i < yajl->depth; i++)
+    {
+        if((i == yajl->depth-1) && ((strcmp(yajl->state[i].key,"avgcount") == 0)
+                || (strcmp(yajl->state[i].key,"sum") == 0)))
+        {
+            if(convert_special_metrics)
+            {
+                /**
+                 * Special case for filestore:JournalWrBytes. For some reason,
+                 * Ceph schema encodes this as a count/sum pair while all
+                 * other "Bytes" data (excluding used/capacity bytes for OSD
+                 * space) uses a single "Derive" type. To spare further
+                 * confusion, keep this KPI as the same type of other "Bytes".
+                 * Instead of keeping an "average" or "rate", use the "sum" in
+                 * the pair and assign that to the derive value.
+                 */
+                if((strcmp(yajl->state[i-1].key, "journal_wr_bytes") == 0) &&
+                        (strcmp(yajl->state[i-2].key,"filestore") == 0) &&
+                        (strcmp(yajl->state[i].key,"avgcount") == 0))
+                {
+                    DEBUG("ceph plugin: Skipping avgcount for filestore.JournalWrBytes");
+                    yajl->depth = (yajl->depth - 1);
+                    return CEPH_CB_CONTINUE;
+                }
+            }
+            //probably a avgcount/sum pair. if not - we'll try full key later
+            latency_type = 1;
+            break;
+        }
+        strncat(key, ".", 1);
+        strncat(key, yajl->state[i].key, yajl->state[i].key_len+1);
+    }
+
+    result = yajl->handler(yajl->handler_arg, buffer, key);
+
+    if((result == RETRY_AVGCOUNT) && latency_type)
+    {
+        strncat(key, ".", 1);
+        strncat(key, yajl->state[yajl->depth-1].key,
+                yajl->state[yajl->depth-1].key_len+1);
+        result = yajl->handler(yajl->handler_arg, buffer, key);
+    }
+
+    if(result == -ENOMEM)
+    {
+        ERROR("ceph plugin: memory allocation failed");
+        return CEPH_CB_ABORT;
+    }
+
+    yajl->depth = (yajl->depth - 1);
+    return CEPH_CB_CONTINUE;
+}
+
+static int ceph_cb_string(void *ctx, const unsigned char *string_val,
+        yajl_len_t string_len)
+{
+    return CEPH_CB_CONTINUE;
+}
+
+static int ceph_cb_start_map(void *ctx)
+{
+    return CEPH_CB_CONTINUE;
+}
+
+static int
+ceph_cb_map_key(void *ctx, const unsigned char *key, yajl_len_t string_len)
+{
+    yajl_struct *yajl = (yajl_struct*)ctx;
+
+    if((yajl->depth+1)  >= YAJL_MAX_DEPTH)
+    {
+        ERROR("ceph plugin: depth exceeds max, aborting.");
+        return CEPH_CB_ABORT;
+    }
+
+    char buffer[string_len+1];
+
+    memcpy(buffer, key, string_len);
+    buffer[sizeof(buffer) - 1] = 0;
+
+    snprintf(yajl->state[yajl->depth].key, sizeof(buffer), "%s", buffer);
+    yajl->state[yajl->depth].key_len = sizeof(buffer);
+    yajl->depth = (yajl->depth + 1);
+
+    return CEPH_CB_CONTINUE;
+}
+
+static int ceph_cb_end_map(void *ctx)
+{
+    yajl_struct *yajl = (yajl_struct*)ctx;
+
+    yajl->depth = (yajl->depth - 1);
+    return CEPH_CB_CONTINUE;
+}
+
+static int ceph_cb_start_array(void *ctx)
+{
+    return CEPH_CB_CONTINUE;
+}
+
+static int ceph_cb_end_array(void *ctx)
+{
+    return CEPH_CB_CONTINUE;
+}
+
+static yajl_callbacks callbacks = {
+        ceph_cb_null,
+        ceph_cb_boolean,
+        NULL,
+        NULL,
+        ceph_cb_number,
+        ceph_cb_string,
+        ceph_cb_start_map,
+        ceph_cb_map_key,
+        ceph_cb_end_map,
+        ceph_cb_start_array,
+        ceph_cb_end_array
+};
+
+static void ceph_daemon_print(const struct ceph_daemon *d)
+{
+    DEBUG("ceph plugin: name=%s, asok_path=%s", d->name, d->asok_path);
+}
+
+static void ceph_daemons_print(void)
+{
+    int i;
+    for(i = 0; i < g_num_daemons; ++i)
+    {
+        ceph_daemon_print(g_daemons[i]);
+    }
+}
+
+static void ceph_daemon_free(struct ceph_daemon *d)
+{
+    int i = 0;
+    for(; i < d->last_idx; i++)
+    {
+        sfree(d->last_poll_data[i]);
+    }
+    sfree(d->last_poll_data);
+    d->last_poll_data = NULL;
+    d->last_idx = 0;
+    for(i = 0; i < d->ds_num; i++)
+    {
+        sfree(d->ds_names[i]);
+    }
+    sfree(d->ds_types);
+    sfree(d->ds_names);
+    sfree(d);
+}
+
+/**
+ * Compact ds name by removing special characters and trimming length to
+ * DATA_MAX_NAME_LEN if necessary
+ */
+static void compact_ds_name(char *source, char *dest)
+{
+    int keys_num = 0, i;
+    char *save_ptr = NULL, *tmp_ptr = source;
+    char *keys[16];
+    char len_str[3];
+    char tmp[DATA_MAX_NAME_LEN];
+    size_t key_chars_remaining = (DATA_MAX_NAME_LEN-1);
+    int reserved = 0;
+    int offset = 0;
+    memset(tmp, 0, sizeof(tmp));
+    if(source == NULL || dest == NULL || source[0] == '\0' || dest[0] != '\0')
+    {
+        return;
+    }
+    size_t src_len = strlen(source);
+    snprintf(len_str, sizeof(len_str), "%zu", src_len);
+    unsigned char append_status = 0x0;
+    append_status |= (source[src_len - 1] == '-') ? 0x1 : 0x0;
+    append_status |= (source[src_len - 1] == '+') ? 0x2 : 0x0;
+    while ((keys[keys_num] = strtok_r(tmp_ptr, ":_-+", &save_ptr)) != NULL)
+    {
+        tmp_ptr = NULL;
+        /** capitalize 1st char **/
+        keys[keys_num][0] = toupper(keys[keys_num][0]);
+        keys_num++;
+        if(keys_num >= 16)
+        {
+            break;
+        }
+    }
+    /** concatenate each part of source string **/
+    for(i = 0; i < keys_num; i++)
+    {
+        strncat(tmp, keys[i], key_chars_remaining);
+        key_chars_remaining -= strlen(keys[i]);
+    }
+    tmp[DATA_MAX_NAME_LEN - 1] = '\0';
+    /** to coordinate limitation of length of type_instance
+     *  we will truncate ds_name
+     *  when the its length is more than
+     *  DATA_MAX_NAME_LEN
+     */
+    if(strlen(tmp) > DATA_MAX_NAME_LEN - 1)
+    {
+        append_status |= 0x4;
+        /** we should reserve space for
+         * len_str
+         */
+        reserved += 2;
+    }
+    if(append_status & 0x1)
+    {
+        /** we should reserve space for
+         * "Minus"
+         */
+        reserved += 5;
+    }
+    if(append_status & 0x2)
+    {
+        /** we should reserve space for
+         * "Plus"
+         */
+        reserved += 4;
+    }
+    snprintf(dest, DATA_MAX_NAME_LEN - reserved, "%s", tmp);
+    offset = strlen(dest);
+    switch (append_status)
+    {
+        case 0x1:
+            memcpy(dest + offset, "Minus", 5);
+            break;
+        case 0x2:
+            memcpy(dest + offset, "Plus", 5);
+            break;
+        case 0x4:
+            memcpy(dest + offset, len_str, 2);
+            break;
+        case 0x5:
+            memcpy(dest + offset, "Minus", 5);
+            memcpy(dest + offset + 5, len_str, 2);
+            break;
+        case 0x6:
+            memcpy(dest + offset, "Plus", 4);
+            memcpy(dest + offset + 4, len_str, 2);
+            break;
+        default:
+            break;
+    }
+}
+
+/**
+ * Parse key to remove "type" if this is for schema and initiate compaction
+ */
+static int parse_keys(const char *key_str, char *ds_name)
+{
+    char *ptr, *rptr;
+    size_t ds_name_len = 0;
+    /**
+     * allow up to 100 characters before compaction - compact_ds_name will not
+     * allow more than DATA_MAX_NAME_LEN chars
+     */
+    int max_str_len = 100;
+    char tmp_ds_name[max_str_len];
+    memset(tmp_ds_name, 0, sizeof(tmp_ds_name));
+    if(ds_name == NULL || key_str == NULL ||  key_str[0] == '\0' ||
+                                                            ds_name[0] != '\0')
+    {
+        return -1;
+    }
+    if((ptr = strchr(key_str, '.')) == NULL
+            || (rptr = strrchr(key_str, '.')) == NULL)
+    {
+        memcpy(tmp_ds_name, key_str, max_str_len - 1);
+        goto compact;
+    }
+
+    ds_name_len = (rptr - ptr) > max_str_len ? max_str_len : (rptr - ptr);
+    if((ds_name_len == 0) || strncmp(rptr + 1, "type", 4))
+    { /** copy whole key **/
+        memcpy(tmp_ds_name, key_str, max_str_len - 1);
+    }
+    else
+    {/** more than two keys **/
+        memcpy(tmp_ds_name, key_str, ((rptr - key_str) > (max_str_len - 1) ?
+                (max_str_len - 1) : (rptr - key_str)));
+    }
+
+    compact: compact_ds_name(tmp_ds_name, ds_name);
+    return 0;
+}
+
+/**
+ * while parsing ceph admin socket schema, save counter name and type for later
+ * data processing
+ */
+static int ceph_daemon_add_ds_entry(struct ceph_daemon *d, const char *name,
+        int pc_type)
+{
+    uint32_t type;
+    char ds_name[DATA_MAX_NAME_LEN];
+    memset(ds_name, 0, sizeof(ds_name));
+
+    if(convert_special_metrics)
+    {
+        /**
+         * Special case for filestore:JournalWrBytes. For some reason, Ceph
+         * schema encodes this as a count/sum pair while all other "Bytes" data
+         * (excluding used/capacity bytes for OSD space) uses a single "Derive"
+         * type. To spare further confusion, keep this KPI as the same type of
+         * other "Bytes". Instead of keeping an "average" or "rate", use the
+         * "sum" in the pair and assign that to the derive value.
+         */
+        if((strcmp(name,"filestore.journal_wr_bytes.type") == 0))
+        {
+            pc_type = 10;
+        }
+    }
+
+    d->ds_names = realloc(d->ds_names, sizeof(char *) * (d->ds_num + 1));
+    if(!d->ds_names)
+    {
+        return -ENOMEM;
+    }
+
+    d->ds_types = realloc(d->ds_types, sizeof(uint32_t) * (d->ds_num + 1));
+    if(!d->ds_types)
+    {
+        return -ENOMEM;
+    }
+
+    d->ds_names[d->ds_num] = malloc(sizeof(char) * DATA_MAX_NAME_LEN);
+    if(!d->ds_names[d->ds_num])
+    {
+        return -ENOMEM;
+    }
+
+    type = (pc_type & PERFCOUNTER_DERIVE) ? DSET_RATE :
+            ((pc_type & PERFCOUNTER_LATENCY) ? DSET_LATENCY : DSET_BYTES);
+    d->ds_types[d->ds_num] = type;
+
+    if(parse_keys(name, ds_name))
+    {
+        return 1;
+    }
+
+    sstrncpy(d->ds_names[d->ds_num], ds_name, DATA_MAX_NAME_LEN -1);
+    d->ds_num = (d->ds_num + 1);
+
+    return 0;
+}
+
+/******* ceph_config *******/
+static int cc_handle_str(struct oconfig_item_s *item, char *dest, int dest_len)
+{
+    const char *val;
+    if(item->values_num != 1)
+    {
+        return -ENOTSUP;
+    }
+    if(item->values[0].type != OCONFIG_TYPE_STRING)
+    {
+        return -ENOTSUP;
+    }
+    val = item->values[0].value.string;
+    if(snprintf(dest, dest_len, "%s", val) > (dest_len - 1))
+    {
+        ERROR("ceph plugin: configuration parameter '%s' is too long.\n",
+                item->key);
+        return -ENAMETOOLONG;
+    }
+    return 0;
+}
+
+static int cc_handle_bool(struct oconfig_item_s *item, int *dest)
+{
+    if(item->values_num != 1)
+    {
+        return -ENOTSUP;
+    }
+
+    if(item->values[0].type != OCONFIG_TYPE_BOOLEAN)
+    {
+        return -ENOTSUP;
+    }
+
+    *dest = (item->values[0].value.boolean) ? 1 : 0;
+    return 0;
+}
+
+static int cc_add_daemon_config(oconfig_item_t *ci)
+{
+    int ret, i;
+    struct ceph_daemon *array, *nd, cd;
+    memset(&cd, 0, sizeof(struct ceph_daemon));
+
+    if((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
+    {
+        WARNING("ceph plugin: `Daemon' blocks need exactly one string "
+                "argument.");
+        return (-1);
+    }
+
+    ret = cc_handle_str(ci, cd.name, DATA_MAX_NAME_LEN);
+    if(ret)
+    {
+        return ret;
+    }
+
+    for(i=0; i < ci->children_num; i++)
+    {
+        oconfig_item_t *child = ci->children + i;
+
+        if(strcasecmp("SocketPath", child->key) == 0)
+        {
+            ret = cc_handle_str(child, cd.asok_path, sizeof(cd.asok_path));
+            if(ret)
+            {
+                return ret;
+            }
+        }
+        else
+        {
+            WARNING("ceph plugin: ignoring unknown option %s", child->key);
+        }
+    }
+    if(cd.name[0] == '\0')
+    {
+        ERROR("ceph plugin: you must configure a daemon name.\n");
+        return -EINVAL;
+    }
+    else if(cd.asok_path[0] == '\0')
+    {
+        ERROR("ceph plugin(name=%s): you must configure an administrative "
+        "socket path.\n", cd.name);
+        return -EINVAL;
+    }
+    else if(!((cd.asok_path[0] == '/') ||
+            (cd.asok_path[0] == '.' && cd.asok_path[1] == '/')))
+    {
+        ERROR("ceph plugin(name=%s): administrative socket paths must begin "
+                "with '/' or './' Can't parse: '%s'\n", cd.name, cd.asok_path);
+        return -EINVAL;
+    }
+
+    array = realloc(g_daemons,
+                    sizeof(struct ceph_daemon *) * (g_num_daemons + 1));
+    if(array == NULL)
+    {
+        /* The positive return value here indicates that this is a
+         * runtime error, not a configuration error.  */
+        return ENOMEM;
+    }
+    g_daemons = (struct ceph_daemon**) array;
+    nd = malloc(sizeof(struct ceph_daemon));
+    if(!nd)
+    {
+        return ENOMEM;
+    }
+    memcpy(nd, &cd, sizeof(struct ceph_daemon));
+    g_daemons[g_num_daemons++] = nd;
+    return 0;
+}
+
+static int ceph_config(oconfig_item_t *ci)
+{
+    int ret, i;
+
+    for(i = 0; i < ci->children_num; ++i)
+    {
+        oconfig_item_t *child = ci->children + i;
+        if(strcasecmp("Daemon", child->key) == 0)
+        {
+            ret = cc_add_daemon_config(child);
+            if(ret == ENOMEM)
+            {
+                ERROR("ceph plugin: Couldn't allocate memory");
+                return ret;
+            }
+            else if(ret)
+            {
+                //process other daemons and ignore this one
+                continue;
+            }
+        }
+        else if(strcasecmp("LongRunAvgLatency", child->key) == 0)
+        {
+            ret = cc_handle_bool(child, &long_run_latency_avg);
+            if(ret)
+            {
+                return ret;
+            }
+        }
+        else if(strcasecmp("ConvertSpecialMetricTypes", child->key) == 0)
+        {
+            ret = cc_handle_bool(child, &convert_special_metrics);
+            if(ret)
+            {
+                return ret;
+            }
+        }
+        else
+        {
+            WARNING("ceph plugin: ignoring unknown option %s", child->key);
+        }
+    }
+    return 0;
+}
+
+/**
+ * Parse JSON and get error message if present
+ */
+static int
+traverse_json(const unsigned char *json, uint32_t json_len, yajl_handle hand)
+{
+    yajl_status status = yajl_parse(hand, json, json_len);
+    unsigned char *msg;
+
+    switch(status)
+    {
+        case yajl_status_error:
+            msg = yajl_get_error(hand, /* verbose = */ 1,
+                                       /* jsonText = */ (unsigned char *) json,
+                                                      (unsigned int) json_len);
+            ERROR ("ceph plugin: yajl_parse failed: %s", msg);
+            yajl_free_error(hand, msg);
+            return 1;
+        case yajl_status_client_canceled:
+            return 1;
+        default:
+            return 0;
+    }
+}
+
+/**
+ * Add entry for each counter while parsing schema
+ */
+static int
+node_handler_define_schema(void *arg, const char *val, const char *key)
+{
+    struct ceph_daemon *d = (struct ceph_daemon *) arg;
+    int pc_type;
+    pc_type = atoi(val);
+    return ceph_daemon_add_ds_entry(d, key, pc_type);
+}
+
+/**
+ * Latency counter does not yet have an entry in last poll data - add it.
+ */
+static int add_last(struct ceph_daemon *d, const char *ds_n, double cur_sum,
+        uint64_t cur_count)
+{
+    d->last_poll_data[d->last_idx] = malloc(1 * sizeof(struct last_data));
+    if(!d->last_poll_data[d->last_idx])
+    {
+        return -ENOMEM;
+    }
+    sstrncpy(d->last_poll_data[d->last_idx]->ds_name,ds_n,
+            sizeof(d->last_poll_data[d->last_idx]->ds_name));
+    d->last_poll_data[d->last_idx]->last_sum = cur_sum;
+    d->last_poll_data[d->last_idx]->last_count = cur_count;
+    d->last_idx = (d->last_idx + 1);
+    return 0;
+}
+
+/**
+ * Update latency counter or add new entry if it doesn't exist
+ */
+static int update_last(struct ceph_daemon *d, const char *ds_n, int index,
+        double cur_sum, uint64_t cur_count)
+{
+    if((d->last_idx > index) && (strcmp(d->last_poll_data[index]->ds_name, ds_n) == 0))
+    {
+        d->last_poll_data[index]->last_sum = cur_sum;
+        d->last_poll_data[index]->last_count = cur_count;
+        return 0;
+    }
+
+    if(!d->last_poll_data)
+    {
+        d->last_poll_data = malloc(1 * sizeof(struct last_data *));
+        if(!d->last_poll_data)
+        {
+            return -ENOMEM;
+        }
+    }
+    else
+    {
+        struct last_data **tmp_last = realloc(d->last_poll_data,
+                ((d->last_idx+1) * sizeof(struct last_data *)));
+        if(!tmp_last)
+        {
+            return -ENOMEM;
+        }
+        d->last_poll_data = tmp_last;
+    }
+    return add_last(d, ds_n, cur_sum, cur_count);
+}
+
+/**
+ * If using index guess failed (shouldn't happen, but possible if counters
+ * get rearranged), resort to searching for counter name
+ */
+static int backup_search_for_last_avg(struct ceph_daemon *d, const char *ds_n)
+{
+    int i = 0;
+    for(; i < d->last_idx; i++)
+    {
+        if(strcmp(d->last_poll_data[i]->ds_name, ds_n) == 0)
+        {
+            return i;
+        }
+    }
+    return -1;
+}
+
+/**
+ * Calculate average b/t current data and last poll data
+ * if last poll data exists
+ */
+static double get_last_avg(struct ceph_daemon *d, const char *ds_n, int index,
+        double cur_sum, uint64_t cur_count)
+{
+    double result = -1.1, sum_delt = 0.0;
+    uint64_t count_delt = 0;
+    int tmp_index = 0;
+    if(d->last_idx > index)
+    {
+        if(strcmp(d->last_poll_data[index]->ds_name, ds_n) == 0)
+        {
+            tmp_index = index;
+        }
+        //test previous index
+        else if((index > 0) && (strcmp(d->last_poll_data[index-1]->ds_name, ds_n) == 0))
+        {
+            tmp_index = (index - 1);
+        }
+        else
+        {
+            tmp_index = backup_search_for_last_avg(d, ds_n);
+        }
+
+        if((tmp_index > -1) && (cur_count > d->last_poll_data[tmp_index]->last_count))
+        {
+            sum_delt = (cur_sum - d->last_poll_data[tmp_index]->last_sum);
+            count_delt = (cur_count - d->last_poll_data[tmp_index]->last_count);
+            result = (sum_delt / count_delt);
+        }
+    }
+
+    if(result == -1.1)
+    {
+        result = NAN;
+    }
+    if(update_last(d, ds_n, tmp_index, cur_sum, cur_count) == -ENOMEM)
+    {
+        return -ENOMEM;
+    }
+    return result;
+}
+
+/**
+ * If using index guess failed, resort to searching for counter name
+ */
+static uint32_t backup_search_for_type(struct ceph_daemon *d, char *ds_name)
+{
+    int idx = 0;
+    for(; idx < d->ds_num; idx++)
+    {
+        if(strcmp(d->ds_names[idx], ds_name) == 0)
+        {
+            return d->ds_types[idx];
+        }
+    }
+    return DSET_TYPE_UNFOUND;
+}
+
+/**
+ * Process counter data and dispatch values
+ */
+static int node_handler_fetch_data(void *arg, const char *val, const char *key)
+{
+    value_t uv;
+    double tmp_d;
+    uint64_t tmp_u;
+    struct values_tmp *vtmp = (struct values_tmp*) arg;
+    uint32_t type = DSET_TYPE_UNFOUND;
+    int index = vtmp->index;
+
+    char ds_name[DATA_MAX_NAME_LEN];
+    memset(ds_name, 0, sizeof(ds_name));
+
+    if(parse_keys(key, ds_name))
+    {
+        return 1;
+    }
+
+    if(index >= vtmp->d->ds_num)
+    {
+        //don't overflow bounds of array
+        index = (vtmp->d->ds_num - 1);
+    }
+
+    /**
+     * counters should remain in same order we parsed schema... we maintain the
+     * index variable to keep track of current point in list of counters. first
+     * use index to guess point in array for retrieving type. if that doesn't
+     * work, use the old way to get the counter type
+     */
+    if(strcmp(ds_name, vtmp->d->ds_names[index]) == 0)
+    {
+        //found match
+        type = vtmp->d->ds_types[index];
+    }
+    else if((index > 0) && (strcmp(ds_name, vtmp->d->ds_names[index-1]) == 0))
+    {
+        //try previous key
+        type = vtmp->d->ds_types[index-1];
+    }
+
+    if(type == DSET_TYPE_UNFOUND)
+    {
+        //couldn't find right type by guessing, check the old way
+        type = backup_search_for_type(vtmp->d, ds_name);
+    }
+
+    switch(type)
+    {
+        case DSET_LATENCY:
+            if(vtmp->avgcount_exists == -1)
+            {
+                sscanf(val, "%" PRIu64, &vtmp->avgcount);
+                vtmp->avgcount_exists = 0;
+                //return after saving avgcount - don't dispatch value
+                //until latency calculation
+                return 0;
+            }
+            else
+            {
+                double sum, result;
+                sscanf(val, "%lf", &sum);
+
+                if(vtmp->avgcount == 0)
+                {
+                    vtmp->avgcount = 1;
+                }
+
+                /** User wants latency values as long run avg */
+                if(long_run_latency_avg)
+                {
+                    result = (sum / vtmp->avgcount);
+                }
+                else
+                {
+                    result = get_last_avg(vtmp->d, ds_name, vtmp->latency_index, sum, vtmp->avgcount);
+                    if(result == -ENOMEM)
+                    {
+                        return -ENOMEM;
+                    }
+                }
+
+                uv.gauge = result;
+                vtmp->avgcount_exists = -1;
+                vtmp->latency_index = (vtmp->latency_index + 1);
+            }
+            break;
+        case DSET_BYTES:
+            sscanf(val, "%lf", &tmp_d);
+            uv.gauge = tmp_d;
+            break;
+        case DSET_RATE:
+            sscanf(val, "%" PRIu64, &tmp_u);
+            uv.derive = tmp_u;
+            break;
+        case DSET_TYPE_UNFOUND:
+        default:
+            ERROR("ceph plugin: ds %s was not properly initialized.", ds_name);
+            return -1;
+    }
+
+    sstrncpy(vtmp->vlist.type, ceph_dset_types[type], sizeof(vtmp->vlist.type));
+    sstrncpy(vtmp->vlist.type_instance, ds_name, sizeof(vtmp->vlist.type_instance));
+    vtmp->vlist.values = &uv;
+    vtmp->vlist.values_len = 1;
+
+    vtmp->index = (vtmp->index + 1);
+    plugin_dispatch_values(&vtmp->vlist);
+
+    return 0;
+}
+
+static int cconn_connect(struct cconn *io)
+{
+    struct sockaddr_un address;
+    int flags, fd, err;
+    if(io->state != CSTATE_UNCONNECTED)
+    {
+        ERROR("ceph plugin: cconn_connect: io->state != CSTATE_UNCONNECTED");
+        return -EDOM;
+    }
+    fd = socket(PF_UNIX, SOCK_STREAM, 0);
+    if(fd < 0)
+    {
+        int err = -errno;
+        ERROR("ceph plugin: cconn_connect: socket(PF_UNIX, SOCK_STREAM, 0) "
+            "failed: error %d", err);
+        return err;
+    }
+    memset(&address, 0, sizeof(struct sockaddr_un));
+    address.sun_family = AF_UNIX;
+    snprintf(address.sun_path, sizeof(address.sun_path), "%s",
+            io->d->asok_path);
+    RETRY_ON_EINTR(err,
+        connect(fd, (struct sockaddr *) &address, sizeof(struct sockaddr_un)));
+    if(err < 0)
+    {
+        ERROR("ceph plugin: cconn_connect: connect(%d) failed: error %d",
+            fd, err);
+        return err;
+    }
+
+    flags = fcntl(fd, F_GETFL, 0);
+    if(fcntl(fd, F_SETFL, flags | O_NONBLOCK) != 0)
+    {
+        err = -errno;
+        ERROR("ceph plugin: cconn_connect: fcntl(%d, O_NONBLOCK) error %d",
+            fd, err);
+        return err;
+    }
+    io->asok = fd;
+    io->state = CSTATE_WRITE_REQUEST;
+    io->amt = 0;
+    io->json_len = 0;
+    io->json = NULL;
+    return 0;
+}
+
+static void cconn_close(struct cconn *io)
+{
+    io->state = CSTATE_UNCONNECTED;
+    if(io->asok != -1)
+    {
+        int res;
+        RETRY_ON_EINTR(res, close(io->asok));
+    }
+    io->asok = -1;
+    io->amt = 0;
+    io->json_len = 0;
+    sfree(io->json);
+    io->json = NULL;
+}
+
+/* Process incoming JSON counter data */
+static int
+cconn_process_data(struct cconn *io, yajl_struct *yajl, yajl_handle hand)
+{
+    int ret;
+    struct values_tmp *vtmp = calloc(1, sizeof(struct values_tmp) * 1);
+    if(!vtmp)
+    {
+        return -ENOMEM;
+    }
+
+    vtmp->vlist = (value_list_t)VALUE_LIST_INIT;
+    sstrncpy(vtmp->vlist.host, hostname_g, sizeof(vtmp->vlist.host));
+    sstrncpy(vtmp->vlist.plugin, "ceph", sizeof(vtmp->vlist.plugin));
+    sstrncpy(vtmp->vlist.plugin_instance, io->d->name, sizeof(vtmp->vlist.plugin_instance));
+
+    vtmp->d = io->d;
+    vtmp->avgcount_exists = -1;
+    vtmp->latency_index = 0;
+    vtmp->index = 0;
+    yajl->handler_arg = vtmp;
+    ret = traverse_json(io->json, io->json_len, hand);
+    sfree(vtmp);
+    return ret;
+}
+
+/**
+ * Initiate JSON parsing and print error if one occurs
+ */
+static int cconn_process_json(struct cconn *io)
+{
+    if((io->request_type != ASOK_REQ_DATA) &&
+            (io->request_type != ASOK_REQ_SCHEMA))
+    {
+        return -EDOM;
+    }
+
+    int result = 1;
+    yajl_handle hand;
+    yajl_status status;
+
+    hand = yajl_alloc(&callbacks,
+#if HAVE_YAJL_V2
+      /* alloc funcs = */ NULL,
+#else
+      /* alloc funcs = */ NULL, NULL,
+#endif
+      /* context = */ (void *)(&io->yajl));
+
+    if(!hand)
+    {
+        ERROR ("ceph plugin: yajl_alloc failed.");
+        return ENOMEM;
+    }
+
+    io->yajl.depth = 0;
+
+    switch(io->request_type)
+    {
+        case ASOK_REQ_DATA:
+            io->yajl.handler = node_handler_fetch_data;
+            result = cconn_process_data(io, &io->yajl, hand);
+            break;
+        case ASOK_REQ_SCHEMA:
+            //init daemon specific variables
+            io->d->ds_num = 0;
+            io->d->last_idx = 0;
+            io->d->last_poll_data = NULL;
+            io->yajl.handler = node_handler_define_schema;
+            io->yajl.handler_arg = io->d;
+            result = traverse_json(io->json, io->json_len, hand);
+            break;
+    }
+
+    if(result)
+    {
+        goto done;
+    }
+
+#if HAVE_YAJL_V2
+    status = yajl_complete_parse(hand);
+#else
+    status = yajl_parse_complete(hand);
+#endif
+
+    if (status != yajl_status_ok)
+    {
+      unsigned char *errmsg = yajl_get_error (hand, /* verbose = */ 0,
+          /* jsonText = */ NULL, /* jsonTextLen = */ 0);
+      ERROR ("ceph plugin: yajl_parse_complete failed: %s",
+          (char *) errmsg);
+      yajl_free_error (hand, errmsg);
+      yajl_free (hand);
+      return 1;
+    }
+
+    done:
+    yajl_free (hand);
+    return result;
+}
+
+static int cconn_validate_revents(struct cconn *io, int revents)
+{
+    if(revents & POLLERR)
+    {
+        ERROR("ceph plugin: cconn_validate_revents(name=%s): got POLLERR",
+            io->d->name);
+        return -EIO;
+    }
+    switch (io->state)
+    {
+        case CSTATE_WRITE_REQUEST:
+            return (revents & POLLOUT) ? 0 : -EINVAL;
+        case CSTATE_READ_VERSION:
+        case CSTATE_READ_AMT:
+        case CSTATE_READ_JSON:
+            return (revents & POLLIN) ? 0 : -EINVAL;
+        default:
+            ERROR("ceph plugin: cconn_validate_revents(name=%s) got to "
+                "illegal state on line %d", io->d->name, __LINE__);
+            return -EDOM;
+    }
+}
+
+/** Handle a network event for a connection */
+static int cconn_handle_event(struct cconn *io)
+{
+    int ret;
+    switch (io->state)
+    {
+        case CSTATE_UNCONNECTED:
+            ERROR("ceph plugin: cconn_handle_event(name=%s) got to illegal "
+                "state on line %d", io->d->name, __LINE__);
+
+            return -EDOM;
+        case CSTATE_WRITE_REQUEST:
+        {
+            char cmd[32];
+            snprintf(cmd, sizeof(cmd), "%s%d%s", "{ \"prefix\": \"",
+                    io->request_type, "\" }\n");
+            size_t cmd_len = strlen(cmd);
+            RETRY_ON_EINTR(ret,
+                  write(io->asok, ((char*)&cmd) + io->amt, cmd_len - io->amt));
+            DEBUG("ceph plugin: cconn_handle_event(name=%s,state=%d,amt=%d,ret=%d)",
+                    io->d->name, io->state, io->amt, ret);
+            if(ret < 0)
+            {
+                return ret;
+            }
+            io->amt += ret;
+            if(io->amt >= cmd_len)
+            {
+                io->amt = 0;
+                switch (io->request_type)
+                {
+                    case ASOK_REQ_VERSION:
+                        io->state = CSTATE_READ_VERSION;
+                        break;
+                    default:
+                        io->state = CSTATE_READ_AMT;
+                        break;
+                }
+            }
+            return 0;
+        }
+        case CSTATE_READ_VERSION:
+        {
+            RETRY_ON_EINTR(ret,
+                    read(io->asok, ((char*)(&io->d->version)) + io->amt,
+                            sizeof(io->d->version) - io->amt));
+            DEBUG("ceph plugin: cconn_handle_event(name=%s,state=%d,ret=%d)",
+                    io->d->name, io->state, ret);
+            if(ret < 0)
+            {
+                return ret;
+            }
+            io->amt += ret;
+            if(io->amt >= sizeof(io->d->version))
+            {
+                io->d->version = ntohl(io->d->version);
+                if(io->d->version != 1)
+                {
+                    ERROR("ceph plugin: cconn_handle_event(name=%s) not "
+                        "expecting version %d!", io->d->name, io->d->version);
+                    return -ENOTSUP;
+                }
+                DEBUG("ceph plugin: cconn_handle_event(name=%s): identified as "
+                        "version %d", io->d->name, io->d->version);
+                io->amt = 0;
+                cconn_close(io);
+                io->request_type = ASOK_REQ_SCHEMA;
+            }
+            return 0;
+        }
+        case CSTATE_READ_AMT:
+        {
+            RETRY_ON_EINTR(ret,
+                    read(io->asok, ((char*)(&io->json_len)) + io->amt,
+                            sizeof(io->json_len) - io->amt));
+            DEBUG("ceph plugin: cconn_handle_event(name=%s,state=%d,ret=%d)",
+                    io->d->name, io->state, ret);
+            if(ret < 0)
+            {
+                return ret;
+            }
+            io->amt += ret;
+            if(io->amt >= sizeof(io->json_len))
+            {
+                io->json_len = ntohl(io->json_len);
+                io->amt = 0;
+                io->state = CSTATE_READ_JSON;
+                io->json = calloc(1, io->json_len + 1);
+                if(!io->json)
+                {
+                    ERROR("ceph plugin: error callocing io->json");
+                    return -ENOMEM;
+                }
+            }
+            return 0;
+        }
+        case CSTATE_READ_JSON:
+        {
+            RETRY_ON_EINTR(ret,
+                   read(io->asok, io->json + io->amt, io->json_len - io->amt));
+            DEBUG("ceph plugin: cconn_handle_event(name=%s,state=%d,ret=%d)",
+                    io->d->name, io->state, ret);
+            if(ret < 0)
+            {
+                return ret;
+            }
+            io->amt += ret;
+            if(io->amt >= io->json_len)
+            {
+                ret = cconn_process_json(io);
+                if(ret)
+                {
+                    return ret;
+                }
+                cconn_close(io);
+                io->request_type = ASOK_REQ_NONE;
+            }
+            return 0;
+        }
+        default:
+            ERROR("ceph plugin: cconn_handle_event(name=%s) got to illegal "
+                "state on line %d", io->d->name, __LINE__);
+            return -EDOM;
+    }
+}
+
+static int cconn_prepare(struct cconn *io, struct pollfd* fds)
+{
+    int ret;
+    if(io->request_type == ASOK_REQ_NONE)
+    {
+        /* The request has already been serviced. */
+        return 0;
+    }
+    else if((io->request_type == ASOK_REQ_DATA) && (io->d->ds_num == 0))
+    {
+        /* If there are no counters to report on, don't bother
+         * connecting */
+        return 0;
+    }
+
+    switch (io->state)
+    {
+        case CSTATE_UNCONNECTED:
+            ret = cconn_connect(io);
+            if(ret > 0)
+            {
+                return -ret;
+            }
+            else if(ret < 0)
+            {
+                return ret;
+            }
+            fds->fd = io->asok;
+            fds->events = POLLOUT;
+            return 1;
+        case CSTATE_WRITE_REQUEST:
+            fds->fd = io->asok;
+            fds->events = POLLOUT;
+            return 1;
+        case CSTATE_READ_VERSION:
+        case CSTATE_READ_AMT:
+        case CSTATE_READ_JSON:
+            fds->fd = io->asok;
+            fds->events = POLLIN;
+            return 1;
+        default:
+            ERROR("ceph plugin: cconn_prepare(name=%s) got to illegal state "
+                "on line %d", io->d->name, __LINE__);
+            return -EDOM;
+    }
+}
+
+/** Returns the difference between two struct timevals in milliseconds.
+ * On overflow, we return max/min int.
+ */
+static int milli_diff(const struct timeval *t1, const struct timeval *t2)
+{
+    int64_t ret;
+    int sec_diff = t1->tv_sec - t2->tv_sec;
+    int usec_diff = t1->tv_usec - t2->tv_usec;
+    ret = usec_diff / 1000;
+    ret += (sec_diff * 1000);
+    return (ret > INT_MAX) ? INT_MAX : ((ret < INT_MIN) ? INT_MIN : (int)ret);
+}
+
+/** This handles the actual network I/O to talk to the Ceph daemons.
+ */
+static int cconn_main_loop(uint32_t request_type)
+{
+    int i, ret, some_unreachable = 0;
+    struct timeval end_tv;
+    struct cconn io_array[g_num_daemons];
+
+    DEBUG("ceph plugin: entering cconn_main_loop(request_type = %d)", request_type);
+
+    /* create cconn array */
+    memset(io_array, 0, sizeof(io_array));
+    for(i = 0; i < g_num_daemons; ++i)
+    {
+        io_array[i].d = g_daemons[i];
+        io_array[i].request_type = request_type;
+        io_array[i].state = CSTATE_UNCONNECTED;
+    }
+
+    /** Calculate the time at which we should give up */
+    gettimeofday(&end_tv, NULL);
+    end_tv.tv_sec += CEPH_TIMEOUT_INTERVAL;
+
+    while (1)
+    {
+        int nfds, diff;
+        struct timeval tv;
+        struct cconn *polled_io_array[g_num_daemons];
+        struct pollfd fds[g_num_daemons];
+        memset(fds, 0, sizeof(fds));
+        nfds = 0;
+        for(i = 0; i < g_num_daemons; ++i)
+        {
+            struct cconn *io = io_array + i;
+            ret = cconn_prepare(io, fds + nfds);
+            if(ret < 0)
+            {
+                WARNING("ceph plugin: cconn_prepare(name=%s,i=%d,st=%d)=%d",
+                        io->d->name, i, io->state, ret);
+                cconn_close(io);
+                io->request_type = ASOK_REQ_NONE;
+                some_unreachable = 1;
+            }
+            else if(ret == 1)
+            {
+                polled_io_array[nfds++] = io_array + i;
+            }
+        }
+        if(nfds == 0)
+        {
+            /* finished */
+            ret = 0;
+            goto done;
+        }
+        gettimeofday(&tv, NULL);
+        diff = milli_diff(&end_tv, &tv);
+        if(diff <= 0)
+        {
+            /* Timed out */
+            ret = -ETIMEDOUT;
+            WARNING("ceph plugin: cconn_main_loop: timed out.");
+            goto done;
+        }
+        RETRY_ON_EINTR(ret, poll(fds, nfds, diff));
+        if(ret < 0)
+        {
+            ERROR("ceph plugin: poll(2) error: %d", ret);
+            goto done;
+        }
+        for(i = 0; i < nfds; ++i)
+        {
+            struct cconn *io = polled_io_array[i];
+            int revents = fds[i].revents;
+            if(revents == 0)
+            {
+                /* do nothing */
+            }
+            else if(cconn_validate_revents(io, revents))
+            {
+                WARNING("ceph plugin: cconn(name=%s,i=%d,st=%d): "
+                "revents validation error: "
+                "revents=0x%08x", io->d->name, i, io->state, revents);
+                cconn_close(io);
+                io->request_type = ASOK_REQ_NONE;
+                some_unreachable = 1;
+            }
+            else
+            {
+                int ret = cconn_handle_event(io);
+                if(ret)
+                {
+                    WARNING("ceph plugin: cconn_handle_event(name=%s,"
+                    "i=%d,st=%d): error %d", io->d->name, i, io->state, ret);
+                    cconn_close(io);
+                    io->request_type = ASOK_REQ_NONE;
+                    some_unreachable = 1;
+                }
+            }
+        }
+    }
+    done: for(i = 0; i < g_num_daemons; ++i)
+    {
+        cconn_close(io_array + i);
+    }
+    if(some_unreachable)
+    {
+        DEBUG("ceph plugin: cconn_main_loop: some Ceph daemons were unreachable.");
+    }
+    else
+    {
+        DEBUG("ceph plugin: cconn_main_loop: reached all Ceph daemons :)");
+    }
+    return ret;
+}
+
+static int ceph_read(void)
+{
+    return cconn_main_loop(ASOK_REQ_DATA);
+}
+
+/******* lifecycle *******/
+static int ceph_init(void)
+{
+    int ret;
+    ceph_daemons_print();
+
+    ret = cconn_main_loop(ASOK_REQ_VERSION);
+
+    return (ret) ? ret : 0;
+}
+
+static int ceph_shutdown(void)
+{
+    int i;
+    for(i = 0; i < g_num_daemons; ++i)
+    {
+        ceph_daemon_free(g_daemons[i]);
+    }
+    sfree(g_daemons);
+    g_daemons = NULL;
+    g_num_daemons = 0;
+    DEBUG("ceph plugin: finished ceph_shutdown");
+    return 0;
+}
+
+void module_register(void)
+{
+    plugin_register_complex_config("ceph", ceph_config);
+    plugin_register_init("ceph", ceph_init);
+    plugin_register_read("ceph", ceph_read);
+    plugin_register_shutdown("ceph", ceph_shutdown);
+}
index 38d790e..f1426de 100644 (file)
@@ -6,9 +6,7 @@ collectd-perl - Documentation of collectd's C<perl plugin>
 
 =head1 SYNOPSIS
 
-  <LoadPlugin perl>
-    Globals true
-  </LoadPlugin>
+  LoadPlugin perl
   # ...
   <Plugin perl>
     IncludeDir "/path/to/perl/plugins"
index 31c4383..0da891f 100644 (file)
@@ -16,9 +16,7 @@ collectd-python - Documentation of collectd's C<python plugin>
 
 =head1 SYNOPSIS
 
-  <LoadPlugin python>
-    Globals true
-  </LoadPlugin>
+  LoadPlugin python
   # ...
   <Plugin python>
     ModulePath "/path/to/your/python/modules"
@@ -47,12 +45,7 @@ The minimum required Python version is I<2.3>.
 
 =item B<LoadPlugin> I<Plugin>
 
-Loads the Python plugin I<Plugin>. Unlike most other LoadPlugin lines, this one
-should be a block containing the line "Globals true". This will cause collectd
-to export the name of all objects in the Python interpreter for all plugins to
-see. If you don't do this or your platform does not support it, the embedded
-interpreter will start anyway but you won't be able to load certain Python
-modules, e.g. "time".
+Loads the Python plugin I<Plugin>.
 
 =item B<Encoding> I<Name>
 
index 34cd24f..8d7622a 100644 (file)
@@ -98,6 +98,7 @@
 #@BUILD_PLUGIN_BAROMETER_TRUE@LoadPlugin barometer
 #@BUILD_PLUGIN_BATTERY_TRUE@LoadPlugin battery
 #@BUILD_PLUGIN_BIND_TRUE@LoadPlugin bind
+#@BUILD_PLUGIN_CEPH_TRUE@LoadPlugin ceph
 #@BUILD_PLUGIN_CONNTRACK_TRUE@LoadPlugin conntrack
 #@BUILD_PLUGIN_CONTEXTSWITCH_TRUE@LoadPlugin contextswitch
 #@BUILD_PLUGIN_CGROUPS_TRUE@LoadPlugin cgroups
 #    RoutingKey "collectd"
 #    Persistent false
 #    StoreRates false
+#    ConnectionRetryDelay 0
 #  </Publish>
 #</Plugin>
 
 #  </View>
 #</Plugin>
 
+#<Plugin ceph>
+#  LongRunAvgLatency false
+#  ConvertSpecialMetricTypes true
+#  <Daemon "osd.0">
+#    SocketPath "/var/run/ceph/ceph-osd.0.asok"
+#  </Daemon>
+#  <Daemon "osd.1">
+#    SocketPath "/var/run/ceph/ceph-osd.1.asok"
+#  </Daemon>
+#  <Daemon "mon.a">
+#    SocketPath "/var/run/ceph/ceph-mon.ceph1.asok"
+#  </Daemon>
+#  <Daemon "mds.a">
+#    SocketPath "/var/run/ceph/ceph-mds.ceph1.asok"
+#  </Daemon>
+#</Plugin>
+
 #<Plugin cgroups>
 #  CGroup "libvirt"
 #  IgnoreSelected false
index 2ee2219..bd78107 100644 (file)
@@ -519,6 +519,7 @@ possibly filtering or messages.
  #   ExchangeType "fanout"
  #   RoutingKey "collectd"
  #   Persistent false
+ #   ConnectionRetryDelay 0
  #   Format "command"
  #   StoreRates false
  #   GraphitePrefix "collectd."
@@ -540,6 +541,7 @@ possibly filtering or messages.
  #   QueueDurable false
  #   QueueAutoDelete true
  #   RoutingKey "collectd.#"
+ #   ConnectionRetryDelay 0
    </Subscribe>
  </Plugin>
 
@@ -630,6 +632,13 @@ mode will be used, i.e. delivery is guaranteed. If set to B<false> (the
 default), the I<transient> delivery mode will be used, i.e. messages may be
 lost due to high load, overflowing queues or similar issues.
 
+=item B<ConnectionRetryDelay> I<Delay>
+
+When the connection to the AMQP broker is lost, defines the time in seconds to
+wait before attempting to reconnect. Defaults to 0, which implies collectd will
+attempt to reconnect at each read interval (in Subscribe mode) or each time
+values are ready for submission (in Publish mode).
+
 =item B<Format> B<Command>|B<JSON>|B<Graphite> (Publish only)
 
 Selects the format in which messages are sent to the broker. If set to
@@ -1169,6 +1178,74 @@ By default no detailed zone information is collected.
 
 =back
 
+=head2 Plugin C<ceph>
+
+The ceph plugin collects values from JSON data to be parsed by B<libyajl>
+(L<https://lloyd.github.io/yajl/>) retrieved from ceph daemon admin sockets.
+
+A separate B<Daemon> block must be configured for each ceph daemon to be
+monitored. The following example will read daemon statistics from four
+separate ceph daemons running on the same device (two OSDs, one MON, one MDS) :
+
+  <Plugin ceph>
+    LongRunAvgLatency false
+    ConvertSpecialMetricTypes true
+    <Daemon "osd.0">
+      SocketPath "/var/run/ceph/ceph-osd.0.asok"
+    </Daemon>
+    <Daemon "osd.1">
+      SocketPath "/var/run/ceph/ceph-osd.1.asok"
+    </Daemon>
+    <Daemon "mon.a">
+      SocketPath "/var/run/ceph/ceph-mon.ceph1.asok"
+    </Daemon>
+    <Daemon "mds.a">
+      SocketPath "/var/run/ceph/ceph-mds.ceph1.asok"
+    </Daemon>
+  </Plugin>
+
+The ceph plugin accepts the following configuration options:
+
+=over 4
+
+=item B<LongRunAvgLatency> B<true>|B<false>
+
+If enabled, latency values(sum,count pairs) are calculated as the long run
+average - average since the ceph daemon was started = (sum / count).
+When disabled, latency values are calculated as the average since the last
+collection = (sum_now - sum_last) / (count_now - count_last).
+
+Default: Disabled
+
+=item B<ConvertSpecialMetricTypes> B<true>|B<false>
+
+If enabled, special metrics (metrics that differ in type from similar counters)
+are converted to the type of those similar counters. This currently only
+applies to filestore.journal_wr_bytes which is a counter for OSD daemons. The
+ceph schema reports this metric type as a sum,count pair while similar counters
+are treated as derive types. When converted, the sum is used as the counter
+value and is treated as a derive type.
+When disabled, all metrics are treated as the types received from the ceph schema.
+
+Default: Enabled
+
+=back
+
+Each B<Daemon> block must have a string argument for the plugin instance name.
+A B<SocketPath> is also required for each B<Daemon> block:
+
+=over 4
+
+=item B<Daemon> I<DaemonName>
+
+Name to be used as the instance name for this daemon.
+
+=item B<SocketPath> I<SocketPath>
+
+Specifies the path to the UNIX admin socket of the ceph daemon.
+
+=back
+
 =head2 Plugin C<cgroups>
 
 This plugin collects the CPU user/system time for each I<cgroup> by reading the
index ece9f75..151e09c 100644 (file)
@@ -147,22 +147,34 @@ statname_lookup_t lookup_table[] = /* {{{ */
   {"recursing-questions",    "dns_question", "recurse"},
   {"tcp-queries",            "dns_question", "tcp"},
   {"udp-queries",            "dns_question", "udp"},
+  {"rd-queries",             "dns_question", "rd"},
 
   /* Answers */
   {"recursing-answers",      "dns_answer",   "recurse"},
   {"tcp-answers",            "dns_answer",   "tcp"},
   {"udp-answers",            "dns_answer",   "udp"},
+  {"recursion-unanswered",   "dns_answer",   "recursion-unanswered"},
+  {"udp-answers-bytes",      "total_bytes",  "udp-answers-bytes"},
 
   /* Cache stuff */
   {"packetcache-hit",        "cache_result", "packet-hit"},
   {"packetcache-miss",       "cache_result", "packet-miss"},
   {"packetcache-size",       "cache_size",   "packet"},
+  {"key-cache-size",         "cache_size",   "key"},
+  {"meta-cache-size",        "cache_size",   "meta"},
+  {"signature-cache-size",   "cache_size",   "signature"},
   {"query-cache-hit",        "cache_result", "query-hit"},
   {"query-cache-miss",       "cache_result", "query-miss"},
 
   /* Latency */
   {"latency",                "latency",      NULL},
 
+  /* DNS updates */
+  {"dnsupdate-answers",      "dns_answer",   "dnsupdate-answer"},
+  {"dnsupdate-changes",      "dns_question", "dnsupdate-changes"},
+  {"dnsupdate-queries",      "dns_question", "dnsupdate-queries"},
+  {"dnsupdate-refused",      "dns_answer",   "dnsupdate-refused"},
+
   /* Other stuff.. */
   {"corrupt-packets",        "ipt_packets",  "corrupt"},
   {"deferred-cache-inserts", "counter",      "cache-deferred_insert"},
@@ -175,6 +187,9 @@ statname_lookup_t lookup_table[] = /* {{{ */
   {"udp4-queries",           "dns_question", "queries-udp4"},
   {"udp6-answers",           "dns_answer",   "udp6"},
   {"udp6-queries",           "dns_question", "queries-udp6"},
+  {"security-status",        "dns_question", "security-status"},
+  {"udp-do-queries",         "dns_question", "udp-do_queries"},
+  {"signatures",             "counter",      "signatures"},
 
   /***********************
    * Recursor statistics *
@@ -224,8 +239,8 @@ statname_lookup_t lookup_table[] = /* {{{ */
   {"throttle-entries",    "gauge",        "entries-throttle"},
   {"unauthorized-tcp",    "counter",      "denied-unauthorized_tcp"},
   {"unauthorized-udp",    "counter",      "denied-unauthorized_udp"},
-  {"unexpected-packets",  "dns_answer",   "unexpected"}
-  /* {"uptime", "", ""} */
+  {"unexpected-packets",  "dns_answer",   "unexpected"},
+  {"uptime",              "uptime",       NULL}
 }; /* }}} */
 int lookup_table_length = STATIC_ARRAY_SIZE (lookup_table);
 
@@ -259,9 +274,6 @@ static void submit (const char *plugin_instance, /* {{{ */
     if (strcmp (lookup_table[i].name, pdns_type) == 0)
       break;
 
-  if (lookup_table[i].type == NULL)
-    return;
-
   if (i >= lookup_table_length)
   {
     INFO ("powerdns plugin: submit: Not found in lookup table: %s = %s;",
@@ -269,6 +281,9 @@ static void submit (const char *plugin_instance, /* {{{ */
     return;
   }
 
+  if (lookup_table[i].type == NULL)
+    return;
+
   type = lookup_table[i].type;
   type_instance = lookup_table[i].type_instance;
 
index ab59638..cb2bb36 100644 (file)
@@ -993,7 +993,8 @@ static value_t csnmp_value_list_to_value (struct variable_list *vl, int type,
       status = parse_value (string, &ret, type);
       if (status != 0)
       {
-        ERROR ("snmp plugin: csnmp_value_list_to_value: Parsing string as %s failed: %s",
+        ERROR ("snmp plugin: host %s: csnmp_value_list_to_value: Parsing string as %s failed: %s",
+            (host_name != NULL) ? host_name : "UNKNOWN",
             DS_TYPE_TO_STRING (type), string);
       }
     }
@@ -1546,7 +1547,8 @@ static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
         if (csnmp_instance_list_add (&instance_list_head, &instance_list_tail,
               res, host, data) != 0)
         {
-          ERROR ("snmp plugin: csnmp_instance_list_add failed.");
+          ERROR ("snmp plugin: host %s: csnmp_instance_list_add failed.",
+              host->name);
           status = -1;
           break;
         }
index a454a2b..be1df7f 100644 (file)
@@ -15,6 +15,9 @@ cache_operation               value:DERIVE:0:U
 cache_ratio            value:GAUGE:0:100
 cache_result           value:DERIVE:0:U
 cache_size             value:GAUGE:0:U
+ceph_bytes             value:GAUGE:U:U
+ceph_latency   value:GAUGE:U:U
+ceph_rate                      value:DERIVE:0:U
 changes_since_last_save   value:GAUGE:0:U
 charge                 value:GAUGE:0:U
 compression_ratio      value:GAUGE:0:2
index 91ddd5f..b0ef359 100644 (file)
  **/
 
 #include "collectd.h"
+#include "plugin.h"
 #include "utils_latency.h"
 #include "common.h"
 
-#ifndef LATENCY_HISTOGRAM_SIZE
-# define LATENCY_HISTOGRAM_SIZE 1000
+#include <math.h>
+
+#ifndef HISTOGRAM_NUM_BINS
+# define HISTOGRAM_NUM_BINS 1000
 #endif
 
+static const int HISTOGRAM_DEFAULT_BIN_WIDTH = 1;
+
 struct latency_counter_s
 {
   cdtime_t start_time;
@@ -42,9 +47,69 @@ struct latency_counter_s
   cdtime_t min;
   cdtime_t max;
 
-  int histogram[LATENCY_HISTOGRAM_SIZE];
+  int bin_width;
+  int histogram[HISTOGRAM_NUM_BINS];
 };
 
+/*
+* Histogram represents the distribution of data, it has a list of "bins".
+* Each bin represents an interval and has a count (frequency) of
+* number of values fall within its interval.
+*
+* Histogram's range is determined by the number of bins and the bin width,
+* There are 1000 bins and all bins have the same width of default 1 millisecond.
+* When a value above this range is added, Histogram's range is increased by
+* increasing the bin width (note that number of bins remains always at 1000).
+* This operation of increasing bin width is little expensive as each bin need
+* to be visited to update it's count. To reduce frequent change of bin width,
+* new bin width will be the next nearest power of 2. Example: 2, 4, 8, 16, 32,
+* 64, 128, 256, 512, 1024, 2048, 5086, ...
+*
+* So, if the required bin width is 300, then new bin width will be 512 as it is
+* the next nearest power of 2.
+*
+*/
+void change_bin_width (latency_counter_t *lc, size_t val) /* {{{ */
+{
+  int i=0;
+  /* This function is called because the new value is above histogram's range.
+   * First find the required bin width:
+   *           requiredBinWidth = (value + 1) / numBins
+   * then get the next nearest power of 2
+   *           newBinWidth = 2^(ceil(log2(requiredBinWidth)))
+   */
+  double required_bin_width = (double)(val + 1) / HISTOGRAM_NUM_BINS;
+  double required_bin_width_logbase2 = log(required_bin_width) / log(2.0);
+  int new_bin_width = (int)(pow(2.0, ceil( required_bin_width_logbase2)));
+  int old_bin_width = lc->bin_width;
+  lc->bin_width = new_bin_width;
+
+  /*
+   * bin width has been increased, now iterate through all bins and move the
+   * old bin's count to new bin.
+   */
+  if (lc->num > 0) // if the histogram has data then iterate else skip
+  {
+      double width_change_ratio = old_bin_width / new_bin_width;
+      for (i=0; i<HISTOGRAM_NUM_BINS; i++)
+      {
+         int new_bin = (int)(i * width_change_ratio);
+         if (i == new_bin)
+             continue;
+         lc->histogram[new_bin] += lc->histogram[i];
+         lc->histogram[i] = 0;
+      }
+      DEBUG("utils_latency: change_bin_width: fixed all bins");
+  }
+
+  DEBUG("utils_latency: change_bin_width: val-[%ld], oldBinWidth-[%d], "
+          "newBinWidth-[%d], required_bin_width-[%f], "
+          "required_bin_width_logbase2-[%f]",
+          val, old_bin_width, new_bin_width, required_bin_width,
+          required_bin_width_logbase2);
+
+} /* }}} void change_bin_width */
+
 latency_counter_t *latency_counter_create () /* {{{ */
 {
   latency_counter_t *lc;
@@ -54,6 +119,7 @@ latency_counter_t *latency_counter_create () /* {{{ */
     return (NULL);
 
   latency_counter_reset (lc);
+  lc->bin_width = HISTOGRAM_DEFAULT_BIN_WIDTH;
   return (lc);
 } /* }}} latency_counter_t *latency_counter_create */
 
@@ -83,8 +149,19 @@ void latency_counter_add (latency_counter_t *lc, cdtime_t latency) /* {{{ */
    * subtract one from the cdtime_t value so that exactly 1.0 ms get sorted
    * accordingly. */
   latency_ms = (size_t) CDTIME_T_TO_MS (latency - 1);
-  if (latency_ms < STATIC_ARRAY_SIZE (lc->histogram))
-    lc->histogram[latency_ms]++;
+
+  int bin = (int)(latency_ms / lc->bin_width);
+  if (bin >= HISTOGRAM_NUM_BINS)
+  {
+      change_bin_width(lc, latency_ms);
+      bin = (int)(latency_ms / lc->bin_width);
+      if (bin >= HISTOGRAM_NUM_BINS)
+      {
+          ERROR("utils_latency: latency_counter_add: Invalid bin %d", bin);
+          return;
+      }
+  }
+  lc->histogram[bin]++;
 } /* }}} void latency_counter_add */
 
 void latency_counter_reset (latency_counter_t *lc) /* {{{ */
@@ -92,7 +169,11 @@ void latency_counter_reset (latency_counter_t *lc) /* {{{ */
   if (lc == NULL)
     return;
 
+  int bin_width = lc->bin_width;
   memset (lc, 0, sizeof (*lc));
+
+  /* preserve bin width */
+  lc->bin_width = bin_width;
   lc->start_time = cdtime ();
 } /* }}} void latency_counter_reset */
 
@@ -153,7 +234,7 @@ cdtime_t latency_counter_get_percentile (latency_counter_t *lc,
   percent_upper = 0.0;
   percent_lower = 0.0;
   sum = 0;
-  for (i = 0; i < LATENCY_HISTOGRAM_SIZE; i++)
+  for (i = 0; i < HISTOGRAM_NUM_BINS; i++)
   {
     percent_lower = percent_upper;
     sum += lc->histogram[i];
@@ -166,14 +247,14 @@ cdtime_t latency_counter_get_percentile (latency_counter_t *lc,
       break;
   }
 
-  if (i >= LATENCY_HISTOGRAM_SIZE)
+  if (i >= HISTOGRAM_NUM_BINS)
     return (0);
 
   assert (percent_upper >= percent);
   assert (percent_lower < percent);
 
-  ms_upper = (double) (i + 1);
-  ms_lower = (double) i;
+  ms_upper = (double) ( (i + 1) * lc->bin_width );
+  ms_lower = (double) ( i * lc->bin_width );
   if (i == 0)
     return (MS_TO_CDTIME_T (ms_upper));
 
index b7f46f1..9008a67 100644 (file)
@@ -365,30 +365,41 @@ static int wt_format_name(char *ret, int ret_len,
 
     if (ds_name != NULL) {
         if (vl->plugin_instance[0] == '\0') {
-            ssnprintf(ret, ret_len, "%s%s.%s",
-                      prefix, vl->plugin, ds_name);
-        } else if (vl->type_instance[0] == '\0') {
-            ssnprintf(ret, ret_len, "%s%s.%s.%s.%s",
-                      prefix, vl->plugin, vl->plugin_instance,
-                      vl->type_instance, ds_name);
-        } else {
-            ssnprintf(ret, ret_len, "%s%s.%s.%s.%s",
-                      prefix, vl->plugin, vl->plugin_instance, vl->type,
-                      ds_name);
+            if (vl->type_instance[0] == '\0') {
+                ssnprintf(ret, ret_len, "%s%s.%s.%s", prefix, vl->plugin,
+                        vl->type, ds_name);
+            } else {
+                ssnprintf(ret, ret_len, "%s%s.%s.%s.%s", prefix, vl->plugin,
+                        vl->type, vl->type_instance, ds_name);
+            }
+        } else { /* vl->plugin_instance != "" */
+            if (vl->type_instance[0] == '\0') {
+                ssnprintf(ret, ret_len, "%s%s.%s.%s.%s", prefix, vl->plugin,
+                        vl->plugin_instance, vl->type, ds_name);
+            } else {
+                ssnprintf(ret, ret_len, "%s%s.%s.%s.%s.%s", prefix,
+                        vl->plugin, vl->plugin_instance, vl->type,
+                        vl->type_instance, ds_name);
+            }
+        }
+    } else { /* ds_name == NULL */
+        if (vl->plugin_instance[0] == '\0') {
+            if (vl->type_instance[0] == '\0') {
+                ssnprintf(ret, ret_len, "%s%s.%s", prefix, vl->plugin,
+                        vl->type);
+            } else {
+                ssnprintf(ret, ret_len, "%s%s.%s.%s", prefix, vl->plugin,
+                        vl->type_instance, vl->type);
+            }
+        } else { /* vl->plugin_instance != "" */
+            if (vl->type_instance[0] == '\0') {
+                ssnprintf(ret, ret_len, "%s%s.%s.%s", prefix, vl->plugin,
+                        vl->plugin_instance, vl->type);
+            } else {
+                ssnprintf(ret, ret_len, "%s%s.%s.%s.%s", prefix, vl->plugin,
+                        vl->plugin_instance, vl->type, vl->type_instance);
+            }
         }
-    } else if (vl->plugin_instance[0] == '\0') {
-        if (vl->type_instance[0] == '\0')
-            ssnprintf(ret, ret_len, "%s%s.%s",
-                      prefix, vl->plugin, vl->type);
-        else
-            ssnprintf(ret, ret_len, "%s%s.%s",
-                      prefix, vl->plugin, vl->type_instance);
-    } else if (vl->type_instance[0] == '\0') {
-        ssnprintf(ret, ret_len, "%s%s.%s.%s",
-                  prefix, vl->plugin, vl->plugin_instance, vl->type);
-    } else {
-        ssnprintf(ret, ret_len, "%s%s.%s.%s.%s",
-                  prefix, vl->plugin, vl->plugin_instance, vl->type, vl->type_instance);
     }
 
     sfree(temp);