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;
84 if ((st->apache_buffer_fill + len) >= st->apache_buffer_size)
88 temp = (char *) realloc (st->apache_buffer,
89 st->apache_buffer_fill + len + 1);
92 ERROR ("apache plugin: realloc failed.");
95 st->apache_buffer = temp;
96 st->apache_buffer_size = st->apache_buffer_fill + len + 1;
99 memcpy (st->apache_buffer + st->apache_buffer_fill, (char *) buf, len);
100 st->apache_buffer_fill += len;
101 st->apache_buffer[st->apache_buffer_fill] = 0;
104 } /* int apache_curl_callback */
106 /* Configuration handling functiions
108 * <Instance "instance_name">
114 static int config_set_string (char **ret_string,
119 if ((ci->values_num != 1)
120 || (ci->values[0].type != OCONFIG_TYPE_STRING))
122 WARNING ("apache plugin: The `%s' config option "
123 "needs exactly one string argument.", ci->key);
127 string = strdup (ci->values[0].value.string);
130 ERROR ("apache plugin: strdup failed.");
134 if (*ret_string != NULL)
136 *ret_string = string;
139 } /* int config_set_string */
141 static int config_add (oconfig_item_t *ci)
147 if ((ci->values_num != 1)
148 || (ci->values[0].type != OCONFIG_TYPE_STRING))
150 WARNING ("apache plugin: The `%s' config option "
151 "needs exactly one string argument.", ci->key);
155 st = (apache_t *) malloc (sizeof (*st));
158 ERROR ("apache plugin: malloc failed.");
162 memset (st, 0, sizeof (*st));
164 status = config_set_string (&st->name, ci);
171 for (i = 0; i < ci->children_num; i++)
173 oconfig_item_t *child = ci->children + i;
175 if (strcasecmp ("URL", child->key) == 0)
176 status = config_set_string (&st->url, child);
177 else if (strcasecmp ("Host", child->key) == 0)
178 status = config_set_string (&st->host, child);
179 else if (strcasecmp ("User", child->key) == 0)
180 status = config_set_string (&st->user, child);
181 else if (strcasecmp ("Password", child->key) == 0)
182 status = config_set_string (&st->pass, child);
183 else if (strcasecmp ("VerifyPeer", child->key) == 0)
184 status = config_set_string (&st->verify_peer, child);
185 else if (strcasecmp ("VerifyHost", child->key) == 0)
186 status = config_set_string (&st->verify_host, child);
187 else if (strcasecmp ("CACert", child->key) == 0)
188 status = config_set_string (&st->cacert, child);
191 WARNING ("apache plugin: Option `%s' not allowed here.", child->key);
202 temp = (apache_t **) realloc (apache, sizeof (*apache) * (apache_num + 1));
205 ERROR ("apache plugin: realloc failed");
211 apache[apache_num] = st;
223 } /* int config_add */
225 static int config (oconfig_item_t *ci)
229 oconfig_item_t *lci = NULL; /* legacy config */
231 for (i = 0; i < ci->children_num; i++)
233 oconfig_item_t *child = ci->children + i;
235 if (strcasecmp ("Instance", child->key) == 0 && child->children_num > 0)
239 /* legacy mode - convert to <Instance ...> config */
242 lci = malloc (sizeof(*lci));
245 ERROR ("apache plugin: malloc failed.");
248 memset (lci, '\0', sizeof (*lci));
253 realloc (lci->children,
254 lci->children_num * sizeof (*child));
255 if (lci->children == NULL)
257 ERROR ("apache plugin: realloc failed.");
260 memcpy (&lci->children[lci->children_num-1], child, sizeof (*child));
262 } /* for (ci->children) */
266 /* create a <Instance ""> entry */
267 lci->key = "Instance";
269 lci->values = (oconfig_value_t *) malloc (lci->values_num * sizeof (oconfig_value_t));
270 lci->values[0].type = OCONFIG_TYPE_STRING;
271 lci->values[0].value.string = "";
273 status = config_add (lci);
275 sfree (lci->children);
283 /* initialize curl for each host */
284 static int init_host (apache_t *st) /* {{{ */
286 static char credentials[1024];
290 WARNING ("apache plugin: init: No URL configured, returning "
295 if (st->curl != NULL)
297 curl_easy_cleanup (st->curl);
300 if ((st->curl = curl_easy_init ()) == NULL)
302 ERROR ("apache plugin: init: `curl_easy_init' failed.");
306 curl_easy_setopt (st->curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
307 curl_easy_setopt (st->curl, CURLOPT_WRITEDATA, st);
308 curl_easy_setopt (st->curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
309 curl_easy_setopt (st->curl, CURLOPT_ERRORBUFFER, st->apache_curl_error);
311 if (st->user != NULL)
315 status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
316 st->user, (st->pass == NULL) ? "" : st->pass);
317 if ((status < 0) || ((size_t) status >= sizeof (credentials)))
319 ERROR ("apache plugin: init: Returning an error "
320 "because the credentials have been "
325 curl_easy_setopt (st->curl, CURLOPT_USERPWD, credentials);
328 curl_easy_setopt (st->curl, CURLOPT_URL, st->url);
330 if ((st->verify_peer == NULL) || (strcmp (st->verify_peer, "true") == 0))
332 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER, 1);
336 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER, 0);
339 if ((st->verify_host == NULL) || (strcmp (st->verify_host, "true") == 0))
341 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST, 2);
345 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST, 0);
348 if (st->cacert != NULL)
350 curl_easy_setopt (st->curl, CURLOPT_CAINFO, st->cacert);
354 } /* int init_host */
356 static int init (void)
362 for (i = 0; i < apache_num; i++)
364 status = init_host (apache[i]);
371 ERROR ("apache plugin init: No host could be initialized. Will return an error so "
372 "the plugin will be delayed.");
379 static void set_plugin_instance (apache_t *st, value_list_t *vl)
381 /* if there is no instance name, don't set plugin_instance */
382 if ( (st->name != NULL)
383 && (apache_num > 0) )
385 sstrncpy (vl->plugin_instance, st->name, sizeof (vl->plugin_instance));
387 } /* void set_plugin */
389 static void submit_counter (const char *type, const char *type_instance,
390 counter_t value, apache_t *st)
393 value_list_t vl = VALUE_LIST_INIT;
395 values[0].counter = value;
399 sstrncpy (vl.host, st->host, sizeof (vl.host));
400 sstrncpy (vl.plugin, "apache", sizeof (vl.plugin));
401 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
402 sstrncpy (vl.type, type, sizeof (vl.type));
404 if (type_instance != NULL)
405 sstrncpy (vl.type_instance, type_instance,
406 sizeof (vl.type_instance));
408 set_plugin_instance (st, &vl);
410 plugin_dispatch_values (&vl);
411 } /* void submit_counter */
413 static void submit_gauge (const char *type, const char *type_instance,
414 gauge_t value, apache_t *st)
417 value_list_t vl = VALUE_LIST_INIT;
419 values[0].gauge = value;
423 sstrncpy (vl.host, st->host, sizeof (vl.host));
424 sstrncpy (vl.plugin, "apache", sizeof (vl.plugin));
425 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
426 sstrncpy (vl.type, type, sizeof (vl.type));
428 if (type_instance != NULL)
429 sstrncpy (vl.type_instance, type_instance,
430 sizeof (vl.type_instance));
432 set_plugin_instance (st, &vl);
434 plugin_dispatch_values (&vl);
435 } /* void submit_counter */
437 static void submit_scoreboard (char *buf, apache_t *st)
441 * "_" Waiting for Connection, "S" Starting up, "R" Reading Request,
442 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
443 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
444 * "I" Idle cleanup of worker, "." Open slot with no current process
446 long long open = 0LL;
447 long long waiting = 0LL;
448 long long starting = 0LL;
449 long long reading = 0LL;
450 long long sending = 0LL;
451 long long keepalive = 0LL;
452 long long dnslookup = 0LL;
453 long long closing = 0LL;
454 long long logging = 0LL;
455 long long finishing = 0LL;
456 long long idle_cleanup = 0LL;
460 for (i = 0; buf[i] != '\0'; i++)
462 if (buf[i] == '.') open++;
463 else if (buf[i] == '_') waiting++;
464 else if (buf[i] == 'S') starting++;
465 else if (buf[i] == 'R') reading++;
466 else if (buf[i] == 'W') sending++;
467 else if (buf[i] == 'K') keepalive++;
468 else if (buf[i] == 'D') dnslookup++;
469 else if (buf[i] == 'C') closing++;
470 else if (buf[i] == 'L') logging++;
471 else if (buf[i] == 'G') finishing++;
472 else if (buf[i] == 'I') idle_cleanup++;
475 submit_gauge ("apache_scoreboard", "open" , open, st);
476 submit_gauge ("apache_scoreboard", "waiting" , waiting, st);
477 submit_gauge ("apache_scoreboard", "starting" , starting, st);
478 submit_gauge ("apache_scoreboard", "reading" , reading, st);
479 submit_gauge ("apache_scoreboard", "sending" , sending, st);
480 submit_gauge ("apache_scoreboard", "keepalive", keepalive, st);
481 submit_gauge ("apache_scoreboard", "dnslookup", dnslookup, st);
482 submit_gauge ("apache_scoreboard", "closing" , closing, st);
483 submit_gauge ("apache_scoreboard", "logging" , logging, st);
484 submit_gauge ("apache_scoreboard", "finishing", finishing, st);
485 submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup, st);
488 static int apache_read_host (apache_t *st)
500 if (st->curl == NULL)
505 st->apache_buffer_fill = 0;
506 if (curl_easy_perform (st->curl) != 0)
508 ERROR ("apache: curl_easy_perform failed: %s",
509 st->apache_curl_error);
513 ptr = st->apache_buffer;
515 while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
524 /* set the host to localhost if st->host is not specified */
525 if ( (st->host == NULL)
526 || (0 == strcmp(st->host, "")) ) {
527 st->host = hostname_g;
530 for (i = 0; i < lines_num; i++)
532 fields_num = strsplit (lines[i], fields, 4);
536 if ((strcmp (fields[0], "Total") == 0)
537 && (strcmp (fields[1], "Accesses:") == 0))
538 submit_counter ("apache_requests", "",
539 atoll (fields[2]), st);
540 else if ((strcmp (fields[0], "Total") == 0)
541 && (strcmp (fields[1], "kBytes:") == 0))
542 submit_counter ("apache_bytes", "",
543 1024LL * atoll (fields[2]), st);
545 else if (fields_num == 2)
547 if (strcmp (fields[0], "Scoreboard:") == 0)
548 submit_scoreboard (fields[1], st);
549 else if (strcmp (fields[0], "BusyServers:") == 0)
550 submit_gauge ("apache_connections", NULL, atol (fields[1]), st);
554 st->apache_buffer_fill = 0;
557 } /* int apache_read_host */
559 static int apache_read (void)
565 for (i = 0; i < apache_num; i++)
567 status = apache_read_host (apache[i]);
574 ERROR ("apache plugin: No host could be read. Will return an error so "
575 "the plugin will be delayed.");
580 } /* int apache_read */
582 void module_register (void)
584 plugin_register_complex_config ("apache", config);
585 plugin_register_init ("apache", init);
586 plugin_register_read ("apache", apache_read);
587 } /* void module_register */