apache plugin: Remove the `connect' variable: It wasn't really being used.
[collectd.git] / src / apache.c
index b808a9e..090ece7 100644 (file)
 
 #include <curl/curl.h>
 
-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 CURL *curl = NULL;
-
-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];
+enum server_type
+{
+       APACHE = 0,
+       LIGHTTPD
+};
 
 struct apache_s
 {
@@ -55,6 +47,10 @@ struct apache_s
        char *verify_peer;
        char *verify_host;
        char *cacert;
+       char *apache_buffer;
+       char apache_curl_error[CURL_ERROR_SIZE];
+       size_t apache_buffer_size;
+       size_t apache_buffer_fill;
        CURL *curl;
 }; /* apache_s */
 
@@ -76,6 +72,7 @@ static void apache_free (apache_t *st)
        sfree (st->verify_peer);
        sfree (st->verify_host);
        sfree (st->cacert);
+       sfree (st->apache_buffer);
        if (st->curl) {
                curl_easy_cleanup(st->curl);
                st->curl = NULL;
@@ -83,31 +80,31 @@ static void apache_free (apache_t *st)
 } /* apache_free */
 
 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb,
-               void __attribute__((unused)) *stream)
+               apache_t *st)
 {
        size_t len = size * nmemb;
 
        if (len <= 0)
                return (len);
 
-       if ((apache_buffer_fill + len) >= apache_buffer_size)
+       if ((st->apache_buffer_fill + len) >= st->apache_buffer_size)
        {
                char *temp;
 
-               temp = (char *) realloc (apache_buffer,
-                               apache_buffer_fill + len + 1);
+               temp = (char *) realloc (st->apache_buffer,
+                               st->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;
+               st->apache_buffer = temp;
+               st->apache_buffer_size = st->apache_buffer_fill + len + 1;
        }
 
-       memcpy (apache_buffer + apache_buffer_fill, (char *) buf, len);
-       apache_buffer_fill += len;
-       apache_buffer[apache_buffer_fill] = 0;
+       memcpy (st->apache_buffer + st->apache_buffer_fill, (char *) buf, len);
+       st->apache_buffer_fill += len;
+       st->apache_buffer[st->apache_buffer_fill] = 0;
 
        return (len);
 } /* int apache_curl_callback */
@@ -173,8 +170,8 @@ static int config_add (oconfig_item_t *ci)
        status = config_set_string (&st->name, ci);
        if (status != 0)
        {
-       sfree (st);
-       return (status);
+               sfree (st);
+               return (status);
        }
 
        for (i = 0; i < ci->children_num; i++)
@@ -256,32 +253,23 @@ static int config (oconfig_item_t *ci)
                                }
                                memset (lci, '\0', sizeof (*lci));
                        }
-                       if (strcasecmp ("Instance", child->key) == 0)
-                       {
-                               lci->key = child->key;
-                               lci->values = child->values;
-                               lci->values_num = child->values_num;
-                               lci->parent = child->parent;
-                       }
-                       else
+
+                       lci->children_num++;
+                       lci->children =
+                               realloc (lci->children,
+                                        lci->children_num * sizeof (*child));
+                       if (lci->children == NULL)
                        {
-                               lci->children_num++;
-                               lci->children =
-                                       realloc (lci->children,
-                                                lci->children_num * sizeof (*child));
-                               if (lci->children == NULL)
-                               {
-                                       ERROR ("apache plugin: realloc failed.");
-                                       return (-1);
-                               }
-                               memcpy (&lci->children[lci->children_num-1], child, sizeof (*child));
+                               ERROR ("apache plugin: realloc failed.");
+                               return (-1);
                        }
+                       memcpy (&lci->children[lci->children_num-1], child, sizeof (*child));
                }
        } /* for (ci->children) */
 
        if (lci)
        {
-               // create a <Instance ""> entry
+               /* create a <Instance ""> entry */
                lci->key = "Instance";
                lci->values_num = 1;
                lci->values = (oconfig_value_t *) malloc (lci->values_num * sizeof (oconfig_value_t));
@@ -289,6 +277,7 @@ static int config (oconfig_item_t *ci)
                lci->values[0].value.string = "";
 
                status = config_add (lci);
+               sfree (lci->values);
                sfree (lci->children);
                sfree (lci);
        }
@@ -297,7 +286,7 @@ static int config (oconfig_item_t *ci)
 } /* int config */
 
 
-// initialize curl for each host
+/* initialize curl for each host */
 static int init_host (apache_t *st) /* {{{ */
 {
        static char credentials[1024];
@@ -321,8 +310,9 @@ static int init_host (apache_t *st) /* {{{ */
        }
 
        curl_easy_setopt (st->curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
+       curl_easy_setopt (st->curl, CURLOPT_WRITEDATA, st);
        curl_easy_setopt (st->curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
-       curl_easy_setopt (st->curl, CURLOPT_ERRORBUFFER, apache_curl_error);
+       curl_easy_setopt (st->curl, CURLOPT_ERRORBUFFER, st->apache_curl_error);
 
        if (st->user != NULL)
        {
@@ -392,21 +382,18 @@ static int init (void)
        return (0);
 } /* int init */
 
-static void set_plugin (apache_t *st, value_list_t *vl)
+static void set_plugin_instance (apache_t *st, value_list_t *vl)
 {
-       // if there is no instance name, assume apache
-       if ( (0 == strcmp(st->name, "")) )
+       /* if there is no instance name, don't set plugin_instance */
+       if ( (st->name != NULL)
+               && (apache_num > 0) )
        {
-               sstrncpy (vl->plugin, "apache", sizeof (vl->plugin));
-       }
-       else
-       {
-               sstrncpy (vl->plugin, st->name, sizeof (vl->plugin));
+               sstrncpy (vl->plugin_instance, st->name, sizeof (vl->plugin_instance));
        }
 } /* void set_plugin */
 
 static void submit_counter (const char *type, const char *type_instance,
-               counter_t value, char *host, apache_t *st)
+               counter_t value, apache_t *st)
 {
        value_t values[1];
        value_list_t vl = VALUE_LIST_INIT;
@@ -415,7 +402,8 @@ static void submit_counter (const char *type, const char *type_instance,
 
        vl.values = values;
        vl.values_len = 1;
-       sstrncpy (vl.host, host, sizeof (vl.host));
+       sstrncpy (vl.host, st->host, 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));
 
@@ -423,13 +411,13 @@ static void submit_counter (const char *type, const char *type_instance,
                sstrncpy (vl.type_instance, type_instance,
                                sizeof (vl.type_instance));
 
-       set_plugin (st, &vl);
+       set_plugin_instance (st, &vl);
 
        plugin_dispatch_values (&vl);
 } /* void submit_counter */
 
 static void submit_gauge (const char *type, const char *type_instance,
-               gauge_t value, char *host, apache_t *st)
+               gauge_t value, apache_t *st)
 {
        value_t values[1];
        value_list_t vl = VALUE_LIST_INIT;
@@ -438,7 +426,8 @@ static void submit_gauge (const char *type, const char *type_instance,
 
        vl.values = values;
        vl.values_len = 1;
-       sstrncpy (vl.host, host, sizeof (vl.host));
+       sstrncpy (vl.host, st->host, 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));
 
@@ -446,19 +435,24 @@ static void submit_gauge (const char *type, const char *type_instance,
                sstrncpy (vl.type_instance, type_instance,
                                sizeof (vl.type_instance));
 
-       set_plugin (st, &vl);
+       set_plugin_instance (st, &vl);
 
        plugin_dispatch_values (&vl);
 } /* void submit_counter */
 
-static void submit_scoreboard (char *buf, char *host, apache_t *st)
+static void submit_scoreboard (char *buf, int server, apache_t *st)
 {
        /*
         * Scoreboard Key:
-        * "_" Waiting for Connection, "S" Starting up, "R" Reading Request,
+        * "_" Waiting for Connection, "S" Starting up,
+        * "R" Reading Request for apache and read-POST for lighttpd,
         * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
         * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
         * "I" Idle cleanup of worker, "." Open slot with no current process
+        * Lighttpd specific legends -
+        * "E" hard error, "." connect, "h" handle-request,
+        * "q" request-start, "Q" request-end, "s" response-start
+        * "S" response-end, "r" read
         */
        long long open      = 0LL;
        long long waiting   = 0LL;
@@ -472,6 +466,15 @@ static void submit_scoreboard (char *buf, char *host, apache_t *st)
        long long finishing = 0LL;
        long long idle_cleanup = 0LL;
 
+       /* lighttpd specific */
+       long long hard_error     = 0LL;
+       long long lighttpd_read  = 0LL;
+       long long handle_request = 0LL;
+       long long request_start  = 0LL;
+       long long request_end    = 0LL;
+       long long response_start = 0LL;
+       long long response_end   = 0LL;
+
        int i;
 
        for (i = 0; buf[i] != '\0'; i++)
@@ -487,19 +490,43 @@ static void submit_scoreboard (char *buf, char *host, apache_t *st)
                else if (buf[i] == 'L') logging++;
                else if (buf[i] == 'G') finishing++;
                else if (buf[i] == 'I') idle_cleanup++;
+               else if (buf[i] == 'r') lighttpd_read++;
+               else if (buf[i] == 'h') handle_request++;
+               else if (buf[i] == 'E') hard_error++;
+               else if (buf[i] == 'q') request_start++;
+               else if (buf[i] == 'Q') request_end++;
+               else if (buf[i] == 's') response_start++;
+               else if (buf[i] == 'S') response_end++;
        }
 
-       submit_gauge ("apache_scoreboard", "open"     , open, host, st);
-       submit_gauge ("apache_scoreboard", "waiting"  , waiting, host, st);
-       submit_gauge ("apache_scoreboard", "starting" , starting, host, st);
-       submit_gauge ("apache_scoreboard", "reading"  , reading, host, st);
-       submit_gauge ("apache_scoreboard", "sending"  , sending, host, st);
-       submit_gauge ("apache_scoreboard", "keepalive", keepalive, host, st);
-       submit_gauge ("apache_scoreboard", "dnslookup", dnslookup, host, st);
-       submit_gauge ("apache_scoreboard", "closing"  , closing, host, st);
-       submit_gauge ("apache_scoreboard", "logging"  , logging, host, st);
-       submit_gauge ("apache_scoreboard", "finishing", finishing, host, st);
-       submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup, host, st);
+       if (server == APACHE)
+       {
+               submit_gauge ("apache_scoreboard", "open"     , open, st);
+               submit_gauge ("apache_scoreboard", "waiting"  , waiting, st);
+               submit_gauge ("apache_scoreboard", "starting" , starting, st);
+               submit_gauge ("apache_scoreboard", "reading"  , reading, st);
+               submit_gauge ("apache_scoreboard", "sending"  , sending, st);
+               submit_gauge ("apache_scoreboard", "keepalive", keepalive, st);
+               submit_gauge ("apache_scoreboard", "dnslookup", dnslookup, st);
+               submit_gauge ("apache_scoreboard", "closing"  , closing, st);
+               submit_gauge ("apache_scoreboard", "logging"  , logging, st);
+               submit_gauge ("apache_scoreboard", "finishing", finishing, st);
+               submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup, st);
+       } else
+       {
+               submit_gauge ("apache_scoreboard", "connect"       , open, st);
+               submit_gauge ("apache_scoreboard", "close"         , closing, st);
+               submit_gauge ("apache_scoreboard", "hard_error"    , hard_error, st);
+               submit_gauge ("apache_scoreboard", "read"          , lighttpd_read, st);
+               submit_gauge ("apache_scoreboard", "read_post"     , reading, st);
+               submit_gauge ("apache_scoreboard", "write"         , sending, st);
+               submit_gauge ("apache_scoreboard", "handle_request", handle_request, st);
+               submit_gauge ("apache_scoreboard", "request_start" , request_start, st);
+               submit_gauge ("apache_scoreboard", "request_end"   , request_end, st);
+               submit_gauge ("apache_scoreboard", "response_start", response_start, st);
+               submit_gauge ("apache_scoreboard", "response_end"  , response_end, st);
+
+       }
 }
 
 static int apache_read_host (apache_t *st)
@@ -512,23 +539,23 @@ static int apache_read_host (apache_t *st)
        int   lines_num = 0;
 
        char *fields[4];
-       char *host;
        int   fields_num;
+       int server = LIGHTTPD; /* default is lighttpd */
 
        if (st->curl == NULL)
                return (-1);
        if (st->url == NULL)
                return (-1);
 
-       apache_buffer_fill = 0;
+       st->apache_buffer_fill = 0;
        if (curl_easy_perform (st->curl) != 0)
        {
                ERROR ("apache: curl_easy_perform failed: %s",
-                               apache_curl_error);
+                               st->apache_curl_error);
                return (-1);
        }
 
-       ptr = apache_buffer;
+       ptr = st->apache_buffer;
        saveptr = NULL;
        while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
        {
@@ -539,7 +566,7 @@ static int apache_read_host (apache_t *st)
                        break;
        }
 
-       // set the host to localhost if st->host is not specified
+       /* set the host to localhost if st->host is not specified */
        if ( (st->host == NULL)
                || (0 == strcmp(st->host, "")) ) {
                st->host = hostname_g;
@@ -554,22 +581,31 @@ static int apache_read_host (apache_t *st)
                        if ((strcmp (fields[0], "Total") == 0)
                                        && (strcmp (fields[1], "Accesses:") == 0))
                                submit_counter ("apache_requests", "",
-                                               atoll (fields[2]), st->host, st);
+                                               atoll (fields[2]), st);
                        else if ((strcmp (fields[0], "Total") == 0)
                                        && (strcmp (fields[1], "kBytes:") == 0))
                                submit_counter ("apache_bytes", "",
-                                               1024LL * atoll (fields[2]), st->host, st);
+                                               1024LL * atoll (fields[2]), st);
                }
                else if (fields_num == 2)
                {
-                       if (strcmp (fields[0], "Scoreboard:") == 0)
-                               submit_scoreboard (fields[1], st->host, st);
+                       /* find out if the server is apache from the mod_status
+                        * output. apache mod_status output has additional
+                        * fields which lighttpd mod_status output doesn't have
+                        * e.g: ReqPerSec. submit_scoreboard needs server type
+                        * information and thus it is important to pick up a
+                        * field before scoreboard gets parsed to set the
+                        * server type */
+                       if (strcmp (fields[0], "ReqPerSec:") == 0)
+                               server = APACHE;
+                       else if (strcmp (fields[0], "Scoreboard:") == 0)
+                               submit_scoreboard (fields[1], server, st);
                        else if (strcmp (fields[0], "BusyServers:") == 0)
-                               submit_gauge ("apache_connections", NULL, atol (fields[1]), st->host, st);
+                               submit_gauge ("apache_connections", NULL, atol (fields[1]), st);
                }
        }
 
-       apache_buffer_fill = 0;
+       st->apache_buffer_fill = 0;
 
        return (0);
 } /* int apache_read_host */
@@ -595,7 +631,7 @@ static int apache_read (void)
        }
 
        return (0);
- } /* int apache_read */
+} /* int apache_read */
 
 void module_register (void)
 {