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>
45 char apache_curl_error[CURL_ERROR_SIZE];
46 size_t apache_buffer_size;
47 size_t apache_buffer_fill;
51 typedef struct apache_s apache_t;
53 /* TODO: Remove this prototype */
54 static int apache_read_host (user_data_t *user_data);
56 static void apache_free (apache_t *st)
67 sfree (st->apache_buffer);
69 curl_easy_cleanup(st->curl);
74 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb,
77 size_t len = size * nmemb;
83 ERROR ("apache plugin: apache_curl_callback: "
84 "user_data pointer is NULL.");
91 if ((st->apache_buffer_fill + len) >= st->apache_buffer_size)
95 temp = (char *) realloc (st->apache_buffer,
96 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 /* Configuration handling functiions
115 * <Instance "instance_name">
121 static int config_set_string (char **ret_string, /* {{{ */
126 if ((ci->values_num != 1)
127 || (ci->values[0].type != OCONFIG_TYPE_STRING))
129 WARNING ("apache plugin: The `%s' config option "
130 "needs exactly one string argument.", ci->key);
134 string = strdup (ci->values[0].value.string);
137 ERROR ("apache plugin: strdup failed.");
141 if (*ret_string != NULL)
143 *ret_string = string;
146 } /* }}} int config_set_string */
148 static int config_set_boolean (int *ret_boolean, /* {{{ */
151 if ((ci->values_num != 1)
152 || ((ci->values[0].type != OCONFIG_TYPE_BOOLEAN)
153 && (ci->values[0].type != OCONFIG_TYPE_STRING)))
155 WARNING ("apache plugin: The `%s' config option "
156 "needs exactly one boolean argument.", ci->key);
160 if (ci->values[0].type != OCONFIG_TYPE_BOOLEAN)
162 if (ci->values[0].value.boolean)
167 else /* if (ci->values[0].type != OCONFIG_TYPE_STRING) */
169 char *string = ci->values[0].value.string;
170 if ((strcasecmp ("true", string) == 0)
171 || (strcasecmp ("yes", string) == 0)
172 || (strcasecmp ("on", string) == 0))
174 else if ((strcasecmp ("false", string) == 0)
175 || (strcasecmp ("no", string) == 0)
176 || (strcasecmp ("off", string) == 0))
180 ERROR ("apache plugin: Cannot parse string "
181 "as boolean value: %s", string);
187 } /* }}} int config_set_boolean */
189 static int config_add (oconfig_item_t *ci)
195 if ((ci->values_num != 1)
196 || (ci->values[0].type != OCONFIG_TYPE_STRING))
198 WARNING ("apache plugin: The `%s' config option "
199 "needs exactly one string argument.", ci->key);
203 st = (apache_t *) malloc (sizeof (*st));
206 ERROR ("apache plugin: malloc failed.");
210 memset (st, 0, sizeof (*st));
212 status = config_set_string (&st->name, ci);
218 assert (st->name != NULL);
220 for (i = 0; i < ci->children_num; i++)
222 oconfig_item_t *child = ci->children + i;
224 if (strcasecmp ("URL", child->key) == 0)
225 status = config_set_string (&st->url, child);
226 else if (strcasecmp ("Host", child->key) == 0)
227 status = config_set_string (&st->host, child);
228 else if (strcasecmp ("User", child->key) == 0)
229 status = config_set_string (&st->user, child);
230 else if (strcasecmp ("Password", child->key) == 0)
231 status = config_set_string (&st->pass, child);
232 else if (strcasecmp ("VerifyPeer", child->key) == 0)
233 status = config_set_boolean (&st->verify_peer, child);
234 else if (strcasecmp ("VerifyHost", child->key) == 0)
235 status = config_set_boolean (&st->verify_host, child);
236 else if (strcasecmp ("CACert", child->key) == 0)
237 status = config_set_string (&st->cacert, child);
240 WARNING ("apache plugin: Option `%s' not allowed here.",
249 /* Check if struct is complete.. */
250 if ((status == 0) && (st->url == NULL))
252 ERROR ("apache plugin: Instance `%s': "
253 "No URL has been configured.",
261 char callback_name[3*DATA_MAX_NAME_LEN];
263 memset (&ud, 0, sizeof (ud));
265 ud.free_func = (void *) apache_free;
267 memset (callback_name, 0, sizeof (callback_name));
268 ssnprintf (callback_name, sizeof (callback_name),
270 (st->host != NULL) ? st->host : hostname_g,
271 (st->name != NULL) ? st->name : "default"),
273 status = plugin_register_complex_read (callback_name,
274 /* callback = */ apache_read_host,
275 /* interval = */ NULL,
276 /* user_data = */ &ud);
286 } /* int config_add */
288 static int config (oconfig_item_t *ci)
292 oconfig_item_t *lci = NULL; /* legacy config */
294 for (i = 0; i < ci->children_num; i++)
296 oconfig_item_t *child = ci->children + i;
298 if (strcasecmp ("Instance", child->key) == 0 && child->children_num > 0)
302 /* legacy mode - convert to <Instance ...> config */
305 lci = malloc (sizeof(*lci));
308 ERROR ("apache plugin: malloc failed.");
311 memset (lci, '\0', sizeof (*lci));
316 realloc (lci->children,
317 lci->children_num * sizeof (*child));
318 if (lci->children == NULL)
320 ERROR ("apache plugin: realloc failed.");
323 memcpy (&lci->children[lci->children_num-1], child, sizeof (*child));
325 } /* for (ci->children) */
329 /* create a <Instance ""> entry */
330 lci->key = "Instance";
332 lci->values = (oconfig_value_t *) malloc (lci->values_num * sizeof (oconfig_value_t));
333 lci->values[0].type = OCONFIG_TYPE_STRING;
334 lci->values[0].value.string = "";
336 status = config_add (lci);
338 sfree (lci->children);
345 /* initialize curl for each host */
346 static int init_host (apache_t *st) /* {{{ */
348 static char credentials[1024];
350 assert (st->url != NULL);
351 /* (Assured by `config_add') */
353 if (st->curl != NULL)
355 curl_easy_cleanup (st->curl);
359 if ((st->curl = curl_easy_init ()) == NULL)
361 ERROR ("apache plugin: init_host: `curl_easy_init' failed.");
365 curl_easy_setopt (st->curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
366 curl_easy_setopt (st->curl, CURLOPT_WRITEDATA, st);
367 curl_easy_setopt (st->curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
368 curl_easy_setopt (st->curl, CURLOPT_ERRORBUFFER, st->apache_curl_error);
370 if (st->user != NULL)
374 status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
375 st->user, (st->pass == NULL) ? "" : st->pass);
376 if ((status < 0) || ((size_t) status >= sizeof (credentials)))
378 ERROR ("apache plugin: init_host: Returning an error "
379 "because the credentials have been "
381 curl_easy_cleanup (st->curl);
386 curl_easy_setopt (st->curl, CURLOPT_USERPWD, credentials);
389 curl_easy_setopt (st->curl, CURLOPT_URL, st->url);
391 if (st->verify_peer != 0)
393 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER, 1);
397 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER, 0);
400 if (st->verify_host != 0)
402 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST, 2);
406 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST, 0);
409 if (st->cacert != NULL)
411 curl_easy_setopt (st->curl, CURLOPT_CAINFO, st->cacert);
415 } /* }}} int init_host */
417 static void submit_value (const char *type, const char *type_instance,
418 value_t value, apache_t *st)
420 value_list_t vl = VALUE_LIST_INIT;
425 sstrncpy (vl.host, (st->host != NULL) ? st->host : hostname_g,
428 sstrncpy (vl.plugin, "apache", sizeof (vl.plugin));
429 if (st->name != NULL)
430 sstrncpy (vl.plugin_instance, st->name,
431 sizeof (vl.plugin_instance));
433 sstrncpy (vl.type, type, sizeof (vl.type));
434 if (type_instance != NULL)
435 sstrncpy (vl.type_instance, type_instance,
436 sizeof (vl.type_instance));
438 plugin_dispatch_values (&vl);
439 } /* void submit_value */
441 static void submit_counter (const char *type, const char *type_instance,
442 counter_t c, apache_t *st)
446 submit_value (type, type_instance, v, st);
447 } /* void submit_counter */
449 static void submit_gauge (const char *type, const char *type_instance,
450 gauge_t g, apache_t *st)
454 submit_value (type, type_instance, v, st);
455 } /* void submit_gauge */
457 static void submit_scoreboard (char *buf, apache_t *st)
461 * "_" Waiting for Connection, "S" Starting up, "R" Reading Request,
462 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
463 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
464 * "I" Idle cleanup of worker, "." Open slot with no current process
466 long long open = 0LL;
467 long long waiting = 0LL;
468 long long starting = 0LL;
469 long long reading = 0LL;
470 long long sending = 0LL;
471 long long keepalive = 0LL;
472 long long dnslookup = 0LL;
473 long long closing = 0LL;
474 long long logging = 0LL;
475 long long finishing = 0LL;
476 long long idle_cleanup = 0LL;
480 for (i = 0; buf[i] != '\0'; i++)
482 if (buf[i] == '.') open++;
483 else if (buf[i] == '_') waiting++;
484 else if (buf[i] == 'S') starting++;
485 else if (buf[i] == 'R') reading++;
486 else if (buf[i] == 'W') sending++;
487 else if (buf[i] == 'K') keepalive++;
488 else if (buf[i] == 'D') dnslookup++;
489 else if (buf[i] == 'C') closing++;
490 else if (buf[i] == 'L') logging++;
491 else if (buf[i] == 'G') finishing++;
492 else if (buf[i] == 'I') idle_cleanup++;
495 submit_gauge ("apache_scoreboard", "open" , open, st);
496 submit_gauge ("apache_scoreboard", "waiting" , waiting, st);
497 submit_gauge ("apache_scoreboard", "starting" , starting, st);
498 submit_gauge ("apache_scoreboard", "reading" , reading, st);
499 submit_gauge ("apache_scoreboard", "sending" , sending, st);
500 submit_gauge ("apache_scoreboard", "keepalive", keepalive, st);
501 submit_gauge ("apache_scoreboard", "dnslookup", dnslookup, st);
502 submit_gauge ("apache_scoreboard", "closing" , closing, st);
503 submit_gauge ("apache_scoreboard", "logging" , logging, st);
504 submit_gauge ("apache_scoreboard", "finishing", finishing, st);
505 submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup, st);
508 static int apache_read_host (user_data_t *user_data) /* {{{ */
522 st = user_data->data;
524 assert (st->url != NULL);
525 /* (Assured by `config_add') */
527 if (st->curl == NULL)
531 status = init_host (st);
535 assert (st->curl != NULL);
537 st->apache_buffer_fill = 0;
538 if (curl_easy_perform (st->curl) != 0)
540 ERROR ("apache: curl_easy_perform failed: %s",
541 st->apache_curl_error);
545 ptr = st->apache_buffer;
547 while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
556 for (i = 0; i < lines_num; i++)
558 fields_num = strsplit (lines[i], fields, 4);
562 if ((strcmp (fields[0], "Total") == 0)
563 && (strcmp (fields[1], "Accesses:") == 0))
564 submit_counter ("apache_requests", "",
565 atoll (fields[2]), st);
566 else if ((strcmp (fields[0], "Total") == 0)
567 && (strcmp (fields[1], "kBytes:") == 0))
568 submit_counter ("apache_bytes", "",
569 1024LL * atoll (fields[2]), st);
571 else if (fields_num == 2)
573 if (strcmp (fields[0], "Scoreboard:") == 0)
574 submit_scoreboard (fields[1], st);
575 else if (strcmp (fields[0], "BusyServers:") == 0)
576 submit_gauge ("apache_connections", NULL, atol (fields[1]), st);
580 st->apache_buffer_fill = 0;
583 } /* }}} int apache_read_host */
585 void module_register (void)
587 plugin_register_complex_config ("apache", config);
588 } /* void module_register */
590 /* vim: set sw=8 noet fdm=marker : */