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>
51 char apache_curl_error[CURL_ERROR_SIZE];
52 size_t apache_buffer_size;
53 size_t apache_buffer_fill;
57 typedef struct apache_s apache_t;
59 /* TODO: Remove this prototype */
60 static int apache_read_host (user_data_t *user_data);
62 static void apache_free (apache_t *st)
73 sfree (st->apache_buffer);
75 curl_easy_cleanup(st->curl);
80 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb,
83 size_t len = size * nmemb;
89 ERROR ("apache plugin: apache_curl_callback: "
90 "user_data pointer is NULL.");
97 if ((st->apache_buffer_fill + len) >= st->apache_buffer_size)
101 temp = (char *) realloc (st->apache_buffer,
102 st->apache_buffer_fill + len + 1);
105 ERROR ("apache plugin: realloc failed.");
108 st->apache_buffer = temp;
109 st->apache_buffer_size = st->apache_buffer_fill + len + 1;
112 memcpy (st->apache_buffer + st->apache_buffer_fill, (char *) buf, len);
113 st->apache_buffer_fill += len;
114 st->apache_buffer[st->apache_buffer_fill] = 0;
117 } /* int apache_curl_callback */
119 /* Configuration handling functiions
121 * <Instance "instance_name">
127 static int config_set_string (char **ret_string, /* {{{ */
132 if ((ci->values_num != 1)
133 || (ci->values[0].type != OCONFIG_TYPE_STRING))
135 WARNING ("apache plugin: The `%s' config option "
136 "needs exactly one string argument.", ci->key);
140 string = strdup (ci->values[0].value.string);
143 ERROR ("apache plugin: strdup failed.");
147 if (*ret_string != NULL)
149 *ret_string = string;
152 } /* }}} int config_set_string */
154 static int config_set_boolean (int *ret_boolean, /* {{{ */
157 if ((ci->values_num != 1)
158 || ((ci->values[0].type != OCONFIG_TYPE_BOOLEAN)
159 && (ci->values[0].type != OCONFIG_TYPE_STRING)))
161 WARNING ("apache plugin: The `%s' config option "
162 "needs exactly one boolean argument.", ci->key);
166 if (ci->values[0].type != OCONFIG_TYPE_BOOLEAN)
168 if (ci->values[0].value.boolean)
173 else /* if (ci->values[0].type != OCONFIG_TYPE_STRING) */
175 char *string = ci->values[0].value.string;
176 if ((strcasecmp ("true", string) == 0)
177 || (strcasecmp ("yes", string) == 0)
178 || (strcasecmp ("on", string) == 0))
180 else if ((strcasecmp ("false", string) == 0)
181 || (strcasecmp ("no", string) == 0)
182 || (strcasecmp ("off", string) == 0))
186 ERROR ("apache plugin: Cannot parse string "
187 "as boolean value: %s", string);
193 } /* }}} int config_set_boolean */
195 static int config_add (oconfig_item_t *ci)
201 if ((ci->values_num != 1)
202 || (ci->values[0].type != OCONFIG_TYPE_STRING))
204 WARNING ("apache plugin: The `%s' config option "
205 "needs exactly one string argument.", ci->key);
209 st = (apache_t *) malloc (sizeof (*st));
212 ERROR ("apache plugin: malloc failed.");
216 memset (st, 0, sizeof (*st));
218 status = config_set_string (&st->name, ci);
224 assert (st->name != NULL);
226 for (i = 0; i < ci->children_num; i++)
228 oconfig_item_t *child = ci->children + i;
230 if (strcasecmp ("URL", child->key) == 0)
231 status = config_set_string (&st->url, child);
232 else if (strcasecmp ("Host", child->key) == 0)
233 status = config_set_string (&st->host, child);
234 else if (strcasecmp ("User", child->key) == 0)
235 status = config_set_string (&st->user, child);
236 else if (strcasecmp ("Password", child->key) == 0)
237 status = config_set_string (&st->pass, child);
238 else if (strcasecmp ("VerifyPeer", child->key) == 0)
239 status = config_set_boolean (&st->verify_peer, child);
240 else if (strcasecmp ("VerifyHost", child->key) == 0)
241 status = config_set_boolean (&st->verify_host, child);
242 else if (strcasecmp ("CACert", child->key) == 0)
243 status = config_set_string (&st->cacert, child);
246 WARNING ("apache plugin: Option `%s' not allowed here.",
255 /* Check if struct is complete.. */
256 if ((status == 0) && (st->url == NULL))
258 ERROR ("apache plugin: Instance `%s': "
259 "No URL has been configured.",
267 char callback_name[3*DATA_MAX_NAME_LEN];
269 memset (&ud, 0, sizeof (ud));
271 ud.free_func = (void *) apache_free;
273 memset (callback_name, 0, sizeof (callback_name));
274 ssnprintf (callback_name, sizeof (callback_name),
276 (st->host != NULL) ? st->host : hostname_g,
277 (st->name != NULL) ? st->name : "default"),
279 status = plugin_register_complex_read (callback_name,
280 /* callback = */ apache_read_host,
281 /* interval = */ NULL,
282 /* user_data = */ &ud);
292 } /* int config_add */
294 static int config (oconfig_item_t *ci)
298 oconfig_item_t *lci = NULL; /* legacy config */
300 for (i = 0; i < ci->children_num; i++)
302 oconfig_item_t *child = ci->children + i;
304 if (strcasecmp ("Instance", child->key) == 0 && child->children_num > 0)
308 /* legacy mode - convert to <Instance ...> config */
311 lci = malloc (sizeof(*lci));
314 ERROR ("apache plugin: malloc failed.");
317 memset (lci, '\0', sizeof (*lci));
322 realloc (lci->children,
323 lci->children_num * sizeof (*child));
324 if (lci->children == NULL)
326 ERROR ("apache plugin: realloc failed.");
329 memcpy (&lci->children[lci->children_num-1], child, sizeof (*child));
331 } /* for (ci->children) */
335 /* create a <Instance ""> entry */
336 lci->key = "Instance";
338 lci->values = (oconfig_value_t *) malloc (lci->values_num * sizeof (oconfig_value_t));
339 lci->values[0].type = OCONFIG_TYPE_STRING;
340 lci->values[0].value.string = "";
342 status = config_add (lci);
344 sfree (lci->children);
351 /* initialize curl for each host */
352 static int init_host (apache_t *st) /* {{{ */
354 static char credentials[1024];
356 assert (st->url != NULL);
357 /* (Assured by `config_add') */
359 if (st->curl != NULL)
361 curl_easy_cleanup (st->curl);
365 if ((st->curl = curl_easy_init ()) == NULL)
367 ERROR ("apache plugin: init_host: `curl_easy_init' failed.");
371 curl_easy_setopt (st->curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
372 curl_easy_setopt (st->curl, CURLOPT_WRITEDATA, st);
373 curl_easy_setopt (st->curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
374 curl_easy_setopt (st->curl, CURLOPT_ERRORBUFFER, st->apache_curl_error);
376 if (st->user != NULL)
380 status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
381 st->user, (st->pass == NULL) ? "" : st->pass);
382 if ((status < 0) || ((size_t) status >= sizeof (credentials)))
384 ERROR ("apache plugin: init_host: Returning an error "
385 "because the credentials have been "
387 curl_easy_cleanup (st->curl);
392 curl_easy_setopt (st->curl, CURLOPT_USERPWD, credentials);
395 curl_easy_setopt (st->curl, CURLOPT_URL, st->url);
397 if (st->verify_peer != 0)
399 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER, 1);
403 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER, 0);
406 if (st->verify_host != 0)
408 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST, 2);
412 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST, 0);
415 if (st->cacert != NULL)
417 curl_easy_setopt (st->curl, CURLOPT_CAINFO, st->cacert);
421 } /* }}} int init_host */
423 static void submit_value (const char *type, const char *type_instance,
424 value_t value, apache_t *st)
426 value_list_t vl = VALUE_LIST_INIT;
431 sstrncpy (vl.host, (st->host != NULL) ? st->host : hostname_g,
434 sstrncpy (vl.plugin, "apache", sizeof (vl.plugin));
435 if (st->name != NULL)
436 sstrncpy (vl.plugin_instance, st->name,
437 sizeof (vl.plugin_instance));
439 sstrncpy (vl.type, type, sizeof (vl.type));
440 if (type_instance != NULL)
441 sstrncpy (vl.type_instance, type_instance,
442 sizeof (vl.type_instance));
444 plugin_dispatch_values (&vl);
445 } /* void submit_value */
447 static void submit_counter (const char *type, const char *type_instance,
448 counter_t c, apache_t *st)
452 submit_value (type, type_instance, v, st);
453 } /* void submit_counter */
455 static void submit_gauge (const char *type, const char *type_instance,
456 gauge_t g, apache_t *st)
460 submit_value (type, type_instance, v, st);
461 } /* void submit_gauge */
463 static void submit_scoreboard (char *buf, int server, apache_t *st)
467 * "_" Waiting for Connection, "S" Starting up,
468 * "R" Reading Request for apache and read-POST for lighttpd,
469 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
470 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
471 * "I" Idle cleanup of worker, "." Open slot with no current process
472 * Lighttpd specific legends -
473 * "E" hard error, "." connect, "h" handle-request,
474 * "q" request-start, "Q" request-end, "s" response-start
475 * "S" response-end, "r" read
477 long long open = 0LL;
478 long long waiting = 0LL;
479 long long starting = 0LL;
480 long long reading = 0LL;
481 long long sending = 0LL;
482 long long keepalive = 0LL;
483 long long dnslookup = 0LL;
484 long long closing = 0LL;
485 long long logging = 0LL;
486 long long finishing = 0LL;
487 long long idle_cleanup = 0LL;
489 /* lighttpd specific */
490 long long hard_error = 0LL;
491 long long lighttpd_read = 0LL;
492 long long handle_request = 0LL;
493 long long request_start = 0LL;
494 long long request_end = 0LL;
495 long long response_start = 0LL;
496 long long response_end = 0LL;
500 for (i = 0; buf[i] != '\0'; i++)
502 if (buf[i] == '.') open++;
503 else if (buf[i] == '_') waiting++;
504 else if (buf[i] == 'S') starting++;
505 else if (buf[i] == 'R') reading++;
506 else if (buf[i] == 'W') sending++;
507 else if (buf[i] == 'K') keepalive++;
508 else if (buf[i] == 'D') dnslookup++;
509 else if (buf[i] == 'C') closing++;
510 else if (buf[i] == 'L') logging++;
511 else if (buf[i] == 'G') finishing++;
512 else if (buf[i] == 'I') idle_cleanup++;
513 else if (buf[i] == 'r') lighttpd_read++;
514 else if (buf[i] == 'h') handle_request++;
515 else if (buf[i] == 'E') hard_error++;
516 else if (buf[i] == 'q') request_start++;
517 else if (buf[i] == 'Q') request_end++;
518 else if (buf[i] == 's') response_start++;
519 else if (buf[i] == 'S') response_end++;
522 if (server == APACHE)
524 submit_gauge ("apache_scoreboard", "open" , open, st);
525 submit_gauge ("apache_scoreboard", "waiting" , waiting, st);
526 submit_gauge ("apache_scoreboard", "starting" , starting, st);
527 submit_gauge ("apache_scoreboard", "reading" , reading, st);
528 submit_gauge ("apache_scoreboard", "sending" , sending, st);
529 submit_gauge ("apache_scoreboard", "keepalive", keepalive, st);
530 submit_gauge ("apache_scoreboard", "dnslookup", dnslookup, st);
531 submit_gauge ("apache_scoreboard", "closing" , closing, st);
532 submit_gauge ("apache_scoreboard", "logging" , logging, st);
533 submit_gauge ("apache_scoreboard", "finishing", finishing, st);
534 submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup, st);
537 submit_gauge ("apache_scoreboard", "connect" , open, st);
538 submit_gauge ("apache_scoreboard", "close" , closing, st);
539 submit_gauge ("apache_scoreboard", "hard_error" , hard_error, st);
540 submit_gauge ("apache_scoreboard", "read" , lighttpd_read, st);
541 submit_gauge ("apache_scoreboard", "read_post" , reading, st);
542 submit_gauge ("apache_scoreboard", "write" , sending, st);
543 submit_gauge ("apache_scoreboard", "handle_request", handle_request, st);
544 submit_gauge ("apache_scoreboard", "request_start" , request_start, st);
545 submit_gauge ("apache_scoreboard", "request_end" , request_end, st);
546 submit_gauge ("apache_scoreboard", "response_start", response_start, st);
547 submit_gauge ("apache_scoreboard", "response_end" , response_end, st);
552 static int apache_read_host (user_data_t *user_data) /* {{{ */
563 int server = LIGHTTPD; /* default is lighttpd */
567 st = user_data->data;
569 assert (st->url != NULL);
570 /* (Assured by `config_add') */
572 if (st->curl == NULL)
576 status = init_host (st);
580 assert (st->curl != NULL);
582 st->apache_buffer_fill = 0;
583 if (curl_easy_perform (st->curl) != 0)
585 ERROR ("apache: curl_easy_perform failed: %s",
586 st->apache_curl_error);
590 ptr = st->apache_buffer;
592 while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
601 for (i = 0; i < lines_num; i++)
603 fields_num = strsplit (lines[i], fields, 4);
607 if ((strcmp (fields[0], "Total") == 0)
608 && (strcmp (fields[1], "Accesses:") == 0))
609 submit_counter ("apache_requests", "",
610 atoll (fields[2]), st);
611 else if ((strcmp (fields[0], "Total") == 0)
612 && (strcmp (fields[1], "kBytes:") == 0))
613 submit_counter ("apache_bytes", "",
614 1024LL * atoll (fields[2]), st);
616 else if (fields_num == 2)
618 /* find out if the server is apache from the mod_status
619 * output. apache mod_status output has additional
620 * fields which lighttpd mod_status output doesn't have
621 * e.g: ReqPerSec. submit_scoreboard needs server type
622 * information and thus it is important to pick up a
623 * field before scoreboard gets parsed to set the
625 if (strcmp (fields[0], "ReqPerSec:") == 0)
627 else if (strcmp (fields[0], "Scoreboard:") == 0)
628 submit_scoreboard (fields[1], server, st);
629 else if (strcmp (fields[0], "BusyServers:") == 0)
630 submit_gauge ("apache_connections", NULL, atol (fields[1]), st);
634 st->apache_buffer_fill = 0;
637 } /* }}} int apache_read_host */
639 void module_register (void)
641 plugin_register_complex_config ("apache", config);
642 } /* void module_register */
644 /* vim: set sw=8 noet fdm=marker : */