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>
34 enum server_enum { APACHE = 0, LIGHTTPD };
47 char *server; /* user specific server type */
49 char apache_curl_error[CURL_ERROR_SIZE];
50 size_t apache_buffer_size;
51 size_t apache_buffer_fill;
56 typedef struct apache_s apache_t;
58 /* TODO: Remove this prototype */
59 static int apache_read_host(user_data_t *user_data);
61 static void apache_free(void *arg) {
73 sfree(st->ssl_ciphers);
75 sfree(st->apache_buffer);
77 curl_easy_cleanup(st->curl);
83 static size_t apache_curl_callback(void *buf, size_t size, size_t nmemb,
85 apache_t *st = user_data;
87 ERROR("apache plugin: apache_curl_callback: "
88 "user_data pointer is NULL.");
92 size_t len = size * nmemb;
96 if ((st->apache_buffer_fill + len) >= st->apache_buffer_size) {
97 char *temp = realloc(st->apache_buffer, st->apache_buffer_fill + len + 1);
99 ERROR("apache plugin: realloc failed.");
102 st->apache_buffer = temp;
103 st->apache_buffer_size = st->apache_buffer_fill + len + 1;
106 memcpy(st->apache_buffer + st->apache_buffer_fill, (char *)buf, len);
107 st->apache_buffer_fill += len;
108 st->apache_buffer[st->apache_buffer_fill] = 0;
111 } /* int apache_curl_callback */
113 static size_t apache_header_callback(void *buf, size_t size, size_t nmemb,
115 apache_t *st = user_data;
117 ERROR("apache plugin: apache_header_callback: "
118 "user_data pointer is NULL.");
122 size_t len = size * nmemb;
126 /* look for the Server header */
127 if (strncasecmp(buf, "Server: ", strlen("Server: ")) != 0)
130 if (strstr(buf, "Apache") != NULL)
131 st->server_type = APACHE;
132 else if (strstr(buf, "lighttpd") != NULL)
133 st->server_type = LIGHTTPD;
134 else if (strstr(buf, "IBM_HTTP_Server") != NULL)
135 st->server_type = APACHE;
137 const char *hdr = buf;
139 hdr += strlen("Server: ");
140 NOTICE("apache plugin: Unknown server software: %s", hdr);
144 } /* apache_header_callback */
146 /* Configuration handling functiions
148 * <Instance "instance_name">
154 static int config_add(oconfig_item_t *ci) {
155 apache_t *st = calloc(1, sizeof(*st));
157 ERROR("apache plugin: calloc failed.");
163 int status = cf_util_get_string(ci, &st->name);
168 assert(st->name != NULL);
170 for (int i = 0; i < ci->children_num; i++) {
171 oconfig_item_t *child = ci->children + i;
173 if (strcasecmp("URL", child->key) == 0)
174 status = cf_util_get_string(child, &st->url);
175 else if (strcasecmp("Host", child->key) == 0)
176 status = cf_util_get_string(child, &st->host);
177 else if (strcasecmp("User", child->key) == 0)
178 status = cf_util_get_string(child, &st->user);
179 else if (strcasecmp("Password", child->key) == 0)
180 status = cf_util_get_string(child, &st->pass);
181 else if (strcasecmp("VerifyPeer", child->key) == 0)
182 status = cf_util_get_boolean(child, &st->verify_peer);
183 else if (strcasecmp("VerifyHost", child->key) == 0)
184 status = cf_util_get_boolean(child, &st->verify_host);
185 else if (strcasecmp("CACert", child->key) == 0)
186 status = cf_util_get_string(child, &st->cacert);
187 else if (strcasecmp("SSLCiphers", child->key) == 0)
188 status = cf_util_get_string(child, &st->ssl_ciphers);
189 else if (strcasecmp("Server", child->key) == 0)
190 status = cf_util_get_string(child, &st->server);
191 else if (strcasecmp("Timeout", child->key) == 0)
192 status = cf_util_get_int(child, &st->timeout);
194 WARNING("apache plugin: Option `%s' not allowed here.", child->key);
202 /* Check if struct is complete.. */
203 if ((status == 0) && (st->url == NULL)) {
204 ERROR("apache plugin: Instance `%s': "
205 "No URL has been configured.",
215 char callback_name[3 * DATA_MAX_NAME_LEN];
217 snprintf(callback_name, sizeof(callback_name), "apache/%s/%s",
218 (st->host != NULL) ? st->host : hostname_g,
219 (st->name != NULL) ? st->name : "default");
221 return plugin_register_complex_read(
223 /* name = */ callback_name,
224 /* callback = */ apache_read_host,
228 .free_func = apache_free,
230 } /* int config_add */
232 static int config(oconfig_item_t *ci) {
233 for (int i = 0; i < ci->children_num; i++) {
234 oconfig_item_t *child = ci->children + i;
236 if (strcasecmp("Instance", child->key) == 0)
239 WARNING("apache plugin: The configuration option "
240 "\"%s\" is not allowed here. Did you "
241 "forget to add an <Instance /> block "
242 "around the configuration?",
244 } /* for (ci->children) */
249 /* initialize curl for each host */
250 static int init_host(apache_t *st) /* {{{ */
252 assert(st->url != NULL);
253 /* (Assured by `config_add') */
255 if (st->curl != NULL) {
256 curl_easy_cleanup(st->curl);
260 if ((st->curl = curl_easy_init()) == NULL) {
261 ERROR("apache plugin: init_host: `curl_easy_init' failed.");
265 curl_easy_setopt(st->curl, CURLOPT_NOSIGNAL, 1L);
266 curl_easy_setopt(st->curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
267 curl_easy_setopt(st->curl, CURLOPT_WRITEDATA, st);
269 /* not set as yet if the user specified string doesn't match apache or
270 * lighttpd, then ignore it. Headers will be parsed to find out the
272 st->server_type = -1;
274 if (st->server != NULL) {
275 if (strcasecmp(st->server, "apache") == 0)
276 st->server_type = APACHE;
277 else if (strcasecmp(st->server, "lighttpd") == 0)
278 st->server_type = LIGHTTPD;
279 else if (strcasecmp(st->server, "ibm_http_server") == 0)
280 st->server_type = APACHE;
282 WARNING("apache plugin: Unknown `Server' setting: %s", st->server);
285 /* if not found register a header callback to determine the server_type */
286 if (st->server_type == -1) {
287 curl_easy_setopt(st->curl, CURLOPT_HEADERFUNCTION, apache_header_callback);
288 curl_easy_setopt(st->curl, CURLOPT_WRITEHEADER, st);
291 curl_easy_setopt(st->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
292 curl_easy_setopt(st->curl, CURLOPT_ERRORBUFFER, st->apache_curl_error);
294 if (st->user != NULL) {
295 #ifdef HAVE_CURLOPT_USERNAME
296 curl_easy_setopt(st->curl, CURLOPT_USERNAME, st->user);
297 curl_easy_setopt(st->curl, CURLOPT_PASSWORD,
298 (st->pass == NULL) ? "" : st->pass);
300 static char credentials[1024];
301 int status = snprintf(credentials, sizeof(credentials), "%s:%s", st->user,
302 (st->pass == NULL) ? "" : st->pass);
303 if ((status < 0) || ((size_t)status >= sizeof(credentials))) {
304 ERROR("apache plugin: init_host: Returning an error "
305 "because the credentials have been "
307 curl_easy_cleanup(st->curl);
312 curl_easy_setopt(st->curl, CURLOPT_USERPWD, credentials);
316 curl_easy_setopt(st->curl, CURLOPT_FOLLOWLOCATION, 1L);
317 curl_easy_setopt(st->curl, CURLOPT_MAXREDIRS, 50L);
319 curl_easy_setopt(st->curl, CURLOPT_SSL_VERIFYPEER, (long)st->verify_peer);
320 curl_easy_setopt(st->curl, CURLOPT_SSL_VERIFYHOST, st->verify_host ? 2L : 0L);
321 if (st->cacert != NULL)
322 curl_easy_setopt(st->curl, CURLOPT_CAINFO, st->cacert);
323 if (st->ssl_ciphers != NULL)
324 curl_easy_setopt(st->curl, CURLOPT_SSL_CIPHER_LIST, st->ssl_ciphers);
326 #ifdef HAVE_CURLOPT_TIMEOUT_MS
327 if (st->timeout >= 0)
328 curl_easy_setopt(st->curl, CURLOPT_TIMEOUT_MS, (long)st->timeout);
330 curl_easy_setopt(st->curl, CURLOPT_TIMEOUT_MS,
331 (long)CDTIME_T_TO_MS(plugin_get_interval()));
335 } /* }}} int init_host */
337 static void submit_value(const char *type, const char *type_instance,
338 value_t value, apache_t *st) {
339 value_list_t vl = VALUE_LIST_INIT;
344 if (st->host != NULL)
345 sstrncpy(vl.host, st->host, sizeof(vl.host));
347 sstrncpy(vl.plugin, "apache", sizeof(vl.plugin));
348 if (st->name != NULL)
349 sstrncpy(vl.plugin_instance, st->name, sizeof(vl.plugin_instance));
351 sstrncpy(vl.type, type, sizeof(vl.type));
352 if (type_instance != NULL)
353 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
355 plugin_dispatch_values(&vl);
356 } /* void submit_value */
358 static void submit_derive(const char *type, const char *type_instance,
359 derive_t d, apache_t *st) {
360 submit_value(type, type_instance, (value_t){.derive = d}, st);
361 } /* void submit_derive */
363 static void submit_gauge(const char *type, const char *type_instance, gauge_t g,
365 submit_value(type, type_instance, (value_t){.gauge = g}, st);
366 } /* void submit_gauge */
368 static void submit_scoreboard(char *buf, apache_t *st) {
371 * "_" Waiting for Connection, "S" Starting up,
372 * "R" Reading Request for apache and read-POST for lighttpd,
373 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
374 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
375 * "I" Idle cleanup of worker, "." Open slot with no current process
376 * Lighttpd specific legends -
377 * "E" hard error, "." connect, "h" handle-request,
378 * "q" request-start, "Q" request-end, "s" response-start
379 * "S" response-end, "r" read
381 long long open = 0LL;
382 long long waiting = 0LL;
383 long long starting = 0LL;
384 long long reading = 0LL;
385 long long sending = 0LL;
386 long long keepalive = 0LL;
387 long long dnslookup = 0LL;
388 long long closing = 0LL;
389 long long logging = 0LL;
390 long long finishing = 0LL;
391 long long idle_cleanup = 0LL;
393 /* lighttpd specific */
394 long long hard_error = 0LL;
395 long long lighttpd_read = 0LL;
396 long long handle_request = 0LL;
397 long long request_start = 0LL;
398 long long request_end = 0LL;
399 long long response_start = 0LL;
400 long long response_end = 0LL;
402 for (int i = 0; buf[i] != '\0'; i++) {
405 else if (buf[i] == '_')
407 else if (buf[i] == 'S')
409 else if (buf[i] == 'R')
411 else if (buf[i] == 'W')
413 else if (buf[i] == 'K')
415 else if (buf[i] == 'D')
417 else if (buf[i] == 'C')
419 else if (buf[i] == 'L')
421 else if (buf[i] == 'G')
423 else if (buf[i] == 'I')
425 else if (buf[i] == 'r')
427 else if (buf[i] == 'h')
429 else if (buf[i] == 'E')
431 else if (buf[i] == 'q')
433 else if (buf[i] == 'Q')
435 else if (buf[i] == 's')
437 else if (buf[i] == 'S')
441 if (st->server_type == APACHE) {
442 submit_gauge("apache_scoreboard", "open", open, st);
443 submit_gauge("apache_scoreboard", "waiting", waiting, st);
444 submit_gauge("apache_scoreboard", "starting", starting, st);
445 submit_gauge("apache_scoreboard", "reading", reading, st);
446 submit_gauge("apache_scoreboard", "sending", sending, st);
447 submit_gauge("apache_scoreboard", "keepalive", keepalive, st);
448 submit_gauge("apache_scoreboard", "dnslookup", dnslookup, st);
449 submit_gauge("apache_scoreboard", "closing", closing, st);
450 submit_gauge("apache_scoreboard", "logging", logging, st);
451 submit_gauge("apache_scoreboard", "finishing", finishing, st);
452 submit_gauge("apache_scoreboard", "idle_cleanup", idle_cleanup, st);
454 submit_gauge("apache_scoreboard", "connect", open, st);
455 submit_gauge("apache_scoreboard", "close", closing, st);
456 submit_gauge("apache_scoreboard", "hard_error", hard_error, st);
457 submit_gauge("apache_scoreboard", "read", lighttpd_read, st);
458 submit_gauge("apache_scoreboard", "read_post", reading, st);
459 submit_gauge("apache_scoreboard", "write", sending, st);
460 submit_gauge("apache_scoreboard", "handle_request", handle_request, st);
461 submit_gauge("apache_scoreboard", "request_start", request_start, st);
462 submit_gauge("apache_scoreboard", "request_end", request_end, st);
463 submit_gauge("apache_scoreboard", "response_start", response_start, st);
464 submit_gauge("apache_scoreboard", "response_end", response_end, st);
468 static int apache_read_host(user_data_t *user_data) /* {{{ */
470 apache_t *st = user_data->data;
472 assert(st->url != NULL);
473 /* (Assured by `config_add') */
475 if (st->curl == NULL) {
476 if (init_host(st) != 0)
479 assert(st->curl != NULL);
481 st->apache_buffer_fill = 0;
483 curl_easy_setopt(st->curl, CURLOPT_URL, st->url);
485 if (curl_easy_perform(st->curl) != CURLE_OK) {
486 ERROR("apache: curl_easy_perform failed: %s", st->apache_curl_error);
490 /* fallback - server_type to apache if not set at this time */
491 if (st->server_type == -1) {
492 WARNING("apache plugin: Unable to determine server software "
493 "automatically. Will assume Apache.");
494 st->server_type = APACHE;
498 static const char *text_plain = "text/plain";
500 curl_easy_getinfo(st->curl, CURLINFO_CONTENT_TYPE, &content_type);
501 if ((status == CURLE_OK) && (content_type != NULL) &&
502 (strncasecmp(content_type, text_plain, strlen(text_plain)) != 0)) {
503 WARNING("apache plugin: `Content-Type' response header is not `%s' "
504 "(received: `%s'). Expecting unparseable data. Please check `URL' "
505 "parameter (missing `?auto' suffix ?)",
506 text_plain, content_type);
509 char *ptr = st->apache_buffer;
510 char *saveptr = NULL;
512 while ((line = strtok_r(ptr, "\n\r", &saveptr)) != NULL) {
516 int fields_num = strsplit(line, fields, STATIC_ARRAY_SIZE(fields));
518 if (fields_num == 3) {
519 if ((strcmp(fields[0], "Total") == 0) &&
520 (strcmp(fields[1], "Accesses:") == 0))
521 submit_derive("apache_requests", "", atoll(fields[2]), st);
522 else if ((strcmp(fields[0], "Total") == 0) &&
523 (strcmp(fields[1], "kBytes:") == 0))
524 submit_derive("apache_bytes", "", 1024LL * atoll(fields[2]), st);
525 } else if (fields_num == 2) {
526 if (strcmp(fields[0], "Scoreboard:") == 0)
527 submit_scoreboard(fields[1], st);
528 else if ((strcmp(fields[0], "BusyServers:") == 0) /* Apache 1.* */
529 || (strcmp(fields[0], "BusyWorkers:") == 0) /* Apache 2.* */)
530 submit_gauge("apache_connections", NULL, atol(fields[1]), st);
531 else if ((strcmp(fields[0], "IdleServers:") == 0) /* Apache 1.x */
532 || (strcmp(fields[0], "IdleWorkers:") == 0) /* Apache 2.x */)
533 submit_gauge("apache_idle_workers", NULL, atol(fields[1]), st);
537 st->apache_buffer_fill = 0;
540 } /* }}} int apache_read_host */
542 static int apache_init(void) /* {{{ */
544 /* Call this while collectd is still single-threaded to avoid
545 * initialization issues in libgcrypt. */
546 curl_global_init(CURL_GLOBAL_SSL);
548 } /* }}} int apache_init */
550 void module_register(void) {
551 plugin_register_complex_config("apache", config);
552 plugin_register_init("apache", apache_init);
553 } /* void module_register */