X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fapache.c;h=573a116a215bdeabc0e52feb8e1ca44c11970687;hb=7e5df1a2c6611bd4ac9fb8ac4b78106f9139ae6e;hp=e27350d382b922c5a4b929fe068dff062e7d673f;hpb=7466a24b81b58d134dfff1170fc1b0c5c503dd51;p=collectd.git diff --git a/src/apache.c b/src/apache.c index e27350d3..573a116a 100644 --- a/src/apache.c +++ b/src/apache.c @@ -1,11 +1,11 @@ /** * 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 - * 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 @@ -18,6 +18,8 @@ * * Authors: * Florian octo Forster + * Florent EppO Monbillard + * - connections/lighttpd extension **/ #include "collectd.h" @@ -25,8 +27,6 @@ #include "plugin.h" #include "configfile.h" -#define MODULE_NAME "apache" - #if HAVE_LIBCURL && HAVE_CURL_CURL_H # define APACHE_HAVE_READ 1 # include @@ -34,59 +34,36 @@ # define APACHE_HAVE_READ 0 #endif -static char *url = NULL; -static char *user = NULL; -static char *pass = NULL; - #if APACHE_HAVE_READ +static char *url = NULL; +static char *user = NULL; +static char *pass = NULL; +static char *cacert = NULL; + 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 -static char *bytes_file = "apache/apache_bytes.rrd"; -static char *bytes_ds_def[] = -{ - "DS:count:COUNTER:25:0:U", - NULL -}; -static int bytes_ds_num = 1; - -static char *requests_file = "apache/apache_requests.rrd"; -static char *requests_ds_def[] = -{ - "DS:count:COUNTER:25:0:U", - NULL -}; -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", - NULL -}; -static int scoreboard_ds_num = 1; - -static char *config_keys[] = +static const char *config_keys[] = { "URL", "User", "Password", + "CACert", NULL }; -static int config_keys_num = 3; - +static int config_keys_num = 4; 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) @@ -99,7 +76,7 @@ static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb, void * return (len); } -static int config_set (char **var, char *value) +static int config_set (char **var, const char *value) { if (*var != NULL) { @@ -113,7 +90,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)); @@ -121,13 +98,14 @@ 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) +static int init (void) { -#if APACHE_HAVE_READ static char credentials[1024]; if (curl != NULL) @@ -137,8 +115,8 @@ static void init (void) if ((curl = curl_easy_init ()) == NULL) { - syslog (LOG_ERR, "apache: `curl_easy_init' failed."); - return; + ERROR ("apache: `curl_easy_init' failed."); + return (-1); } curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, apache_curl_callback); @@ -149,8 +127,8 @@ static void init (void) { if (snprintf (credentials, 1024, "%s:%s", user, pass == NULL ? "" : pass) >= 1024) { - syslog (LOG_ERR, "apache: Credentials would have been truncated."); - return; + ERROR ("apache: Credentials would have been truncated."); + return (-1); } curl_easy_setopt (curl, CURLOPT_USERPWD, credentials); @@ -160,49 +138,61 @@ static void init (void) { curl_easy_setopt (curl, CURLOPT_URL, url); } -#endif /* APACHE_HAVE_READ */ -} -static void bytes_write (char *host, char *inst, char *val) -{ - rrd_update_file (host, bytes_file, val, bytes_ds_def, bytes_ds_num); -} + if (cacert != NULL) + { + curl_easy_setopt (curl, CURLOPT_CAINFO, cacert); + } -static void requests_write (char *host, char *inst, char *val) -{ - rrd_update_file (host, requests_file, val, requests_ds_def, requests_ds_num); -} + return (0); +} /* int init */ -static void scoreboard_write (char *host, char *inst, char *val) +static void submit_counter (const char *type, const char *type_instance, + unsigned long long value) { - char buf[1024]; + value_t values[1]; + value_list_t vl = VALUE_LIST_INIT; - if (snprintf (buf, 1024, scoreboard_file, inst) >= 1024) - return; + DEBUG ("type = %s; type_instance = %s; value = %llu;", + type, type_instance, value); - rrd_update_file (host, buf, val, scoreboard_ds_def, scoreboard_ds_num); -} + values[0].counter = value; -#if APACHE_HAVE_READ -static void submit (char *type, char *inst, long long value) + vl.values = values; + vl.values_len = 1; + vl.time = time (NULL); + strcpy (vl.host, hostname_g); + strcpy (vl.plugin, "apache"); + strcpy (vl.plugin_instance, ""); + strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance)); + + plugin_dispatch_values (type, &vl); +} /* void submit_counter */ + +static void submit_gauge (const char *type, const char *type_instance, + double 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; - } + DEBUG ("type = %s; type_instance = %s; value = %lf;", + type, type_instance, value); - plugin_submit (type, inst, buf); -} + values[0].gauge = value; + + vl.values = values; + vl.values_len = 1; + vl.time = time (NULL); + strcpy (vl.host, hostname_g); + strcpy (vl.plugin, "apache"); + strcpy (vl.plugin_instance, ""); + + if (type_instance != NULL) + strncpy (vl.type_instance, type_instance, + sizeof (vl.type_instance)); + + plugin_dispatch_values (type, &vl); +} /* void submit_counter */ static void submit_scoreboard (char *buf) { @@ -242,24 +232,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; @@ -267,18 +258,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; 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++; @@ -295,31 +289,34 @@ 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_gauge ("apache_connections", NULL, atol (fields[1])); } } apache_buffer_len = 0; -} -#else -# define apache_read NULL + + return (0); +} /* int apache_read */ #endif /* APACHE_HAVE_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 +#if APACHE_HAVE_READ + plugin_register_config ("apache", config, + config_keys, config_keys_num); + plugin_register_init ("apache", init); + plugin_register_read ("apache", apache_read); +#endif +} /* void module_register */