2 * collectd - src/apache.c
3 * Copyright (C) 2006-2008 Florian octo Forster
4 * Copyright (C) 2007 Florent EppO Monbillard
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at verplant.org>
21 * Florent EppO Monbillard <eppo at darox.net>
22 * - connections/lighttpd extension
28 #include "configfile.h"
30 #include <curl/curl.h>
32 static char *url = NULL;
33 static char *user = NULL;
34 static char *pass = NULL;
35 static char *verify_peer = NULL;
36 static char *verify_host = NULL;
37 static char *cacert = NULL;
39 static CURL *curl = NULL;
41 static char *apache_buffer = NULL;
42 static size_t apache_buffer_size = 0;
43 static size_t apache_buffer_fill = 0;
44 static char apache_curl_error[CURL_ERROR_SIZE];
59 typedef struct apache_s apache_t;
61 static apache_t **apache = NULL;
62 static size_t apache_num = 0;
64 static void apache_free (apache_t *st)
74 sfree (st->verify_peer);
75 sfree (st->verify_host);
78 curl_easy_cleanup(st->curl);
83 static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb,
84 void __attribute__((unused)) *stream)
86 size_t len = size * nmemb;
91 if ((apache_buffer_fill + len) >= apache_buffer_size)
95 temp = (char *) realloc (apache_buffer,
96 apache_buffer_fill + len + 1);
99 ERROR ("apache plugin: realloc failed.");
102 apache_buffer = temp;
103 apache_buffer_size = apache_buffer_fill + len + 1;
106 memcpy (apache_buffer + apache_buffer_fill, (char *) buf, len);
107 apache_buffer_fill += len;
108 apache_buffer[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_add (oconfig_item_t *ci)
154 if ((ci->values_num != 1)
155 || (ci->values[0].type != OCONFIG_TYPE_STRING))
157 WARNING ("apache plugin: The `%s' config option "
158 "needs exactly one string argument.", ci->key);
162 st = (apache_t *) malloc (sizeof (*st));
165 ERROR ("apache plugin: malloc failed.");
169 memset (st, 0, sizeof (*st));
171 status = config_set_string (&st->name, ci);
178 for (i = 0; i < ci->children_num; i++)
180 oconfig_item_t *child = ci->children + i;
182 if (strcasecmp ("URL", child->key) == 0)
183 status = config_set_string (&st->url, child);
184 else if (strcasecmp ("Host", child->key) == 0)
185 status = config_set_string (&st->host, child);
186 else if (strcasecmp ("User", child->key) == 0)
187 status = config_set_string (&st->user, child);
188 else if (strcasecmp ("Password", child->key) == 0)
189 status = config_set_string (&st->pass, child);
190 else if (strcasecmp ("VerifyPeer", child->key) == 0)
191 status = config_set_string (&st->verify_peer, child);
192 else if (strcasecmp ("VerifyHost", child->key) == 0)
193 status = config_set_string (&st->verify_host, child);
194 else if (strcasecmp ("CACert", child->key) == 0)
195 status = config_set_string (&st->cacert, child);
198 WARNING ("apache plugin: Option `%s' not allowed here.", child->key);
209 temp = (apache_t **) realloc (apache, sizeof (*apache) * (apache_num + 1));
212 ERROR ("apache plugin: realloc failed");
218 apache[apache_num] = st;
230 } /* int config_add */
232 static int config (oconfig_item_t *ci)
236 oconfig_item_t *lci = NULL; /* legacy config */
238 for (i = 0; i < ci->children_num; i++)
240 oconfig_item_t *child = ci->children + i;
242 if (strcasecmp ("Instance", child->key) == 0 && child->children_num > 0)
246 /* legacy mode - convert to <Instance ...> config */
249 lci = malloc (sizeof(*lci));
252 ERROR ("apache plugin: malloc failed.");
255 memset (lci, '\0', sizeof (*lci));
257 if (strcasecmp ("Instance", child->key) == 0)
259 lci->key = child->key;
260 lci->values = child->values;
261 lci->values_num = child->values_num;
262 lci->parent = child->parent;
268 realloc (lci->children,
269 lci->children_num * sizeof (*child));
270 if (lci->children == NULL)
272 ERROR ("apache plugin: realloc failed.");
275 memcpy (&lci->children[lci->children_num-1], child, sizeof (*child));
278 } /* for (ci->children) */
282 // create a <Instance ""> entry
283 lci->key = "Instance";
285 lci->values = (oconfig_value_t *) malloc (lci->values_num * sizeof (oconfig_value_t));
286 lci->values[0].type = OCONFIG_TYPE_STRING;
287 lci->values[0].value.string = "";
289 status = config_add (lci);
290 sfree (lci->children);
298 // initialize curl for each host
299 static int init_host (apache_t *st) /* {{{ */
301 static char credentials[1024];
305 WARNING ("apache plugin: init: No URL configured, returning "
310 if (st->curl != NULL)
312 curl_easy_cleanup (st->curl);
315 if ((st->curl = curl_easy_init ()) == NULL)
317 ERROR ("apache plugin: init: `curl_easy_init' failed.");
321 curl_easy_setopt (st->curl, CURLOPT_WRITEFUNCTION, apache_curl_callback);
322 curl_easy_setopt (st->curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
323 curl_easy_setopt (st->curl, CURLOPT_ERRORBUFFER, apache_curl_error);
325 if (st->user != NULL)
329 status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
330 st->user, (st->pass == NULL) ? "" : st->pass);
331 if ((status < 0) || ((size_t) status >= sizeof (credentials)))
333 ERROR ("apache plugin: init: Returning an error "
334 "because the credentials have been "
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 int init (void)
376 for (i = 0; i < apache_num; i++)
378 status = init_host (apache[i]);
385 ERROR ("apache plugin init: No host could be initialized. Will return an error so "
386 "the plugin will be delayed.");
393 static void set_plugin (apache_t *st, value_list_t *vl)
395 // if there is no instance name, assume apache
396 if ( (0 == strcmp(st->name, "")) )
398 sstrncpy (vl->plugin, "apache", sizeof (vl->plugin));
402 sstrncpy (vl->plugin, st->name, sizeof (vl->plugin));
404 } /* void set_plugin */
406 static void submit_counter (const char *type, const char *type_instance,
407 counter_t value, char *host, apache_t *st)
410 value_list_t vl = VALUE_LIST_INIT;
412 values[0].counter = value;
416 sstrncpy (vl.host, host, sizeof (vl.host));
417 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
418 sstrncpy (vl.type, type, sizeof (vl.type));
420 if (type_instance != NULL)
421 sstrncpy (vl.type_instance, type_instance,
422 sizeof (vl.type_instance));
424 set_plugin (st, &vl);
426 plugin_dispatch_values (&vl);
427 } /* void submit_counter */
429 static void submit_gauge (const char *type, const char *type_instance,
430 gauge_t value, char *host, apache_t *st)
433 value_list_t vl = VALUE_LIST_INIT;
435 values[0].gauge = value;
439 sstrncpy (vl.host, host, sizeof (vl.host));
440 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
441 sstrncpy (vl.type, type, sizeof (vl.type));
443 if (type_instance != NULL)
444 sstrncpy (vl.type_instance, type_instance,
445 sizeof (vl.type_instance));
447 set_plugin (st, &vl);
449 plugin_dispatch_values (&vl);
450 } /* void submit_counter */
452 static void submit_scoreboard (char *buf, char *host, apache_t *st)
456 * "_" Waiting for Connection, "S" Starting up, "R" Reading Request,
457 * "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
458 * "C" Closing connection, "L" Logging, "G" Gracefully finishing,
459 * "I" Idle cleanup of worker, "." Open slot with no current process
461 long long open = 0LL;
462 long long waiting = 0LL;
463 long long starting = 0LL;
464 long long reading = 0LL;
465 long long sending = 0LL;
466 long long keepalive = 0LL;
467 long long dnslookup = 0LL;
468 long long closing = 0LL;
469 long long logging = 0LL;
470 long long finishing = 0LL;
471 long long idle_cleanup = 0LL;
475 for (i = 0; buf[i] != '\0'; i++)
477 if (buf[i] == '.') open++;
478 else if (buf[i] == '_') waiting++;
479 else if (buf[i] == 'S') starting++;
480 else if (buf[i] == 'R') reading++;
481 else if (buf[i] == 'W') sending++;
482 else if (buf[i] == 'K') keepalive++;
483 else if (buf[i] == 'D') dnslookup++;
484 else if (buf[i] == 'C') closing++;
485 else if (buf[i] == 'L') logging++;
486 else if (buf[i] == 'G') finishing++;
487 else if (buf[i] == 'I') idle_cleanup++;
490 submit_gauge ("apache_scoreboard", "open" , open, host, st);
491 submit_gauge ("apache_scoreboard", "waiting" , waiting, host, st);
492 submit_gauge ("apache_scoreboard", "starting" , starting, host, st);
493 submit_gauge ("apache_scoreboard", "reading" , reading, host, st);
494 submit_gauge ("apache_scoreboard", "sending" , sending, host, st);
495 submit_gauge ("apache_scoreboard", "keepalive", keepalive, host, st);
496 submit_gauge ("apache_scoreboard", "dnslookup", dnslookup, host, st);
497 submit_gauge ("apache_scoreboard", "closing" , closing, host, st);
498 submit_gauge ("apache_scoreboard", "logging" , logging, host, st);
499 submit_gauge ("apache_scoreboard", "finishing", finishing, host, st);
500 submit_gauge ("apache_scoreboard", "idle_cleanup", idle_cleanup, host, st);
503 static int apache_read_host (apache_t *st)
516 if (st->curl == NULL)
521 apache_buffer_fill = 0;
522 if (curl_easy_perform (st->curl) != 0)
524 ERROR ("apache: curl_easy_perform failed: %s",
531 while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
540 // set the host to localhost if st->host is not specified
541 if ( (st->host == NULL)
542 || (0 == strcmp(st->host, "")) ) {
543 st->host = hostname_g;
546 for (i = 0; i < lines_num; i++)
548 fields_num = strsplit (lines[i], fields, 4);
552 if ((strcmp (fields[0], "Total") == 0)
553 && (strcmp (fields[1], "Accesses:") == 0))
554 submit_counter ("apache_requests", "",
555 atoll (fields[2]), st->host, st);
556 else if ((strcmp (fields[0], "Total") == 0)
557 && (strcmp (fields[1], "kBytes:") == 0))
558 submit_counter ("apache_bytes", "",
559 1024LL * atoll (fields[2]), st->host, st);
561 else if (fields_num == 2)
563 if (strcmp (fields[0], "Scoreboard:") == 0)
564 submit_scoreboard (fields[1], st->host, st);
565 else if (strcmp (fields[0], "BusyServers:") == 0)
566 submit_gauge ("apache_connections", NULL, atol (fields[1]), st->host, st);
570 apache_buffer_fill = 0;
573 } /* int apache_read_host */
575 static int apache_read (void)
581 for (i = 0; i < apache_num; i++)
583 status = apache_read_host (apache[i]);
590 ERROR ("apache plugin: No host could be read. Will return an error so "
591 "the plugin will be delayed.");
596 } /* int apache_read */
598 void module_register (void)
600 plugin_register_complex_config ("apache", config);
601 plugin_register_init ("apache", init);
602 plugin_register_read ("apache", apache_read);
603 } /* void module_register */