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>
51 char apache_curl_error[CURL_ERROR_SIZE];
52 size_t apache_buffer_size;
53 size_t apache_buffer_fill;
57 typedef struct apache_s apache_t;
59 static apache_t **apache = NULL;
60 static size_t apache_num = 0;
62 static void apache_free (apache_t *st)
72 sfree (st->verify_peer);
73 sfree (st->verify_host);
75 sfree (st->apache_buffer);
77 curl_easy_cleanup(st->curl);
82 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb,
85 size_t len = size * nmemb;
90 if ((st->apache_buffer_fill + len) >= st->apache_buffer_size)
94 temp = (char *) realloc (st->apache_buffer,
95 st->apache_buffer_fill + len + 1);
98 ERROR ("apache plugin: realloc failed.");
101 st->apache_buffer = temp;
102 st->apache_buffer_size = st->apache_buffer_fill + len + 1;
105 memcpy (st->apache_buffer + st->apache_buffer_fill, (char *) buf, len);
106 st->apache_buffer_fill += len;
107 st->apache_buffer[st->apache_buffer_fill] = 0;
110 } /* int apache_curl_callback */
112 /* Configuration handling functiions
114 * <Instance "instance_name">
120 static int config_set_string (char **ret_string,
125 if ((ci->values_num != 1)
126 || (ci->values[0].type != OCONFIG_TYPE_STRING))
128 WARNING ("apache plugin: The `%s' config option "
129 "needs exactly one string argument.", ci->key);
133 string = strdup (ci->values[0].value.string);
136 ERROR ("apache plugin: strdup failed.");
140 if (*ret_string != NULL)
142 *ret_string = string;
145 } /* int config_set_string */
147 static int config_add (oconfig_item_t *ci)
153 if ((ci->values_num != 1)
154 || (ci->values[0].type != OCONFIG_TYPE_STRING))
156 WARNING ("apache plugin: The `%s' config option "
157 "needs exactly one string argument.", ci->key);
161 st = (apache_t *) malloc (sizeof (*st));
164 ERROR ("apache plugin: malloc failed.");
168 memset (st, 0, sizeof (*st));
170 status = config_set_string (&st->name, ci);
177 for (i = 0; i < ci->children_num; i++)
179 oconfig_item_t *child = ci->children + i;
181 if (strcasecmp ("URL", child->key) == 0)
182 status = config_set_string (&st->url, child);
183 else if (strcasecmp ("Host", child->key) == 0)
184 status = config_set_string (&st->host, child);
185 else if (strcasecmp ("User", child->key) == 0)
186 status = config_set_string (&st->user, child);
187 else if (strcasecmp ("Password", child->key) == 0)
188 status = config_set_string (&st->pass, child);
189 else if (strcasecmp ("VerifyPeer", child->key) == 0)
190 status = config_set_string (&st->verify_peer, child);
191 else if (strcasecmp ("VerifyHost", child->key) == 0)
192 status = config_set_string (&st->verify_host, child);
193 else if (strcasecmp ("CACert", child->key) == 0)
194 status = config_set_string (&st->cacert, child);
197 WARNING ("apache plugin: Option `%s' not allowed here.", child->key);
208 temp = (apache_t **) realloc (apache, sizeof (*apache) * (apache_num + 1));
211 ERROR ("apache plugin: realloc failed");
217 apache[apache_num] = st;
229 } /* int config_add */
231 static int config (oconfig_item_t *ci)
235 oconfig_item_t *lci = NULL; /* legacy config */
237 for (i = 0; i < ci->children_num; i++)
239 oconfig_item_t *child = ci->children + i;
241 if (strcasecmp ("Instance", child->key) == 0 && child->children_num > 0)
245 /* legacy mode - convert to <Instance ...> config */
248 lci = malloc (sizeof(*lci));
251 ERROR ("apache plugin: malloc failed.");
254 memset (lci, '\0', sizeof (*lci));
259 realloc (lci->children,
260 lci->children_num * sizeof (*child));
261 if (lci->children == NULL)
263 ERROR ("apache plugin: realloc failed.");
266 memcpy (&lci->children[lci->children_num-1], child, sizeof (*child));
268 } /* for (ci->children) */
272 /* create a <Instance ""> entry */
273 lci->key = "Instance";
275 lci->values = (oconfig_value_t *) malloc (lci->values_num * sizeof (oconfig_value_t));
276 lci->values[0].type = OCONFIG_TYPE_STRING;
277 lci->values[0].value.string = "";
279 status = config_add (lci);
281 sfree (lci->children);
289 /* initialize curl for each host */
290 static int init_host (apache_t *st) /* {{{ */
292 static char credentials[1024];
296 WARNING ("apache plugin: init: No URL configured, returning "
301 if (st->curl != NULL)
303 curl_easy_cleanup (st->curl);
306 if ((st->curl = curl_easy_init ()) == NULL)
308 ERROR ("apache plugin: init: `curl_easy_init' failed.");
312 curl_easy_setopt (st->curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
313 curl_easy_setopt (st->curl, CURLOPT_WRITEDATA, st);
314 curl_easy_setopt (st->curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
315 curl_easy_setopt (st->curl, CURLOPT_ERRORBUFFER, st->apache_curl_error);
317 if (st->user != NULL)
321 status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
322 st->user, (st->pass == NULL) ? "" : st->pass);
323 if ((status < 0) || ((size_t) status >= sizeof (credentials)))
325 ERROR ("apache plugin: init: Returning an error "
326 "because the credentials have been "
331 curl_easy_setopt (st->curl, CURLOPT_USERPWD, credentials);
334 curl_easy_setopt (st->curl, CURLOPT_URL, st->url);
336 if ((st->verify_peer == NULL) || (strcmp (st->verify_peer, "true") == 0))
338 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER, 1);
342 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER, 0);
345 if ((st->verify_host == NULL) || (strcmp (st->verify_host, "true") == 0))
347 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST, 2);
351 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST, 0);
354 if (st->cacert != NULL)
356 curl_easy_setopt (st->curl, CURLOPT_CAINFO, st->cacert);
360 } /* int init_host */
362 static int init (void)
368 for (i = 0; i < apache_num; i++)
370 status = init_host (apache[i]);
377 ERROR ("apache plugin init: No host could be initialized. Will return an error so "
378 "the plugin will be delayed.");
385 static void set_plugin_instance (apache_t *st, value_list_t *vl)
387 /* if there is no instance name, don't set plugin_instance */
388 if ( (st->name != NULL)
389 && (apache_num > 0) )
391 sstrncpy (vl->plugin_instance, st->name, sizeof (vl->plugin_instance));
393 } /* void set_plugin */
395 static void submit_counter (const char *type, const char *type_instance,
396 counter_t value, apache_t *st)
399 value_list_t vl = VALUE_LIST_INIT;
401 values[0].counter = value;
405 sstrncpy (vl.host, st->host, sizeof (vl.host));
406 sstrncpy (vl.plugin, "apache", sizeof (vl.plugin));
407 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
408 sstrncpy (vl.type, type, sizeof (vl.type));
410 if (type_instance != NULL)
411 sstrncpy (vl.type_instance, type_instance,
412 sizeof (vl.type_instance));
414 set_plugin_instance (st, &vl);
416 plugin_dispatch_values (&vl);
417 } /* void submit_counter */
419 static void submit_gauge (const char *type, const char *type_instance,
420 gauge_t value, apache_t *st)
423 value_list_t vl = VALUE_LIST_INIT;
425 values[0].gauge = value;
429 sstrncpy (vl.host, st->host, sizeof (vl.host));
430 sstrncpy (vl.plugin, "apache", sizeof (vl.plugin));
431 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
432 sstrncpy (vl.type, type, sizeof (vl.type));
434 if (type_instance != NULL)
435 sstrncpy (vl.type_instance, type_instance,
436 sizeof (vl.type_instance));
438 set_plugin_instance (st, &vl);
440 plugin_dispatch_values (&vl);
441 } /* void submit_counter */
443 static void submit_scoreboard (char *buf, int server, apache_t *st)
447 * "_" Waiting for Connection, "S" Starting up,
448 * "R" Reading Request for apache and read-POST for lighttpd,
449 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
450 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
451 * "I" Idle cleanup of worker, "." Open slot with no current process
452 * Lighttpd specific legends -
453 * "E" hard error, "." connect, "h" handle-request,
454 * "q" request-start, "Q" request-end, "s" response-start
455 * "S" response-end, "r" read
457 long long open = 0LL;
458 long long waiting = 0LL;
459 long long starting = 0LL;
460 long long reading = 0LL;
461 long long sending = 0LL;
462 long long keepalive = 0LL;
463 long long dnslookup = 0LL;
464 long long closing = 0LL;
465 long long logging = 0LL;
466 long long finishing = 0LL;
467 long long idle_cleanup = 0LL;
469 /* lighttpd specific */
470 long long hard_error = 0LL;
471 long long lighttpd_read = 0LL;
472 long long handle_request = 0LL;
473 long long request_start = 0LL;
474 long long request_end = 0LL;
475 long long response_start = 0LL;
476 long long response_end = 0LL;
480 for (i = 0; buf[i] != '\0'; i++)
482 if (buf[i] == '.') open++;
483 else if (buf[i] == '_') waiting++;
484 else if (buf[i] == 'S') starting++;
485 else if (buf[i] == 'R') reading++;
486 else if (buf[i] == 'W') sending++;
487 else if (buf[i] == 'K') keepalive++;
488 else if (buf[i] == 'D') dnslookup++;
489 else if (buf[i] == 'C') closing++;
490 else if (buf[i] == 'L') logging++;
491 else if (buf[i] == 'G') finishing++;
492 else if (buf[i] == 'I') idle_cleanup++;
493 else if (buf[i] == 'r') lighttpd_read++;
494 else if (buf[i] == 'h') handle_request++;
495 else if (buf[i] == 'E') hard_error++;
496 else if (buf[i] == 'q') request_start++;
497 else if (buf[i] == 'Q') request_end++;
498 else if (buf[i] == 's') response_start++;
499 else if (buf[i] == 'S') response_end++;
502 if (server == APACHE)
504 submit_gauge ("apache_scoreboard", "open" , open, st);
505 submit_gauge ("apache_scoreboard", "waiting" , waiting, st);
506 submit_gauge ("apache_scoreboard", "starting" , starting, st);
507 submit_gauge ("apache_scoreboard", "reading" , reading, st);
508 submit_gauge ("apache_scoreboard", "sending" , sending, st);
509 submit_gauge ("apache_scoreboard", "keepalive", keepalive, st);
510 submit_gauge ("apache_scoreboard", "dnslookup", dnslookup, st);
511 submit_gauge ("apache_scoreboard", "closing" , closing, st);
512 submit_gauge ("apache_scoreboard", "logging" , logging, st);
513 submit_gauge ("apache_scoreboard", "finishing", finishing, st);
514 submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup, st);
517 submit_gauge ("apache_scoreboard", "connect" , open, st);
518 submit_gauge ("apache_scoreboard", "close" , closing, st);
519 submit_gauge ("apache_scoreboard", "hard_error" , hard_error, st);
520 submit_gauge ("apache_scoreboard", "read" , lighttpd_read, st);
521 submit_gauge ("apache_scoreboard", "read_post" , reading, st);
522 submit_gauge ("apache_scoreboard", "write" , sending, st);
523 submit_gauge ("apache_scoreboard", "handle_request", handle_request, st);
524 submit_gauge ("apache_scoreboard", "request_start" , request_start, st);
525 submit_gauge ("apache_scoreboard", "request_end" , request_end, st);
526 submit_gauge ("apache_scoreboard", "response_start", response_start, st);
527 submit_gauge ("apache_scoreboard", "response_end" , response_end, st);
532 static int apache_read_host (apache_t *st)
543 int server = LIGHTTPD; /* default is lighttpd */
545 if (st->curl == NULL)
550 st->apache_buffer_fill = 0;
551 if (curl_easy_perform (st->curl) != 0)
553 ERROR ("apache: curl_easy_perform failed: %s",
554 st->apache_curl_error);
558 ptr = st->apache_buffer;
560 while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
569 /* set the host to localhost if st->host is not specified */
570 if ( (st->host == NULL)
571 || (0 == strcmp(st->host, "")) ) {
572 st->host = hostname_g;
575 for (i = 0; i < lines_num; i++)
577 fields_num = strsplit (lines[i], fields, 4);
581 if ((strcmp (fields[0], "Total") == 0)
582 && (strcmp (fields[1], "Accesses:") == 0))
583 submit_counter ("apache_requests", "",
584 atoll (fields[2]), st);
585 else if ((strcmp (fields[0], "Total") == 0)
586 && (strcmp (fields[1], "kBytes:") == 0))
587 submit_counter ("apache_bytes", "",
588 1024LL * atoll (fields[2]), st);
590 else if (fields_num == 2)
592 /* find out if the server is apache from the mod_status
593 * output. apache mod_status output has additional
594 * fields which lighttpd mod_status output doesn't have
595 * e.g: ReqPerSec. submit_scoreboard needs server type
596 * information and thus it is important to pick up a
597 * field before scoreboard gets parsed to set the
599 if (strcmp (fields[0], "ReqPerSec:") == 0)
601 else if (strcmp (fields[0], "Scoreboard:") == 0)
602 submit_scoreboard (fields[1], server, st);
603 else if (strcmp (fields[0], "BusyServers:") == 0)
604 submit_gauge ("apache_connections", NULL, atol (fields[1]), st);
608 st->apache_buffer_fill = 0;
611 } /* int apache_read_host */
613 static int apache_read (void)
619 for (i = 0; i < apache_num; i++)
621 status = apache_read_host (apache[i]);
628 ERROR ("apache plugin: No host could be read. Will return an error so "
629 "the plugin will be delayed.");
634 } /* int apache_read */
636 void module_register (void)
638 plugin_register_complex_config ("apache", config);
639 plugin_register_init ("apache", init);
640 plugin_register_read ("apache", apache_read);
641 } /* void module_register */