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>
30 #include "configfile.h"
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;
60 typedef struct apache_s apache_t;
62 /* TODO: Remove this prototype */
63 static int apache_read_host (user_data_t *user_data);
65 static void apache_free (apache_t *st)
76 sfree (st->ssl_ciphers);
78 sfree (st->apache_buffer);
80 curl_easy_cleanup(st->curl);
85 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb,
88 size_t len = size * nmemb;
94 ERROR ("apache plugin: apache_curl_callback: "
95 "user_data pointer is NULL.");
102 if ((st->apache_buffer_fill + len) >= st->apache_buffer_size)
106 temp = (char *) realloc (st->apache_buffer,
107 st->apache_buffer_fill + len + 1);
110 ERROR ("apache plugin: realloc failed.");
113 st->apache_buffer = temp;
114 st->apache_buffer_size = st->apache_buffer_fill + len + 1;
117 memcpy (st->apache_buffer + st->apache_buffer_fill, (char *) buf, len);
118 st->apache_buffer_fill += len;
119 st->apache_buffer[st->apache_buffer_fill] = 0;
122 } /* int apache_curl_callback */
124 static size_t apache_header_callback (void *buf, size_t size, size_t nmemb,
127 size_t len = size * nmemb;
133 ERROR ("apache plugin: apache_header_callback: "
134 "user_data pointer is NULL.");
141 /* look for the Server header */
142 if (strncasecmp (buf, "Server: ", strlen ("Server: ")) != 0)
145 if (strstr (buf, "Apache") != NULL)
146 st->server_type = APACHE;
147 else if (strstr (buf, "lighttpd") != NULL)
148 st->server_type = LIGHTTPD;
149 else if (strstr (buf, "IBM_HTTP_Server") != NULL)
150 st->server_type = APACHE;
153 const char *hdr = buf;
155 hdr += strlen ("Server: ");
156 NOTICE ("apache plugin: Unknown server software: %s", hdr);
160 } /* apache_header_callback */
162 /* Configuration handling functiions
164 * <Instance "instance_name">
170 static int config_add (oconfig_item_t *ci)
176 st = malloc (sizeof (*st));
179 ERROR ("apache plugin: malloc failed.");
182 memset (st, 0, sizeof (*st));
184 status = cf_util_get_string (ci, &st->name);
190 assert (st->name != NULL);
192 for (i = 0; i < ci->children_num; i++)
194 oconfig_item_t *child = ci->children + i;
196 if (strcasecmp ("URL", child->key) == 0)
197 status = cf_util_get_string (child, &st->url);
198 else if (strcasecmp ("Host", child->key) == 0)
199 status = cf_util_get_string (child, &st->host);
200 else if (strcasecmp ("User", child->key) == 0)
201 status = cf_util_get_string (child, &st->user);
202 else if (strcasecmp ("Password", child->key) == 0)
203 status = cf_util_get_string (child, &st->pass);
204 else if (strcasecmp ("VerifyPeer", child->key) == 0)
205 status = cf_util_get_boolean (child, &st->verify_peer);
206 else if (strcasecmp ("VerifyHost", child->key) == 0)
207 status = cf_util_get_boolean (child, &st->verify_host);
208 else if (strcasecmp ("CACert", child->key) == 0)
209 status = cf_util_get_string (child, &st->cacert);
210 else if (strcasecmp ("SSLCiphers", child->key) == 0)
211 status = cf_util_get_string (child, &st->ssl_ciphers);
212 else if (strcasecmp ("Server", child->key) == 0)
213 status = cf_util_get_string (child, &st->server);
216 WARNING ("apache plugin: Option `%s' not allowed here.",
225 /* Check if struct is complete.. */
226 if ((status == 0) && (st->url == NULL))
228 ERROR ("apache plugin: Instance `%s': "
229 "No URL has been configured.",
237 char callback_name[3*DATA_MAX_NAME_LEN];
239 memset (&ud, 0, sizeof (ud));
241 ud.free_func = (void *) apache_free;
243 memset (callback_name, 0, sizeof (callback_name));
244 ssnprintf (callback_name, sizeof (callback_name),
246 (st->host != NULL) ? st->host : hostname_g,
247 (st->name != NULL) ? st->name : "default"),
249 status = plugin_register_complex_read (/* group = */ NULL,
250 /* name = */ callback_name,
251 /* callback = */ apache_read_host,
252 /* interval = */ NULL,
253 /* user_data = */ &ud);
263 } /* int config_add */
265 static int config (oconfig_item_t *ci)
270 for (i = 0; i < ci->children_num; i++)
272 oconfig_item_t *child = ci->children + i;
274 if (strcasecmp ("Instance", child->key) == 0)
277 WARNING ("apache plugin: The configuration option "
278 "\"%s\" is not allowed here. Did you "
279 "forget to add an <Instance /> block "
280 "around the configuration?",
282 } /* for (ci->children) */
287 /* initialize curl for each host */
288 static int init_host (apache_t *st) /* {{{ */
290 static char credentials[1024];
292 assert (st->url != NULL);
293 /* (Assured by `config_add') */
295 if (st->curl != NULL)
297 curl_easy_cleanup (st->curl);
301 if ((st->curl = curl_easy_init ()) == NULL)
303 ERROR ("apache plugin: init_host: `curl_easy_init' failed.");
307 curl_easy_setopt (st->curl, CURLOPT_NOSIGNAL, 1L);
308 curl_easy_setopt (st->curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
309 curl_easy_setopt (st->curl, CURLOPT_WRITEDATA, st);
311 /* not set as yet if the user specified string doesn't match apache or
312 * lighttpd, then ignore it. Headers will be parsed to find out the
314 st->server_type = -1;
316 if (st->server != NULL)
318 if (strcasecmp(st->server, "apache") == 0)
319 st->server_type = APACHE;
320 else if (strcasecmp(st->server, "lighttpd") == 0)
321 st->server_type = LIGHTTPD;
322 else if (strcasecmp(st->server, "ibm_http_server") == 0)
323 st->server_type = APACHE;
325 WARNING ("apache plugin: Unknown `Server' setting: %s",
329 /* if not found register a header callback to determine the server_type */
330 if (st->server_type == -1)
332 curl_easy_setopt (st->curl, CURLOPT_HEADERFUNCTION, apache_header_callback);
333 curl_easy_setopt (st->curl, CURLOPT_WRITEHEADER, st);
336 curl_easy_setopt (st->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
337 curl_easy_setopt (st->curl, CURLOPT_ERRORBUFFER, st->apache_curl_error);
339 if (st->user != NULL)
343 status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
344 st->user, (st->pass == NULL) ? "" : st->pass);
345 if ((status < 0) || ((size_t) status >= sizeof (credentials)))
347 ERROR ("apache plugin: init_host: Returning an error "
348 "because the credentials have been "
350 curl_easy_cleanup (st->curl);
355 curl_easy_setopt (st->curl, CURLOPT_USERPWD, credentials);
358 curl_easy_setopt (st->curl, CURLOPT_URL, st->url);
359 curl_easy_setopt (st->curl, CURLOPT_FOLLOWLOCATION, 1L);
360 curl_easy_setopt (st->curl, CURLOPT_MAXREDIRS, 50L);
362 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER,
363 (long) st->verify_peer);
364 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST,
365 st->verify_host ? 2L : 0L);
366 if (st->cacert != NULL)
367 curl_easy_setopt (st->curl, CURLOPT_CAINFO, st->cacert);
368 if (st->ssl_ciphers != NULL)
369 curl_easy_setopt (st->curl, CURLOPT_SSL_CIPHER_LIST,st->ssl_ciphers);
372 } /* }}} int init_host */
374 static void submit_value (const char *type, const char *type_instance,
375 value_t value, apache_t *st)
377 value_list_t vl = VALUE_LIST_INIT;
382 sstrncpy (vl.host, (st->host != NULL) ? st->host : hostname_g,
385 sstrncpy (vl.plugin, "apache", sizeof (vl.plugin));
386 if (st->name != NULL)
387 sstrncpy (vl.plugin_instance, st->name,
388 sizeof (vl.plugin_instance));
390 sstrncpy (vl.type, type, sizeof (vl.type));
391 if (type_instance != NULL)
392 sstrncpy (vl.type_instance, type_instance,
393 sizeof (vl.type_instance));
395 plugin_dispatch_values (&vl);
396 } /* void submit_value */
398 static void submit_derive (const char *type, const char *type_instance,
399 derive_t c, apache_t *st)
403 submit_value (type, type_instance, v, st);
404 } /* void submit_derive */
406 static void submit_gauge (const char *type, const char *type_instance,
407 gauge_t g, apache_t *st)
411 submit_value (type, type_instance, v, st);
412 } /* void submit_gauge */
414 static void submit_scoreboard (char *buf, apache_t *st)
418 * "_" Waiting for Connection, "S" Starting up,
419 * "R" Reading Request for apache and read-POST for lighttpd,
420 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
421 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
422 * "I" Idle cleanup of worker, "." Open slot with no current process
423 * Lighttpd specific legends -
424 * "E" hard error, "." connect, "h" handle-request,
425 * "q" request-start, "Q" request-end, "s" response-start
426 * "S" response-end, "r" read
428 long long open = 0LL;
429 long long waiting = 0LL;
430 long long starting = 0LL;
431 long long reading = 0LL;
432 long long sending = 0LL;
433 long long keepalive = 0LL;
434 long long dnslookup = 0LL;
435 long long closing = 0LL;
436 long long logging = 0LL;
437 long long finishing = 0LL;
438 long long idle_cleanup = 0LL;
440 /* lighttpd specific */
441 long long hard_error = 0LL;
442 long long lighttpd_read = 0LL;
443 long long handle_request = 0LL;
444 long long request_start = 0LL;
445 long long request_end = 0LL;
446 long long response_start = 0LL;
447 long long response_end = 0LL;
450 for (i = 0; buf[i] != '\0'; i++)
452 if (buf[i] == '.') open++;
453 else if (buf[i] == '_') waiting++;
454 else if (buf[i] == 'S') starting++;
455 else if (buf[i] == 'R') reading++;
456 else if (buf[i] == 'W') sending++;
457 else if (buf[i] == 'K') keepalive++;
458 else if (buf[i] == 'D') dnslookup++;
459 else if (buf[i] == 'C') closing++;
460 else if (buf[i] == 'L') logging++;
461 else if (buf[i] == 'G') finishing++;
462 else if (buf[i] == 'I') idle_cleanup++;
463 else if (buf[i] == 'r') lighttpd_read++;
464 else if (buf[i] == 'h') handle_request++;
465 else if (buf[i] == 'E') hard_error++;
466 else if (buf[i] == 'q') request_start++;
467 else if (buf[i] == 'Q') request_end++;
468 else if (buf[i] == 's') response_start++;
469 else if (buf[i] == 'S') response_end++;
472 if (st->server_type == APACHE)
474 submit_gauge ("apache_scoreboard", "open" , open, st);
475 submit_gauge ("apache_scoreboard", "waiting" , waiting, st);
476 submit_gauge ("apache_scoreboard", "starting" , starting, st);
477 submit_gauge ("apache_scoreboard", "reading" , reading, st);
478 submit_gauge ("apache_scoreboard", "sending" , sending, st);
479 submit_gauge ("apache_scoreboard", "keepalive", keepalive, st);
480 submit_gauge ("apache_scoreboard", "dnslookup", dnslookup, st);
481 submit_gauge ("apache_scoreboard", "closing" , closing, st);
482 submit_gauge ("apache_scoreboard", "logging" , logging, st);
483 submit_gauge ("apache_scoreboard", "finishing", finishing, st);
484 submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup, st);
488 submit_gauge ("apache_scoreboard", "connect" , open, st);
489 submit_gauge ("apache_scoreboard", "close" , closing, st);
490 submit_gauge ("apache_scoreboard", "hard_error" , hard_error, st);
491 submit_gauge ("apache_scoreboard", "read" , lighttpd_read, st);
492 submit_gauge ("apache_scoreboard", "read_post" , reading, st);
493 submit_gauge ("apache_scoreboard", "write" , sending, st);
494 submit_gauge ("apache_scoreboard", "handle_request", handle_request, st);
495 submit_gauge ("apache_scoreboard", "request_start" , request_start, st);
496 submit_gauge ("apache_scoreboard", "request_end" , request_end, st);
497 submit_gauge ("apache_scoreboard", "response_start", response_start, st);
498 submit_gauge ("apache_scoreboard", "response_end" , response_end, st);
502 static int apache_read_host (user_data_t *user_data) /* {{{ */
516 st = user_data->data;
518 assert (st->url != NULL);
519 /* (Assured by `config_add') */
521 if (st->curl == NULL)
525 status = init_host (st);
529 assert (st->curl != NULL);
531 st->apache_buffer_fill = 0;
532 if (curl_easy_perform (st->curl) != CURLE_OK)
534 ERROR ("apache: curl_easy_perform failed: %s",
535 st->apache_curl_error);
539 /* fallback - server_type to apache if not set at this time */
540 if (st->server_type == -1)
542 WARNING ("apache plugin: Unable to determine server software "
543 "automatically. Will assume Apache.");
544 st->server_type = APACHE;
547 ptr = st->apache_buffer;
549 while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
558 for (i = 0; i < lines_num; i++)
560 fields_num = strsplit (lines[i], fields, 4);
564 if ((strcmp (fields[0], "Total") == 0)
565 && (strcmp (fields[1], "Accesses:") == 0))
566 submit_derive ("apache_requests", "",
567 atoll (fields[2]), st);
568 else if ((strcmp (fields[0], "Total") == 0)
569 && (strcmp (fields[1], "kBytes:") == 0))
570 submit_derive ("apache_bytes", "",
571 1024LL * atoll (fields[2]), st);
573 else if (fields_num == 2)
575 if (strcmp (fields[0], "Scoreboard:") == 0)
576 submit_scoreboard (fields[1], st);
577 else if ((strcmp (fields[0], "BusyServers:") == 0) /* Apache 1.* */
578 || (strcmp (fields[0], "BusyWorkers:") == 0) /* Apache 2.* */)
579 submit_gauge ("apache_connections", NULL, atol (fields[1]), st);
580 else if ((strcmp (fields[0], "IdleServers:") == 0) /* Apache 1.x */
581 || (strcmp (fields[0], "IdleWorkers:") == 0) /* Apache 2.x */)
582 submit_gauge ("apache_idle_workers", NULL, atol (fields[1]), st);
586 st->apache_buffer_fill = 0;
589 } /* }}} int apache_read_host */
591 static int apache_init (void) /* {{{ */
593 /* Call this while collectd is still single-threaded to avoid
594 * initialization issues in libgcrypt. */
595 curl_global_init (CURL_GLOBAL_SSL);
597 } /* }}} int apache_init */
599 void module_register (void)
601 plugin_register_complex_config ("apache", config);
602 plugin_register_init ("apache", apache_init);
603 } /* void module_register */
605 /* vim: set sw=8 noet fdm=marker : */