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>
32 #include <curl/curl.h>
52 char *server; /* user specific server type */
54 char apache_curl_error[CURL_ERROR_SIZE];
55 size_t apache_buffer_size;
56 size_t apache_buffer_fill;
61 typedef struct apache_s apache_t;
63 /* TODO: Remove this prototype */
64 static int apache_read_host (user_data_t *user_data);
66 static void apache_free (void *arg)
79 sfree (st->ssl_ciphers);
81 sfree (st->apache_buffer);
83 curl_easy_cleanup(st->curl);
89 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb,
92 size_t len = size * nmemb;
98 ERROR ("apache plugin: apache_curl_callback: "
99 "user_data pointer is NULL.");
106 if ((st->apache_buffer_fill + len) >= st->apache_buffer_size)
110 temp = realloc (st->apache_buffer,
111 st->apache_buffer_fill + len + 1);
114 ERROR ("apache plugin: realloc failed.");
117 st->apache_buffer = temp;
118 st->apache_buffer_size = st->apache_buffer_fill + len + 1;
121 memcpy (st->apache_buffer + st->apache_buffer_fill, (char *) buf, len);
122 st->apache_buffer_fill += len;
123 st->apache_buffer[st->apache_buffer_fill] = 0;
126 } /* int apache_curl_callback */
128 static size_t apache_header_callback (void *buf, size_t size, size_t nmemb,
131 size_t len = size * nmemb;
137 ERROR ("apache plugin: apache_header_callback: "
138 "user_data pointer is NULL.");
145 /* look for the Server header */
146 if (strncasecmp (buf, "Server: ", strlen ("Server: ")) != 0)
149 if (strstr (buf, "Apache") != NULL)
150 st->server_type = APACHE;
151 else if (strstr (buf, "lighttpd") != NULL)
152 st->server_type = LIGHTTPD;
153 else if (strstr (buf, "IBM_HTTP_Server") != NULL)
154 st->server_type = APACHE;
157 const char *hdr = buf;
159 hdr += strlen ("Server: ");
160 NOTICE ("apache plugin: Unknown server software: %s", hdr);
164 } /* apache_header_callback */
166 /* Configuration handling functiions
168 * <Instance "instance_name">
174 static int config_add (oconfig_item_t *ci)
179 st = calloc (1, sizeof (*st));
182 ERROR ("apache plugin: calloc failed.");
188 status = cf_util_get_string (ci, &st->name);
194 assert (st->name != NULL);
196 for (int i = 0; i < ci->children_num; i++)
198 oconfig_item_t *child = ci->children + i;
200 if (strcasecmp ("URL", child->key) == 0)
201 status = cf_util_get_string (child, &st->url);
202 else if (strcasecmp ("Host", child->key) == 0)
203 status = cf_util_get_string (child, &st->host);
204 else if (strcasecmp ("User", child->key) == 0)
205 status = cf_util_get_string (child, &st->user);
206 else if (strcasecmp ("Password", child->key) == 0)
207 status = cf_util_get_string (child, &st->pass);
208 else if (strcasecmp ("VerifyPeer", child->key) == 0)
209 status = cf_util_get_boolean (child, &st->verify_peer);
210 else if (strcasecmp ("VerifyHost", child->key) == 0)
211 status = cf_util_get_boolean (child, &st->verify_host);
212 else if (strcasecmp ("CACert", child->key) == 0)
213 status = cf_util_get_string (child, &st->cacert);
214 else if (strcasecmp ("SSLCiphers", child->key) == 0)
215 status = cf_util_get_string (child, &st->ssl_ciphers);
216 else if (strcasecmp ("Server", child->key) == 0)
217 status = cf_util_get_string (child, &st->server);
218 else if (strcasecmp ("Timeout", child->key) == 0)
219 status = cf_util_get_int (child, &st->timeout);
222 WARNING ("apache plugin: Option `%s' not allowed here.",
231 /* Check if struct is complete.. */
232 if ((status == 0) && (st->url == NULL))
234 ERROR ("apache plugin: Instance `%s': "
235 "No URL has been configured.",
244 .free_func = apache_free
247 char callback_name[3*DATA_MAX_NAME_LEN];
249 ssnprintf (callback_name, sizeof (callback_name),
251 (st->host != NULL) ? st->host : hostname_g,
252 (st->name != NULL) ? st->name : "default"),
254 status = plugin_register_complex_read (/* group = */ NULL,
255 /* name = */ callback_name,
256 /* callback = */ apache_read_host,
258 /* user_data = */ &ud);
268 } /* int config_add */
270 static int config (oconfig_item_t *ci)
274 for (int i = 0; i < ci->children_num; i++)
276 oconfig_item_t *child = ci->children + i;
278 if (strcasecmp ("Instance", child->key) == 0)
281 WARNING ("apache plugin: The configuration option "
282 "\"%s\" is not allowed here. Did you "
283 "forget to add an <Instance /> block "
284 "around the configuration?",
286 } /* for (ci->children) */
291 /* initialize curl for each host */
292 static int init_host (apache_t *st) /* {{{ */
294 assert (st->url != NULL);
295 /* (Assured by `config_add') */
297 if (st->curl != NULL)
299 curl_easy_cleanup (st->curl);
303 if ((st->curl = curl_easy_init ()) == NULL)
305 ERROR ("apache plugin: init_host: `curl_easy_init' failed.");
309 curl_easy_setopt (st->curl, CURLOPT_NOSIGNAL, 1L);
310 curl_easy_setopt (st->curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
311 curl_easy_setopt (st->curl, CURLOPT_WRITEDATA, st);
313 /* not set as yet if the user specified string doesn't match apache or
314 * lighttpd, then ignore it. Headers will be parsed to find out the
316 st->server_type = -1;
318 if (st->server != NULL)
320 if (strcasecmp(st->server, "apache") == 0)
321 st->server_type = APACHE;
322 else if (strcasecmp(st->server, "lighttpd") == 0)
323 st->server_type = LIGHTTPD;
324 else if (strcasecmp(st->server, "ibm_http_server") == 0)
325 st->server_type = APACHE;
327 WARNING ("apache plugin: Unknown `Server' setting: %s",
331 /* if not found register a header callback to determine the server_type */
332 if (st->server_type == -1)
334 curl_easy_setopt (st->curl, CURLOPT_HEADERFUNCTION, apache_header_callback);
335 curl_easy_setopt (st->curl, CURLOPT_WRITEHEADER, st);
338 curl_easy_setopt (st->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
339 curl_easy_setopt (st->curl, CURLOPT_ERRORBUFFER, st->apache_curl_error);
341 if (st->user != NULL)
343 #ifdef HAVE_CURLOPT_USERNAME
344 curl_easy_setopt (st->curl, CURLOPT_USERNAME, st->user);
345 curl_easy_setopt (st->curl, CURLOPT_PASSWORD,
346 (st->pass == NULL) ? "" : st->pass);
348 static char credentials[1024];
351 status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
352 st->user, (st->pass == NULL) ? "" : st->pass);
353 if ((status < 0) || ((size_t) status >= sizeof (credentials)))
355 ERROR ("apache plugin: init_host: Returning an error "
356 "because the credentials have been "
358 curl_easy_cleanup (st->curl);
363 curl_easy_setopt (st->curl, CURLOPT_USERPWD, credentials);
367 curl_easy_setopt (st->curl, CURLOPT_URL, st->url);
368 curl_easy_setopt (st->curl, CURLOPT_FOLLOWLOCATION, 1L);
369 curl_easy_setopt (st->curl, CURLOPT_MAXREDIRS, 50L);
371 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER,
372 (long) st->verify_peer);
373 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST,
374 st->verify_host ? 2L : 0L);
375 if (st->cacert != NULL)
376 curl_easy_setopt (st->curl, CURLOPT_CAINFO, st->cacert);
377 if (st->ssl_ciphers != NULL)
378 curl_easy_setopt (st->curl, CURLOPT_SSL_CIPHER_LIST,st->ssl_ciphers);
380 #ifdef HAVE_CURLOPT_TIMEOUT_MS
381 if (st->timeout >= 0)
382 curl_easy_setopt (st->curl, CURLOPT_TIMEOUT_MS, (long) st->timeout);
384 curl_easy_setopt (st->curl, CURLOPT_TIMEOUT_MS, (long) CDTIME_T_TO_MS(plugin_get_interval()));
388 } /* }}} int init_host */
390 static void submit_value (const char *type, const char *type_instance,
391 value_t value, apache_t *st)
393 value_list_t vl = VALUE_LIST_INIT;
398 sstrncpy (vl.host, (st->host != NULL) ? st->host : hostname_g,
401 sstrncpy (vl.plugin, "apache", sizeof (vl.plugin));
402 if (st->name != NULL)
403 sstrncpy (vl.plugin_instance, st->name,
404 sizeof (vl.plugin_instance));
406 sstrncpy (vl.type, type, sizeof (vl.type));
407 if (type_instance != NULL)
408 sstrncpy (vl.type_instance, type_instance,
409 sizeof (vl.type_instance));
411 plugin_dispatch_values (&vl);
412 } /* void submit_value */
414 static void submit_derive (const char *type, const char *type_instance,
415 derive_t c, apache_t *st)
419 submit_value (type, type_instance, v, st);
420 } /* void submit_derive */
422 static void submit_gauge (const char *type, const char *type_instance,
423 gauge_t g, apache_t *st)
427 submit_value (type, type_instance, v, st);
428 } /* void submit_gauge */
430 static void submit_scoreboard (char *buf, apache_t *st)
434 * "_" Waiting for Connection, "S" Starting up,
435 * "R" Reading Request for apache and read-POST for lighttpd,
436 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
437 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
438 * "I" Idle cleanup of worker, "." Open slot with no current process
439 * Lighttpd specific legends -
440 * "E" hard error, "." connect, "h" handle-request,
441 * "q" request-start, "Q" request-end, "s" response-start
442 * "S" response-end, "r" read
444 long long open = 0LL;
445 long long waiting = 0LL;
446 long long starting = 0LL;
447 long long reading = 0LL;
448 long long sending = 0LL;
449 long long keepalive = 0LL;
450 long long dnslookup = 0LL;
451 long long closing = 0LL;
452 long long logging = 0LL;
453 long long finishing = 0LL;
454 long long idle_cleanup = 0LL;
456 /* lighttpd specific */
457 long long hard_error = 0LL;
458 long long lighttpd_read = 0LL;
459 long long handle_request = 0LL;
460 long long request_start = 0LL;
461 long long request_end = 0LL;
462 long long response_start = 0LL;
463 long long response_end = 0LL;
465 for (int i = 0; buf[i] != '\0'; i++)
467 if (buf[i] == '.') open++;
468 else if (buf[i] == '_') waiting++;
469 else if (buf[i] == 'S') starting++;
470 else if (buf[i] == 'R') reading++;
471 else if (buf[i] == 'W') sending++;
472 else if (buf[i] == 'K') keepalive++;
473 else if (buf[i] == 'D') dnslookup++;
474 else if (buf[i] == 'C') closing++;
475 else if (buf[i] == 'L') logging++;
476 else if (buf[i] == 'G') finishing++;
477 else if (buf[i] == 'I') idle_cleanup++;
478 else if (buf[i] == 'r') lighttpd_read++;
479 else if (buf[i] == 'h') handle_request++;
480 else if (buf[i] == 'E') hard_error++;
481 else if (buf[i] == 'q') request_start++;
482 else if (buf[i] == 'Q') request_end++;
483 else if (buf[i] == 's') response_start++;
484 else if (buf[i] == 'S') response_end++;
487 if (st->server_type == APACHE)
489 submit_gauge ("apache_scoreboard", "open" , open, st);
490 submit_gauge ("apache_scoreboard", "waiting" , waiting, st);
491 submit_gauge ("apache_scoreboard", "starting" , starting, st);
492 submit_gauge ("apache_scoreboard", "reading" , reading, st);
493 submit_gauge ("apache_scoreboard", "sending" , sending, st);
494 submit_gauge ("apache_scoreboard", "keepalive", keepalive, st);
495 submit_gauge ("apache_scoreboard", "dnslookup", dnslookup, st);
496 submit_gauge ("apache_scoreboard", "closing" , closing, st);
497 submit_gauge ("apache_scoreboard", "logging" , logging, st);
498 submit_gauge ("apache_scoreboard", "finishing", finishing, st);
499 submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup, st);
503 submit_gauge ("apache_scoreboard", "connect" , open, st);
504 submit_gauge ("apache_scoreboard", "close" , closing, st);
505 submit_gauge ("apache_scoreboard", "hard_error" , hard_error, st);
506 submit_gauge ("apache_scoreboard", "read" , lighttpd_read, st);
507 submit_gauge ("apache_scoreboard", "read_post" , reading, st);
508 submit_gauge ("apache_scoreboard", "write" , sending, st);
509 submit_gauge ("apache_scoreboard", "handle_request", handle_request, st);
510 submit_gauge ("apache_scoreboard", "request_start" , request_start, st);
511 submit_gauge ("apache_scoreboard", "request_end" , request_end, st);
512 submit_gauge ("apache_scoreboard", "response_start", response_start, st);
513 submit_gauge ("apache_scoreboard", "response_end" , response_end, st);
517 static int apache_read_host (user_data_t *user_data) /* {{{ */
528 st = user_data->data;
533 static const char *text_plain = "text/plain";
535 assert (st->url != NULL);
536 /* (Assured by `config_add') */
538 if (st->curl == NULL)
540 status = init_host (st);
544 assert (st->curl != NULL);
546 st->apache_buffer_fill = 0;
547 if (curl_easy_perform (st->curl) != CURLE_OK)
549 ERROR ("apache: curl_easy_perform failed: %s",
550 st->apache_curl_error);
554 /* fallback - server_type to apache if not set at this time */
555 if (st->server_type == -1)
557 WARNING ("apache plugin: Unable to determine server software "
558 "automatically. Will assume Apache.");
559 st->server_type = APACHE;
562 status = curl_easy_getinfo (st->curl, CURLINFO_CONTENT_TYPE, &content_type);
563 if ((status == CURLE_OK) && (content_type != NULL) &&
564 (strncasecmp (content_type, text_plain, strlen (text_plain)) != 0))
566 WARNING ("apache plugin: `Content-Type' response header is not `%s' "
567 "(received: `%s'). Expecting unparseable data. Please check `URL' "
568 "parameter (missing `?auto' suffix ?)",
569 text_plain, content_type);
572 ptr = st->apache_buffer;
574 while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
577 fields_num = strsplit (line, fields, STATIC_ARRAY_SIZE (fields));
581 if ((strcmp (fields[0], "Total") == 0) && (strcmp (fields[1], "Accesses:") == 0))
582 submit_derive ("apache_requests", "", atoll (fields[2]), st);
583 else if ((strcmp (fields[0], "Total") == 0) && (strcmp (fields[1], "kBytes:") == 0))
584 submit_derive ("apache_bytes", "", 1024LL * atoll (fields[2]), st);
586 else if (fields_num == 2)
588 if (strcmp (fields[0], "Scoreboard:") == 0)
589 submit_scoreboard (fields[1], st);
590 else if ((strcmp (fields[0], "BusyServers:") == 0) /* Apache 1.* */
591 || (strcmp (fields[0], "BusyWorkers:") == 0) /* Apache 2.* */)
592 submit_gauge ("apache_connections", NULL, atol (fields[1]), st);
593 else if ((strcmp (fields[0], "IdleServers:") == 0) /* Apache 1.x */
594 || (strcmp (fields[0], "IdleWorkers:") == 0) /* Apache 2.x */)
595 submit_gauge ("apache_idle_workers", NULL, atol (fields[1]), st);
599 st->apache_buffer_fill = 0;
602 } /* }}} int apache_read_host */
604 static int apache_init (void) /* {{{ */
606 /* Call this while collectd is still single-threaded to avoid
607 * initialization issues in libgcrypt. */
608 curl_global_init (CURL_GLOBAL_SSL);
610 } /* }}} int apache_init */
612 void module_register (void)
614 plugin_register_complex_config ("apache", config);
615 plugin_register_init ("apache", apache_init);
616 } /* void module_register */
618 /* vim: set sw=8 noet fdm=marker : */