2 * collectd - src/apache.c
3 * Copyright (C) 2006-2010 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 *server; /* user specific server type */
53 char apache_curl_error[CURL_ERROR_SIZE];
54 size_t apache_buffer_size;
55 size_t apache_buffer_fill;
59 typedef struct apache_s apache_t;
61 /* TODO: Remove this prototype */
62 static int apache_read_host (user_data_t *user_data);
64 static void apache_free (apache_t *st)
76 sfree (st->apache_buffer);
78 curl_easy_cleanup(st->curl);
84 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb,
87 size_t len = size * nmemb;
93 ERROR ("apache plugin: apache_curl_callback: "
94 "user_data pointer is NULL.");
101 if ((st->apache_buffer_fill + len) >= st->apache_buffer_size)
105 temp = (char *) realloc (st->apache_buffer,
106 st->apache_buffer_fill + len + 1);
109 ERROR ("apache plugin: realloc failed.");
112 st->apache_buffer = temp;
113 st->apache_buffer_size = st->apache_buffer_fill + len + 1;
116 memcpy (st->apache_buffer + st->apache_buffer_fill, (char *) buf, len);
117 st->apache_buffer_fill += len;
118 st->apache_buffer[st->apache_buffer_fill] = 0;
121 } /* int apache_curl_callback */
123 static size_t apache_header_callback (void *buf, size_t size, size_t nmemb,
126 size_t len = size * nmemb;
132 ERROR ("apache plugin: apache_header_callback: "
133 "user_data pointer is NULL.");
140 /* look for the Server header */
141 if (strncasecmp (buf, "Server: ", strlen ("Server: ")) != 0)
144 if (strstr (buf, "Apache") != NULL)
145 st->server_type = APACHE;
146 else if (strstr (buf, "lighttpd") != NULL)
147 st->server_type = LIGHTTPD;
148 else if (strstr (buf, "IBM_HTTP_Server") != NULL)
149 st->server_type = APACHE;
152 const char *hdr = buf;
154 hdr += strlen ("Server: ");
155 NOTICE ("apache plugin: Unknown server software: %s", hdr);
159 } /* apache_header_callback */
161 /* Configuration handling functiions
163 * <Instance "instance_name">
169 static int config_set_string (char **ret_string, /* {{{ */
174 if ((ci->values_num != 1)
175 || (ci->values[0].type != OCONFIG_TYPE_STRING))
177 WARNING ("apache plugin: The `%s' config option "
178 "needs exactly one string argument.", ci->key);
182 string = strdup (ci->values[0].value.string);
185 ERROR ("apache plugin: strdup failed.");
189 if (*ret_string != NULL)
191 *ret_string = string;
194 } /* }}} int config_set_string */
196 static int config_set_boolean (int *ret_boolean, /* {{{ */
199 if ((ci->values_num != 1)
200 || ((ci->values[0].type != OCONFIG_TYPE_BOOLEAN)
201 && (ci->values[0].type != OCONFIG_TYPE_STRING)))
203 WARNING ("apache plugin: The `%s' config option "
204 "needs exactly one boolean argument.", ci->key);
208 if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
210 if (ci->values[0].value.boolean)
215 else /* if (ci->values[0].type != OCONFIG_TYPE_STRING) */
217 char *string = ci->values[0].value.string;
218 if (IS_TRUE (string))
220 else if (IS_FALSE (string))
224 ERROR ("apache plugin: Cannot parse string "
225 "as boolean value: %s", string);
231 } /* }}} int config_set_boolean */
233 static int config_add (oconfig_item_t *ci)
239 if ((ci->values_num != 1)
240 || (ci->values[0].type != OCONFIG_TYPE_STRING))
242 WARNING ("apache plugin: The `%s' config option "
243 "needs exactly one string argument.", ci->key);
247 st = (apache_t *) malloc (sizeof (*st));
250 ERROR ("apache plugin: malloc failed.");
254 memset (st, 0, sizeof (*st));
256 status = config_set_string (&st->name, ci);
262 assert (st->name != NULL);
264 for (i = 0; i < ci->children_num; i++)
266 oconfig_item_t *child = ci->children + i;
268 if (strcasecmp ("URL", child->key) == 0)
269 status = config_set_string (&st->url, child);
270 else if (strcasecmp ("Host", child->key) == 0)
271 status = config_set_string (&st->host, child);
272 else if (strcasecmp ("User", child->key) == 0)
273 status = config_set_string (&st->user, child);
274 else if (strcasecmp ("Password", child->key) == 0)
275 status = config_set_string (&st->pass, child);
276 else if (strcasecmp ("VerifyPeer", child->key) == 0)
277 status = config_set_boolean (&st->verify_peer, child);
278 else if (strcasecmp ("VerifyHost", child->key) == 0)
279 status = config_set_boolean (&st->verify_host, child);
280 else if (strcasecmp ("CACert", child->key) == 0)
281 status = config_set_string (&st->cacert, child);
282 else if (strcasecmp ("Server", child->key) == 0)
283 status = config_set_string (&st->server, child);
286 WARNING ("apache plugin: Option `%s' not allowed here.",
295 /* Check if struct is complete.. */
296 if ((status == 0) && (st->url == NULL))
298 ERROR ("apache plugin: Instance `%s': "
299 "No URL has been configured.",
307 char callback_name[3*DATA_MAX_NAME_LEN];
309 memset (&ud, 0, sizeof (ud));
311 ud.free_func = (void *) apache_free;
313 memset (callback_name, 0, sizeof (callback_name));
314 ssnprintf (callback_name, sizeof (callback_name),
316 (st->host != NULL) ? st->host : hostname_g,
317 (st->name != NULL) ? st->name : "default"),
319 status = plugin_register_complex_read (/* group = */ NULL,
320 /* name = */ callback_name,
321 /* callback = */ apache_read_host,
322 /* interval = */ NULL,
323 /* user_data = */ &ud);
333 } /* int config_add */
335 static int config (oconfig_item_t *ci)
340 for (i = 0; i < ci->children_num; i++)
342 oconfig_item_t *child = ci->children + i;
344 if (strcasecmp ("Instance", child->key) == 0)
347 WARNING ("apache plugin: The configuration option "
348 "\"%s\" is not allowed here. Did you "
349 "forget to add an <Instance /> block "
350 "around the configuration?",
352 } /* for (ci->children) */
357 /* initialize curl for each host */
358 static int init_host (apache_t *st) /* {{{ */
360 static char credentials[1024];
362 assert (st->url != NULL);
363 /* (Assured by `config_add') */
365 if (st->curl != NULL)
367 curl_easy_cleanup (st->curl);
371 if ((st->curl = curl_easy_init ()) == NULL)
373 ERROR ("apache plugin: init_host: `curl_easy_init' failed.");
377 curl_easy_setopt (st->curl, CURLOPT_NOSIGNAL, 1L);
378 curl_easy_setopt (st->curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
379 curl_easy_setopt (st->curl, CURLOPT_WRITEDATA, st);
381 /* not set as yet if the user specified string doesn't match apache or
382 * lighttpd, then ignore it. Headers will be parsed to find out the
384 st->server_type = -1;
386 if (st->server != NULL)
388 if (strcasecmp(st->server, "apache") == 0)
389 st->server_type = APACHE;
390 else if (strcasecmp(st->server, "lighttpd") == 0)
391 st->server_type = LIGHTTPD;
392 else if (strcasecmp(st->server, "ibm_http_server") == 0)
393 st->server_type = APACHE;
395 WARNING ("apache plugin: Unknown `Server' setting: %s",
399 /* if not found register a header callback to determine the server_type */
400 if (st->server_type == -1)
402 curl_easy_setopt (st->curl, CURLOPT_HEADERFUNCTION, apache_header_callback);
403 curl_easy_setopt (st->curl, CURLOPT_WRITEHEADER, st);
406 curl_easy_setopt (st->curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
407 curl_easy_setopt (st->curl, CURLOPT_ERRORBUFFER, st->apache_curl_error);
409 if (st->user != NULL)
413 status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
414 st->user, (st->pass == NULL) ? "" : st->pass);
415 if ((status < 0) || ((size_t) status >= sizeof (credentials)))
417 ERROR ("apache plugin: init_host: Returning an error "
418 "because the credentials have been "
420 curl_easy_cleanup (st->curl);
425 curl_easy_setopt (st->curl, CURLOPT_USERPWD, credentials);
428 curl_easy_setopt (st->curl, CURLOPT_URL, st->url);
429 curl_easy_setopt (st->curl, CURLOPT_FOLLOWLOCATION, 1L);
430 curl_easy_setopt (st->curl, CURLOPT_MAXREDIRS, 50L);
432 if (st->verify_peer != 0)
434 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER, 1L);
438 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER, 0L);
441 if (st->verify_host != 0)
443 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST, 2L);
447 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST, 0L);
450 if (st->cacert != NULL)
452 curl_easy_setopt (st->curl, CURLOPT_CAINFO, st->cacert);
456 } /* }}} int init_host */
458 static void submit_value (const char *type, const char *type_instance,
459 value_t value, apache_t *st)
461 value_list_t vl = VALUE_LIST_INIT;
466 sstrncpy (vl.host, (st->host != NULL) ? st->host : hostname_g,
469 sstrncpy (vl.plugin, "apache", sizeof (vl.plugin));
470 if (st->name != NULL)
471 sstrncpy (vl.plugin_instance, st->name,
472 sizeof (vl.plugin_instance));
474 sstrncpy (vl.type, type, sizeof (vl.type));
475 if (type_instance != NULL)
476 sstrncpy (vl.type_instance, type_instance,
477 sizeof (vl.type_instance));
479 plugin_dispatch_values (&vl);
480 } /* void submit_value */
482 static void submit_derive (const char *type, const char *type_instance,
483 derive_t c, apache_t *st)
487 submit_value (type, type_instance, v, st);
488 } /* void submit_derive */
490 static void submit_gauge (const char *type, const char *type_instance,
491 gauge_t g, apache_t *st)
495 submit_value (type, type_instance, v, st);
496 } /* void submit_gauge */
498 static void submit_scoreboard (char *buf, apache_t *st)
502 * "_" Waiting for Connection, "S" Starting up,
503 * "R" Reading Request for apache and read-POST for lighttpd,
504 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
505 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
506 * "I" Idle cleanup of worker, "." Open slot with no current process
507 * Lighttpd specific legends -
508 * "E" hard error, "." connect, "h" handle-request,
509 * "q" request-start, "Q" request-end, "s" response-start
510 * "S" response-end, "r" read
512 long long open = 0LL;
513 long long waiting = 0LL;
514 long long starting = 0LL;
515 long long reading = 0LL;
516 long long sending = 0LL;
517 long long keepalive = 0LL;
518 long long dnslookup = 0LL;
519 long long closing = 0LL;
520 long long logging = 0LL;
521 long long finishing = 0LL;
522 long long idle_cleanup = 0LL;
524 /* lighttpd specific */
525 long long hard_error = 0LL;
526 long long lighttpd_read = 0LL;
527 long long handle_request = 0LL;
528 long long request_start = 0LL;
529 long long request_end = 0LL;
530 long long response_start = 0LL;
531 long long response_end = 0LL;
534 for (i = 0; buf[i] != '\0'; i++)
536 if (buf[i] == '.') open++;
537 else if (buf[i] == '_') waiting++;
538 else if (buf[i] == 'S') starting++;
539 else if (buf[i] == 'R') reading++;
540 else if (buf[i] == 'W') sending++;
541 else if (buf[i] == 'K') keepalive++;
542 else if (buf[i] == 'D') dnslookup++;
543 else if (buf[i] == 'C') closing++;
544 else if (buf[i] == 'L') logging++;
545 else if (buf[i] == 'G') finishing++;
546 else if (buf[i] == 'I') idle_cleanup++;
547 else if (buf[i] == 'r') lighttpd_read++;
548 else if (buf[i] == 'h') handle_request++;
549 else if (buf[i] == 'E') hard_error++;
550 else if (buf[i] == 'q') request_start++;
551 else if (buf[i] == 'Q') request_end++;
552 else if (buf[i] == 's') response_start++;
553 else if (buf[i] == 'S') response_end++;
556 if (st->server_type == APACHE)
558 submit_gauge ("apache_scoreboard", "open" , open, st);
559 submit_gauge ("apache_scoreboard", "waiting" , waiting, st);
560 submit_gauge ("apache_scoreboard", "starting" , starting, st);
561 submit_gauge ("apache_scoreboard", "reading" , reading, st);
562 submit_gauge ("apache_scoreboard", "sending" , sending, st);
563 submit_gauge ("apache_scoreboard", "keepalive", keepalive, st);
564 submit_gauge ("apache_scoreboard", "dnslookup", dnslookup, st);
565 submit_gauge ("apache_scoreboard", "closing" , closing, st);
566 submit_gauge ("apache_scoreboard", "logging" , logging, st);
567 submit_gauge ("apache_scoreboard", "finishing", finishing, st);
568 submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup, st);
572 submit_gauge ("apache_scoreboard", "connect" , open, st);
573 submit_gauge ("apache_scoreboard", "close" , closing, st);
574 submit_gauge ("apache_scoreboard", "hard_error" , hard_error, st);
575 submit_gauge ("apache_scoreboard", "read" , lighttpd_read, st);
576 submit_gauge ("apache_scoreboard", "read_post" , reading, st);
577 submit_gauge ("apache_scoreboard", "write" , sending, st);
578 submit_gauge ("apache_scoreboard", "handle_request", handle_request, st);
579 submit_gauge ("apache_scoreboard", "request_start" , request_start, st);
580 submit_gauge ("apache_scoreboard", "request_end" , request_end, st);
581 submit_gauge ("apache_scoreboard", "response_start", response_start, st);
582 submit_gauge ("apache_scoreboard", "response_end" , response_end, st);
586 static int apache_read_host (user_data_t *user_data) /* {{{ */
597 st = user_data->data;
599 assert (st->url != NULL);
600 /* (Assured by `config_add') */
602 if (st->curl == NULL)
606 status = init_host (st);
610 assert (st->curl != NULL);
612 st->apache_buffer_fill = 0;
613 if (curl_easy_perform (st->curl) != CURLE_OK)
615 ERROR ("apache: curl_easy_perform failed: %s",
616 st->apache_curl_error);
620 /* fallback - server_type to apache if not set at this time */
621 if (st->server_type == -1)
623 WARNING ("apache plugin: Unable to determine server software "
624 "automatically. Will assume Apache.");
625 st->server_type = APACHE;
628 ptr = st->apache_buffer;
630 while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
633 fields_num = strsplit (line, fields, STATIC_ARRAY_SIZE (fields));
637 if ((strcmp (fields[0], "Total") == 0) && (strcmp (fields[1], "Accesses:") == 0))
638 submit_derive ("apache_requests", "", atoll (fields[2]), st);
639 else if ((strcmp (fields[0], "Total") == 0) && (strcmp (fields[1], "kBytes:") == 0))
640 submit_derive ("apache_bytes", "", 1024LL * atoll (fields[2]), st);
642 else if (fields_num == 2)
644 if (strcmp (fields[0], "Scoreboard:") == 0)
645 submit_scoreboard (fields[1], st);
646 else if ((strcmp (fields[0], "BusyServers:") == 0) /* Apache 1.* */
647 || (strcmp (fields[0], "BusyWorkers:") == 0) /* Apache 2.* */)
648 submit_gauge ("apache_connections", NULL, atol (fields[1]), st);
649 else if ((strcmp (fields[0], "IdleServers:") == 0) /* Apache 1.x */
650 || (strcmp (fields[0], "IdleWorkers:") == 0) /* Apache 2.x */)
651 submit_gauge ("apache_idle_workers", NULL, atol (fields[1]), st);
655 st->apache_buffer_fill = 0;
658 } /* }}} int apache_read_host */
660 static int apache_init (void) /* {{{ */
662 /* Call this while collectd is still single-threaded to avoid
663 * initialization issues in libgcrypt. */
664 curl_global_init (CURL_GLOBAL_SSL);
666 } /* }}} int apache_init */
668 void module_register (void)
670 plugin_register_complex_config ("apache", config);
671 plugin_register_init ("apache", apache_init);
672 } /* void module_register */
674 /* vim: set sw=8 noet fdm=marker : */