2 * collectd - src/apache.c
3 * Copyright (C) 2006-2009 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>
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)
77 sfree (st->apache_buffer);
79 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 ((strstr(buf, "Server: ") != NULL) &&
142 (strstr(buf, "lighttpd") != NULL)) {
143 st->server_type = LIGHTTPD;
147 } /* apache_header_callback */
149 /* Configuration handling functiions
151 * <Instance "instance_name">
157 static int config_set_string (char **ret_string, /* {{{ */
162 if ((ci->values_num != 1)
163 || (ci->values[0].type != OCONFIG_TYPE_STRING))
165 WARNING ("apache plugin: The `%s' config option "
166 "needs exactly one string argument.", ci->key);
170 string = strdup (ci->values[0].value.string);
173 ERROR ("apache plugin: strdup failed.");
177 if (*ret_string != NULL)
179 *ret_string = string;
182 } /* }}} int config_set_string */
184 static int config_set_boolean (int *ret_boolean, /* {{{ */
187 if ((ci->values_num != 1)
188 || ((ci->values[0].type != OCONFIG_TYPE_BOOLEAN)
189 && (ci->values[0].type != OCONFIG_TYPE_STRING)))
191 WARNING ("apache plugin: The `%s' config option "
192 "needs exactly one boolean argument.", ci->key);
196 if (ci->values[0].type != OCONFIG_TYPE_BOOLEAN)
198 if (ci->values[0].value.boolean)
203 else /* if (ci->values[0].type != OCONFIG_TYPE_STRING) */
205 char *string = ci->values[0].value.string;
206 if ((strcasecmp ("true", string) == 0)
207 || (strcasecmp ("yes", string) == 0)
208 || (strcasecmp ("on", string) == 0))
210 else if ((strcasecmp ("false", string) == 0)
211 || (strcasecmp ("no", string) == 0)
212 || (strcasecmp ("off", string) == 0))
216 ERROR ("apache plugin: Cannot parse string "
217 "as boolean value: %s", string);
223 } /* }}} int config_set_boolean */
225 static int config_add (oconfig_item_t *ci)
231 if ((ci->values_num != 1)
232 || (ci->values[0].type != OCONFIG_TYPE_STRING))
234 WARNING ("apache plugin: The `%s' config option "
235 "needs exactly one string argument.", ci->key);
239 st = (apache_t *) malloc (sizeof (*st));
242 ERROR ("apache plugin: malloc failed.");
246 memset (st, 0, sizeof (*st));
248 status = config_set_string (&st->name, ci);
254 assert (st->name != NULL);
256 for (i = 0; i < ci->children_num; i++)
258 oconfig_item_t *child = ci->children + i;
260 if (strcasecmp ("URL", child->key) == 0)
261 status = config_set_string (&st->url, child);
262 else if (strcasecmp ("Host", child->key) == 0)
263 status = config_set_string (&st->host, child);
264 else if (strcasecmp ("User", child->key) == 0)
265 status = config_set_string (&st->user, child);
266 else if (strcasecmp ("Password", child->key) == 0)
267 status = config_set_string (&st->pass, child);
268 else if (strcasecmp ("VerifyPeer", child->key) == 0)
269 status = config_set_boolean (&st->verify_peer, child);
270 else if (strcasecmp ("VerifyHost", child->key) == 0)
271 status = config_set_boolean (&st->verify_host, child);
272 else if (strcasecmp ("CACert", child->key) == 0)
273 status = config_set_string (&st->cacert, child);
274 else if (strcasecmp ("Server", child->key) == 0)
275 status = config_set_string (&st->server, child);
278 WARNING ("apache plugin: Option `%s' not allowed here.",
287 /* Check if struct is complete.. */
288 if ((status == 0) && (st->url == NULL))
290 ERROR ("apache plugin: Instance `%s': "
291 "No URL has been configured.",
299 char callback_name[3*DATA_MAX_NAME_LEN];
301 memset (&ud, 0, sizeof (ud));
303 ud.free_func = (void *) apache_free;
305 memset (callback_name, 0, sizeof (callback_name));
306 ssnprintf (callback_name, sizeof (callback_name),
308 (st->host != NULL) ? st->host : hostname_g,
309 (st->name != NULL) ? st->name : "default"),
311 status = plugin_register_complex_read (callback_name,
312 /* callback = */ apache_read_host,
313 /* interval = */ NULL,
314 /* user_data = */ &ud);
324 } /* int config_add */
326 static int config (oconfig_item_t *ci)
330 oconfig_item_t *lci = NULL; /* legacy config */
332 for (i = 0; i < ci->children_num; i++)
334 oconfig_item_t *child = ci->children + i;
336 if (strcasecmp ("Instance", child->key) == 0 && child->children_num > 0)
340 /* legacy mode - convert to <Instance ...> config */
343 lci = malloc (sizeof(*lci));
346 ERROR ("apache plugin: malloc failed.");
349 memset (lci, '\0', sizeof (*lci));
354 realloc (lci->children,
355 lci->children_num * sizeof (*child));
356 if (lci->children == NULL)
358 ERROR ("apache plugin: realloc failed.");
361 memcpy (&lci->children[lci->children_num-1], child, sizeof (*child));
363 } /* for (ci->children) */
367 /* create a <Instance ""> entry */
368 lci->key = "Instance";
370 lci->values = (oconfig_value_t *) malloc (lci->values_num * sizeof (oconfig_value_t));
371 lci->values[0].type = OCONFIG_TYPE_STRING;
372 lci->values[0].value.string = "";
374 status = config_add (lci);
376 sfree (lci->children);
383 /* initialize curl for each host */
384 static int init_host (apache_t *st) /* {{{ */
386 static char credentials[1024];
388 assert (st->url != NULL);
389 /* (Assured by `config_add') */
391 if (st->curl != NULL)
393 curl_easy_cleanup (st->curl);
397 if ((st->curl = curl_easy_init ()) == NULL)
399 ERROR ("apache plugin: init_host: `curl_easy_init' failed.");
403 curl_easy_setopt (st->curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
404 curl_easy_setopt (st->curl, CURLOPT_WRITEDATA, st);
406 st->server_type = -1; // not set as yet
407 // if the user specified string doesn't match apache or lighttpd, then
408 // ignore it. Headers will be parsed to find out the server type
409 if (st->server != NULL)
411 if (strcasecmp(st->server, "apache") == 0)
412 st->server_type = APACHE;
413 else if (strcasecmp(st->server, "lighttpd") == 0)
414 st->server_type = LIGHTTPD;
417 // if not found register a header callback to determine the
419 if (st->server_type == -1)
421 curl_easy_setopt (st->curl, CURLOPT_HEADERFUNCTION, apache_header_callback);
422 curl_easy_setopt (st->curl, CURLOPT_WRITEHEADER, st);
425 curl_easy_setopt (st->curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
426 curl_easy_setopt (st->curl, CURLOPT_ERRORBUFFER, st->apache_curl_error);
428 if (st->user != NULL)
432 status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
433 st->user, (st->pass == NULL) ? "" : st->pass);
434 if ((status < 0) || ((size_t) status >= sizeof (credentials)))
436 ERROR ("apache plugin: init_host: Returning an error "
437 "because the credentials have been "
439 curl_easy_cleanup (st->curl);
444 curl_easy_setopt (st->curl, CURLOPT_USERPWD, credentials);
447 curl_easy_setopt (st->curl, CURLOPT_URL, st->url);
449 if (st->verify_peer != 0)
451 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER, 1);
455 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER, 0);
458 if (st->verify_host != 0)
460 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST, 2);
464 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST, 0);
467 if (st->cacert != NULL)
469 curl_easy_setopt (st->curl, CURLOPT_CAINFO, st->cacert);
473 } /* }}} int init_host */
475 static void submit_value (const char *type, const char *type_instance,
476 value_t value, apache_t *st)
478 value_list_t vl = VALUE_LIST_INIT;
483 sstrncpy (vl.host, (st->host != NULL) ? st->host : hostname_g,
486 sstrncpy (vl.plugin, "apache", sizeof (vl.plugin));
487 if (st->name != NULL)
488 sstrncpy (vl.plugin_instance, st->name,
489 sizeof (vl.plugin_instance));
491 sstrncpy (vl.type, type, sizeof (vl.type));
492 if (type_instance != NULL)
493 sstrncpy (vl.type_instance, type_instance,
494 sizeof (vl.type_instance));
496 plugin_dispatch_values (&vl);
497 } /* void submit_value */
499 static void submit_counter (const char *type, const char *type_instance,
500 counter_t c, apache_t *st)
504 submit_value (type, type_instance, v, st);
505 } /* void submit_counter */
507 static void submit_gauge (const char *type, const char *type_instance,
508 gauge_t g, apache_t *st)
512 submit_value (type, type_instance, v, st);
513 } /* void submit_gauge */
515 static void submit_scoreboard (char *buf, apache_t *st)
519 * "_" Waiting for Connection, "S" Starting up,
520 * "R" Reading Request for apache and read-POST for lighttpd,
521 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
522 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
523 * "I" Idle cleanup of worker, "." Open slot with no current process
524 * Lighttpd specific legends -
525 * "E" hard error, "." connect, "h" handle-request,
526 * "q" request-start, "Q" request-end, "s" response-start
527 * "S" response-end, "r" read
529 long long open = 0LL;
530 long long waiting = 0LL;
531 long long starting = 0LL;
532 long long reading = 0LL;
533 long long sending = 0LL;
534 long long keepalive = 0LL;
535 long long dnslookup = 0LL;
536 long long closing = 0LL;
537 long long logging = 0LL;
538 long long finishing = 0LL;
539 long long idle_cleanup = 0LL;
541 /* lighttpd specific */
542 long long hard_error = 0LL;
543 long long lighttpd_read = 0LL;
544 long long handle_request = 0LL;
545 long long request_start = 0LL;
546 long long request_end = 0LL;
547 long long response_start = 0LL;
548 long long response_end = 0LL;
551 for (i = 0; buf[i] != '\0'; i++)
553 if (buf[i] == '.') open++;
554 else if (buf[i] == '_') waiting++;
555 else if (buf[i] == 'S') starting++;
556 else if (buf[i] == 'R') reading++;
557 else if (buf[i] == 'W') sending++;
558 else if (buf[i] == 'K') keepalive++;
559 else if (buf[i] == 'D') dnslookup++;
560 else if (buf[i] == 'C') closing++;
561 else if (buf[i] == 'L') logging++;
562 else if (buf[i] == 'G') finishing++;
563 else if (buf[i] == 'I') idle_cleanup++;
564 else if (buf[i] == 'r') lighttpd_read++;
565 else if (buf[i] == 'h') handle_request++;
566 else if (buf[i] == 'E') hard_error++;
567 else if (buf[i] == 'q') request_start++;
568 else if (buf[i] == 'Q') request_end++;
569 else if (buf[i] == 's') response_start++;
570 else if (buf[i] == 'S') response_end++;
573 if (st->server_type == APACHE)
575 submit_gauge ("apache_scoreboard", "open" , open, st);
576 submit_gauge ("apache_scoreboard", "waiting" , waiting, st);
577 submit_gauge ("apache_scoreboard", "starting" , starting, st);
578 submit_gauge ("apache_scoreboard", "reading" , reading, st);
579 submit_gauge ("apache_scoreboard", "sending" , sending, st);
580 submit_gauge ("apache_scoreboard", "keepalive", keepalive, st);
581 submit_gauge ("apache_scoreboard", "dnslookup", dnslookup, st);
582 submit_gauge ("apache_scoreboard", "closing" , closing, st);
583 submit_gauge ("apache_scoreboard", "logging" , logging, st);
584 submit_gauge ("apache_scoreboard", "finishing", finishing, st);
585 submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup, st);
588 submit_gauge ("apache_scoreboard", "connect" , open, st);
589 submit_gauge ("apache_scoreboard", "close" , closing, st);
590 submit_gauge ("apache_scoreboard", "hard_error" , hard_error, st);
591 submit_gauge ("apache_scoreboard", "read" , lighttpd_read, st);
592 submit_gauge ("apache_scoreboard", "read_post" , reading, st);
593 submit_gauge ("apache_scoreboard", "write" , sending, st);
594 submit_gauge ("apache_scoreboard", "handle_request", handle_request, st);
595 submit_gauge ("apache_scoreboard", "request_start" , request_start, st);
596 submit_gauge ("apache_scoreboard", "request_end" , request_end, st);
597 submit_gauge ("apache_scoreboard", "response_start", response_start, st);
598 submit_gauge ("apache_scoreboard", "response_end" , response_end, st);
602 static int apache_read_host (user_data_t *user_data) /* {{{ */
616 st = user_data->data;
618 assert (st->url != NULL);
619 /* (Assured by `config_add') */
621 if (st->curl == NULL)
625 status = init_host (st);
629 assert (st->curl != NULL);
631 st->apache_buffer_fill = 0;
632 if (curl_easy_perform (st->curl) != 0)
634 ERROR ("apache: curl_easy_perform failed: %s",
635 st->apache_curl_error);
639 // fallback - server_type to apache if not set at this time
640 if (st->server_type == -1)
641 st->server_type = APACHE;
643 ptr = st->apache_buffer;
645 while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
654 for (i = 0; i < lines_num; i++)
656 fields_num = strsplit (lines[i], fields, 4);
660 if ((strcmp (fields[0], "Total") == 0)
661 && (strcmp (fields[1], "Accesses:") == 0))
662 submit_counter ("apache_requests", "",
663 atoll (fields[2]), st);
664 else if ((strcmp (fields[0], "Total") == 0)
665 && (strcmp (fields[1], "kBytes:") == 0))
666 submit_counter ("apache_bytes", "",
667 1024LL * atoll (fields[2]), st);
669 else if (fields_num == 2)
671 if (strcmp (fields[0], "Scoreboard:") == 0)
672 submit_scoreboard (fields[1], st);
673 else if (strcmp (fields[0], "BusyServers:") == 0)
674 submit_gauge ("apache_connections", NULL, atol (fields[1]), st);
678 st->apache_buffer_fill = 0;
681 } /* }}} int apache_read_host */
683 void module_register (void)
685 plugin_register_complex_config ("apache", config);
686 } /* void module_register */
688 /* vim: set sw=8 noet fdm=marker : */