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 collectd.org>
22 * Florent EppO Monbillard <eppo at darox.net>
23 * - connections/lighttpd extension
24 * Amit Gupta <amit.gupta221 at gmail.com>
31 #include "configfile.h"
33 #include <curl/curl.h>
53 char *server; /* user specific server type */
55 char apache_curl_error[CURL_ERROR_SIZE];
56 size_t apache_buffer_size;
57 size_t apache_buffer_fill;
62 typedef struct apache_s apache_t;
64 /* TODO: Remove this prototype */
65 static int apache_read_host (user_data_t *user_data);
67 static void apache_free (void *arg)
80 sfree (st->ssl_ciphers);
82 sfree (st->apache_buffer);
84 curl_easy_cleanup(st->curl);
90 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb,
93 size_t len = size * nmemb;
99 ERROR ("apache plugin: apache_curl_callback: "
100 "user_data pointer is NULL.");
107 if ((st->apache_buffer_fill + len) >= st->apache_buffer_size)
111 temp = realloc (st->apache_buffer,
112 st->apache_buffer_fill + len + 1);
115 ERROR ("apache plugin: realloc failed.");
118 st->apache_buffer = temp;
119 st->apache_buffer_size = st->apache_buffer_fill + len + 1;
122 memcpy (st->apache_buffer + st->apache_buffer_fill, (char *) buf, len);
123 st->apache_buffer_fill += len;
124 st->apache_buffer[st->apache_buffer_fill] = 0;
127 } /* int apache_curl_callback */
129 static size_t apache_header_callback (void *buf, size_t size, size_t nmemb,
132 size_t len = size * nmemb;
138 ERROR ("apache plugin: apache_header_callback: "
139 "user_data pointer is NULL.");
146 /* look for the Server header */
147 if (strncasecmp (buf, "Server: ", strlen ("Server: ")) != 0)
150 if (strstr (buf, "Apache") != NULL)
151 st->server_type = APACHE;
152 else if (strstr (buf, "lighttpd") != NULL)
153 st->server_type = LIGHTTPD;
154 else if (strstr (buf, "IBM_HTTP_Server") != NULL)
155 st->server_type = APACHE;
158 const char *hdr = buf;
160 hdr += strlen ("Server: ");
161 NOTICE ("apache plugin: Unknown server software: %s", hdr);
165 } /* apache_header_callback */
167 /* Configuration handling functiions
169 * <Instance "instance_name">
175 static int config_add (oconfig_item_t *ci)
180 st = calloc (1, sizeof (*st));
183 ERROR ("apache plugin: calloc failed.");
189 status = cf_util_get_string (ci, &st->name);
195 assert (st->name != NULL);
197 for (int i = 0; i < ci->children_num; i++)
199 oconfig_item_t *child = ci->children + i;
201 if (strcasecmp ("URL", child->key) == 0)
202 status = cf_util_get_string (child, &st->url);
203 else if (strcasecmp ("Host", child->key) == 0)
204 status = cf_util_get_string (child, &st->host);
205 else if (strcasecmp ("User", child->key) == 0)
206 status = cf_util_get_string (child, &st->user);
207 else if (strcasecmp ("Password", child->key) == 0)
208 status = cf_util_get_string (child, &st->pass);
209 else if (strcasecmp ("VerifyPeer", child->key) == 0)
210 status = cf_util_get_boolean (child, &st->verify_peer);
211 else if (strcasecmp ("VerifyHost", child->key) == 0)
212 status = cf_util_get_boolean (child, &st->verify_host);
213 else if (strcasecmp ("CACert", child->key) == 0)
214 status = cf_util_get_string (child, &st->cacert);
215 else if (strcasecmp ("SSLCiphers", child->key) == 0)
216 status = cf_util_get_string (child, &st->ssl_ciphers);
217 else if (strcasecmp ("Server", child->key) == 0)
218 status = cf_util_get_string (child, &st->server);
219 else if (strcasecmp ("Timeout", child->key) == 0)
220 status = cf_util_get_int (child, &st->timeout);
223 WARNING ("apache plugin: Option `%s' not allowed here.",
232 /* Check if struct is complete.. */
233 if ((status == 0) && (st->url == NULL))
235 ERROR ("apache plugin: Instance `%s': "
236 "No URL has been configured.",
245 .free_func = apache_free
248 char callback_name[3*DATA_MAX_NAME_LEN];
250 ssnprintf (callback_name, sizeof (callback_name),
252 (st->host != NULL) ? st->host : hostname_g,
253 (st->name != NULL) ? st->name : "default"),
255 status = plugin_register_complex_read (/* group = */ NULL,
256 /* name = */ callback_name,
257 /* callback = */ apache_read_host,
259 /* user_data = */ &ud);
269 } /* int config_add */
271 static int config (oconfig_item_t *ci)
275 for (int i = 0; i < ci->children_num; i++)
277 oconfig_item_t *child = ci->children + i;
279 if (strcasecmp ("Instance", child->key) == 0)
282 WARNING ("apache plugin: The configuration option "
283 "\"%s\" is not allowed here. Did you "
284 "forget to add an <Instance /> block "
285 "around the configuration?",
287 } /* for (ci->children) */
292 /* initialize curl for each host */
293 static int init_host (apache_t *st) /* {{{ */
295 assert (st->url != NULL);
296 /* (Assured by `config_add') */
298 if (st->curl != NULL)
300 curl_easy_cleanup (st->curl);
304 if ((st->curl = curl_easy_init ()) == NULL)
306 ERROR ("apache plugin: init_host: `curl_easy_init' failed.");
310 curl_easy_setopt (st->curl, CURLOPT_NOSIGNAL, 1L);
311 curl_easy_setopt (st->curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
312 curl_easy_setopt (st->curl, CURLOPT_WRITEDATA, st);
314 /* not set as yet if the user specified string doesn't match apache or
315 * lighttpd, then ignore it. Headers will be parsed to find out the
317 st->server_type = -1;
319 if (st->server != NULL)
321 if (strcasecmp(st->server, "apache") == 0)
322 st->server_type = APACHE;
323 else if (strcasecmp(st->server, "lighttpd") == 0)
324 st->server_type = LIGHTTPD;
325 else if (strcasecmp(st->server, "ibm_http_server") == 0)
326 st->server_type = APACHE;
328 WARNING ("apache plugin: Unknown `Server' setting: %s",
332 /* if not found register a header callback to determine the server_type */
333 if (st->server_type == -1)
335 curl_easy_setopt (st->curl, CURLOPT_HEADERFUNCTION, apache_header_callback);
336 curl_easy_setopt (st->curl, CURLOPT_WRITEHEADER, st);
339 curl_easy_setopt (st->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
340 curl_easy_setopt (st->curl, CURLOPT_ERRORBUFFER, st->apache_curl_error);
342 if (st->user != NULL)
344 #ifdef HAVE_CURLOPT_USERNAME
345 curl_easy_setopt (st->curl, CURLOPT_USERNAME, st->user);
346 curl_easy_setopt (st->curl, CURLOPT_PASSWORD,
347 (st->pass == NULL) ? "" : st->pass);
349 static char credentials[1024];
352 status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
353 st->user, (st->pass == NULL) ? "" : st->pass);
354 if ((status < 0) || ((size_t) status >= sizeof (credentials)))
356 ERROR ("apache plugin: init_host: Returning an error "
357 "because the credentials have been "
359 curl_easy_cleanup (st->curl);
364 curl_easy_setopt (st->curl, CURLOPT_USERPWD, credentials);
368 curl_easy_setopt (st->curl, CURLOPT_URL, st->url);
369 curl_easy_setopt (st->curl, CURLOPT_FOLLOWLOCATION, 1L);
370 curl_easy_setopt (st->curl, CURLOPT_MAXREDIRS, 50L);
372 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER,
373 (long) st->verify_peer);
374 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST,
375 st->verify_host ? 2L : 0L);
376 if (st->cacert != NULL)
377 curl_easy_setopt (st->curl, CURLOPT_CAINFO, st->cacert);
378 if (st->ssl_ciphers != NULL)
379 curl_easy_setopt (st->curl, CURLOPT_SSL_CIPHER_LIST,st->ssl_ciphers);
381 #ifdef HAVE_CURLOPT_TIMEOUT_MS
382 if (st->timeout >= 0)
383 curl_easy_setopt (st->curl, CURLOPT_TIMEOUT_MS, (long) st->timeout);
385 curl_easy_setopt (st->curl, CURLOPT_TIMEOUT_MS, (long) CDTIME_T_TO_MS(plugin_get_interval()));
389 } /* }}} int init_host */
391 static void submit_value (const char *type, const char *type_instance,
392 value_t value, apache_t *st)
394 value_list_t vl = VALUE_LIST_INIT;
399 sstrncpy (vl.host, (st->host != NULL) ? st->host : hostname_g,
402 sstrncpy (vl.plugin, "apache", sizeof (vl.plugin));
403 if (st->name != NULL)
404 sstrncpy (vl.plugin_instance, st->name,
405 sizeof (vl.plugin_instance));
407 sstrncpy (vl.type, type, sizeof (vl.type));
408 if (type_instance != NULL)
409 sstrncpy (vl.type_instance, type_instance,
410 sizeof (vl.type_instance));
412 plugin_dispatch_values (&vl);
413 } /* void submit_value */
415 static void submit_derive (const char *type, const char *type_instance,
416 derive_t c, apache_t *st)
420 submit_value (type, type_instance, v, st);
421 } /* void submit_derive */
423 static void submit_gauge (const char *type, const char *type_instance,
424 gauge_t g, apache_t *st)
428 submit_value (type, type_instance, v, st);
429 } /* void submit_gauge */
431 static void submit_scoreboard (char *buf, apache_t *st)
435 * "_" Waiting for Connection, "S" Starting up,
436 * "R" Reading Request for apache and read-POST for lighttpd,
437 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
438 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
439 * "I" Idle cleanup of worker, "." Open slot with no current process
440 * Lighttpd specific legends -
441 * "E" hard error, "." connect, "h" handle-request,
442 * "q" request-start, "Q" request-end, "s" response-start
443 * "S" response-end, "r" read
445 long long open = 0LL;
446 long long waiting = 0LL;
447 long long starting = 0LL;
448 long long reading = 0LL;
449 long long sending = 0LL;
450 long long keepalive = 0LL;
451 long long dnslookup = 0LL;
452 long long closing = 0LL;
453 long long logging = 0LL;
454 long long finishing = 0LL;
455 long long idle_cleanup = 0LL;
457 /* lighttpd specific */
458 long long hard_error = 0LL;
459 long long lighttpd_read = 0LL;
460 long long handle_request = 0LL;
461 long long request_start = 0LL;
462 long long request_end = 0LL;
463 long long response_start = 0LL;
464 long long response_end = 0LL;
466 for (int i = 0; buf[i] != '\0'; i++)
468 if (buf[i] == '.') open++;
469 else if (buf[i] == '_') waiting++;
470 else if (buf[i] == 'S') starting++;
471 else if (buf[i] == 'R') reading++;
472 else if (buf[i] == 'W') sending++;
473 else if (buf[i] == 'K') keepalive++;
474 else if (buf[i] == 'D') dnslookup++;
475 else if (buf[i] == 'C') closing++;
476 else if (buf[i] == 'L') logging++;
477 else if (buf[i] == 'G') finishing++;
478 else if (buf[i] == 'I') idle_cleanup++;
479 else if (buf[i] == 'r') lighttpd_read++;
480 else if (buf[i] == 'h') handle_request++;
481 else if (buf[i] == 'E') hard_error++;
482 else if (buf[i] == 'q') request_start++;
483 else if (buf[i] == 'Q') request_end++;
484 else if (buf[i] == 's') response_start++;
485 else if (buf[i] == 'S') response_end++;
488 if (st->server_type == APACHE)
490 submit_gauge ("apache_scoreboard", "open" , open, st);
491 submit_gauge ("apache_scoreboard", "waiting" , waiting, st);
492 submit_gauge ("apache_scoreboard", "starting" , starting, st);
493 submit_gauge ("apache_scoreboard", "reading" , reading, st);
494 submit_gauge ("apache_scoreboard", "sending" , sending, st);
495 submit_gauge ("apache_scoreboard", "keepalive", keepalive, st);
496 submit_gauge ("apache_scoreboard", "dnslookup", dnslookup, st);
497 submit_gauge ("apache_scoreboard", "closing" , closing, st);
498 submit_gauge ("apache_scoreboard", "logging" , logging, st);
499 submit_gauge ("apache_scoreboard", "finishing", finishing, st);
500 submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup, st);
504 submit_gauge ("apache_scoreboard", "connect" , open, st);
505 submit_gauge ("apache_scoreboard", "close" , closing, st);
506 submit_gauge ("apache_scoreboard", "hard_error" , hard_error, st);
507 submit_gauge ("apache_scoreboard", "read" , lighttpd_read, st);
508 submit_gauge ("apache_scoreboard", "read_post" , reading, st);
509 submit_gauge ("apache_scoreboard", "write" , sending, st);
510 submit_gauge ("apache_scoreboard", "handle_request", handle_request, st);
511 submit_gauge ("apache_scoreboard", "request_start" , request_start, st);
512 submit_gauge ("apache_scoreboard", "request_end" , request_end, st);
513 submit_gauge ("apache_scoreboard", "response_start", response_start, st);
514 submit_gauge ("apache_scoreboard", "response_end" , response_end, st);
518 static int apache_read_host (user_data_t *user_data) /* {{{ */
529 st = user_data->data;
534 static const char *text_plain = "text/plain";
536 assert (st->url != NULL);
537 /* (Assured by `config_add') */
539 if (st->curl == NULL)
541 status = init_host (st);
545 assert (st->curl != NULL);
547 st->apache_buffer_fill = 0;
548 if (curl_easy_perform (st->curl) != CURLE_OK)
550 ERROR ("apache: curl_easy_perform failed: %s",
551 st->apache_curl_error);
555 /* fallback - server_type to apache if not set at this time */
556 if (st->server_type == -1)
558 WARNING ("apache plugin: Unable to determine server software "
559 "automatically. Will assume Apache.");
560 st->server_type = APACHE;
563 status = curl_easy_getinfo (st->curl, CURLINFO_CONTENT_TYPE, &content_type);
564 if ((status == CURLE_OK) && (content_type != NULL) &&
565 (strncasecmp (content_type, text_plain, strlen (text_plain)) != 0))
567 WARNING ("apache plugin: `Content-Type' response header is not `%s' "
568 "(received: `%s'). Expecting unparseable data. Please check `URL' "
569 "parameter (missing `?auto' suffix ?)",
570 text_plain, content_type);
573 ptr = st->apache_buffer;
575 while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
578 fields_num = strsplit (line, fields, STATIC_ARRAY_SIZE (fields));
582 if ((strcmp (fields[0], "Total") == 0) && (strcmp (fields[1], "Accesses:") == 0))
583 submit_derive ("apache_requests", "", atoll (fields[2]), st);
584 else if ((strcmp (fields[0], "Total") == 0) && (strcmp (fields[1], "kBytes:") == 0))
585 submit_derive ("apache_bytes", "", 1024LL * atoll (fields[2]), st);
587 else if (fields_num == 2)
589 if (strcmp (fields[0], "Scoreboard:") == 0)
590 submit_scoreboard (fields[1], st);
591 else if ((strcmp (fields[0], "BusyServers:") == 0) /* Apache 1.* */
592 || (strcmp (fields[0], "BusyWorkers:") == 0) /* Apache 2.* */)
593 submit_gauge ("apache_connections", NULL, atol (fields[1]), st);
594 else if ((strcmp (fields[0], "IdleServers:") == 0) /* Apache 1.x */
595 || (strcmp (fields[0], "IdleWorkers:") == 0) /* Apache 2.x */)
596 submit_gauge ("apache_idle_workers", NULL, atol (fields[1]), st);
600 st->apache_buffer_fill = 0;
603 } /* }}} int apache_read_host */
605 static int apache_init (void) /* {{{ */
607 /* Call this while collectd is still single-threaded to avoid
608 * initialization issues in libgcrypt. */
609 curl_global_init (CURL_GLOBAL_SSL);
611 } /* }}} int apache_init */
613 void module_register (void)
615 plugin_register_complex_config ("apache", config);
616 plugin_register_init ("apache", apache_init);
617 } /* void module_register */
619 /* vim: set sw=8 noet fdm=marker : */