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)
66 sfree (st->verify_peer);
67 sfree (st->verify_host);
69 sfree (st->apache_buffer);
71 curl_easy_cleanup(st->curl);
76 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb,
79 size_t len = size * nmemb;
85 ERROR ("apache plugin: apache_curl_callback: "
86 "user_data pointer is NULL.");
93 if ((st->apache_buffer_fill + len) >= st->apache_buffer_size)
97 temp = (char *) realloc (st->apache_buffer,
98 st->apache_buffer_fill + len + 1);
101 ERROR ("apache plugin: realloc failed.");
104 st->apache_buffer = temp;
105 st->apache_buffer_size = st->apache_buffer_fill + len + 1;
108 memcpy (st->apache_buffer + st->apache_buffer_fill, (char *) buf, len);
109 st->apache_buffer_fill += len;
110 st->apache_buffer[st->apache_buffer_fill] = 0;
113 } /* int apache_curl_callback */
115 /* Configuration handling functiions
117 * <Instance "instance_name">
123 static int config_set_string (char **ret_string,
128 if ((ci->values_num != 1)
129 || (ci->values[0].type != OCONFIG_TYPE_STRING))
131 WARNING ("apache plugin: The `%s' config option "
132 "needs exactly one string argument.", ci->key);
136 string = strdup (ci->values[0].value.string);
139 ERROR ("apache plugin: strdup failed.");
143 if (*ret_string != NULL)
145 *ret_string = string;
148 } /* int config_set_string */
150 static int config_add (oconfig_item_t *ci)
156 if ((ci->values_num != 1)
157 || (ci->values[0].type != OCONFIG_TYPE_STRING))
159 WARNING ("apache plugin: The `%s' config option "
160 "needs exactly one string argument.", ci->key);
164 st = (apache_t *) malloc (sizeof (*st));
167 ERROR ("apache plugin: malloc failed.");
171 memset (st, 0, sizeof (*st));
173 status = config_set_string (&st->name, ci);
180 for (i = 0; i < ci->children_num; i++)
182 oconfig_item_t *child = ci->children + i;
184 if (strcasecmp ("URL", child->key) == 0)
185 status = config_set_string (&st->url, child);
186 else if (strcasecmp ("Host", child->key) == 0)
187 status = config_set_string (&st->host, child);
188 else if (strcasecmp ("User", child->key) == 0)
189 status = config_set_string (&st->user, child);
190 else if (strcasecmp ("Password", child->key) == 0)
191 status = config_set_string (&st->pass, child);
192 else if (strcasecmp ("VerifyPeer", child->key) == 0)
193 status = config_set_string (&st->verify_peer, child);
194 else if (strcasecmp ("VerifyHost", child->key) == 0)
195 status = config_set_string (&st->verify_host, child);
196 else if (strcasecmp ("CACert", child->key) == 0)
197 status = config_set_string (&st->cacert, child);
200 WARNING ("apache plugin: Option `%s' not allowed here.", child->key);
211 char callback_name[3*DATA_MAX_NAME_LEN];
213 memset (&ud, 0, sizeof (ud));
215 ud.free_func = (void *) apache_free;
217 memset (callback_name, 0, sizeof (callback_name));
218 ssnprintf (callback_name, sizeof (callback_name),
220 (st->host != NULL) ? st->host : hostname_g,
221 (st->name != NULL) ? st->name : "default"),
223 status = plugin_register_complex_read (callback_name,
224 /* callback = */ apache_read_host,
225 /* interval = */ NULL,
226 /* user_data = */ &ud);
236 } /* int config_add */
238 static int config (oconfig_item_t *ci)
242 oconfig_item_t *lci = NULL; /* legacy config */
244 for (i = 0; i < ci->children_num; i++)
246 oconfig_item_t *child = ci->children + i;
248 if (strcasecmp ("Instance", child->key) == 0 && child->children_num > 0)
252 /* legacy mode - convert to <Instance ...> config */
255 lci = malloc (sizeof(*lci));
258 ERROR ("apache plugin: malloc failed.");
261 memset (lci, '\0', sizeof (*lci));
266 realloc (lci->children,
267 lci->children_num * sizeof (*child));
268 if (lci->children == NULL)
270 ERROR ("apache plugin: realloc failed.");
273 memcpy (&lci->children[lci->children_num-1], child, sizeof (*child));
275 } /* for (ci->children) */
279 /* create a <Instance ""> entry */
280 lci->key = "Instance";
282 lci->values = (oconfig_value_t *) malloc (lci->values_num * sizeof (oconfig_value_t));
283 lci->values[0].type = OCONFIG_TYPE_STRING;
284 lci->values[0].value.string = "";
286 status = config_add (lci);
288 sfree (lci->children);
295 /* initialize curl for each host */
296 static int init_host (apache_t *st) /* {{{ */
298 static char credentials[1024];
302 WARNING ("apache plugin: init_host: No URL configured, returning "
307 if (st->curl != NULL)
309 curl_easy_cleanup (st->curl);
312 if ((st->curl = curl_easy_init ()) == NULL)
314 ERROR ("apache plugin: init_host: `curl_easy_init' failed.");
318 curl_easy_setopt (st->curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
319 curl_easy_setopt (st->curl, CURLOPT_WRITEDATA, st);
320 curl_easy_setopt (st->curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
321 curl_easy_setopt (st->curl, CURLOPT_ERRORBUFFER, st->apache_curl_error);
323 if (st->user != NULL)
327 status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
328 st->user, (st->pass == NULL) ? "" : st->pass);
329 if ((status < 0) || ((size_t) status >= sizeof (credentials)))
331 ERROR ("apache plugin: init_host: Returning an error "
332 "because the credentials have been "
334 curl_easy_cleanup (st->curl);
339 curl_easy_setopt (st->curl, CURLOPT_USERPWD, credentials);
342 curl_easy_setopt (st->curl, CURLOPT_URL, st->url);
344 if ((st->verify_peer == NULL) || (strcmp (st->verify_peer, "true") == 0))
346 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER, 1);
350 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER, 0);
353 if ((st->verify_host == NULL) || (strcmp (st->verify_host, "true") == 0))
355 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST, 2);
359 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST, 0);
362 if (st->cacert != NULL)
364 curl_easy_setopt (st->curl, CURLOPT_CAINFO, st->cacert);
368 } /* }}} int init_host */
370 static void submit_value (const char *type, const char *type_instance,
371 value_t value, apache_t *st)
373 value_list_t vl = VALUE_LIST_INIT;
378 sstrncpy (vl.host, (st->host != NULL) ? st->host : hostname_g,
381 sstrncpy (vl.plugin, "apache", sizeof (vl.plugin));
382 if (st->name != NULL)
383 sstrncpy (vl.plugin_instance, st->name,
384 sizeof (vl.plugin_instance));
386 sstrncpy (vl.type, type, sizeof (vl.type));
387 if (type_instance != NULL)
388 sstrncpy (vl.type_instance, type_instance,
389 sizeof (vl.type_instance));
391 plugin_dispatch_values (&vl);
392 } /* void submit_value */
394 static void submit_counter (const char *type, const char *type_instance,
395 counter_t c, apache_t *st)
399 submit_value (type, type_instance, v, st);
400 } /* void submit_counter */
402 static void submit_gauge (const char *type, const char *type_instance,
403 gauge_t g, apache_t *st)
407 submit_value (type, type_instance, v, st);
408 } /* void submit_gauge */
410 static void submit_scoreboard (char *buf, apache_t *st)
414 * "_" Waiting for Connection, "S" Starting up, "R" Reading Request,
415 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
416 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
417 * "I" Idle cleanup of worker, "." Open slot with no current process
419 long long open = 0LL;
420 long long waiting = 0LL;
421 long long starting = 0LL;
422 long long reading = 0LL;
423 long long sending = 0LL;
424 long long keepalive = 0LL;
425 long long dnslookup = 0LL;
426 long long closing = 0LL;
427 long long logging = 0LL;
428 long long finishing = 0LL;
429 long long idle_cleanup = 0LL;
433 for (i = 0; buf[i] != '\0'; i++)
435 if (buf[i] == '.') open++;
436 else if (buf[i] == '_') waiting++;
437 else if (buf[i] == 'S') starting++;
438 else if (buf[i] == 'R') reading++;
439 else if (buf[i] == 'W') sending++;
440 else if (buf[i] == 'K') keepalive++;
441 else if (buf[i] == 'D') dnslookup++;
442 else if (buf[i] == 'C') closing++;
443 else if (buf[i] == 'L') logging++;
444 else if (buf[i] == 'G') finishing++;
445 else if (buf[i] == 'I') idle_cleanup++;
448 submit_gauge ("apache_scoreboard", "open" , open, st);
449 submit_gauge ("apache_scoreboard", "waiting" , waiting, st);
450 submit_gauge ("apache_scoreboard", "starting" , starting, st);
451 submit_gauge ("apache_scoreboard", "reading" , reading, st);
452 submit_gauge ("apache_scoreboard", "sending" , sending, st);
453 submit_gauge ("apache_scoreboard", "keepalive", keepalive, st);
454 submit_gauge ("apache_scoreboard", "dnslookup", dnslookup, st);
455 submit_gauge ("apache_scoreboard", "closing" , closing, st);
456 submit_gauge ("apache_scoreboard", "logging" , logging, st);
457 submit_gauge ("apache_scoreboard", "finishing", finishing, st);
458 submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup, st);
461 static int apache_read_host (user_data_t *user_data) /* {{{ */
475 st = user_data->data;
477 if (st->curl == NULL)
481 status = init_host (st);
485 assert (st->curl != NULL);
490 st->apache_buffer_fill = 0;
491 if (curl_easy_perform (st->curl) != 0)
493 ERROR ("apache: curl_easy_perform failed: %s",
494 st->apache_curl_error);
498 ptr = st->apache_buffer;
500 while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
509 for (i = 0; i < lines_num; i++)
511 fields_num = strsplit (lines[i], fields, 4);
515 if ((strcmp (fields[0], "Total") == 0)
516 && (strcmp (fields[1], "Accesses:") == 0))
517 submit_counter ("apache_requests", "",
518 atoll (fields[2]), st);
519 else if ((strcmp (fields[0], "Total") == 0)
520 && (strcmp (fields[1], "kBytes:") == 0))
521 submit_counter ("apache_bytes", "",
522 1024LL * atoll (fields[2]), st);
524 else if (fields_num == 2)
526 if (strcmp (fields[0], "Scoreboard:") == 0)
527 submit_scoreboard (fields[1], st);
528 else if (strcmp (fields[0], "BusyServers:") == 0)
529 submit_gauge ("apache_connections", NULL, atol (fields[1]), st);
533 st->apache_buffer_fill = 0;
536 } /* }}} int apache_read_host */
538 void module_register (void)
540 plugin_register_complex_config ("apache", config);
541 } /* void module_register */
543 /* vim: set sw=8 noet fdm=marker : */