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 *server; /* user specific server type */
53 char apache_curl_error[CURL_ERROR_SIZE];
54 size_t apache_buffer_size;
55 size_t apache_buffer_fill;
59 typedef struct apache_s apache_t;
61 /* TODO: Remove this prototype */
62 static int apache_read_host (user_data_t *user_data);
64 static void apache_free (apache_t *st)
76 sfree (st->apache_buffer);
78 curl_easy_cleanup(st->curl);
83 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb,
86 size_t len = size * nmemb;
92 ERROR ("apache plugin: apache_curl_callback: "
93 "user_data pointer is NULL.");
100 if ((st->apache_buffer_fill + len) >= st->apache_buffer_size)
104 temp = (char *) realloc (st->apache_buffer,
105 st->apache_buffer_fill + len + 1);
108 ERROR ("apache plugin: realloc failed.");
111 st->apache_buffer = temp;
112 st->apache_buffer_size = st->apache_buffer_fill + len + 1;
115 memcpy (st->apache_buffer + st->apache_buffer_fill, (char *) buf, len);
116 st->apache_buffer_fill += len;
117 st->apache_buffer[st->apache_buffer_fill] = 0;
120 } /* int apache_curl_callback */
122 static size_t apache_header_callback (void *buf, size_t size, size_t nmemb,
125 size_t len = size * nmemb;
131 ERROR ("apache plugin: apache_header_callback: "
132 "user_data pointer is NULL.");
139 /* look for the Server header */
140 if (strncasecmp (buf, "Server: ", strlen ("Server: ")) != 0)
143 if (strstr (buf, "Apache") != NULL)
144 st->server_type = APACHE;
145 else if (strstr (buf, "lighttpd") != NULL)
146 st->server_type = LIGHTTPD;
149 const char *hdr = buf;
151 hdr += strlen ("Server: ");
152 NOTICE ("apache plugin: Unknown server software: %s", hdr);
156 } /* apache_header_callback */
158 /* Configuration handling functiions
160 * <Instance "instance_name">
166 static int config_set_string (char **ret_string, /* {{{ */
171 if ((ci->values_num != 1)
172 || (ci->values[0].type != OCONFIG_TYPE_STRING))
174 WARNING ("apache plugin: The `%s' config option "
175 "needs exactly one string argument.", ci->key);
179 string = strdup (ci->values[0].value.string);
182 ERROR ("apache plugin: strdup failed.");
186 if (*ret_string != NULL)
188 *ret_string = string;
191 } /* }}} int config_set_string */
193 static int config_set_boolean (int *ret_boolean, /* {{{ */
196 if ((ci->values_num != 1)
197 || ((ci->values[0].type != OCONFIG_TYPE_BOOLEAN)
198 && (ci->values[0].type != OCONFIG_TYPE_STRING)))
200 WARNING ("apache plugin: The `%s' config option "
201 "needs exactly one boolean argument.", ci->key);
205 if (ci->values[0].type != OCONFIG_TYPE_BOOLEAN)
207 if (ci->values[0].value.boolean)
212 else /* if (ci->values[0].type != OCONFIG_TYPE_STRING) */
214 char *string = ci->values[0].value.string;
215 if (IS_TRUE (string))
217 else if (IS_FALSE (string))
221 ERROR ("apache plugin: Cannot parse string "
222 "as boolean value: %s", string);
228 } /* }}} int config_set_boolean */
230 static int config_add (oconfig_item_t *ci)
236 if ((ci->values_num != 1)
237 || (ci->values[0].type != OCONFIG_TYPE_STRING))
239 WARNING ("apache plugin: The `%s' config option "
240 "needs exactly one string argument.", ci->key);
244 st = (apache_t *) malloc (sizeof (*st));
247 ERROR ("apache plugin: malloc failed.");
251 memset (st, 0, sizeof (*st));
253 status = config_set_string (&st->name, ci);
259 assert (st->name != NULL);
261 for (i = 0; i < ci->children_num; i++)
263 oconfig_item_t *child = ci->children + i;
265 if (strcasecmp ("URL", child->key) == 0)
266 status = config_set_string (&st->url, child);
267 else if (strcasecmp ("Host", child->key) == 0)
268 status = config_set_string (&st->host, child);
269 else if (strcasecmp ("User", child->key) == 0)
270 status = config_set_string (&st->user, child);
271 else if (strcasecmp ("Password", child->key) == 0)
272 status = config_set_string (&st->pass, child);
273 else if (strcasecmp ("VerifyPeer", child->key) == 0)
274 status = config_set_boolean (&st->verify_peer, child);
275 else if (strcasecmp ("VerifyHost", child->key) == 0)
276 status = config_set_boolean (&st->verify_host, child);
277 else if (strcasecmp ("CACert", child->key) == 0)
278 status = config_set_string (&st->cacert, child);
279 else if (strcasecmp ("Server", child->key) == 0)
280 status = config_set_string (&st->server, child);
283 WARNING ("apache plugin: Option `%s' not allowed here.",
292 /* Check if struct is complete.. */
293 if ((status == 0) && (st->url == NULL))
295 ERROR ("apache plugin: Instance `%s': "
296 "No URL has been configured.",
304 char callback_name[3*DATA_MAX_NAME_LEN];
306 memset (&ud, 0, sizeof (ud));
308 ud.free_func = (void *) apache_free;
310 memset (callback_name, 0, sizeof (callback_name));
311 ssnprintf (callback_name, sizeof (callback_name),
313 (st->host != NULL) ? st->host : hostname_g,
314 (st->name != NULL) ? st->name : "default"),
316 status = plugin_register_complex_read (callback_name,
317 /* callback = */ apache_read_host,
318 /* interval = */ NULL,
319 /* user_data = */ &ud);
329 } /* int config_add */
331 static int config (oconfig_item_t *ci)
335 oconfig_item_t *lci = NULL; /* legacy config */
337 for (i = 0; i < ci->children_num; i++)
339 oconfig_item_t *child = ci->children + i;
341 if (strcasecmp ("Instance", child->key) == 0 && child->children_num > 0)
345 /* legacy mode - convert to <Instance ...> config */
348 lci = malloc (sizeof(*lci));
351 ERROR ("apache plugin: malloc failed.");
354 memset (lci, '\0', sizeof (*lci));
359 realloc (lci->children,
360 lci->children_num * sizeof (*child));
361 if (lci->children == NULL)
363 ERROR ("apache plugin: realloc failed.");
366 memcpy (&lci->children[lci->children_num-1], child, sizeof (*child));
368 } /* for (ci->children) */
372 /* create a <Instance ""> entry */
373 lci->key = "Instance";
375 lci->values = (oconfig_value_t *) malloc (lci->values_num * sizeof (oconfig_value_t));
376 lci->values[0].type = OCONFIG_TYPE_STRING;
377 lci->values[0].value.string = "";
379 status = config_add (lci);
381 sfree (lci->children);
388 /* initialize curl for each host */
389 static int init_host (apache_t *st) /* {{{ */
391 static char credentials[1024];
393 assert (st->url != NULL);
394 /* (Assured by `config_add') */
396 if (st->curl != NULL)
398 curl_easy_cleanup (st->curl);
402 if ((st->curl = curl_easy_init ()) == NULL)
404 ERROR ("apache plugin: init_host: `curl_easy_init' failed.");
408 curl_easy_setopt (st->curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
409 curl_easy_setopt (st->curl, CURLOPT_WRITEDATA, st);
411 /* not set as yet if the user specified string doesn't match apache or
412 * lighttpd, then ignore it. Headers will be parsed to find out the
414 st->server_type = -1;
416 if (st->server != NULL)
418 if (strcasecmp(st->server, "apache") == 0)
419 st->server_type = APACHE;
420 else if (strcasecmp(st->server, "lighttpd") == 0)
421 st->server_type = LIGHTTPD;
423 WARNING ("apache plugin: Unknown `Server' setting: %s",
427 /* if not found register a header callback to determine the server_type */
428 if (st->server_type == -1)
430 curl_easy_setopt (st->curl, CURLOPT_HEADERFUNCTION, apache_header_callback);
431 curl_easy_setopt (st->curl, CURLOPT_WRITEHEADER, st);
434 curl_easy_setopt (st->curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
435 curl_easy_setopt (st->curl, CURLOPT_ERRORBUFFER, st->apache_curl_error);
437 if (st->user != NULL)
441 status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
442 st->user, (st->pass == NULL) ? "" : st->pass);
443 if ((status < 0) || ((size_t) status >= sizeof (credentials)))
445 ERROR ("apache plugin: init_host: Returning an error "
446 "because the credentials have been "
448 curl_easy_cleanup (st->curl);
453 curl_easy_setopt (st->curl, CURLOPT_USERPWD, credentials);
456 curl_easy_setopt (st->curl, CURLOPT_URL, st->url);
457 curl_easy_setopt (st->curl, CURLOPT_FOLLOWLOCATION, 1);
459 if (st->verify_peer != 0)
461 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER, 1);
465 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER, 0);
468 if (st->verify_host != 0)
470 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST, 2);
474 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST, 0);
477 if (st->cacert != NULL)
479 curl_easy_setopt (st->curl, CURLOPT_CAINFO, st->cacert);
483 } /* }}} int init_host */
485 static void submit_value (const char *type, const char *type_instance,
486 value_t value, apache_t *st)
488 value_list_t vl = VALUE_LIST_INIT;
493 sstrncpy (vl.host, (st->host != NULL) ? st->host : hostname_g,
496 sstrncpy (vl.plugin, "apache", sizeof (vl.plugin));
497 if (st->name != NULL)
498 sstrncpy (vl.plugin_instance, st->name,
499 sizeof (vl.plugin_instance));
501 sstrncpy (vl.type, type, sizeof (vl.type));
502 if (type_instance != NULL)
503 sstrncpy (vl.type_instance, type_instance,
504 sizeof (vl.type_instance));
506 plugin_dispatch_values (&vl);
507 } /* void submit_value */
509 static void submit_counter (const char *type, const char *type_instance,
510 counter_t c, apache_t *st)
514 submit_value (type, type_instance, v, st);
515 } /* void submit_counter */
517 static void submit_gauge (const char *type, const char *type_instance,
518 gauge_t g, apache_t *st)
522 submit_value (type, type_instance, v, st);
523 } /* void submit_gauge */
525 static void submit_scoreboard (char *buf, apache_t *st)
529 * "_" Waiting for Connection, "S" Starting up,
530 * "R" Reading Request for apache and read-POST for lighttpd,
531 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
532 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
533 * "I" Idle cleanup of worker, "." Open slot with no current process
534 * Lighttpd specific legends -
535 * "E" hard error, "." connect, "h" handle-request,
536 * "q" request-start, "Q" request-end, "s" response-start
537 * "S" response-end, "r" read
539 long long open = 0LL;
540 long long waiting = 0LL;
541 long long starting = 0LL;
542 long long reading = 0LL;
543 long long sending = 0LL;
544 long long keepalive = 0LL;
545 long long dnslookup = 0LL;
546 long long closing = 0LL;
547 long long logging = 0LL;
548 long long finishing = 0LL;
549 long long idle_cleanup = 0LL;
551 /* lighttpd specific */
552 long long hard_error = 0LL;
553 long long lighttpd_read = 0LL;
554 long long handle_request = 0LL;
555 long long request_start = 0LL;
556 long long request_end = 0LL;
557 long long response_start = 0LL;
558 long long response_end = 0LL;
561 for (i = 0; buf[i] != '\0'; i++)
563 if (buf[i] == '.') open++;
564 else if (buf[i] == '_') waiting++;
565 else if (buf[i] == 'S') starting++;
566 else if (buf[i] == 'R') reading++;
567 else if (buf[i] == 'W') sending++;
568 else if (buf[i] == 'K') keepalive++;
569 else if (buf[i] == 'D') dnslookup++;
570 else if (buf[i] == 'C') closing++;
571 else if (buf[i] == 'L') logging++;
572 else if (buf[i] == 'G') finishing++;
573 else if (buf[i] == 'I') idle_cleanup++;
574 else if (buf[i] == 'r') lighttpd_read++;
575 else if (buf[i] == 'h') handle_request++;
576 else if (buf[i] == 'E') hard_error++;
577 else if (buf[i] == 'q') request_start++;
578 else if (buf[i] == 'Q') request_end++;
579 else if (buf[i] == 's') response_start++;
580 else if (buf[i] == 'S') response_end++;
583 if (st->server_type == APACHE)
585 submit_gauge ("apache_scoreboard", "open" , open, st);
586 submit_gauge ("apache_scoreboard", "waiting" , waiting, st);
587 submit_gauge ("apache_scoreboard", "starting" , starting, st);
588 submit_gauge ("apache_scoreboard", "reading" , reading, st);
589 submit_gauge ("apache_scoreboard", "sending" , sending, st);
590 submit_gauge ("apache_scoreboard", "keepalive", keepalive, st);
591 submit_gauge ("apache_scoreboard", "dnslookup", dnslookup, st);
592 submit_gauge ("apache_scoreboard", "closing" , closing, st);
593 submit_gauge ("apache_scoreboard", "logging" , logging, st);
594 submit_gauge ("apache_scoreboard", "finishing", finishing, st);
595 submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup, st);
599 submit_gauge ("apache_scoreboard", "connect" , open, st);
600 submit_gauge ("apache_scoreboard", "close" , closing, st);
601 submit_gauge ("apache_scoreboard", "hard_error" , hard_error, st);
602 submit_gauge ("apache_scoreboard", "read" , lighttpd_read, st);
603 submit_gauge ("apache_scoreboard", "read_post" , reading, st);
604 submit_gauge ("apache_scoreboard", "write" , sending, st);
605 submit_gauge ("apache_scoreboard", "handle_request", handle_request, st);
606 submit_gauge ("apache_scoreboard", "request_start" , request_start, st);
607 submit_gauge ("apache_scoreboard", "request_end" , request_end, st);
608 submit_gauge ("apache_scoreboard", "response_start", response_start, st);
609 submit_gauge ("apache_scoreboard", "response_end" , response_end, st);
613 static int apache_read_host (user_data_t *user_data) /* {{{ */
627 st = user_data->data;
629 assert (st->url != NULL);
630 /* (Assured by `config_add') */
632 if (st->curl == NULL)
636 status = init_host (st);
640 assert (st->curl != NULL);
642 st->apache_buffer_fill = 0;
643 if (curl_easy_perform (st->curl) != 0)
645 ERROR ("apache: curl_easy_perform failed: %s",
646 st->apache_curl_error);
650 /* fallback - server_type to apache if not set at this time */
651 if (st->server_type == -1)
653 WARNING ("apache plugin: Unable to determine server software "
654 "automatically. Will assume Apache.");
655 st->server_type = APACHE;
658 ptr = st->apache_buffer;
660 while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
669 for (i = 0; i < lines_num; i++)
671 fields_num = strsplit (lines[i], fields, 4);
675 if ((strcmp (fields[0], "Total") == 0)
676 && (strcmp (fields[1], "Accesses:") == 0))
677 submit_counter ("apache_requests", "",
678 atoll (fields[2]), st);
679 else if ((strcmp (fields[0], "Total") == 0)
680 && (strcmp (fields[1], "kBytes:") == 0))
681 submit_counter ("apache_bytes", "",
682 1024LL * atoll (fields[2]), st);
684 else if (fields_num == 2)
686 if (strcmp (fields[0], "Scoreboard:") == 0)
687 submit_scoreboard (fields[1], st);
688 else if (strcmp (fields[0], "BusyServers:") == 0)
689 submit_gauge ("apache_connections", NULL, atol (fields[1]), st);
693 st->apache_buffer_fill = 0;
696 } /* }}} int apache_read_host */
698 void module_register (void)
700 plugin_register_complex_config ("apache", config);
701 } /* void module_register */
703 /* vim: set sw=8 noet fdm=marker : */