Replace all occurrences of `strcpy' with `sstrncpy'.
[collectd.git] / src / apache.c
index 7c48f08..54a5d56 100644 (file)
@@ -1,12 +1,11 @@
 /**
  * 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
 #include "plugin.h"
 #include "configfile.h"
 
-#define MODULE_NAME "apache"
-
-#if HAVE_LIBCURL && HAVE_CURL_CURL_H
-#  define APACHE_HAVE_READ 1
-#  include <curl/curl.h>
-#else
-#  define APACHE_HAVE_READ 0
-#endif
+#include <curl/curl.h>
 
 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 *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  *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];
 
-/* 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[] =
+static const char *config_keys[] =
 {
        "URL",
        "User",
        "Password",
-       "CACert",
-       NULL
+       "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)
        {
@@ -132,7 +94,7 @@ static int config_set (char **var, char *value)
                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));
@@ -146,11 +108,17 @@ static int config (char *key, char *value)
                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);
@@ -158,8 +126,8 @@ static void init (void)
 
        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);
@@ -168,73 +136,81 @@ static void init (void)
 
        if (user != NULL)
        {
-               if (snprintf (credentials, 1024, "%s:%s", user, pass == NULL ? "" : pass) >= 1024)
+               int status;
+
+               status = snprintf (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);
                }
+               credentials[sizeof (credentials) - 1] = '\0';
 
                curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
        }
 
-       if (url != NULL)
-       {
-               curl_easy_setopt (curl, CURLOPT_URL, url);
-       }
+       curl_easy_setopt (curl, CURLOPT_URL, url);
 
        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));
 
-       rrd_update_file (host, buf, val, scoreboard_ds_def, scoreboard_ds_num);
-}
+       if (type_instance != NULL)
+       {
+               strncpy (vl.type_instance, type_instance,
+                               sizeof (vl.type_instance));
+               vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
+       }
 
-static void connections_write (char *host, char *inst, char *val)
-{
-       rrd_update_file (host, connections_file, val, connections_ds_def,
-                       connections_ds_num);
-}
+       plugin_dispatch_values (type, &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)
+       values[0].gauge = value;
+
+       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));
+
+       if (type_instance != NULL)
        {
-               syslog (LOG_WARNING, "apache: bytes_submit: snprintf was truncated");
-               return;
+               strncpy (vl.type_instance, type_instance,
+                               sizeof (vl.type_instance));
+               vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
        }
 
-       plugin_submit (type, inst, buf);
-}
+       plugin_dispatch_values (type, &vl);
+} /* void submit_counter */
 
 static void submit_scoreboard (char *buf)
 {
@@ -274,24 +250,25 @@ 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;
 
@@ -299,19 +276,21 @@ static void apache_read (void)
        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++;
@@ -328,34 +307,31 @@ static void apache_read (void)
                {
                        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 ("apache_connections", NULL, atol (fields[1]));
+                               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);
-       plugin_register ("apache_connections", NULL, NULL, connections_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 */