Merge remote-tracking branch 'github/pr/1956'
[collectd.git] / src / utils_curl_stats.c
index 6e98bcf..0509ff4 100644 (file)
@@ -25,6 +25,7 @@
  **/
 
 #include "collectd.h"
+
 #include "common.h"
 #include "utils_curl_stats.h"
 
@@ -110,32 +111,35 @@ static int dispatch_size (CURL *curl, CURLINFO info, value_list_t *vl)
 
 static struct {
        const char *name;
+       const char *config_key;
        size_t offset;
 
        int (*dispatcher)(CURL *, CURLINFO, value_list_t *);
        const char *type;
        CURLINFO info;
 } field_specs[] = {
-#define SPEC(name, dispatcher, type, info) \
-       { #name, offsetof (curl_stats_t, name), dispatcher, type, info }
-
-       SPEC (total_time,              dispatch_gauge, "duration", CURLINFO_TOTAL_TIME),
-       SPEC (namelookup_time,         dispatch_gauge, "duration", CURLINFO_NAMELOOKUP_TIME),
-       SPEC (connect_time,            dispatch_gauge, "duration", CURLINFO_CONNECT_TIME),
-       SPEC (pretransfer_time,        dispatch_gauge, "duration", CURLINFO_PRETRANSFER_TIME),
-       SPEC (size_upload,             dispatch_gauge, "bytes",    CURLINFO_SIZE_UPLOAD),
-       SPEC (size_download,           dispatch_gauge, "bytes",    CURLINFO_SIZE_DOWNLOAD),
-       SPEC (speed_download,          dispatch_speed, "bitrate",  CURLINFO_SPEED_DOWNLOAD),
-       SPEC (speed_upload,            dispatch_speed, "bitrate",  CURLINFO_SPEED_UPLOAD),
-       SPEC (header_size,             dispatch_size,  "bytes",    CURLINFO_HEADER_SIZE),
-       SPEC (request_size,            dispatch_size,  "bytes",    CURLINFO_REQUEST_SIZE),
-       SPEC (content_length_download, dispatch_gauge, "bytes",    CURLINFO_CONTENT_LENGTH_DOWNLOAD),
-       SPEC (content_length_upload,   dispatch_gauge, "bytes",    CURLINFO_CONTENT_LENGTH_UPLOAD),
-       SPEC (starttransfer_time,      dispatch_gauge, "duration", CURLINFO_STARTTRANSFER_TIME),
-       SPEC (redirect_time,           dispatch_gauge, "duration", CURLINFO_REDIRECT_TIME),
-       SPEC (redirect_count,          dispatch_size,  "count",    CURLINFO_REDIRECT_COUNT),
-       SPEC (num_connects,            dispatch_size,  "count",    CURLINFO_NUM_CONNECTS),
-       SPEC (appconnect_time,         dispatch_gauge, "duration", CURLINFO_APPCONNECT_TIME),
+#define SPEC(name, config_key, dispatcher, type, info) \
+       { #name, config_key, offsetof (curl_stats_t, name), dispatcher, type, info }
+
+       SPEC (total_time,              "TotalTime",              dispatch_gauge, "duration", CURLINFO_TOTAL_TIME),
+       SPEC (namelookup_time,         "NamelookupTime",         dispatch_gauge, "duration", CURLINFO_NAMELOOKUP_TIME),
+       SPEC (connect_time,            "ConnectTime",            dispatch_gauge, "duration", CURLINFO_CONNECT_TIME),
+       SPEC (pretransfer_time,        "PretransferTime",        dispatch_gauge, "duration", CURLINFO_PRETRANSFER_TIME),
+       SPEC (size_upload,             "SizeUpload",             dispatch_gauge, "bytes",    CURLINFO_SIZE_UPLOAD),
+       SPEC (size_download,           "SizeDownload",           dispatch_gauge, "bytes",    CURLINFO_SIZE_DOWNLOAD),
+       SPEC (speed_download,          "SpeedDownload",          dispatch_speed, "bitrate",  CURLINFO_SPEED_DOWNLOAD),
+       SPEC (speed_upload,            "SpeedUpload",            dispatch_speed, "bitrate",  CURLINFO_SPEED_UPLOAD),
+       SPEC (header_size,             "HeaderSize",             dispatch_size,  "bytes",    CURLINFO_HEADER_SIZE),
+       SPEC (request_size,            "RequestSize",            dispatch_size,  "bytes",    CURLINFO_REQUEST_SIZE),
+       SPEC (content_length_download, "ContentLengthDownload",  dispatch_gauge, "bytes",    CURLINFO_CONTENT_LENGTH_DOWNLOAD),
+       SPEC (content_length_upload,   "ContentLengthUpload",    dispatch_gauge, "bytes",    CURLINFO_CONTENT_LENGTH_UPLOAD),
+       SPEC (starttransfer_time,      "StarttransferTime",      dispatch_gauge, "duration", CURLINFO_STARTTRANSFER_TIME),
+       SPEC (redirect_time,           "RedirectTime",           dispatch_gauge, "duration", CURLINFO_REDIRECT_TIME),
+       SPEC (redirect_count,          "RedirectCount",          dispatch_size,  "count",    CURLINFO_REDIRECT_COUNT),
+       SPEC (num_connects,            "NumConnects",            dispatch_size,  "count",    CURLINFO_NUM_CONNECTS),
+#ifdef HAVE_CURLINFO_APPCONNECT_TIME
+       SPEC (appconnect_time,         "AppconnectTime",         dispatch_gauge, "duration", CURLINFO_APPCONNECT_TIME),
+#endif
 
 #undef SPEC
 };
@@ -156,23 +160,27 @@ static bool field_enabled (curl_stats_t *s, size_t offset)
 curl_stats_t *curl_stats_from_config (oconfig_item_t *ci)
 {
        curl_stats_t *s;
-       int i;
 
        if (ci == NULL)
                return NULL;
 
-       s = calloc (sizeof (*s), 1);
+       s = calloc (1, sizeof (*s));
        if (s == NULL)
                return NULL;
 
-       for (i = 0; i < ci->children_num; ++i)
+       for (int i = 0; i < ci->children_num; ++i)
        {
                oconfig_item_t *c = ci->children + i;
                size_t field;
 
-               for (field = 0; field < STATIC_ARRAY_SIZE (field_specs); ++field)
+               _Bool enabled = 0;
+
+               for (field = 0; field < STATIC_ARRAY_SIZE (field_specs); ++field) {
+                       if (! strcasecmp (c->key, field_specs[field].config_key))
+                               break;
                        if (! strcasecmp (c->key, field_specs[field].name))
                                break;
+               }
                if (field >= STATIC_ARRAY_SIZE (field_specs))
                {
                        ERROR ("curl stats: Unknown field name %s", c->key);
@@ -180,18 +188,12 @@ curl_stats_t *curl_stats_from_config (oconfig_item_t *ci)
                        return NULL;
                }
 
-               if ((c->values_num != 1)
-                               || ((c->values[0].type != OCONFIG_TYPE_STRING)
-                                       && (c->values[0].type != OCONFIG_TYPE_BOOLEAN))) {
-                       ERROR ("curl stats: `%s' expects a single boolean argument", c->key);
+
+               if (cf_util_get_boolean (c, &enabled) != 0) {
                        free (s);
                        return NULL;
                }
-
-               if (((c->values[0].type == OCONFIG_TYPE_STRING)
-                                       && IS_TRUE (c->values[0].value.string))
-                               || ((c->values[0].type == OCONFIG_TYPE_BOOLEAN)
-                                       && c->values[0].value.boolean))
+               if (enabled)
                        enable_field (s, field_specs[field].offset);
        }
 
@@ -205,25 +207,27 @@ void curl_stats_destroy (curl_stats_t *s)
 } /* curl_stats_destroy */
 
 int curl_stats_dispatch (curl_stats_t *s, CURL *curl,
-               const char *hostname, const char *plugin, const char *plugin_instance,
-               const char *instance_prefix)
+               const char *hostname, const char *plugin, const char *plugin_instance)
 {
        value_list_t vl = VALUE_LIST_INIT;
-       size_t field;
 
        if (s == NULL)
                return 0;
-       if (curl == NULL)
+       if ((curl == NULL) || (hostname == NULL) || (plugin == NULL))
+       {
+               ERROR ("curl stats: dispatch() called with missing arguments "
+                               "(curl=%p; hostname=%s; plugin=%s)", curl,
+                               hostname == NULL ? "<NULL>" : hostname,
+                               plugin == NULL ? "<NULL>" : plugin);
                return -1;
+       }
 
-       if (hostname != NULL)
-               sstrncpy (vl.host, hostname, sizeof (vl.host));
-       if (plugin != NULL)
-               sstrncpy (vl.plugin, plugin, sizeof (vl.plugin));
+       sstrncpy (vl.host, hostname, sizeof (vl.host));
+       sstrncpy (vl.plugin, plugin, sizeof (vl.plugin));
        if (plugin_instance != NULL)
                sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
 
-       for (field = 0; field < STATIC_ARRAY_SIZE (field_specs); ++field)
+       for (size_t field = 0; field < STATIC_ARRAY_SIZE (field_specs); ++field)
        {
                int status;
 
@@ -231,8 +235,7 @@ int curl_stats_dispatch (curl_stats_t *s, CURL *curl,
                        continue;
 
                sstrncpy (vl.type, field_specs[field].type, sizeof (vl.type));
-               ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s%s",
-                               instance_prefix ? instance_prefix : "", field_specs[field].name);
+               sstrncpy (vl.type_instance, field_specs[field].name, sizeof (vl.type_instance));
 
                vl.values = NULL;
                vl.values_len = 0;