/**
* collectd - src/apache.c
- * Copyright (C) 2006 Florian octo Forster
+ * Copyright (C) 2006-2008 Florian octo Forster
+ * Copyright (C) 2007 Florent EppO Monbillard
*
* 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; either version 2 of the License, or (at your
- * option) any later version.
+ * 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
*
* Authors:
* Florian octo Forster <octo at verplant.org>
+ * Florent EppO Monbillard <eppo at darox.net>
+ * - connections/lighttpd extension
**/
#include "collectd.h"
#include "plugin.h"
#include "configfile.h"
-#define MODULE_NAME "apache"
+#include <curl/curl.h>
-#if HAVE_LIBCURL && HAVE_CURL_CURL_H
-# define APACHE_HAVE_READ 1
-# include <curl/curl.h>
-#else
-# define APACHE_HAVE_READ 0
-#endif
+static char *url = NULL;
+static char *user = NULL;
+static char *pass = NULL;
+static char *verify_peer = NULL;
+static char *verify_host = NULL;
+static char *cacert = NULL;
-static char *url = NULL;
-static char *user = NULL;
-static char *pass = NULL;
-static char *cacert = NULL;
-
-#if HAVE_LIBCURL
static CURL *curl = NULL;
-#define ABUFFER_SIZE 16384
-static char apache_buffer[ABUFFER_SIZE];
-static int apache_buffer_len = 0;
-static char apache_curl_error[CURL_ERROR_SIZE];
-#endif /* HAVE_LIBCURL */
-
-/* Limit to 2^27 bytes/s. That's what a gigabit-ethernet link can handle, in
- * theory. */
-static char *bytes_file = "apache/apache_bytes.rrd";
-static char *bytes_ds_def[] =
-{
- "DS:count:COUNTER:"COLLECTD_HEARTBEAT":0:134217728",
- NULL
-};
-static int bytes_ds_num = 1;
-
-/* Limit to 2^20 requests/s */
-static char *requests_file = "apache/apache_requests.rrd";
-static char *requests_ds_def[] =
-{
- "DS:count:COUNTER:"COLLECTD_HEARTBEAT":0:1048576",
- NULL
-};
-static int requests_ds_num = 1;
+static char *apache_buffer = NULL;
+static size_t apache_buffer_size = 0;
+static size_t apache_buffer_fill = 0;
+static char apache_curl_error[CURL_ERROR_SIZE];
-static char *scoreboard_file = "apache/apache_scoreboard-%s.rrd";
-static char *scoreboard_ds_def[] =
-{
- "DS:count:GAUGE:"COLLECTD_HEARTBEAT":0:U",
- NULL
-};
-static int scoreboard_ds_num = 1;
-
-static char *config_keys[] =
+static const char *config_keys[] =
{
"URL",
"User",
"Password",
- "CACert",
- NULL
+ "VerifyPeer",
+ "VerifyHost",
+ "CACert"
};
-static int config_keys_num = 4;
+static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
-#if HAVE_LIBCURL
-static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb, void *stream)
+static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb,
+ void *stream)
{
size_t len = size * nmemb;
- if ((apache_buffer_len + len) >= ABUFFER_SIZE)
- {
- len = (ABUFFER_SIZE - 1) - apache_buffer_len;
- }
-
if (len <= 0)
return (len);
- memcpy (apache_buffer + apache_buffer_len, (char *) buf, len);
- apache_buffer_len += len;
- apache_buffer[apache_buffer_len] = '\0';
+ if ((apache_buffer_fill + len) >= apache_buffer_size)
+ {
+ char *temp;
+
+ temp = (char *) realloc (apache_buffer,
+ apache_buffer_fill + len + 1);
+ if (temp == NULL)
+ {
+ ERROR ("apache plugin: realloc failed.");
+ return (0);
+ }
+ apache_buffer = temp;
+ apache_buffer_size = apache_buffer_fill + len + 1;
+ }
+
+ memcpy (apache_buffer + apache_buffer_fill, (char *) buf, len);
+ apache_buffer_fill += len;
+ apache_buffer[apache_buffer_fill] = 0;
return (len);
}
-#endif /* HAVE_LIBCURL */
-static int config_set (char **var, char *value)
+static int config_set (char **var, const char *value)
{
if (*var != NULL)
{
return (0);
}
-static int config (char *key, char *value)
+static int config (const char *key, const char *value)
{
if (strcasecmp (key, "url") == 0)
return (config_set (&url, value));
return (config_set (&user, value));
else if (strcasecmp (key, "password") == 0)
return (config_set (&pass, value));
+ else if (strcasecmp (key, "verifypeer") == 0)
+ return (config_set (&verify_peer, value));
+ else if (strcasecmp (key, "verifyhost") == 0)
+ return (config_set (&verify_host, value));
else if (strcasecmp (key, "cacert") == 0)
return (config_set (&cacert, value));
else
return (-1);
}
-static void init (void)
+static int init (void)
{
-#if HAVE_LIBCURL
static char credentials[1024];
+ if (url == NULL)
+ {
+ WARNING ("apache plugin: init: No URL configured, returning "
+ "an error.");
+ return (-1);
+ }
+
if (curl != NULL)
{
curl_easy_cleanup (curl);
if ((curl = curl_easy_init ()) == NULL)
{
- syslog (LOG_ERR, "apache: `curl_easy_init' failed.");
- return;
+ ERROR ("apache plugin: init: `curl_easy_init' failed.");
+ return (-1);
}
curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
if (user != NULL)
{
- if (snprintf (credentials, 1024, "%s:%s", user, pass == NULL ? "" : pass) >= 1024)
+ int status;
+
+ status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
+ user, (pass == NULL) ? "" : pass);
+ if (status >= sizeof (credentials))
{
- syslog (LOG_ERR, "apache: Credentials would have been truncated.");
- return;
+ ERROR ("apache plugin: init: Returning an error "
+ "because the credentials have been "
+ "truncated.");
+ return (-1);
}
curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
}
- if (url != NULL)
+ curl_easy_setopt (curl, CURLOPT_URL, url);
+
+ if ((verify_peer == NULL) || (strcmp (verify_peer, "true") == 0))
{
- curl_easy_setopt (curl, CURLOPT_URL, url);
+ curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 1);
+ }
+ else
+ {
+ curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 0);
+ }
+
+ if ((verify_host == NULL) || (strcmp (verify_host, "true") == 0))
+ {
+ curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 2);
+ }
+ else
+ {
+ curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 0);
}
if (cacert != NULL)
{
curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
}
-#endif /* HAVE_LIBCURL */
-}
-static void bytes_write (char *host, char *inst, char *val)
-{
- rrd_update_file (host, bytes_file, val, bytes_ds_def, bytes_ds_num);
-}
+ return (0);
+} /* int init */
-static void requests_write (char *host, char *inst, char *val)
+static void submit_counter (const char *type, const char *type_instance,
+ counter_t value)
{
- rrd_update_file (host, requests_file, val, requests_ds_def, requests_ds_num);
-}
+ value_t values[1];
+ value_list_t vl = VALUE_LIST_INIT;
-static void scoreboard_write (char *host, char *inst, char *val)
-{
- char buf[1024];
+ values[0].counter = value;
- if (snprintf (buf, 1024, scoreboard_file, inst) >= 1024)
- return;
+ vl.values = values;
+ vl.values_len = 1;
+ vl.time = time (NULL);
+ sstrncpy (vl.host, hostname_g, sizeof (vl.host));
+ sstrncpy (vl.plugin, "apache", sizeof (vl.plugin));
+ sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
+ sstrncpy (vl.type, type, sizeof (vl.type));
- rrd_update_file (host, buf, val, scoreboard_ds_def, scoreboard_ds_num);
-}
+ if (type_instance != NULL)
+ sstrncpy (vl.type_instance, type_instance,
+ sizeof (vl.type_instance));
+
+ plugin_dispatch_values (&vl);
+} /* void submit_counter */
-#if APACHE_HAVE_READ
-static void submit (char *type, char *inst, long long value)
+static void submit_gauge (const char *type, const char *type_instance,
+ gauge_t value)
{
- char buf[1024];
- int status;
+ value_t values[1];
+ value_list_t vl = VALUE_LIST_INIT;
- status = snprintf (buf, 1024, "%u:%lli", (unsigned int) curtime, value);
- if (status < 0)
- {
- syslog (LOG_ERR, "apache: bytes_submit: snprintf failed");
- return;
- }
- else if (status >= 1024)
- {
- syslog (LOG_WARNING, "apache: bytes_submit: snprintf was truncated");
- return;
- }
+ values[0].gauge = value;
- plugin_submit (type, inst, buf);
-}
+ vl.values = values;
+ vl.values_len = 1;
+ vl.time = time (NULL);
+ sstrncpy (vl.host, hostname_g, sizeof (vl.host));
+ sstrncpy (vl.plugin, "apache", sizeof (vl.plugin));
+ sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
+ sstrncpy (vl.type, type, sizeof (vl.type));
+
+ if (type_instance != NULL)
+ sstrncpy (vl.type_instance, type_instance,
+ sizeof (vl.type_instance));
+
+ plugin_dispatch_values (&vl);
+} /* void submit_counter */
static void submit_scoreboard (char *buf)
{
else if (buf[i] == 'I') idle_cleanup++;
}
- submit ("apache_scoreboard", "open" , open);
- submit ("apache_scoreboard", "waiting" , waiting);
- submit ("apache_scoreboard", "starting" , starting);
- submit ("apache_scoreboard", "reading" , reading);
- submit ("apache_scoreboard", "sending" , sending);
- submit ("apache_scoreboard", "keepalive", keepalive);
- submit ("apache_scoreboard", "dnslookup", dnslookup);
- submit ("apache_scoreboard", "closing" , closing);
- submit ("apache_scoreboard", "logging" , logging);
- submit ("apache_scoreboard", "finishing", finishing);
- submit ("apache_scoreboard", "idle_cleanup", idle_cleanup);
+ submit_gauge ("apache_scoreboard", "open" , open);
+ submit_gauge ("apache_scoreboard", "waiting" , waiting);
+ submit_gauge ("apache_scoreboard", "starting" , starting);
+ submit_gauge ("apache_scoreboard", "reading" , reading);
+ submit_gauge ("apache_scoreboard", "sending" , sending);
+ submit_gauge ("apache_scoreboard", "keepalive", keepalive);
+ submit_gauge ("apache_scoreboard", "dnslookup", dnslookup);
+ submit_gauge ("apache_scoreboard", "closing" , closing);
+ submit_gauge ("apache_scoreboard", "logging" , logging);
+ submit_gauge ("apache_scoreboard", "finishing", finishing);
+ submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup);
}
-static void apache_read (void)
+static int apache_read (void)
{
int i;
char *ptr;
+ char *saveptr;
char *lines[16];
int lines_num = 0;
int fields_num;
if (curl == NULL)
- return;
+ return (-1);
if (url == NULL)
- return;
+ return (-1);
- apache_buffer_len = 0;
+ apache_buffer_fill = 0;
if (curl_easy_perform (curl) != 0)
{
- syslog (LOG_WARNING, "apache: curl_easy_perform failed: %s", apache_curl_error);
- return;
+ ERROR ("apache: curl_easy_perform failed: %s",
+ apache_curl_error);
+ return (-1);
}
ptr = apache_buffer;
- while ((lines[lines_num] = strtok (ptr, "\n\r")) != NULL)
+ saveptr = NULL;
+ while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
{
ptr = NULL;
lines_num++;
{
if ((strcmp (fields[0], "Total") == 0)
&& (strcmp (fields[1], "Accesses:") == 0))
- submit ("apache_requests", NULL, atoll (fields[2]));
+ submit_counter ("apache_requests", "",
+ atoll (fields[2]));
else if ((strcmp (fields[0], "Total") == 0)
&& (strcmp (fields[1], "kBytes:") == 0))
- submit ("apache_bytes", NULL, 1024LL * atoll (fields[2]));
+ submit_counter ("apache_bytes", "",
+ 1024LL * atoll (fields[2]));
}
else if (fields_num == 2)
{
if (strcmp (fields[0], "Scoreboard:") == 0)
submit_scoreboard (fields[1]);
+ else if (strcmp (fields[0], "BusyServers:") == 0)
+ submit_gauge ("apache_connections", NULL, atol (fields[1]));
}
}
- apache_buffer_len = 0;
-}
-#else
-# define apache_read NULL
-#endif /* APACHE_HAVE_READ */
+ apache_buffer_fill = 0;
+
+ return (0);
+} /* int apache_read */
void module_register (void)
{
- plugin_register (MODULE_NAME, init, apache_read, NULL);
- plugin_register ("apache_requests", NULL, NULL, requests_write);
- plugin_register ("apache_bytes", NULL, NULL, bytes_write);
- plugin_register ("apache_scoreboard", NULL, NULL, scoreboard_write);
- cf_register (MODULE_NAME, config, config_keys, config_keys_num);
-}
-
-#undef MODULE_NAME
+ plugin_register_config ("apache", config,
+ config_keys, config_keys_num);
+ plugin_register_init ("apache", init);
+ plugin_register_read ("apache", apache_read);
+} /* void module_register */