Merge branch 'collectd-5.4' into collectd-5.5
[collectd.git] / src / apache.c
index 75ef3e1..9c4b496 100644 (file)
@@ -48,11 +48,13 @@ struct apache_s
        _Bool verify_peer;
        _Bool verify_host;
        char *cacert;
+       char *ssl_ciphers;
        char *server; /* user specific server type */
        char *apache_buffer;
        char apache_curl_error[CURL_ERROR_SIZE];
        size_t apache_buffer_size;
        size_t apache_buffer_fill;
+       int timeout;
        CURL *curl;
 }; /* apache_s */
 
@@ -72,12 +74,14 @@ static void apache_free (apache_t *st)
        sfree (st->user);
        sfree (st->pass);
        sfree (st->cacert);
+       sfree (st->ssl_ciphers);
        sfree (st->server);
        sfree (st->apache_buffer);
        if (st->curl) {
                curl_easy_cleanup(st->curl);
                st->curl = NULL;
        }
+       sfree (st);
 } /* apache_free */
 
 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb,
@@ -179,6 +183,8 @@ static int config_add (oconfig_item_t *ci)
        }
        memset (st, 0, sizeof (*st));
 
+       st->timeout = -1;
+
        status = cf_util_get_string (ci, &st->name);
        if (status != 0)
        {
@@ -205,8 +211,12 @@ static int config_add (oconfig_item_t *ci)
                        status = cf_util_get_boolean (child, &st->verify_host);
                else if (strcasecmp ("CACert", child->key) == 0)
                        status = cf_util_get_string (child, &st->cacert);
+               else if (strcasecmp ("SSLCiphers", child->key) == 0)
+                       status = cf_util_get_string (child, &st->ssl_ciphers);
                else if (strcasecmp ("Server", child->key) == 0)
                        status = cf_util_get_string (child, &st->server);
+               else if (strcasecmp ("Timeout", child->key) == 0)
+                       status = cf_util_get_int (child, &st->timeout);
                else
                {
                        WARNING ("apache plugin: Option `%s' not allowed here.",
@@ -283,8 +293,6 @@ static int config (oconfig_item_t *ci)
 /* initialize curl for each host */
 static int init_host (apache_t *st) /* {{{ */
 {
-       static char credentials[1024];
-
        assert (st->url != NULL);
        /* (Assured by `config_add') */
 
@@ -334,6 +342,12 @@ static int init_host (apache_t *st) /* {{{ */
 
        if (st->user != NULL)
        {
+#ifdef HAVE_CURLOPT_USERNAME
+               curl_easy_setopt (st->curl, CURLOPT_USERNAME, st->user);
+               curl_easy_setopt (st->curl, CURLOPT_PASSWORD,
+                               (st->pass == NULL) ? "" : st->pass);
+#else
+               static char credentials[1024];
                int status;
 
                status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
@@ -349,6 +363,7 @@ static int init_host (apache_t *st) /* {{{ */
                }
 
                curl_easy_setopt (st->curl, CURLOPT_USERPWD, credentials);
+#endif
        }
 
        curl_easy_setopt (st->curl, CURLOPT_URL, st->url);
@@ -361,6 +376,16 @@ static int init_host (apache_t *st) /* {{{ */
                        st->verify_host ? 2L : 0L);
        if (st->cacert != NULL)
                curl_easy_setopt (st->curl, CURLOPT_CAINFO, st->cacert);
+       if (st->ssl_ciphers != NULL)
+               curl_easy_setopt (st->curl, CURLOPT_SSL_CIPHER_LIST,st->ssl_ciphers);
+
+#ifdef HAVE_CURLOPT_TIMEOUT_MS
+       if (st->timeout >= 0)
+               curl_easy_setopt (st->curl, CURLOPT_TIMEOUT_MS, (long) st->timeout);
+       else
+               curl_easy_setopt (st->curl, CURLOPT_TIMEOUT_MS,
+                               CDTIME_T_TO_MS(plugin_get_interval()));
+#endif
 
        return (0);
 } /* }}} int init_host */
@@ -495,12 +520,9 @@ static void submit_scoreboard (char *buf, apache_t *st)
 
 static int apache_read_host (user_data_t *user_data) /* {{{ */
 {
-       int i;
-
        char *ptr;
        char *saveptr;
-       char *lines[16];
-       int   lines_num = 0;
+       char *line;
 
        char *fields[4];
        int   fields_num;
@@ -540,29 +562,17 @@ static int apache_read_host (user_data_t *user_data) /* {{{ */
 
        ptr = st->apache_buffer;
        saveptr = NULL;
-       while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
+       while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
        {
                ptr = NULL;
-               lines_num++;
-
-               if (lines_num >= 16)
-                       break;
-       }
-
-       for (i = 0; i < lines_num; i++)
-       {
-               fields_num = strsplit (lines[i], fields, 4);
+               fields_num = strsplit (line, fields, STATIC_ARRAY_SIZE (fields));
 
                if (fields_num == 3)
                {
-                       if ((strcmp (fields[0], "Total") == 0)
-                                       && (strcmp (fields[1], "Accesses:") == 0))
-                               submit_derive ("apache_requests", "",
-                                               atoll (fields[2]), st);
-                       else if ((strcmp (fields[0], "Total") == 0)
-                                       && (strcmp (fields[1], "kBytes:") == 0))
-                               submit_derive ("apache_bytes", "",
-                                               1024LL * atoll (fields[2]), st);
+                       if ((strcmp (fields[0], "Total") == 0) && (strcmp (fields[1], "Accesses:") == 0))
+                               submit_derive ("apache_requests", "", atoll (fields[2]), st);
+                       else if ((strcmp (fields[0], "Total") == 0) && (strcmp (fields[1], "kBytes:") == 0))
+                               submit_derive ("apache_bytes", "", 1024LL * atoll (fields[2]), st);
                }
                else if (fields_num == 2)
                {