2 * collectd - src/apache.c
3 * Copyright (C) 2006-2008 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>
34 static char *apache_buffer = NULL;
35 static size_t apache_buffer_size = 0;
36 static size_t apache_buffer_fill = 0;
37 static char apache_curl_error[CURL_ERROR_SIZE];
52 typedef struct apache_s apache_t;
54 static apache_t **apache = NULL;
55 static size_t apache_num = 0;
57 static void apache_free (apache_t *st)
67 sfree (st->verify_peer);
68 sfree (st->verify_host);
71 curl_easy_cleanup(st->curl);
76 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb,
77 void __attribute__((unused)) *stream)
79 size_t len = size * nmemb;
84 if ((apache_buffer_fill + len) >= apache_buffer_size)
88 temp = (char *) realloc (apache_buffer,
89 apache_buffer_fill + len + 1);
92 ERROR ("apache plugin: realloc failed.");
96 apache_buffer_size = apache_buffer_fill + len + 1;
99 memcpy (apache_buffer + apache_buffer_fill, (char *) buf, len);
100 apache_buffer_fill += len;
101 apache_buffer[apache_buffer_fill] = 0;
104 } /* int apache_curl_callback */
106 /* Configuration handling functiions
108 * <Instance "instance_name">
114 static int config_set_string (char **ret_string,
119 if ((ci->values_num != 1)
120 || (ci->values[0].type != OCONFIG_TYPE_STRING))
122 WARNING ("apache plugin: The `%s' config option "
123 "needs exactly one string argument.", ci->key);
127 string = strdup (ci->values[0].value.string);
130 ERROR ("apache plugin: strdup failed.");
134 if (*ret_string != NULL)
136 *ret_string = string;
139 } /* int config_set_string */
141 static int config_add (oconfig_item_t *ci)
147 if ((ci->values_num != 1)
148 || (ci->values[0].type != OCONFIG_TYPE_STRING))
150 WARNING ("apache plugin: The `%s' config option "
151 "needs exactly one string argument.", ci->key);
155 st = (apache_t *) malloc (sizeof (*st));
158 ERROR ("apache plugin: malloc failed.");
162 memset (st, 0, sizeof (*st));
164 status = config_set_string (&st->name, ci);
171 for (i = 0; i < ci->children_num; i++)
173 oconfig_item_t *child = ci->children + i;
175 if (strcasecmp ("URL", child->key) == 0)
176 status = config_set_string (&st->url, child);
177 else if (strcasecmp ("Host", child->key) == 0)
178 status = config_set_string (&st->host, child);
179 else if (strcasecmp ("User", child->key) == 0)
180 status = config_set_string (&st->user, child);
181 else if (strcasecmp ("Password", child->key) == 0)
182 status = config_set_string (&st->pass, child);
183 else if (strcasecmp ("VerifyPeer", child->key) == 0)
184 status = config_set_string (&st->verify_peer, child);
185 else if (strcasecmp ("VerifyHost", child->key) == 0)
186 status = config_set_string (&st->verify_host, child);
187 else if (strcasecmp ("CACert", child->key) == 0)
188 status = config_set_string (&st->cacert, child);
191 WARNING ("apache plugin: Option `%s' not allowed here.", child->key);
202 temp = (apache_t **) realloc (apache, sizeof (*apache) * (apache_num + 1));
205 ERROR ("apache plugin: realloc failed");
211 apache[apache_num] = st;
223 } /* int config_add */
225 static int config (oconfig_item_t *ci)
229 oconfig_item_t *lci = NULL; /* legacy config */
231 for (i = 0; i < ci->children_num; i++)
233 oconfig_item_t *child = ci->children + i;
235 if (strcasecmp ("Instance", child->key) == 0 && child->children_num > 0)
239 /* legacy mode - convert to <Instance ...> config */
242 lci = malloc (sizeof(*lci));
245 ERROR ("apache plugin: malloc failed.");
248 memset (lci, '\0', sizeof (*lci));
250 if (strcasecmp ("Instance", child->key) == 0)
252 lci->key = child->key;
253 lci->values = child->values;
254 lci->values_num = child->values_num;
255 lci->parent = child->parent;
261 realloc (lci->children,
262 lci->children_num * sizeof (*child));
263 if (lci->children == NULL)
265 ERROR ("apache plugin: realloc failed.");
268 memcpy (&lci->children[lci->children_num-1], child, sizeof (*child));
271 } /* for (ci->children) */
275 // create a <Instance ""> entry
276 lci->key = "Instance";
278 lci->values = (oconfig_value_t *) malloc (lci->values_num * sizeof (oconfig_value_t));
279 lci->values[0].type = OCONFIG_TYPE_STRING;
280 lci->values[0].value.string = "";
282 status = config_add (lci);
283 sfree (lci->children);
291 // initialize curl for each host
292 static int init_host (apache_t *st) /* {{{ */
294 static char credentials[1024];
298 WARNING ("apache plugin: init: No URL configured, returning "
303 if (st->curl != NULL)
305 curl_easy_cleanup (st->curl);
308 if ((st->curl = curl_easy_init ()) == NULL)
310 ERROR ("apache plugin: init: `curl_easy_init' failed.");
314 curl_easy_setopt (st->curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
315 curl_easy_setopt (st->curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
316 curl_easy_setopt (st->curl, CURLOPT_ERRORBUFFER, apache_curl_error);
318 if (st->user != NULL)
322 status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
323 st->user, (st->pass == NULL) ? "" : st->pass);
324 if ((status < 0) || ((size_t) status >= sizeof (credentials)))
326 ERROR ("apache plugin: init: Returning an error "
327 "because the credentials have been "
332 curl_easy_setopt (st->curl, CURLOPT_USERPWD, credentials);
335 curl_easy_setopt (st->curl, CURLOPT_URL, st->url);
337 if ((st->verify_peer == NULL) || (strcmp (st->verify_peer, "true") == 0))
339 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER, 1);
343 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYPEER, 0);
346 if ((st->verify_host == NULL) || (strcmp (st->verify_host, "true") == 0))
348 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST, 2);
352 curl_easy_setopt (st->curl, CURLOPT_SSL_VERIFYHOST, 0);
355 if (st->cacert != NULL)
357 curl_easy_setopt (st->curl, CURLOPT_CAINFO, st->cacert);
361 } /* int init_host */
363 static int init (void)
369 for (i = 0; i < apache_num; i++)
371 status = init_host (apache[i]);
378 ERROR ("apache plugin init: No host could be initialized. Will return an error so "
379 "the plugin will be delayed.");
386 static void set_plugin (apache_t *st, value_list_t *vl)
388 // if there is no instance name, assume apache
389 if ( (0 == strcmp(st->name, "")) )
391 sstrncpy (vl->plugin, "apache", sizeof (vl->plugin));
395 sstrncpy (vl->plugin, st->name, sizeof (vl->plugin));
397 } /* void set_plugin */
399 static void submit_counter (const char *type, const char *type_instance,
400 counter_t value, char *host, apache_t *st)
403 value_list_t vl = VALUE_LIST_INIT;
405 values[0].counter = value;
409 sstrncpy (vl.host, host, sizeof (vl.host));
410 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
411 sstrncpy (vl.type, type, sizeof (vl.type));
413 if (type_instance != NULL)
414 sstrncpy (vl.type_instance, type_instance,
415 sizeof (vl.type_instance));
417 set_plugin (st, &vl);
419 plugin_dispatch_values (&vl);
420 } /* void submit_counter */
422 static void submit_gauge (const char *type, const char *type_instance,
423 gauge_t value, char *host, apache_t *st)
426 value_list_t vl = VALUE_LIST_INIT;
428 values[0].gauge = value;
432 sstrncpy (vl.host, host, sizeof (vl.host));
433 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
434 sstrncpy (vl.type, type, sizeof (vl.type));
436 if (type_instance != NULL)
437 sstrncpy (vl.type_instance, type_instance,
438 sizeof (vl.type_instance));
440 set_plugin (st, &vl);
442 plugin_dispatch_values (&vl);
443 } /* void submit_counter */
445 static void submit_scoreboard (char *buf, char *host, apache_t *st)
449 * "_" Waiting for Connection, "S" Starting up, "R" Reading Request,
450 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
451 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
452 * "I" Idle cleanup of worker, "." Open slot with no current process
454 long long open = 0LL;
455 long long waiting = 0LL;
456 long long starting = 0LL;
457 long long reading = 0LL;
458 long long sending = 0LL;
459 long long keepalive = 0LL;
460 long long dnslookup = 0LL;
461 long long closing = 0LL;
462 long long logging = 0LL;
463 long long finishing = 0LL;
464 long long idle_cleanup = 0LL;
468 for (i = 0; buf[i] != '\0'; i++)
470 if (buf[i] == '.') open++;
471 else if (buf[i] == '_') waiting++;
472 else if (buf[i] == 'S') starting++;
473 else if (buf[i] == 'R') reading++;
474 else if (buf[i] == 'W') sending++;
475 else if (buf[i] == 'K') keepalive++;
476 else if (buf[i] == 'D') dnslookup++;
477 else if (buf[i] == 'C') closing++;
478 else if (buf[i] == 'L') logging++;
479 else if (buf[i] == 'G') finishing++;
480 else if (buf[i] == 'I') idle_cleanup++;
483 submit_gauge ("apache_scoreboard", "open" , open, host, st);
484 submit_gauge ("apache_scoreboard", "waiting" , waiting, host, st);
485 submit_gauge ("apache_scoreboard", "starting" , starting, host, st);
486 submit_gauge ("apache_scoreboard", "reading" , reading, host, st);
487 submit_gauge ("apache_scoreboard", "sending" , sending, host, st);
488 submit_gauge ("apache_scoreboard", "keepalive", keepalive, host, st);
489 submit_gauge ("apache_scoreboard", "dnslookup", dnslookup, host, st);
490 submit_gauge ("apache_scoreboard", "closing" , closing, host, st);
491 submit_gauge ("apache_scoreboard", "logging" , logging, host, st);
492 submit_gauge ("apache_scoreboard", "finishing", finishing, host, st);
493 submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup, host, st);
496 static int apache_read_host (apache_t *st)
508 if (st->curl == NULL)
513 apache_buffer_fill = 0;
514 if (curl_easy_perform (st->curl) != 0)
516 ERROR ("apache: curl_easy_perform failed: %s",
523 while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
532 // set the host to localhost if st->host is not specified
533 if ( (st->host == NULL)
534 || (0 == strcmp(st->host, "")) ) {
535 st->host = hostname_g;
538 for (i = 0; i < lines_num; i++)
540 fields_num = strsplit (lines[i], fields, 4);
544 if ((strcmp (fields[0], "Total") == 0)
545 && (strcmp (fields[1], "Accesses:") == 0))
546 submit_counter ("apache_requests", "",
547 atoll (fields[2]), st->host, st);
548 else if ((strcmp (fields[0], "Total") == 0)
549 && (strcmp (fields[1], "kBytes:") == 0))
550 submit_counter ("apache_bytes", "",
551 1024LL * atoll (fields[2]), st->host, st);
553 else if (fields_num == 2)
555 if (strcmp (fields[0], "Scoreboard:") == 0)
556 submit_scoreboard (fields[1], st->host, st);
557 else if (strcmp (fields[0], "BusyServers:") == 0)
558 submit_gauge ("apache_connections", NULL, atol (fields[1]), st->host, st);
562 apache_buffer_fill = 0;
565 } /* int apache_read_host */
567 static int apache_read (void)
573 for (i = 0; i < apache_num; i++)
575 status = apache_read_host (apache[i]);
582 ERROR ("apache plugin: No host could be read. Will return an error so "
583 "the plugin will be delayed.");
588 } /* int apache_read */
590 void module_register (void)
592 plugin_register_complex_config ("apache", config);
593 plugin_register_init ("apache", init);
594 plugin_register_read ("apache", apache_read);
595 } /* void module_register */