Merge branch 'collectd-3.11'
[collectd.git] / src / apache.c
index e27350d..7c48f08 100644 (file)
@@ -1,6 +1,7 @@
 /**
  * collectd - src/apache.c
  * Copyright (C) 2006  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
@@ -18,6 +19,8 @@
  *
  * Authors:
  *   Florian octo Forster <octo at verplant.org>
+ *   Florent EppO Monbillard <eppo at darox.net>
+ *   - connections/lighttpd extension
  **/
 
 #include "collectd.h"
 #  define APACHE_HAVE_READ 0
 #endif
 
-static char *url  = NULL;
-static char *user = NULL;
-static char *pass = NULL;
+static char *url    = NULL;
+static char *user   = NULL;
+static char *pass   = NULL;
+static char *cacert = NULL;
 
-#if APACHE_HAVE_READ
+#if HAVE_LIBCURL
 static CURL *curl = NULL;
 
-static char apache_buffer[4096];
+#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
+#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:25:0:U",
+       "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:25:0:U",
+       "DS:count:COUNTER:"COLLECTD_HEARTBEAT":0:1048576",
        NULL
 };
 static int requests_ds_num = 1;
@@ -65,28 +73,38 @@ static int requests_ds_num = 1;
 static char *scoreboard_file = "apache/apache_scoreboard-%s.rrd";
 static char *scoreboard_ds_def[] =
 {
-       "DS:count:GAUGE:25:0:U",
+       "DS:count:GAUGE:"COLLECTD_HEARTBEAT":0:U",
        NULL
 };
 static int scoreboard_ds_num = 1;
 
+/* for lighttpd; Limit to 65536 active connections */
+static char *connections_file = "apache/apache_connections.rrd";
+static char *connections_ds_def[] =
+{
+       "DS:connections:GAUGE:"COLLECTD_HEARTBEAT":0:65536", 
+       NULL
+};
+static int connections_ds_num = 1;
+
 static char *config_keys[] =
 {
        "URL",
        "User",
        "Password",
+       "CACert",
        NULL
 };
-static int config_keys_num = 3;
-
+static int config_keys_num = 4;
 
+#if HAVE_LIBCURL
 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) >= 4096)
+       if ((apache_buffer_len + len) >= ABUFFER_SIZE)
        {
-               len = 4095 - apache_buffer_len;
+               len = (ABUFFER_SIZE - 1) - apache_buffer_len;
        }
 
        if (len <= 0)
@@ -98,6 +116,7 @@ static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb, void *
 
        return (len);
 }
+#endif /* HAVE_LIBCURL */
 
 static int config_set (char **var, char *value)
 {
@@ -121,13 +140,15 @@ static int config (char *key, char *value)
                return (config_set (&user, value));
        else if (strcasecmp (key, "password") == 0)
                return (config_set (&pass, value));
+       else if (strcasecmp (key, "cacert") == 0)
+               return (config_set (&cacert, value));
        else
                return (-1);
 }
 
 static void init (void)
 {
-#if APACHE_HAVE_READ
+#if HAVE_LIBCURL
        static char credentials[1024];
 
        if (curl != NULL)
@@ -160,7 +181,12 @@ static void init (void)
        {
                curl_easy_setopt (curl, CURLOPT_URL, url);
        }
-#endif /* APACHE_HAVE_READ */
+
+       if (cacert != NULL)
+       {
+               curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
+       }
+#endif /* HAVE_LIBCURL */
 }
 
 static void bytes_write (char *host, char *inst, char *val)
@@ -183,6 +209,12 @@ static void scoreboard_write (char *host, char *inst, char *val)
        rrd_update_file (host, buf, val, scoreboard_ds_def, scoreboard_ds_num);
 }
 
+static void connections_write (char *host, char *inst, char *val)
+{
+       rrd_update_file (host, connections_file, val, connections_ds_def,
+                       connections_ds_num);
+}
+
 #if APACHE_HAVE_READ
 static void submit (char *type, char *inst, long long value)
 {
@@ -271,6 +303,7 @@ static void apache_read (void)
        if (url == NULL)
                return;
 
+       apache_buffer_len = 0;
        if (curl_easy_perform (curl) != 0)
        {
                syslog (LOG_WARNING, "apache: curl_easy_perform failed: %s", apache_curl_error);
@@ -304,6 +337,8 @@ static void apache_read (void)
                {
                        if (strcmp (fields[0], "Scoreboard:") == 0)
                                submit_scoreboard (fields[1]);
+                       else if (strcmp (fields[0], "BusyServers:") == 0)
+                               submit ("apache_connections", NULL, atol (fields[1]));
                }
        }
 
@@ -319,6 +354,7 @@ void module_register (void)
        plugin_register ("apache_requests",   NULL, NULL, requests_write);
        plugin_register ("apache_bytes",      NULL, NULL, bytes_write);
        plugin_register ("apache_scoreboard", NULL, NULL, scoreboard_write);
+       plugin_register ("apache_connections", NULL, NULL, connections_write);
        cf_register (MODULE_NAME, config, config_keys, config_keys_num);
 }