diff --git a/src/apache.c b/src/apache.c
index 41b807af7541fb748634d2b5c3660198659dc43a..b57d08edc447c9005d32fded65b85d00d425b111 100644 (file)
--- a/src/apache.c
+++ b/src/apache.c
**/
#include "collectd.h"
+
#include "common.h"
#include "plugin.h"
-#include "configfile.h"
#include <curl/curl.h>
/* TODO: Remove this prototype */
static int apache_read_host (user_data_t *user_data);
-static void apache_free (apache_t *st)
+static void apache_free (void *arg)
{
+ apache_t *st = arg;
+
if (st == NULL)
return;
return (0);
}
- if (len <= 0)
+ if (len == 0)
return (len);
if ((st->apache_buffer_fill + len) >= st->apache_buffer_size)
{
char *temp;
- temp = (char *) realloc (st->apache_buffer,
+ temp = realloc (st->apache_buffer,
st->apache_buffer_fill + len + 1);
if (temp == NULL)
{
return (0);
}
- if (len <= 0)
+ if (len == 0)
return (len);
/* look for the Server header */
static int config_add (oconfig_item_t *ci)
{
apache_t *st;
- int i;
int status;
- st = malloc (sizeof (*st));
+ st = calloc (1, sizeof (*st));
if (st == NULL)
{
- ERROR ("apache plugin: malloc failed.");
+ ERROR ("apache plugin: calloc failed.");
return (-1);
}
- memset (st, 0, sizeof (*st));
st->timeout = -1;
}
assert (st->name != NULL);
- for (i = 0; i < ci->children_num; i++)
+ for (int i = 0; i < ci->children_num; i++)
{
oconfig_item_t *child = ci->children + i;
if (status == 0)
{
- user_data_t ud;
- char callback_name[3*DATA_MAX_NAME_LEN];
+ user_data_t ud = {
+ .data = st,
+ .free_func = apache_free
+ };
- memset (&ud, 0, sizeof (ud));
- ud.data = st;
- ud.free_func = (void *) apache_free;
+ char callback_name[3*DATA_MAX_NAME_LEN];
- memset (callback_name, 0, sizeof (callback_name));
ssnprintf (callback_name, sizeof (callback_name),
"apache/%s/%s",
(st->host != NULL) ? st->host : hostname_g,
- (st->name != NULL) ? st->name : "default"),
+ (st->name != NULL) ? st->name : "default");
status = plugin_register_complex_read (/* group = */ NULL,
/* name = */ callback_name,
static int config (oconfig_item_t *ci)
{
int status = 0;
- int i;
- for (i = 0; i < ci->children_num; i++)
+ for (int i = 0; i < ci->children_num; i++)
{
oconfig_item_t *child = ci->children + i;
long long response_start = 0LL;
long long response_end = 0LL;
- int i;
- for (i = 0; buf[i] != '\0'; i++)
+ for (int i = 0; buf[i] != '\0'; i++)
{
if (buf[i] == '.') open++;
else if (buf[i] == '_') waiting++;
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;
st = user_data->data;
+ int status;
+
+ char *content_type;
+ static const char *text_plain = "text/plain";
+
assert (st->url != NULL);
/* (Assured by `config_add') */
if (st->curl == NULL)
{
- int status;
-
status = init_host (st);
if (status != 0)
return (-1);
st->server_type = APACHE;
}
- ptr = st->apache_buffer;
- saveptr = NULL;
- while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
+ status = curl_easy_getinfo (st->curl, CURLINFO_CONTENT_TYPE, &content_type);
+ if ((status == CURLE_OK) && (content_type != NULL) &&
+ (strncasecmp (content_type, text_plain, strlen (text_plain)) != 0))
{
- ptr = NULL;
- lines_num++;
-
- if (lines_num >= 16)
- break;
+ WARNING ("apache plugin: `Content-Type' response header is not `%s' "
+ "(received: `%s'). Expecting unparseable data. Please check `URL' "
+ "parameter (missing `?auto' suffix ?)",
+ text_plain, content_type);
}
- for (i = 0; i < lines_num; i++)
+ ptr = st->apache_buffer;
+ saveptr = NULL;
+ while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
{
- fields_num = strsplit (lines[i], fields, 4);
+ ptr = NULL;
+ 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)
{