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.",
252 char callback_name[3*DATA_MAX_NAME_LEN];
254 memset (&ud, 0, sizeof (ud));
256 ud.free_func = (void *) apache_free;
258 memset (callback_name, 0, sizeof (callback_name));
259 ssnprintf (callback_name, sizeof (callback_name),
261 (st->host != NULL) ? st->host : hostname_g,
262 (st->name != NULL) ? st->name : "default"),
264 status = plugin_register_complex_read (callback_name,
265 /* callback = */ apache_read_host,
266 /* interval = */ NULL,
267 /* user_data = */ &ud);
277 } /* int config_add */
279 static int config (oconfig_item_t *ci)
283 oconfig_item_t *lci = NULL; /* legacy config */
285 for (i = 0; i < ci->children_num; i++)
287 oconfig_item_t *child = ci->children + i;
289 if (strcasecmp ("Instance", child->key) == 0 && child->children_num > 0)
293 /* legacy mode - convert to <Instance ...> config */
296 lci = malloc (sizeof(*lci));
299 ERROR ("apache plugin: malloc failed.");
302 memset (lci, '\0', sizeof (*lci));
307 realloc (lci->children,
308 lci->children_num * sizeof (*child));
309 if (lci->children == NULL)
311 ERROR ("apache plugin: realloc failed.");
314 memcpy (&lci->children[lci->children_num-1], child, sizeof (*child));
316 } /* for (ci->children) */
320 /* create a <Instance ""> entry */
321 lci->key = "Instance";
323 lci->values = (oconfig_value_t *) malloc (lci->values_num * sizeof (oconfig_value_t));
324 lci->values[0].type = OCONFIG_TYPE_STRING;
325 lci->values[0].value.string = "";
327 status = config_add (lci);
329 sfree (lci->children);
336 /* initialize curl for each host */
337 static int init_host (apache_t *st) /* {{{ */
339 static char credentials[1024];
343 WARNING ("apache plugin: init_host: No URL configured, returning "
348 if (st->curl != NULL)
350 curl_easy_cleanup (st->curl);
353 if ((st->curl = curl_easy_init ()) == NULL)
355 ERROR ("apache plugin: init_host: `curl_easy_init' failed.");
359 curl_easy_setopt (st->curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
360 curl_easy_setopt (st->curl, CURLOPT_WRITEDATA, st);
361 curl_easy_setopt (st->curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
362 curl_easy_setopt (st->curl, CURLOPT_ERRORBUFFER, st->apache_curl_error);
364 if (st->user != NULL)
368 status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
369 st->user, (st->pass == NULL) ? "" : st->pass);
370 if ((status < 0) || ((size_t) status >= sizeof (credentials)))
372 ERROR ("apache plugin: init_host: Returning an error "
373 "because the credentials have been "
375 curl_easy_cleanup (st->curl);
380 curl_easy_setopt (st->curl, CURLOPT_USERPWD, credentials);
383 curl_easy_setopt (st->curl, CURLOPT_URL, st->url);
385 if (st->verify_peer != 0)
387 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER, 1);
391 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER, 0);
394 if (st->verify_host != 0)
396 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST, 2);
400 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST, 0);
403 if (st->cacert != NULL)
405 curl_easy_setopt (st->curl, CURLOPT_CAINFO, st->cacert);
409 } /* }}} int init_host */
411 static void submit_value (const char *type, const char *type_instance,
412 value_t value, apache_t *st)
414 value_list_t vl = VALUE_LIST_INIT;
419 sstrncpy (vl.host, (st->host != NULL) ? st->host : hostname_g,
422 sstrncpy (vl.plugin, "apache", sizeof (vl.plugin));
423 if (st->name != NULL)
424 sstrncpy (vl.plugin_instance, st->name,
425 sizeof (vl.plugin_instance));
427 sstrncpy (vl.type, type, sizeof (vl.type));
428 if (type_instance != NULL)
429 sstrncpy (vl.type_instance, type_instance,
430 sizeof (vl.type_instance));
432 plugin_dispatch_values (&vl);
433 } /* void submit_value */
435 static void submit_counter (const char *type, const char *type_instance,
436 counter_t c, apache_t *st)
440 submit_value (type, type_instance, v, st);
441 } /* void submit_counter */
443 static void submit_gauge (const char *type, const char *type_instance,
444 gauge_t g, apache_t *st)
448 submit_value (type, type_instance, v, st);
449 } /* void submit_gauge */
451 static void submit_scoreboard (char *buf, apache_t *st)
455 * "_" Waiting for Connection, "S" Starting up, "R" Reading Request,
456 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
457 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
458 * "I" Idle cleanup of worker, "." Open slot with no current process
460 long long open = 0LL;
461 long long waiting = 0LL;
462 long long starting = 0LL;
463 long long reading = 0LL;
464 long long sending = 0LL;
465 long long keepalive = 0LL;
466 long long dnslookup = 0LL;
467 long long closing = 0LL;
468 long long logging = 0LL;
469 long long finishing = 0LL;
470 long long idle_cleanup = 0LL;
474 for (i = 0; buf[i] != '\0'; i++)
476 if (buf[i] == '.') open++;
477 else if (buf[i] == '_') waiting++;
478 else if (buf[i] == 'S') starting++;
479 else if (buf[i] == 'R') reading++;
480 else if (buf[i] == 'W') sending++;
481 else if (buf[i] == 'K') keepalive++;
482 else if (buf[i] == 'D') dnslookup++;
483 else if (buf[i] == 'C') closing++;
484 else if (buf[i] == 'L') logging++;
485 else if (buf[i] == 'G') finishing++;
486 else if (buf[i] == 'I') idle_cleanup++;
489 submit_gauge ("apache_scoreboard", "open" , open, st);
490 submit_gauge ("apache_scoreboard", "waiting" , waiting, st);
491 submit_gauge ("apache_scoreboard", "starting" , starting, st);
492 submit_gauge ("apache_scoreboard", "reading" , reading, st);
493 submit_gauge ("apache_scoreboard", "sending" , sending, st);
494 submit_gauge ("apache_scoreboard", "keepalive", keepalive, st);
495 submit_gauge ("apache_scoreboard", "dnslookup", dnslookup, st);
496 submit_gauge ("apache_scoreboard", "closing" , closing, st);
497 submit_gauge ("apache_scoreboard", "logging" , logging, st);
498 submit_gauge ("apache_scoreboard", "finishing", finishing, st);
499 submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup, st);
502 static int apache_read_host (user_data_t *user_data) /* {{{ */
516 st = user_data->data;
518 if (st->curl == NULL)
522 status = init_host (st);
526 assert (st->curl != NULL);
531 st->apache_buffer_fill = 0;
532 if (curl_easy_perform (st->curl) != 0)
534 ERROR ("apache: curl_easy_perform failed: %s",
535 st->apache_curl_error);
539 ptr = st->apache_buffer;
541 while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
550 for (i = 0; i < lines_num; i++)
552 fields_num = strsplit (lines[i], fields, 4);
556 if ((strcmp (fields[0], "Total") == 0)
557 && (strcmp (fields[1], "Accesses:") == 0))
558 submit_counter ("apache_requests", "",
559 atoll (fields[2]), st);
560 else if ((strcmp (fields[0], "Total") == 0)
561 && (strcmp (fields[1], "kBytes:") == 0))
562 submit_counter ("apache_bytes", "",
563 1024LL * atoll (fields[2]), st);
565 else if (fields_num == 2)
567 if (strcmp (fields[0], "Scoreboard:") == 0)
568 submit_scoreboard (fields[1], st);
569 else if (strcmp (fields[0], "BusyServers:") == 0)
570 submit_gauge ("apache_connections", NULL, atol (fields[1]), st);
574 st->apache_buffer_fill = 0;
577 } /* }}} int apache_read_host */
579 void module_register (void)
581 plugin_register_complex_config ("apache", config);
582 } /* void module_register */
584 /* vim: set sw=8 noet fdm=marker : */