2 * collectd - src/apache.c
3 * Copyright (C) 2006-2008 Florian octo Forster
4 * Copyright (C) 2007 Florent EppO Monbillard
5 * Copyright (C) 2009 Amit Gupta
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; only version 2 of the License is applicable.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 * Florian octo Forster <octo at verplant.org>
22 * Florent EppO Monbillard <eppo at darox.net>
23 * - connections/lighttpd extension
24 * Amit Gupta <amit.gupta221 at gmail.com>
30 #include "configfile.h"
32 #include <curl/curl.h>
45 char apache_curl_error[CURL_ERROR_SIZE];
46 size_t apache_buffer_size;
47 size_t apache_buffer_fill;
51 typedef struct apache_s apache_t;
53 static apache_t **apache = NULL;
54 static size_t apache_num = 0;
56 static void apache_free (apache_t *st)
66 sfree (st->verify_peer);
67 sfree (st->verify_host);
69 sfree (st->apache_buffer);
71 curl_easy_cleanup(st->curl);
76 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb,
79 size_t len = size * nmemb;
85 ERROR ("apache plugin: apache_curl_callback: "
86 "user_data pointer is NULL.");
93 if ((st->apache_buffer_fill + len) >= st->apache_buffer_size)
97 temp = (char *) realloc (st->apache_buffer,
98 st->apache_buffer_fill + len + 1);
101 ERROR ("apache plugin: realloc failed.");
104 st->apache_buffer = temp;
105 st->apache_buffer_size = st->apache_buffer_fill + len + 1;
108 memcpy (st->apache_buffer + st->apache_buffer_fill, (char *) buf, len);
109 st->apache_buffer_fill += len;
110 st->apache_buffer[st->apache_buffer_fill] = 0;
113 } /* int apache_curl_callback */
115 /* Configuration handling functiions
117 * <Instance "instance_name">
123 static int config_set_string (char **ret_string,
128 if ((ci->values_num != 1)
129 || (ci->values[0].type != OCONFIG_TYPE_STRING))
131 WARNING ("apache plugin: The `%s' config option "
132 "needs exactly one string argument.", ci->key);
136 string = strdup (ci->values[0].value.string);
139 ERROR ("apache plugin: strdup failed.");
143 if (*ret_string != NULL)
145 *ret_string = string;
148 } /* int config_set_string */
150 static int config_add (oconfig_item_t *ci)
156 if ((ci->values_num != 1)
157 || (ci->values[0].type != OCONFIG_TYPE_STRING))
159 WARNING ("apache plugin: The `%s' config option "
160 "needs exactly one string argument.", ci->key);
164 st = (apache_t *) malloc (sizeof (*st));
167 ERROR ("apache plugin: malloc failed.");
171 memset (st, 0, sizeof (*st));
173 status = config_set_string (&st->name, ci);
180 for (i = 0; i < ci->children_num; i++)
182 oconfig_item_t *child = ci->children + i;
184 if (strcasecmp ("URL", child->key) == 0)
185 status = config_set_string (&st->url, child);
186 else if (strcasecmp ("Host", child->key) == 0)
187 status = config_set_string (&st->host, child);
188 else if (strcasecmp ("User", child->key) == 0)
189 status = config_set_string (&st->user, child);
190 else if (strcasecmp ("Password", child->key) == 0)
191 status = config_set_string (&st->pass, child);
192 else if (strcasecmp ("VerifyPeer", child->key) == 0)
193 status = config_set_string (&st->verify_peer, child);
194 else if (strcasecmp ("VerifyHost", child->key) == 0)
195 status = config_set_string (&st->verify_host, child);
196 else if (strcasecmp ("CACert", child->key) == 0)
197 status = config_set_string (&st->cacert, child);
200 WARNING ("apache plugin: Option `%s' not allowed here.", child->key);
211 temp = (apache_t **) realloc (apache, sizeof (*apache) * (apache_num + 1));
214 ERROR ("apache plugin: realloc failed");
220 apache[apache_num] = st;
232 } /* int config_add */
234 static int config (oconfig_item_t *ci)
238 oconfig_item_t *lci = NULL; /* legacy config */
240 for (i = 0; i < ci->children_num; i++)
242 oconfig_item_t *child = ci->children + i;
244 if (strcasecmp ("Instance", child->key) == 0 && child->children_num > 0)
248 /* legacy mode - convert to <Instance ...> config */
251 lci = malloc (sizeof(*lci));
254 ERROR ("apache plugin: malloc failed.");
257 memset (lci, '\0', sizeof (*lci));
262 realloc (lci->children,
263 lci->children_num * sizeof (*child));
264 if (lci->children == NULL)
266 ERROR ("apache plugin: realloc failed.");
269 memcpy (&lci->children[lci->children_num-1], child, sizeof (*child));
271 } /* for (ci->children) */
275 /* create a <Instance ""> entry */
276 lci->key = "Instance";
278 lci->values = (oconfig_value_t *) malloc (lci->values_num * sizeof (oconfig_value_t));
279 lci->values[0].type = OCONFIG_TYPE_STRING;
280 lci->values[0].value.string = "";
282 status = config_add (lci);
284 sfree (lci->children);
292 /* initialize curl for each host */
293 static int init_host (apache_t *st) /* {{{ */
295 static char credentials[1024];
299 WARNING ("apache plugin: init: No URL configured, returning "
304 if (st->curl != NULL)
306 curl_easy_cleanup (st->curl);
309 if ((st->curl = curl_easy_init ()) == NULL)
311 ERROR ("apache plugin: init: `curl_easy_init' failed.");
315 curl_easy_setopt (st->curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
316 curl_easy_setopt (st->curl, CURLOPT_WRITEDATA, st);
317 curl_easy_setopt (st->curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
318 curl_easy_setopt (st->curl, CURLOPT_ERRORBUFFER, st->apache_curl_error);
320 if (st->user != NULL)
324 status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
325 st->user, (st->pass == NULL) ? "" : st->pass);
326 if ((status < 0) || ((size_t) status >= sizeof (credentials)))
328 ERROR ("apache plugin: init: Returning an error "
329 "because the credentials have been "
334 curl_easy_setopt (st->curl, CURLOPT_USERPWD, credentials);
337 curl_easy_setopt (st->curl, CURLOPT_URL, st->url);
339 if ((st->verify_peer == NULL) || (strcmp (st->verify_peer, "true") == 0))
341 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER, 1);
345 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER, 0);
348 if ((st->verify_host == NULL) || (strcmp (st->verify_host, "true") == 0))
350 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST, 2);
354 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST, 0);
357 if (st->cacert != NULL)
359 curl_easy_setopt (st->curl, CURLOPT_CAINFO, st->cacert);
363 } /* int init_host */
365 static int init (void)
371 for (i = 0; i < apache_num; i++)
373 status = init_host (apache[i]);
380 ERROR ("apache plugin init: No host could be initialized. Will return an error so "
381 "the plugin will be delayed.");
388 static void set_plugin_instance (apache_t *st, value_list_t *vl)
390 /* if there is no instance name, don't set plugin_instance */
391 if ( (st->name != NULL)
392 && (apache_num > 0) )
394 sstrncpy (vl->plugin_instance, st->name, sizeof (vl->plugin_instance));
396 } /* void set_plugin */
398 static void submit_counter (const char *type, const char *type_instance,
399 counter_t value, apache_t *st)
402 value_list_t vl = VALUE_LIST_INIT;
404 values[0].counter = value;
408 sstrncpy (vl.host, st->host, sizeof (vl.host));
409 sstrncpy (vl.plugin, "apache", sizeof (vl.plugin));
410 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
411 sstrncpy (vl.type, type, sizeof (vl.type));
413 if (type_instance != NULL)
414 sstrncpy (vl.type_instance, type_instance,
415 sizeof (vl.type_instance));
417 set_plugin_instance (st, &vl);
419 plugin_dispatch_values (&vl);
420 } /* void submit_counter */
422 static void submit_gauge (const char *type, const char *type_instance,
423 gauge_t value, apache_t *st)
426 value_list_t vl = VALUE_LIST_INIT;
428 values[0].gauge = value;
432 sstrncpy (vl.host, st->host, sizeof (vl.host));
433 sstrncpy (vl.plugin, "apache", sizeof (vl.plugin));
434 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
435 sstrncpy (vl.type, type, sizeof (vl.type));
437 if (type_instance != NULL)
438 sstrncpy (vl.type_instance, type_instance,
439 sizeof (vl.type_instance));
441 set_plugin_instance (st, &vl);
443 plugin_dispatch_values (&vl);
444 } /* void submit_counter */
446 static void submit_scoreboard (char *buf, apache_t *st)
450 * "_" Waiting for Connection, "S" Starting up, "R" Reading Request,
451 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
452 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
453 * "I" Idle cleanup of worker, "." Open slot with no current process
455 long long open = 0LL;
456 long long waiting = 0LL;
457 long long starting = 0LL;
458 long long reading = 0LL;
459 long long sending = 0LL;
460 long long keepalive = 0LL;
461 long long dnslookup = 0LL;
462 long long closing = 0LL;
463 long long logging = 0LL;
464 long long finishing = 0LL;
465 long long idle_cleanup = 0LL;
469 for (i = 0; buf[i] != '\0'; i++)
471 if (buf[i] == '.') open++;
472 else if (buf[i] == '_') waiting++;
473 else if (buf[i] == 'S') starting++;
474 else if (buf[i] == 'R') reading++;
475 else if (buf[i] == 'W') sending++;
476 else if (buf[i] == 'K') keepalive++;
477 else if (buf[i] == 'D') dnslookup++;
478 else if (buf[i] == 'C') closing++;
479 else if (buf[i] == 'L') logging++;
480 else if (buf[i] == 'G') finishing++;
481 else if (buf[i] == 'I') idle_cleanup++;
484 submit_gauge ("apache_scoreboard", "open" , open, st);
485 submit_gauge ("apache_scoreboard", "waiting" , waiting, st);
486 submit_gauge ("apache_scoreboard", "starting" , starting, st);
487 submit_gauge ("apache_scoreboard", "reading" , reading, st);
488 submit_gauge ("apache_scoreboard", "sending" , sending, st);
489 submit_gauge ("apache_scoreboard", "keepalive", keepalive, st);
490 submit_gauge ("apache_scoreboard", "dnslookup", dnslookup, st);
491 submit_gauge ("apache_scoreboard", "closing" , closing, st);
492 submit_gauge ("apache_scoreboard", "logging" , logging, st);
493 submit_gauge ("apache_scoreboard", "finishing", finishing, st);
494 submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup, st);
497 static int apache_read_host (apache_t *st)
509 if (st->curl == NULL)
514 st->apache_buffer_fill = 0;
515 if (curl_easy_perform (st->curl) != 0)
517 ERROR ("apache: curl_easy_perform failed: %s",
518 st->apache_curl_error);
522 ptr = st->apache_buffer;
524 while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
533 /* set the host to localhost if st->host is not specified */
534 if ( (st->host == NULL)
535 || (0 == strcmp(st->host, "")) ) {
536 st->host = hostname_g;
539 for (i = 0; i < lines_num; i++)
541 fields_num = strsplit (lines[i], fields, 4);
545 if ((strcmp (fields[0], "Total") == 0)
546 && (strcmp (fields[1], "Accesses:") == 0))
547 submit_counter ("apache_requests", "",
548 atoll (fields[2]), st);
549 else if ((strcmp (fields[0], "Total") == 0)
550 && (strcmp (fields[1], "kBytes:") == 0))
551 submit_counter ("apache_bytes", "",
552 1024LL * atoll (fields[2]), st);
554 else if (fields_num == 2)
556 if (strcmp (fields[0], "Scoreboard:") == 0)
557 submit_scoreboard (fields[1], st);
558 else if (strcmp (fields[0], "BusyServers:") == 0)
559 submit_gauge ("apache_connections", NULL, atol (fields[1]), st);
563 st->apache_buffer_fill = 0;
566 } /* int apache_read_host */
568 static int apache_read (void)
574 for (i = 0; i < apache_num; i++)
576 status = apache_read_host (apache[i]);
583 ERROR ("apache plugin: No host could be read. Will return an error so "
584 "the plugin will be delayed.");
589 } /* int apache_read */
591 void module_register (void)
593 plugin_register_complex_config ("apache", config);
594 plugin_register_init ("apache", init);
595 plugin_register_read ("apache", apache_read);
596 } /* void module_register */